Add mastership listener to Initialization callbacks. 43/49243/18
authorJozef Bacigal <jozef.bacigal@pantheon.tech>
Mon, 12 Dec 2016 12:44:28 +0000 (13:44 +0100)
committerJozef Bacigal <jozef.bacigal@pantheon.tech>
Tue, 4 Apr 2017 07:56:14 +0000 (09:56 +0200)
Change-Id: I743feb5429578ffe571bc366786d317efa0cacfa
Signed-off-by: Jozef Bacigal <jozef.bacigal@pantheon.tech>
openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/handlers/ClusterInitializationPhaseHandler.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/lifecycle/LifecycleServiceImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/rpc/RpcContextImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsContextImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/statistics/StatisticsManagerImpl.java

index e55c0d4e1b65f9ccdad8df375ad0398f5635e6b9..4f2e9d0a307eb42d891be8c8ccbc88caa0254503 100644 (file)
@@ -7,7 +7,7 @@
  */
 package org.opendaylight.openflowplugin.api.openflow.device.handlers;
 
-import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.MastershipChangeListener;
 
 /**
  * Interface handles MASTER initialization on ownership change.
@@ -15,10 +15,10 @@ import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext
 public interface ClusterInitializationPhaseHandler {
 
     /**
-     * Method for initialization cycle between contexts.
-     * @param connectionContext to check actual connection state
+     * Method for initialization cycle between contexts
+     * @param mastershipChangeListener - listener if something goes wrong with initialization
      */
-    boolean onContextInstantiateService(final ConnectionContext connectionContext);
+    boolean onContextInstantiateService(final MastershipChangeListener mastershipChangeListener);
 
     /**
      * Method for initial submit transaction after successful initial gathering.
index 83ded66f5325d9ce8a7ed22bb8f176f3280fdcdf..44d67f18d6b061a6689ea43cfaf325dffee39229 100644 (file)
@@ -52,6 +52,7 @@ import org.opendaylight.openflowplugin.api.openflow.device.Xid;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.ClusterInitializationPhaseHandler;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.MastershipChangeListener;
 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
@@ -721,7 +722,7 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
     }
 
     @Override
-    public boolean onContextInstantiateService(final ConnectionContext connectionContext) {
+    public boolean onContextInstantiateService(final MastershipChangeListener mastershipChangeListener) {
 
         if (getPrimaryConnectionContext().getConnectionState().equals(ConnectionContext.CONNECTION_STATE.RIP)) {
             LOG.warn("Connection on device {} was interrupted, will stop starting master services.",
@@ -750,12 +751,13 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
             return false;
         }
 
-        Futures.addCallback(sendRoleChangeToDevice(OfpRole.BECOMEMASTER), new RpcResultFutureCallback());
+        Futures.addCallback(sendRoleChangeToDevice(OfpRole.BECOMEMASTER),
+                new RpcResultFutureCallback(mastershipChangeListener));
 
         final ListenableFuture<List<Optional<FlowCapableNode>>> deviceFlowRegistryFill = getDeviceFlowRegistry().fill();
         Futures.addCallback(deviceFlowRegistryFill, new DeviceFlowRegistryCallback(deviceFlowRegistryFill));
 
-        return this.clusterInitializationPhaseHandler.onContextInstantiateService(getPrimaryConnectionContext());
+        return this.clusterInitializationPhaseHandler.onContextInstantiateService(mastershipChangeListener);
     }
 
     @VisibleForTesting
@@ -818,6 +820,13 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
     }
 
     private class RpcResultFutureCallback implements FutureCallback<RpcResult<SetRoleOutput>> {
+
+        private final MastershipChangeListener mastershipChangeListener;
+
+        RpcResultFutureCallback(final MastershipChangeListener mastershipChangeListener) {
+            this.mastershipChangeListener = mastershipChangeListener;
+        }
+
         @Override
         public void onSuccess(@Nullable RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
             if (LOG.isDebugEnabled()) {
@@ -829,7 +838,7 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
         @Override
         public void onFailure(final Throwable throwable) {
             LOG.warn("Was not able to set MASTER role on device, node {}", deviceInfo.getLOGValue());
-            shutdownConnection();
+            mastershipChangeListener.onNotAbleToStartMastership(deviceInfo);
         }
     }
 
index 8f4c1fa2af03c3347db086550f24ca1d7e2f1481..d30f334505916ee420a7257bc6f55e65335f4c03 100644 (file)
@@ -50,19 +50,21 @@ public class LifecycleServiceImpl implements LifecycleService {
     @Override
     public void makeDeviceSlave(final DeviceContext deviceContext) {
 
+        final DeviceInfo deviceInf = Objects.isNull(deviceInfo) ? deviceContext.getDeviceInfo() : deviceInfo;
+
         Futures.addCallback(deviceContext.makeDeviceSlave(), new FutureCallback<RpcResult<SetRoleOutput>>() {
             @Override
             public void onSuccess(@Nullable RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
                 if (LOG.isDebugEnabled()) {
                     LOG.debug("Role SLAVE was successfully propagated on device, node {}", deviceContext.getDeviceInfo().getLOGValue());
                 }
-                mastershipChangeListener.onSlaveRoleAcquired(deviceInfo);
+                mastershipChangeListener.onSlaveRoleAcquired(deviceInf);
             }
 
             @Override
             public void onFailure(Throwable throwable) {
                 LOG.warn("Was not able to set role SLAVE to device on node {} ",deviceContext.getDeviceInfo().getLOGValue());
-                mastershipChangeListener.onSlaveRoleNotAcquired(deviceInfo);
+                mastershipChangeListener.onSlaveRoleNotAcquired(deviceInf);
             }
         });
 
@@ -74,6 +76,8 @@ public class LifecycleServiceImpl implements LifecycleService {
         LOG.info("Starting clustering MASTER services for node {}", deviceInfo.getLOGValue());
         if (!clusterInitializationPhaseHandler.onContextInstantiateService(null)) {
             mastershipChangeListener.onNotAbleToStartMastership(deviceInfo);
+        } else {
+            mastershipChangeListener.onMasterRoleAcquired(deviceInfo);
         }
 
     }
index 0141f0db2af6fc8c0094677ca7dbf900ac8539fa..c188c65a8e2762ac5f49f839206b66f00d77c756 100644 (file)
@@ -28,6 +28,7 @@ import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.ClusterInitializationPhaseHandler;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.MastershipChangeListener;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
@@ -220,11 +221,7 @@ class RpcContextImpl implements RpcContext {
     }
 
     @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;
-        }
+    public boolean onContextInstantiateService(final MastershipChangeListener mastershipChangeListener) {
 
         MdSalRegistrationUtils.registerServices(this, deviceContext, extensionConverterProvider, convertorExecutor);
 
index a95347a696bf90f2b677d00395a86be59237c16f..5a608d1aa150bc992c519365dda7025456d9464e 100644 (file)
@@ -37,6 +37,7 @@ import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.ClusterInitializationPhaseHandler;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.MastershipChangeListener;
 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
@@ -500,11 +501,7 @@ class StatisticsContextImpl<T extends OfHeader> implements StatisticsContext {
     }
 
     @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;
-        }
+    public boolean onContextInstantiateService(final MastershipChangeListener mastershipChangeListener) {
 
         LOG.info("Starting statistics context cluster services for node {}", deviceInfo.getLOGValue());
 
@@ -519,6 +516,7 @@ class StatisticsContextImpl<T extends OfHeader> implements StatisticsContext {
             @Override
             public void onFailure(@Nonnull Throwable throwable) {
                 LOG.warn("Initial gathering statistics unsuccessful for node {}", deviceInfo.getLOGValue());
+                mastershipChangeListener.onNotAbleToStartMastership(deviceInfo);
             }
         });
 
@@ -526,7 +524,7 @@ class StatisticsContextImpl<T extends OfHeader> implements StatisticsContext {
             myManager.startScheduling(deviceInfo);
         }
 
-        return this.clusterInitializationPhaseHandler.onContextInstantiateService(connectionContext);
+        return this.clusterInitializationPhaseHandler.onContextInstantiateService(mastershipChangeListener);
     }
 
     @Override
index 86933a4a2e497bb85afe49f248ddd02471368866..d74a407e789d039a35f1c5fec660409c03ed2a7a 100644 (file)
@@ -347,7 +347,8 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
         final MultipartWriterProvider statisticsWriterProvider = MultipartWriterProviderFactory
             .createDefaultProvider(deviceContext);
 
-        return deviceContext.canUseSingleLayerSerialization() ?
+        final StatisticsContext statisticsContext =
+            deviceContext.canUseSingleLayerSerialization() ?
             new StatisticsContextImpl<MultipartReply>(
                 isStatisticsPollingOn,
                 deviceContext,
@@ -361,6 +362,9 @@ public class StatisticsManagerImpl implements StatisticsManager, StatisticsManag
                 converterExecutor,
                     this,
                 statisticsWriterProvider);
+        contexts.putIfAbsent(deviceContext.getDeviceInfo(), statisticsContext);
+
+        return statisticsContext;
     }
 
     public void onDeviceRemoved(DeviceInfo deviceInfo) {