Fix context chain closing
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / rpc / RpcContextImpl.java
index 2c3db2a49820223f3427807913c262ffa726aa86..f450b96160b137e3b426601d5af3ea5ae4917e53 100644 (file)
@@ -16,7 +16,6 @@ import java.util.Iterator;
 import java.util.Map.Entry;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Semaphore;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
@@ -27,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;
@@ -51,7 +51,7 @@ class RpcContextImpl implements RpcContext {
     // TODO: add private Sal salBroker
     private final ConcurrentMap<Class<?>, RoutedRpcRegistration<?>> rpcRegistrations = new ConcurrentHashMap<>();
     private final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
-    private volatile CONTEXT_STATE state = CONTEXT_STATE.INITIALIZATION;
+    private volatile ContextState state = ContextState.INITIALIZATION;
     private final DeviceInfo deviceInfo;
     private final DeviceContext deviceContext;
     private final ExtensionConverterProvider extensionConverterProvider;
@@ -110,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
@@ -135,7 +156,7 @@ class RpcContextImpl implements RpcContext {
                 tracker.release();
                 final long xid = getXid().getValue();
                 LOG.trace("Removed request context with xid {}", xid);
-                messageSpy.spyMessage(RpcContextImpl.class, MessageSpy.STATISTIC_GROUP.REQUEST_STACK_FREED);
+                messageSpy.spyMessage(RpcContextImpl.class, MessageSpy.StatisticsGroup.REQUEST_STACK_FREED);
             }
         };
     }
@@ -156,11 +177,6 @@ class RpcContextImpl implements RpcContext {
         return this.rpcRegistrations.isEmpty();
     }
 
-    @Override
-    public CONTEXT_STATE getState() {
-        return this.state;
-    }
-
     @Override
     public ServiceGroupIdentifier getServiceIdentifier() {
         return this.deviceInfo.getServiceIdentifier();
@@ -173,7 +189,7 @@ class RpcContextImpl implements RpcContext {
 
     @Override
     public ListenableFuture<Void> stopClusterServices() {
-        if (CONTEXT_STATE.TERMINATION.equals(this.state)) {
+        if (ContextState.TERMINATION.equals(this.state)) {
             return Futures.immediateCancelledFuture();
         }
 
@@ -181,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;
             }
         });
@@ -200,10 +205,10 @@ 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) {
+        if (isStatisticsRpcEnabled && !deviceContext.canUseSingleLayerSerialization()) {
             MdSalRegistrationUtils.registerStatCompatibilityServices(
                     this,
                     deviceContext,
@@ -211,6 +216,7 @@ class RpcContextImpl implements RpcContext {
                     convertorExecutor);
         }
 
+        mastershipChangeListener.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
         return true;
     }
 }