Merge "Report (TCP) port number for switches"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / rpc / RpcContextImpl.java
index 7d21f862cb11bc083e2a3cd62bc42d06f920e740..f450b96160b137e3b426601d5af3ea5ae4917e53 100644 (file)
@@ -26,6 +26,7 @@ import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
 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.lifecycle.ContextChainMastershipState;
 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;
@@ -109,7 +110,28 @@ class RpcContextImpl implements RpcContext {
      */
     @Override
     public void close() {
-        //NOOP
+        if (ContextState.TERMINATION.equals(state)) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("RpcContext for node {} is already in TERMINATION state.", getDeviceInfo().getLOGValue());
+            }
+        } else {
+            this.state = ContextState.TERMINATION;
+            unregisterRPCs();
+        }
+    }
+
+    private void unregisterRPCs() {
+        for (final Iterator<Entry<Class<?>, RoutedRpcRegistration<?>>> iterator = Iterators
+                .consumingIterator(rpcRegistrations.entrySet().iterator()); iterator.hasNext(); ) {
+            final RoutedRpcRegistration<?> rpcRegistration = iterator.next().getValue();
+            rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier);
+            rpcRegistration.close();
+
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Closing RPC Registration of service {} for device {}.", rpcRegistration.getServiceType().getSimpleName(),
+                        nodeInstanceIdentifier.getKey().getId().getValue());
+            }
+        }
     }
 
     @Override
@@ -155,11 +177,6 @@ class RpcContextImpl implements RpcContext {
         return this.rpcRegistrations.isEmpty();
     }
 
-    @Override
-    public ContextState getState() {
-        return this.state;
-    }
-
     @Override
     public ServiceGroupIdentifier getServiceIdentifier() {
         return this.deviceInfo.getServiceIdentifier();
@@ -180,18 +197,7 @@ class RpcContextImpl implements RpcContext {
             @Nullable
             @Override
             public Void apply(@Nullable Object input) {
-                for (final Iterator<Entry<Class<?>, RoutedRpcRegistration<?>>> iterator = Iterators
-                        .consumingIterator(rpcRegistrations.entrySet().iterator()); iterator.hasNext(); ) {
-                    final RoutedRpcRegistration<?> rpcRegistration = iterator.next().getValue();
-                    rpcRegistration.unregisterPath(NodeContext.class, nodeInstanceIdentifier);
-                    rpcRegistration.close();
-
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Closing RPC Registration of service {} for device {}.", rpcRegistration.getServiceType().getSimpleName(),
-                                nodeInstanceIdentifier.getKey().getId().getValue());
-                    }
-                }
-
+                unregisterRPCs();
                 return null;
             }
         });
@@ -199,7 +205,7 @@ class RpcContextImpl implements RpcContext {
 
     @Override
     public boolean onContextInstantiateService(final MastershipChangeListener mastershipChangeListener) {
-
+        LOG.info("Starting rpc context cluster services for node {}", deviceInfo.getLOGValue());
         MdSalRegistrationUtils.registerServices(this, deviceContext, extensionConverterProvider, convertorExecutor);
 
         if (isStatisticsRpcEnabled && !deviceContext.canUseSingleLayerSerialization()) {
@@ -210,6 +216,7 @@ class RpcContextImpl implements RpcContext {
                     convertorExecutor);
         }
 
+        mastershipChangeListener.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
         return true;
     }
 }