import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
+import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Future;
watchingEntities.remove(roleContext.getTxEntity());
contexts.remove(roleContext.getDeviceInfo());
if (roleContext.isTxCandidateRegistered()) {
- LOG.info("Node {} was holder txEntity, so trying to remove device from operational DS.");
+ LOG.info("Node {} was holder txEntity, so trying to remove device from operational DS.", roleContext.getDeviceInfo().getNodeId().getValue());
removeDeviceFromOperationalDS(roleContext.getDeviceInfo(), MAX_CLEAN_DS_RETRIES);
- } else {
- roleContext.close();
}
+ roleContext.close();
}
}
@Override
public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
LOG.trace("onDeviceContextLevelDown for node {}", deviceInfo.getNodeId());
- final RoleContext roleContext = contexts.get(deviceInfo);
+ final RoleContext roleContext = contexts.remove(deviceInfo);
if (roleContext != null) {
LOG.debug("Found roleContext associated to deviceContext: {}, now trying close the roleContext", deviceInfo.getNodeId());
- if (roleContext.isMainCandidateRegistered()) {
- roleContext.unregisterCandidate(roleContext.getEntity());
- } else {
- contexts.remove(deviceInfo.getNodeId(), roleContext);
- roleContext.close();
+ roleContext.setState(OFPContext.CONTEXT_STATE.TERMINATION);
+ roleContext.unregisterCandidate(roleContext.getEntity());
+ if (roleContext.isTxCandidateRegistered()) {
+ LOG.info("Node {} was holder txEntity, so trying to remove device from operational DS.", deviceInfo.getNodeId().getValue());
+ removeDeviceFromOperationalDS(roleContext.getDeviceInfo(), MAX_CLEAN_DS_RETRIES);
}
+ roleContext.close();
}
deviceTerminationPhaseHandler.onDeviceContextLevelDown(deviceInfo);
}
Preconditions.checkArgument(ownershipChange != null);
final RoleContext roleContext = watchingEntities.get(ownershipChange.getEntity());
- LOG.debug("Received EOS message: wasOwner:{} isOwner:{} hasOwner:{} inJeopardy:{} for entity type {} and node {}",
- ownershipChange.wasOwner(), ownershipChange.isOwner(), ownershipChange.hasOwner(), ownershipChange.inJeopardy(),
- ownershipChange.getEntity().getType(),
- roleContext != null ? roleContext.getDeviceInfo().getNodeId() : "-> no watching entity, disregarding notification <-");
+ if (Objects.nonNull(roleContext) && !roleContext.getState().equals(OFPContext.CONTEXT_STATE.TERMINATION)) {
+
+ LOG.debug("Received EOS message: wasOwner:{} isOwner:{} hasOwner:{} inJeopardy:{} for entity type {} and node {}",
+ ownershipChange.wasOwner(), ownershipChange.isOwner(), ownershipChange.hasOwner(), ownershipChange.inJeopardy(),
+ ownershipChange.getEntity().getType(),
+ roleContext.getDeviceInfo().getNodeId());
- if (roleContext != null) {
if (ownershipChange.getEntity().equals(roleContext.getEntity())) {
changeOwnershipForMainEntity(ownershipChange, roleContext);
} else {
changeOwnershipForTxEntity(ownershipChange, roleContext);
}
+
} else {
- LOG.debug("OwnershipChange {}", ownershipChange);
+
+ LOG.debug("Role context for entity type {} is in state closing, disregarding ownership change notification.", ownershipChange.getEntity().getType());
+ watchingEntities.remove(ownershipChange.getEntity());
+
}
}
LOG.debug("Trying to remove from operational node: {}", roleContext.getDeviceInfo().getNodeId());
removeDeviceFromOperationalDS(roleContext.getDeviceInfo(), MAX_CLEAN_DS_RETRIES);
} else {
- final NodeId nodeId = roleContext.getDeviceInfo().getNodeId();
contexts.remove(roleContext.getDeviceInfo(), roleContext);
roleContext.close();
conductor.closeConnection(roleContext.getDeviceInfo());
@Override
public void onSuccess(@Nullable final RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
LOG.info("Role {} successfully set on device {}", role, roleContext.getDeviceInfo().getNodeId());
- notifyListenersRoleChangeOnDevice(roleContext.getDeviceInfo(), true, role, init);
+ if (!init) {
+ notifyListenersRoleChangeOnDevice(roleContext.getDeviceInfo(), role);
+ }
}
@Override
public void onFailure(@Nonnull final Throwable throwable) {
LOG.warn("Unable to set role {} on device {}", role, roleContext.getDeviceInfo().getNodeId());
- notifyListenersRoleChangeOnDevice(roleContext.getDeviceInfo(), false, role, init);
+ conductor.closeConnection(roleContext.getDeviceInfo());
}
});
}
* Notifies registered listener on role change. Role is the new role on device
* If initialization phase is true, we may skip service starting
* @param deviceInfo
- * @param success true if role change on device done ok, false otherwise
* @param role new role meant to be set on device
- * @param initializationPhase if true, then skipp services start
*/
@VisibleForTesting
- void notifyListenersRoleChangeOnDevice(final DeviceInfo deviceInfo, final boolean success, final OfpRole role, final boolean initializationPhase){
+ void notifyListenersRoleChangeOnDevice(final DeviceInfo deviceInfo, final OfpRole role){
LOG.debug("Notifying registered listeners for role change, no. of listeners {}", listeners.size());
for (final RoleChangeListener listener : listeners) {
- listener.roleChangeOnDevice(deviceInfo, success, role, initializationPhase);
+ listener.roleChangeOnDevice(deviceInfo, role);
}
}
@Override
- public <T extends OFPContext> T gainContext(DeviceInfo deviceInfo) {
+ public <T extends OFPContext> T gainContext(final DeviceInfo deviceInfo) {
return (T) contexts.get(deviceInfo);
}
}