Bug 6465 Controller goes into slave mode
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / role / RoleContextImpl.java
index 483b8c7fafbbc219395c875c46f4c09fcfbb8851..0a1f81095baed3b82ea9f2da1522271bd1a2472c 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.openflowplugin.impl.role;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.FutureCallback;
@@ -22,9 +23,10 @@ import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
 import org.opendaylight.openflowplugin.api.OFConstants;
+import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
-import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
+import org.opendaylight.openflowplugin.api.openflow.device.handlers.ClusterInitializationPhaseHandler;
 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
 import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
@@ -40,8 +42,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Role context hold information about entity ownership registration,
- * register and unregister candidate (main and tx)
+ * Role context try to make change device role on device
  */
 class RoleContextImpl implements RoleContext {
 
@@ -54,14 +55,12 @@ class RoleContextImpl implements RoleContext {
     private final DeviceInfo deviceInfo;
     private CONTEXT_STATE state;
     private final RoleManager myManager;
-    private final LifecycleService lifecycleService;
+    private ClusterInitializationPhaseHandler clusterInitializationPhaseHandler;
 
     RoleContextImpl(final DeviceInfo deviceInfo,
                     final HashedWheelTimer hashedWheelTimer,
-                    final RoleManager myManager,
-                    final LifecycleService lifecycleService) {
+                    final RoleManager myManager) {
         this.deviceInfo = deviceInfo;
-        this.lifecycleService = lifecycleService;
         state = CONTEXT_STATE.WORKING;
         this.myManager = myManager;
         this.hashedWheelTimer = hashedWheelTimer;
@@ -83,11 +82,6 @@ class RoleContextImpl implements RoleContext {
         this.salRoleService = salRoleService;
     }
 
-    @Override
-    public SalRoleService getSalRoleService() {
-        return this.salRoleService;
-    }
-
     @Override
     public CONTEXT_STATE getState() {
         return this.state;
@@ -113,46 +107,49 @@ class RoleContextImpl implements RoleContext {
             @Override
             public void onSuccess(@Nullable RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
                 if (LOG.isDebugEnabled()) {
-                    LOG.debug("Role MASTER was successfully set on device, node {}", deviceInfo.getNodeId().getValue());
+                    LOG.debug("Role MASTER was successfully set on device, node {}", deviceInfo.getLOGValue());
                 }
             }
 
             @Override
             public void onFailure(final Throwable throwable) {
-                LOG.warn("Was not able to set MASTER role on device, node {}", deviceInfo.getNodeId().getValue());
-                lifecycleService.closeConnection();
+                LOG.warn("Was not able to set MASTER role on device, node {}", deviceInfo.getLOGValue());
             }
         });
     }
 
     @Override
-    public ListenableFuture<Void> stopClusterServices() {
-        ListenableFuture<Void> future = Futures.transform(makeDeviceSlave(), new Function<RpcResult<SetRoleOutput>, Void>() {
-                    @Nullable
-                    @Override
-                    public Void apply(@Nullable RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
-                        return null;
-                    }
-                });
-
-                Futures.addCallback(future, new FutureCallback<Void>() {
-                    @Override
-                    public void onSuccess(@Nullable Void aVoid) {
-                        if (LOG.isDebugEnabled()) {
-                            LOG.debug("Role SLAVE was successfully propagated on device, node {}", deviceInfo.getNodeId().getValue());
-                        }
-                        myManager.removeDeviceFromOperationalDS(deviceInfo, MAX_CLEAN_DS_RETRIES);
+    public ListenableFuture<Void> stopClusterServices(final boolean deviceDisconnected) {
+
+        if (!deviceDisconnected) {
+            ListenableFuture<Void> future = Futures.transform(makeDeviceSlave(), new Function<RpcResult<SetRoleOutput>, Void>() {
+                @Nullable
+                @Override
+                public Void apply(@Nullable RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
+                    return null;
+                }
+            });
+
+            Futures.addCallback(future, new FutureCallback<Void>() {
+                @Override
+                public void onSuccess(@Nullable Void aVoid) {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Role SLAVE was successfully propagated on device, node {}", deviceInfo.getLOGValue());
                     }
+                }
 
-                    @Override
-                    public void onFailure(final Throwable throwable) {
-                        LOG.warn("Was not able to set role SLAVE to device on node {} ", deviceInfo.getNodeId().getValue());
-                        LOG.trace("Error occurred on device role setting, probably connection loss: ", throwable);
-                        myManager.removeDeviceFromOperationalDS(deviceInfo, MAX_CLEAN_DS_RETRIES);
+                @Override
+                public void onFailure(final Throwable throwable) {
+                    LOG.warn("Was not able to set role SLAVE to device on node {} ", deviceInfo.getLOGValue());
+                    LOG.trace("Error occurred on device role setting, probably connection loss: ", throwable);
+                    myManager.removeDeviceFromOperationalDS(deviceInfo, MAX_CLEAN_DS_RETRIES);
 
-                    }
-                });
-        return future;
+                }
+            });
+            return future;
+        } else {
+            return myManager.removeDeviceFromOperationalDS(deviceInfo, MAX_CLEAN_DS_RETRIES);
+        }
     }
 
     @Override
@@ -160,7 +157,8 @@ class RoleContextImpl implements RoleContext {
         return sendRoleChangeToDevice(OfpRole.BECOMESLAVE);
     }
 
-    private ListenableFuture<RpcResult<SetRoleOutput>> sendRoleChangeToDevice(final OfpRole newRole) {
+    @VisibleForTesting
+    ListenableFuture<RpcResult<SetRoleOutput>> sendRoleChangeToDevice(final OfpRole newRole) {
         LOG.debug("Sending new role {} to device {}", newRole, deviceInfo.getNodeId());
         final Future<RpcResult<SetRoleOutput>> setRoleOutputFuture;
         final Short version = deviceInfo.getVersion();
@@ -174,10 +172,10 @@ class RoleContextImpl implements RoleContext {
         } else {
             final SetRoleInput setRoleInput = (new SetRoleInputBuilder()).setControllerRole(newRole)
                     .setNode(new NodeRef(DeviceStateUtil.createNodeInstanceIdentifier(deviceInfo.getNodeId()))).build();
-            setRoleOutputFuture = getSalRoleService().setRole(setRoleInput);
+            setRoleOutputFuture = this.salRoleService.setRole(setRoleInput);
             final TimerTask timerTask = timeout -> {
                 if (!setRoleOutputFuture.isDone()) {
-                    LOG.warn("New role {} was not propagated to device {} during 10 sec", newRole, deviceInfo.getNodeId());
+                    LOG.warn("New role {} was not propagated to device {} during 10 sec", newRole, deviceInfo.getLOGValue());
                     setRoleOutputFuture.cancel(true);
                 }
             };
@@ -186,4 +184,33 @@ class RoleContextImpl implements RoleContext {
         return JdkFutureAdapters.listenInPoolThread(setRoleOutputFuture);
     }
 
+    @Override
+    public void setLifecycleInitializationPhaseHandler(final ClusterInitializationPhaseHandler handler) {
+        this.clusterInitializationPhaseHandler = handler;
+    }
+
+    @Override
+    public boolean onContextInstantiateService(final ConnectionContext connectionContext) {
+
+        if (connectionContext.getConnectionState().equals(ConnectionContext.CONNECTION_STATE.RIP)) {
+            LOG.warn("Connection on device {} was interrupted, will stop starting master services.", deviceInfo.getLOGValue());
+            return false;
+        }
+
+        Futures.addCallback(sendRoleChangeToDevice(OfpRole.BECOMEMASTER), new FutureCallback<RpcResult<SetRoleOutput>>() {
+            @Override
+            public void onSuccess(@Nullable RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Role MASTER was successfully set on device, node {}", deviceInfo.getLOGValue());
+                }
+            }
+
+            @Override
+            public void onFailure(final Throwable throwable) {
+                LOG.warn("Was not able to set MASTER role on device, node {}", deviceInfo.getLOGValue());
+            }
+        });
+
+        return this.clusterInitializationPhaseHandler.onContextInstantiateService(connectionContext);
+    }
 }