Added context state to all contexts 34/40534/2
authorJozef Bacigal <jbacigal@cisco.com>
Mon, 20 Jun 2016 07:17:38 +0000 (09:17 +0200)
committerJozef Bacigal <jbacigal@cisco.com>
Mon, 20 Jun 2016 07:18:39 +0000 (07:18 +0000)
Change-Id: Ib2330821f71571c1dc6bcd81bec81fe288c2d83d
Signed-off-by: Jozef Bacigal <jbacigal@cisco.com>
openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/OFPContext.java
openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/DeviceContext.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/role/RoleContextImpl.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

index 6e2dfedddbeccabf70690e71a3eed6a676c1cefc..788f0c5e2ab4c2286e356b6c29f113f17c178041 100644 (file)
@@ -11,4 +11,25 @@ package org.opendaylight.openflowplugin.api.openflow;
  * General API for all OFP Context
  */
 public interface OFPContext {
+
+    /**
+     * distinguished device context states
+     */
+    enum CONTEXT_STATE {
+        /**
+         * initial phase
+         */
+        INITIALIZATION,
+        /**
+         * standard phase
+         */
+        WORKING,
+        /**
+         * termination phase
+         */
+        TERMINATION
+    }
+
+    CONTEXT_STATE getState();
+
 }
index 00bfdeb4b996f0ae7501451d86b2680854b64149..c5baf45cf6dade2d5cfdbc072af403f4efb421ee 100644 (file)
@@ -44,31 +44,6 @@ public interface DeviceContext extends AutoCloseable,
         OFPContext,
         DeviceRegistry{
 
-    /**
-     * distinguished device context states
-     */
-    enum DEVICE_CONTEXT_STATE {
-        /**
-         * initial phase of talking to switch
-         */
-        INITIALIZATION,
-        /**
-         * standard phase - interacting with switch
-         */
-        WORKING,
-        /**
-         * termination phase of talking to switch
-         */
-        TERMINATION
-    }
-
-    /**
-     * Method returns current device context state.
-     *
-     * @return {@link DeviceContext.DEVICE_CONTEXT_STATE}
-     */
-    DEVICE_CONTEXT_STATE getDeviceContextState();
-
     /**
      * Method close all auxiliary connections and primary connection.
      */
index f25e3aa010bec9f2b89b18e9277e42172dcf4c4d..bcf4259fc9632f3fcdb507737f14fc0255bc380d 100644 (file)
@@ -131,7 +131,7 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
 
     private final DeviceInfo deviceInfo;
 
-    private volatile DEVICE_CONTEXT_STATE deviceCtxState;
+    private volatile CONTEXT_STATE contextState;
 
     @VisibleForTesting
     DeviceContextImpl(@Nonnull final ConnectionContext primaryConnectionContext,
@@ -168,7 +168,7 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
         itemLifeCycleSourceRegistry = new ItemLifeCycleRegistryImpl();
         flowLifeCycleKeeper = new ItemLifeCycleSourceImpl();
         itemLifeCycleSourceRegistry.registerLifeCycleSource(flowLifeCycleKeeper);
-        deviceCtxState = DEVICE_CONTEXT_STATE.INITIALIZATION;
+        contextState = CONTEXT_STATE.INITIALIZATION;
     }
 
     /**
@@ -448,8 +448,8 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
 
     @Override
     public void onPublished() {
-        Verify.verify(DEVICE_CONTEXT_STATE.INITIALIZATION.equals(deviceCtxState));
-        deviceCtxState = DEVICE_CONTEXT_STATE.WORKING;
+        Verify.verify(CONTEXT_STATE.INITIALIZATION.equals(contextState));
+        contextState = CONTEXT_STATE.WORKING;
         primaryConnectionContext.getConnectionAdapter().setPacketInFiltering(false);
         for (final ConnectionContext switchAuxConnectionContext : auxiliaryConnectionContexts.values()) {
             switchAuxConnectionContext.getConnectionAdapter().setPacketInFiltering(false);
@@ -484,11 +484,11 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
     @Override
     public synchronized void shutdownConnection() {
         LOG.debug("Shutdown method for node {}", deviceInfo.getNodeId());
-        if (DEVICE_CONTEXT_STATE.TERMINATION.equals(deviceCtxState)) {
+        if (CONTEXT_STATE.TERMINATION.equals(contextState)) {
             LOG.debug("DeviceCtx for Node {} is in termination process.", deviceInfo.getNodeId());
             return;
         }
-        deviceCtxState = DEVICE_CONTEXT_STATE.TERMINATION;
+        contextState = CONTEXT_STATE.TERMINATION;
 
         if (ConnectionContext.CONNECTION_STATE.RIP.equals(getPrimaryConnectionContext().getConnectionState())) {
             LOG.debug("ConnectionCtx for Node {} is in RIP state.", deviceInfo.getNodeId());
@@ -507,11 +507,6 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
         deviceMeterRegistry.close();
     }
 
-    @Override
-    public DEVICE_CONTEXT_STATE getDeviceContextState() {
-        return deviceCtxState;
-    }
-
     @Override
     public ListenableFuture<Void> shuttingDownDataStoreTransactions() {
         return transactionChainManager.shuttingDown();
@@ -521,4 +516,9 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
     TransactionChainManager getTransactionChainManager() {
         return this.transactionChainManager;
     }
+
+    @Override
+    public CONTEXT_STATE getState() {
+        return this.contextState;
+    }
 }
index b29a1e6c42bee27edccadc51648e744d33674251..a19efba66b48b6f5d093522f97b93d7f8ef2c7d1 100644 (file)
@@ -374,7 +374,7 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
                     @Override
                     public ListenableFuture<Boolean> apply(@Nonnull final Void input) throws Exception {
                         statisticsContext.statListForCollectingInitialization();
-                        return statisticsContext.gatherDynamicData();
+                        return statisticsContext.initialGatherDynamicData();
                     }
                 });
 
index e0392a7cc95dce245b214aaa9b9e2ca55d0e8e26..5ddf504fbb066de0a50927683df0f363e042e2c9 100644 (file)
@@ -16,6 +16,7 @@ import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlready
 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
+import org.opendaylight.openflowplugin.api.openflow.OFPContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
@@ -47,6 +48,7 @@ class RoleContextImpl implements RoleContext {
     private final Semaphore roleChangeGuard = new Semaphore(1, true);
 
     private final LifecycleConductor conductor;
+    private volatile CONTEXT_STATE contextState;
 
     RoleContextImpl(final DeviceInfo deviceInfo, final EntityOwnershipService entityOwnershipService, final Entity entity, final Entity txEntity, final LifecycleConductor lifecycleConductor) {
         this.entityOwnershipService = entityOwnershipService;
@@ -54,11 +56,13 @@ class RoleContextImpl implements RoleContext {
         this.txEntity = txEntity;
         this.deviceInfo = deviceInfo;
         this.conductor = lifecycleConductor;
+        contextState = CONTEXT_STATE.INITIALIZATION;
     }
 
     @Override
     public boolean initialization() {
         LOG.info("Initialization main candidate for node {}", deviceInfo.getNodeId());
+        contextState = CONTEXT_STATE.WORKING;
         return registerCandidate(this.entity);
     }
 
@@ -183,10 +187,16 @@ class RoleContextImpl implements RoleContext {
 
     @Override
     public void close() {
+        contextState = CONTEXT_STATE.TERMINATION;
         unregisterAllCandidates();
     }
 
     public boolean isMaster(){
         return (txEntityOwnershipCandidateRegistration != null && entityOwnershipCandidateRegistration != null);
     }
+
+    @Override
+    public CONTEXT_STATE getState() {
+        return contextState;
+    }
 }
index dfbe5fbb745e0363d471961847c822945090e0c1..c91a1346754ff0168e5025fb40bae76f075ddf74 100644 (file)
@@ -17,6 +17,7 @@ import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.Semaphore;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.openflowplugin.api.openflow.OFPContext;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.device.XidSequencer;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
@@ -37,6 +38,8 @@ class RpcContextImpl implements RpcContext {
     private final XidSequencer xidSequencer;
     private boolean isStatisticsRpcEnabled;
 
+    private volatile CONTEXT_STATE contextState;
+
     // TODO: add private Sal salBroker
     private final ConcurrentMap<Class<?>, RoutedRpcRegistration<?>> rpcRegistrations = new ConcurrentHashMap<>();
     private final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
@@ -52,6 +55,7 @@ class RpcContextImpl implements RpcContext {
         this.nodeInstanceIdentifier = nodeInstanceIdentifier;
 
         tracker = new Semaphore(maxRequests, true);
+        contextState = CONTEXT_STATE.WORKING;
     }
 
     /**
@@ -84,13 +88,20 @@ class RpcContextImpl implements RpcContext {
      */
     @Override
     public void close() {
-        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();
-            LOG.debug("Closing RPC Registration of service {} for device {}.", rpcRegistration.getServiceType(),
-                    nodeInstanceIdentifier);
+        if (CONTEXT_STATE.TERMINATION.equals(contextState)){
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("RpcContext is already in TERMINATION state.");
+            }
+        } else {
+            contextState = CONTEXT_STATE.TERMINATION;
+            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();
+                LOG.debug("Closing RPC Registration of service {} for device {}.", rpcRegistration.getServiceType(),
+                        nodeInstanceIdentifier);
+            }
         }
     }
 
@@ -146,4 +157,9 @@ class RpcContextImpl implements RpcContext {
     public boolean isStatisticsRpcEnabled() {
         return isStatisticsRpcEnabled;
     }
+
+    @Override
+    public CONTEXT_STATE getState() {
+        return contextState;
+    }
 }
index 5d560025855ee71ebf4d292df4ce54d1f9c81d15..362fbb6b1300dddf2777fc56e46cebf5e3ea92e1 100644 (file)
@@ -27,6 +27,7 @@ import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import javax.annotation.concurrent.GuardedBy;
+import org.opendaylight.openflowplugin.api.openflow.OFPContext;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
@@ -64,6 +65,7 @@ class StatisticsContextImpl implements StatisticsContext {
     private Timeout pollTimeout;
 
     private volatile boolean schedulingEnabled;
+    private volatile CONTEXT_STATE contextState;
 
     StatisticsContextImpl(@CheckForNull final DeviceInfo deviceInfo, final boolean shuttingDownStatisticsPolling, final LifecycleConductor lifecycleConductor) {
         this.deviceContext = Preconditions.checkNotNull(lifecycleConductor.getDeviceContext(deviceInfo));
@@ -74,6 +76,7 @@ class StatisticsContextImpl implements StatisticsContext {
         statisticsGatheringOnTheFlyService = new StatisticsGatheringOnTheFlyService(this, deviceContext);
         itemLifeCycleListener = new ItemLifecycleListenerImpl(deviceContext);
         statListForCollectingInitialization();
+        contextState = CONTEXT_STATE.WORKING;
     }
 
     @Override
@@ -187,13 +190,20 @@ class StatisticsContextImpl implements StatisticsContext {
 
     @Override
     public void close() {
-        schedulingEnabled = false;
-        for (final Iterator<RequestContext<?>> iterator = Iterators.consumingIterator(requestContexts.iterator());
-                iterator.hasNext();) {
-            RequestContextUtil.closeRequestContextWithRpcError(iterator.next(), CONNECTION_CLOSED);
-        }
-        if (null != pollTimeout && !pollTimeout.isExpired()) {
-            pollTimeout.cancel();
+        if (CONTEXT_STATE.TERMINATION.equals(contextState)) {
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("Statistics context is already in state TERMINATION.");
+            }
+        } else {
+            contextState = CONTEXT_STATE.TERMINATION;
+            schedulingEnabled = false;
+            for (final Iterator<RequestContext<?>> iterator = Iterators.consumingIterator(requestContexts.iterator());
+                 iterator.hasNext(); ) {
+                RequestContextUtil.closeRequestContextWithRpcError(iterator.next(), CONNECTION_CLOSED);
+            }
+            if (null != pollTimeout && !pollTimeout.isExpired()) {
+                pollTimeout.cancel();
+            }
         }
     }
 
@@ -369,4 +379,8 @@ class StatisticsContextImpl implements StatisticsContext {
         return itemLifeCycleListener;
     }
 
+    @Override
+    public CONTEXT_STATE getState() {
+        return contextState;
+    }
 }