Switch to MD-SAL APIs
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / rpc / RpcManagerImpl.java
index 61b114056d1e4e9be5f672bc92ba39791f633f82..28ed3c378db25104e1db8ee3364bbfa33662bad5 100644 (file)
@@ -7,74 +7,44 @@
  */
 package org.opendaylight.openflowplugin.impl.rpc;
 
-import com.google.common.base.Verify;
+import com.google.common.annotations.VisibleForTesting;
 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 javax.annotation.Nonnull;
+import org.opendaylight.mdsal.binding.api.NotificationPublishService;
+import org.opendaylight.mdsal.binding.api.RpcProviderService;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
-import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
-import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
-import org.opendaylight.openflowplugin.impl.util.MdSalRegistrationUtils;
-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.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 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<NodeId, RpcContext> contexts = new ConcurrentHashMap<>();
-    private boolean isStatisticsRpcEnabled;
-    private NotificationPublishService notificationPublishService;
-
-    public RpcManagerImpl(final RpcProviderRegistry rpcProviderRegistry,
-                          final int quotaValue) {
+    private final OpenflowProviderConfig config;
+    private final RpcProviderService rpcProviderRegistry;
+    private final ConcurrentMap<DeviceInfo, RpcContext> contexts = new ConcurrentHashMap<>();
+    private final ExtensionConverterProvider extensionConverterProvider;
+    private final ConvertorExecutor convertorExecutor;
+    private final NotificationPublishService notificationPublishService;
+
+    public RpcManagerImpl(final OpenflowProviderConfig config,
+                          final RpcProviderService rpcProviderRegistry,
+                          final ExtensionConverterProvider extensionConverterProvider,
+                          final ConvertorExecutor convertorExecutor,
+                          final NotificationPublishService notificationPublishService) {
+        this.config = config;
         this.rpcProviderRegistry = rpcProviderRegistry;
-        maxRequestsQuota = quotaValue;
-    }
-
-    @Override
-    public void setDeviceInitializationPhaseHandler(final DeviceInitializationPhaseHandler handler) {
-        deviceInitPhaseHandler = handler;
-    }
-
-    @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(nodeId, rpcContext) == null, "RpcCtx still not closed for node {}", nodeId);
-        deviceContext.addDeviceContextClosedHandler(this);
-
-        if (OfpRole.BECOMEMASTER.equals(ofpRole)) {
-            LOG.info("Registering Openflow Master RPCs for node:{}, role:{}", nodeId, ofpRole);
-            MdSalRegistrationUtils.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("Unregister RPC services (if any) for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
-            MdSalRegistrationUtils.registerSlaveServices(rpcContext, ofpRole);
-        } else {
-            // if we don't know role, we need to unregister rpcs if any have been registered
-            LOG.info("Unregister RPC services (if any) for slave role for node:{}", deviceContext.getDeviceState().getNodeId());
-            MdSalRegistrationUtils.unregisterServices(rpcContext);
-        }
-
-        // finish device initialization cycle back to DeviceManager
-        deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
+        this.extensionConverterProvider = extensionConverterProvider;
+        this.convertorExecutor = convertorExecutor;
+        this.notificationPublishService = notificationPublishService;
     }
 
     @Override
@@ -85,28 +55,36 @@ public class RpcManagerImpl implements RpcManager {
         }
     }
 
-
-    @Override
-    public void onDeviceContextLevelDown(final DeviceContext deviceContext) {
-        final RpcContext removedContext = contexts.remove(deviceContext.getDeviceState().getNodeId());
-        if (removedContext != null) {
-            LOG.info("Unregister RPCs services for device context closure");
-            removedContext.close();
+    /**
+     * This method is only for testing.
+     */
+    @VisibleForTesting
+    void addRecordToContexts(DeviceInfo deviceInfo, RpcContext rpcContexts) {
+        if (!contexts.containsKey(deviceInfo)) {
+            this.contexts.put(deviceInfo, rpcContexts);
         }
-        deviceTerminPhaseHandler.onDeviceContextLevelDown(deviceContext);
-    }
-    @Override
-    public void setStatisticsRpcEnabled(final boolean isStatisticsRpcEnabled) {
-        this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
     }
 
     @Override
-    public void setNotificationPublishService(final NotificationPublishService notificationPublishService) {
-        this.notificationPublishService = notificationPublishService;
+    public RpcContext createContext(final @Nonnull DeviceContext deviceContext) {
+        final RpcContextImpl rpcContext = new RpcContextImpl(
+                rpcProviderRegistry,
+                config.getRpcRequestsQuota().getValue(),
+                deviceContext,
+                extensionConverterProvider,
+                convertorExecutor,
+                notificationPublishService,
+                config.isIsStatisticsRpcEnabled());
+
+        contexts.put(deviceContext.getDeviceInfo(), rpcContext);
+        return rpcContext;
     }
 
     @Override
-    public void setDeviceTerminationPhaseHandler(final DeviceTerminationPhaseHandler handler) {
-        this.deviceTerminPhaseHandler = handler;
+    public void onDeviceRemoved(final DeviceInfo deviceInfo) {
+        contexts.remove(deviceInfo);
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Rpc context removed for node {}", deviceInfo);
+        }
     }
 }