package org.opendaylight.openflowplugin.impl.role;
import com.google.common.base.Preconditions;
-
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
+import javax.annotation.Nonnull;
import javax.annotation.Nullable;
-
import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
+import org.opendaylight.openflowplugin.api.openflow.OFPContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
-import org.opendaylight.openflowplugin.impl.LifecycleConductor;
import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class RoleContextImpl implements RoleContext {
private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
- public static final int TIMEOUT = 12;
+ private static final int TIMEOUT = 12;
- private final NodeId nodeId;
+ private final DeviceInfo deviceInfo;
private final EntityOwnershipService entityOwnershipService;
private volatile EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration = null;
private volatile EntityOwnershipCandidateRegistration txEntityOwnershipCandidateRegistration = null;
private final Semaphore roleChangeGuard = new Semaphore(1, true);
- public RoleContextImpl(NodeId nodeId, EntityOwnershipService entityOwnershipService, Entity entity, Entity txEntity) {
+ private final LifecycleConductor conductor;
+ private volatile CONTEXT_STATE contextState;
+
+ RoleContextImpl(final DeviceInfo deviceInfo, final EntityOwnershipService entityOwnershipService, final Entity entity, final Entity txEntity, final LifecycleConductor lifecycleConductor) {
this.entityOwnershipService = entityOwnershipService;
this.entity = entity;
this.txEntity = txEntity;
- this.nodeId = nodeId;
+ this.deviceInfo = deviceInfo;
+ this.conductor = lifecycleConductor;
+ contextState = CONTEXT_STATE.INITIALIZATION;
}
@Override
public boolean initialization() {
- LOG.info("Initialization main candidate for node {}", nodeId);
+ LOG.info("Initialization main candidate for node {}", deviceInfo.getNodeId());
+ contextState = CONTEXT_STATE.WORKING;
return registerCandidate(this.entity);
}
@Override
public void unregisterAllCandidates() {
- LOG.info("Role context closed, unregistering all candidates for ownership for node {}", nodeId);
+ LOG.info("Role context closed, unregistering all candidates for ownership for node {}", deviceInfo.getNodeId());
if (isMainCandidateRegistered()) {
unregisterCandidate(this.entity);
}
@Nullable
@Override
public <T> RequestContext<T> createRequestContext() {
- final AbstractRequestContext<T> ret = new AbstractRequestContext<T>(LifecycleConductor.getInstance().reserveXidForDeviceMessage(nodeId)) {
+ return new AbstractRequestContext<T>(conductor.reserveXidForDeviceMessage(deviceInfo)) {
@Override
public void close() {
}
};
- return ret;
}
@Override
- public void setSalRoleService(final SalRoleService salRoleService) {
+ public void setSalRoleService(@Nonnull final SalRoleService salRoleService) {
Preconditions.checkNotNull(salRoleService);
this.salRoleService = salRoleService;
}
}
@Override
- public NodeId getNodeId() {
- return nodeId;
+ public DeviceInfo getDeviceInfo() {
+ return deviceInfo;
}
@Override
} else {
return false;
}
- } catch (CandidateAlreadyRegisteredException e) {
+ } catch (final CandidateAlreadyRegisteredException e) {
LOG.warn("Candidate for entity {} is already registered.", entity_.getType());
return false;
- } catch (InterruptedException e) {
+ } catch (final InterruptedException e) {
LOG.warn("Cannot acquire semaphore for register entity {} candidate.", entity_.getType());
return false;
} finally {
} else {
return false;
}
- } catch (InterruptedException e) {
+ } catch (final InterruptedException e) {
LOG.warn("Cannot acquire semaphore for unregister entity {} candidate.", entity_.getType());
return false;
} finally {
@Override
public void close() {
+ contextState = CONTEXT_STATE.TERMINATION;
unregisterAllCandidates();
}
public boolean isMaster(){
return (txEntityOwnershipCandidateRegistration != null && entityOwnershipCandidateRegistration != null);
}
+
+ @Override
+ public CONTEXT_STATE getState() {
+ return contextState;
+ }
}