Bug 5596 Created lifecycle service
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / rpc / RpcContextImpl.java
index dfbe5fbb745e0363d471961847c822945090e0c1..1a8cb845f1452b8760ae60369c48f0bf7b4e0fe8 100644 (file)
@@ -17,6 +17,8 @@ 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.mdsal.singleton.common.api.ServiceGroupIdentifier;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.device.XidSequencer;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
@@ -40,18 +42,23 @@ class RpcContextImpl implements RpcContext {
     // TODO: add private Sal salBroker
     private final ConcurrentMap<Class<?>, RoutedRpcRegistration<?>> rpcRegistrations = new ConcurrentHashMap<>();
     private final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
+    private CONTEXT_STATE state;
+    private final DeviceInfo deviceInfo;
 
-    RpcContextImpl(final RpcProviderRegistry rpcProviderRegistry,
-                          final XidSequencer xidSequencer,
-                          final MessageSpy messageSpy,
-                          final int maxRequests,
-                          final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier) {
+    RpcContextImpl(final DeviceInfo deviceInfo,
+                   final RpcProviderRegistry rpcProviderRegistry,
+                   final XidSequencer xidSequencer,
+                   final MessageSpy messageSpy,
+                   final int maxRequests,
+                   final KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier) {
         this.xidSequencer = Preconditions.checkNotNull(xidSequencer);
         this.messageSpy = Preconditions.checkNotNull(messageSpy);
         this.rpcProviderRegistry = Preconditions.checkNotNull(rpcProviderRegistry);
         this.nodeInstanceIdentifier = nodeInstanceIdentifier;
 
         tracker = new Semaphore(maxRequests, true);
+        setState(CONTEXT_STATE.WORKING);
+        this.deviceInfo = deviceInfo;
     }
 
     /**
@@ -84,13 +91,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(getState())){
+            if (LOG.isDebugEnabled()) {
+                LOG.debug("RpcContext is already in TERMINATION state.");
+            }
+        } else {
+            setState(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 +160,24 @@ class RpcContextImpl implements RpcContext {
     public boolean isStatisticsRpcEnabled() {
         return isStatisticsRpcEnabled;
     }
+
+    @Override
+    public CONTEXT_STATE getState() {
+        return this.state;
+    }
+
+    @Override
+    public void setState(CONTEXT_STATE state) {
+        this.state = state;
+    }
+
+    @Override
+    public ServiceGroupIdentifier getServiceIdentifier() {
+        return this.deviceInfo.getServiceIdentifier();
+    }
+
+    @Override
+    public DeviceInfo getDeviceInfo() {
+        return this.deviceInfo;
+    }
 }