Merge "Bug-4957 Cluster Role change fix"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / rpc / RpcManagerImpl.java
index 6fddf66d20c430eb1b7e422867950bd88d8209c7..67d08bb0a72c3d0fa2dc9e71d3e5eddf6e2dcd30 100644 (file)
@@ -44,9 +44,9 @@ public class RpcManagerImpl implements RpcManager {
     }
 
     @Override
-    public void onDeviceContextLevelUp(final DeviceContext deviceContext) {
-        NodeId nodeId = deviceContext.getDeviceState().getNodeId();
-        OfpRole ofpRole = deviceContext.getDeviceState().getRole();
+    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);
 
@@ -56,28 +56,26 @@ public class RpcManagerImpl implements RpcManager {
             contexts.put(deviceContext, rpcContext);
         }
 
+        deviceContext.addDeviceContextClosedHandler(this);
 
-        if (ofpRole == OfpRole.BECOMESLAVE) {
-            // 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());
-            try {
-                MdSalRegistratorUtils.unregisterServices(rpcContext);
-            } catch (Exception e) {
-                LOG.error("Exception while unregistering rpcs for slave role for node:{}. But continuing.", nodeId, e);
-            }
-
-        } else {
+        if (OfpRole.BECOMEMASTER.equals(ofpRole)) {
             LOG.info("Registering Openflow RPCs for node:{}, role:{}", nodeId, ofpRole);
-            MdSalRegistratorUtils.registerServices(rpcContext, deviceContext, ofpRole);
+            MdSalRegistratorUtils.registerMasterServices(rpcContext, deviceContext, ofpRole);
 
             if (isStatisticsRpcEnabled) {
                 MdSalRegistratorUtils.registerStatCompatibilityServices(rpcContext, deviceContext,
                         notificationPublishService, new AtomicLong());
             }
+        } 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);
         }
 
-        deviceContext.addDeviceContextClosedHandler(this);
-
         // finish device initialization cycle back to DeviceManager
         deviceInitPhaseHandler.onDeviceContextLevelUp(deviceContext);
     }
@@ -89,24 +87,25 @@ public class RpcManagerImpl implements RpcManager {
 
 
     @Override
-    public void onDeviceContextClosed(DeviceContext deviceContext) {
-        RpcContext removedContext = contexts.remove(deviceContext);
+    public void onDeviceContextClosed(final DeviceContext deviceContext) {
+        final RpcContext removedContext = contexts.remove(deviceContext);
         if (removedContext != null) {
             try {
                 LOG.info("Unregistering rpcs for device context closure");
                 removedContext.close();
-            } catch (Exception e) {
+            } catch (final Exception e) {
                 LOG.error("Exception while unregistering rpcs onDeviceContextClosed handler for node:{}. But continuing.",
                         deviceContext.getDeviceState().getNodeId(), e);
             }
         }
     }
-    public void setStatisticsRpcEnabled(boolean isStatisticsRpcEnabled) {
+    @Override
+    public void setStatisticsRpcEnabled(final boolean isStatisticsRpcEnabled) {
         this.isStatisticsRpcEnabled = isStatisticsRpcEnabled;
     }
 
     @Override
-    public void setNotificationPublishService(NotificationPublishService notificationPublishService) {
+    public void setNotificationPublishService(final NotificationPublishService notificationPublishService) {
         this.notificationPublishService = notificationPublishService;
     }
 }