Bug 5596 Created lifecycle service
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / rpc / RpcManagerImpl.java
index f95d5ff809878e0136f20b207ff04be397fd34ae..a09a395c2a51d4195f4958508dcc15f456467fa9 100644 (file)
@@ -7,18 +7,22 @@
  */
 package org.opendaylight.openflowplugin.impl.rpc;
 
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
 import com.google.common.base.Verify;
+import com.google.common.collect.Iterators;
+import java.util.Iterator;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
-import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.openflowplugin.api.openflow.OFPContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
+import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
-import org.opendaylight.openflowplugin.impl.util.MdSalRegistratorUtils;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -27,15 +31,20 @@ public class RpcManagerImpl implements RpcManager {
     private static final Logger LOG = LoggerFactory.getLogger(RpcManagerImpl.class);
     private final RpcProviderRegistry rpcProviderRegistry;
     private DeviceInitializationPhaseHandler deviceInitPhaseHandler;
+    private DeviceTerminationPhaseHandler deviceTerminPhaseHandler;
     private final int maxRequestsQuota;
-    private final ConcurrentMap<DeviceContext, RpcContext> contexts = new ConcurrentHashMap<>();
+    private final ConcurrentMap<DeviceInfo, RpcContext> contexts = new ConcurrentHashMap<>();
     private boolean isStatisticsRpcEnabled;
-    private NotificationPublishService notificationPublishService;
 
-    public RpcManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
-                          final int quotaValue) {
+    private final LifecycleConductor conductor;
+
+    public RpcManagerImpl(
+            final RpcProviderRegistry rpcProviderRegistry,
+            final int quotaValue,
+            final LifecycleConductor lifecycleConductor) {
         this.rpcProviderRegistry = rpcProviderRegistry;
         maxRequestsQuota = quotaValue;
+        this.conductor = lifecycleConductor;
     }
 
     @Override
@@ -44,61 +53,67 @@ public class RpcManagerImpl implements RpcManager {
     }
 
     @Override
-    public void onDeviceContextLevelUp(final DeviceContext deviceContext) throws Exception {
-        final NodeId nodeId = deviceContext.getDeviceState().getNodeId();
-        final OfpRole ofpRole = deviceContext.getDeviceState().getRole();
-
-        LOG.debug("Node:{}, deviceContext.getDeviceState().getRole():{}", nodeId, ofpRole);
-        final RpcContext rpcContext = new RpcContextImpl(deviceContext.getMessageSpy(), rpcProviderRegistry,
-                deviceContext, maxRequestsQuota, isStatisticsRpcEnabled, notificationPublishService);
-
-        Verify.verify(contexts.putIfAbsent(deviceContext, rpcContext) == null, "RpcCtx still not closed for node {}", nodeId);
-        deviceContext.addDeviceContextClosedHandler(this);
-
-        //FIXME : propagate isStatisticsRpcEnabled to DeviceContext
-
-        if (OfpRole.BECOMEMASTER.equals(ofpRole)) {
-            LOG.info("Registering Openflow RPCs for node:{}, role:{}", nodeId, ofpRole);
-            MdSalRegistratorUtils.registerMasterServices(rpcContext, deviceContext, ofpRole);
-
-        } else if(OfpRole.BECOMESLAVE.equals(ofpRole)) {
-            // if slave, we need to de-register rpcs if any have been registered, in case of master to slave
-            LOG.info("Unregistering RPC registration (if any) for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
-            MdSalRegistratorUtils.registerSlaveServices(rpcContext, ofpRole);
-        } else {
-            // if we don't know role, we need to unregister rpcs if any have been registered
-            LOG.info("Unregistering RPC registration (if any) for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
-            MdSalRegistratorUtils.unregisterServices(rpcContext);
-        }
+    public void onDeviceContextLevelUp(final DeviceInfo deviceInfo) throws Exception {
+
+        final DeviceContext deviceContext = Preconditions.checkNotNull(conductor.getDeviceContext(deviceInfo));
+
+        final RpcContext rpcContext = new RpcContextImpl(
+                deviceInfo,
+                rpcProviderRegistry,
+                deviceContext,
+                deviceContext.getMessageSpy(),
+                maxRequestsQuota,
+                deviceInfo.getNodeInstanceIdentifier());
+
+        Verify.verify(contexts.putIfAbsent(deviceInfo, rpcContext) == null, "RpcCtx still not closed for node {}", deviceInfo.getNodeId());
+
+        rpcContext.setStatisticsRpcEnabled(isStatisticsRpcEnabled);
 
         // finish device initialization cycle back to DeviceManager
-        deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
+        deviceInitPhaseHandler.onDeviceContextLevelUp(deviceInfo);
     }
 
     @Override
-    public void close() throws Exception {
-        for(final RpcContext ctx : contexts.values()) {
-            ctx.close();
+    public void close() {
+        for (final Iterator<RpcContext> iterator = Iterators.consumingIterator(contexts.values().iterator());
+                iterator.hasNext();) {
+            iterator.next().close();
         }
-        contexts.clear();
     }
 
-
     @Override
-    public void onDeviceContextClosed(final DeviceContext deviceContext) {
-        final RpcContext removedContext = contexts.remove(deviceContext);
+    public void onDeviceContextLevelDown(final DeviceInfo deviceInfo) {
+        final RpcContext removedContext = contexts.remove(deviceInfo);
         if (removedContext != null) {
-            LOG.info("Unregistering rpcs for device context closure");
+            LOG.info("Unregister RPCs services for device context closure");
             removedContext.close();
         }
+        deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceInfo);
     }
+
     @Override
-    public void setStatisticsRpcEnabled(final boolean isStatisticsRpcEnabled) {
-        this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
+    public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
+        this.deviceTerminPhaseHandler = handler;
     }
 
+    /**
+     * This method is only for testing
+     */
+    @VisibleForTesting
+    void addRecordToContexts(DeviceInfo deviceInfo, RpcContext rpcContexts) {
+        if(!contexts.containsKey(deviceInfo)) {
+            this.contexts.put(deviceInfo,rpcContexts);
+        }
+    }
+
+    @Override
+    public <T extends OFPContext> T gainContext(DeviceInfo deviceInfo) {
+        return (T) contexts.get(deviceInfo);
+    }
+
+
     @Override
-    public void setNotificationPublishService(final NotificationPublishService notificationPublishService) {
-        this.notificationPublishService = notificationPublishService;
+    public void setStatisticsRpcEnabled(boolean statisticsRpcEnabled) {
+        isStatisticsRpcEnabled = statisticsRpcEnabled;
     }
 }