Conductor to OFPManager dependency
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / LifecycleConductorImpl.java
index b55bff8720b3431029b134b7a853f3fe21e8fa26..0213b20aa767d787d7fc7a092b654cf1b0b74897 100644 (file)
@@ -21,13 +21,17 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
+
+import org.opendaylight.openflowplugin.api.openflow.OFPManager;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
+import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.DeviceContextChangeListener;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.RoleChangeListener;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListener;
+import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
@@ -37,7 +41,7 @@ import org.slf4j.LoggerFactory;
 
 /**
  */
-public final class LifecycleConductorImpl implements LifecycleConductor, RoleChangeListener, DeviceContextChangeListener {
+final class LifecycleConductorImpl implements LifecycleConductor, RoleChangeListener, DeviceContextChangeListener {
 
     private static final Logger LOG = LoggerFactory.getLogger(LifecycleConductorImpl.class);
     private static final int TICKS_PER_WHEEL = 500;
@@ -45,103 +49,111 @@ public final class LifecycleConductorImpl implements LifecycleConductor, RoleCha
 
     private final HashedWheelTimer hashedWheelTimer = new HashedWheelTimer(TICK_DURATION, TimeUnit.MILLISECONDS, TICKS_PER_WHEEL);
     private DeviceManager deviceManager;
-    private final MessageIntelligenceAgency messageIntelligenceAgency;
-    private ConcurrentHashMap<NodeId, ServiceChangeListener> serviceChangeListeners = new ConcurrentHashMap<>();
     private StatisticsManager statisticsManager;
+    private RpcManager rpcManager;
+    private final MessageIntelligenceAgency messageIntelligenceAgency;
+    private ConcurrentHashMap<DeviceInfo, ServiceChangeListener> serviceChangeListeners = new ConcurrentHashMap<>();
 
-    public LifecycleConductorImpl(final MessageIntelligenceAgency messageIntelligenceAgency) {
+    LifecycleConductorImpl(final MessageIntelligenceAgency messageIntelligenceAgency) {
         Preconditions.checkNotNull(messageIntelligenceAgency);
         this.messageIntelligenceAgency = messageIntelligenceAgency;
     }
 
-    public void setSafelyDeviceManager(final DeviceManager deviceManager) {
-        if (this.deviceManager == null) {
-            this.deviceManager = deviceManager;
+    @Override
+    public void setSafelyManager(final OFPManager manager){
+        if (manager == null) {
+            LOG.info("Manager {} is already defined in conductor. ", manager);
         }
-    }
-
-    public void setSafelyStatisticsManager(final StatisticsManager statisticsManager) {
-        if (this.statisticsManager == null) {
-            this.statisticsManager = statisticsManager;
+        if (manager instanceof RpcManager) {
+            this.rpcManager = (RpcManager) manager;
+        } else {
+            if (manager instanceof StatisticsManager) {
+                this.statisticsManager = (StatisticsManager) manager;
+            } else {
+                if (manager instanceof DeviceManager) {
+                    this.deviceManager = (DeviceManager) manager;
+                }
+            }
         }
     }
 
-    public void addOneTimeListenerWhenServicesChangesDone(final ServiceChangeListener manager, final NodeId nodeId){
-        LOG.debug("Listener {} for service change for node {} registered.", manager, nodeId);
-        serviceChangeListeners.put(nodeId, manager);
+    @Override
+    public void addOneTimeListenerWhenServicesChangesDone(final ServiceChangeListener manager, final DeviceInfo deviceInfo){
+        LOG.debug("Listener {} for service change for node {} registered.", manager, deviceInfo.getNodeId());
+        serviceChangeListeners.put(deviceInfo, manager);
     }
 
     @VisibleForTesting
-    void notifyServiceChangeListeners(final NodeId nodeId, final boolean success){
+    void notifyServiceChangeListeners(final DeviceInfo deviceInfo, final boolean success){
         if (serviceChangeListeners.size() == 0) {
             return;
         }
         LOG.debug("Notifying registered listeners for service change, no. of listeners {}", serviceChangeListeners.size());
-        for (final Map.Entry<NodeId, ServiceChangeListener> nodeIdServiceChangeListenerEntry : serviceChangeListeners.entrySet()) {
-            if (nodeIdServiceChangeListenerEntry.getKey().equals(nodeId)) {
-                LOG.debug("Listener {} for service change for node {} was notified. Success was set on {}", nodeIdServiceChangeListenerEntry.getValue(), nodeId, success);
-                nodeIdServiceChangeListenerEntry.getValue().servicesChangeDone(nodeId, success);
-                serviceChangeListeners.remove(nodeId);
+        for (final Map.Entry<DeviceInfo, ServiceChangeListener> nodeIdServiceChangeListenerEntry : serviceChangeListeners.entrySet()) {
+            if (nodeIdServiceChangeListenerEntry.getKey().equals(deviceInfo)) {
+                LOG.debug("Listener {} for service change for node {} was notified. Success was set on {}", nodeIdServiceChangeListenerEntry.getValue(), deviceInfo, success);
+                nodeIdServiceChangeListenerEntry.getValue().servicesChangeDone(deviceInfo, success);
+                serviceChangeListeners.remove(deviceInfo);
             }
         }
     }
 
     @Override
-    public void roleInitializationDone(final NodeId nodeId, final boolean success) {
+    public void roleInitializationDone(final DeviceInfo deviceInfo, final boolean success) {
         if (!success) {
-            LOG.warn("Initialization phase for node {} in role context was NOT successful, closing connection.", nodeId);
-            closeConnection(nodeId);
+            LOG.warn("Initialization phase for node {} in role context was NOT successful, closing connection.", deviceInfo);
+            closeConnection(deviceInfo);
         } else {
-            LOG.info("initialization phase for node {} in role context was successful, continuing to next context.", nodeId);
+            LOG.info("initialization phase for node {} in role context was successful, continuing to next context.", deviceInfo);
         }
     }
 
-    public void closeConnection(final NodeId nodeId) {
-        LOG.debug("Close connection called for node {}", nodeId);
-        final DeviceContext deviceContext = getDeviceContext(nodeId);
+    public void closeConnection(final DeviceInfo deviceInfo) {
+        LOG.debug("Close connection called for node {}", deviceInfo);
+        final DeviceContext deviceContext = getDeviceContext(deviceInfo);
         if (null != deviceContext) {
             deviceContext.shutdownConnection();
         }
     }
 
     @Override
-    public void roleChangeOnDevice(final NodeId nodeId, final boolean success, final OfpRole newRole, final boolean initializationPhase) {
+    public void roleChangeOnDevice(final DeviceInfo deviceInfo, final boolean success, final OfpRole newRole, final boolean initializationPhase) {
 
-        final DeviceContext deviceContext = getDeviceContext(nodeId);
+        final DeviceContext deviceContext = getDeviceContext(deviceInfo);
 
         if (null == deviceContext) {
             LOG.warn("Something went wrong, device context for nodeId: {} doesn't exists");
             return;
         }
         if (!success) {
-            LOG.warn("Role change to {} in role context for node {} was NOT successful, closing connection", newRole, nodeId);
-            closeConnection(nodeId);
+            LOG.warn("Role change to {} in role context for node {} was NOT successful, closing connection", newRole, deviceInfo);
+            closeConnection(deviceInfo);
         } else {
             if (initializationPhase) {
                 LOG.debug("Initialization phase skipping starting services.");
                 return;
             }
 
-            LOG.info("Role change to {} in role context for node {} was successful, starting/stopping services.", newRole, nodeId);
+            LOG.info("Role change to {} in role context for node {} was successful, starting/stopping services.", newRole, deviceInfo);
 
             if (OfpRole.BECOMEMASTER.equals(newRole)) {
-                statisticsManager.startScheduling(nodeId);
+                statisticsManager.startScheduling(deviceContext.getPrimaryConnectionContext().getDeviceInfo());
             } else {
-                statisticsManager.stopScheduling(nodeId);
+                statisticsManager.stopScheduling(deviceContext.getPrimaryConnectionContext().getDeviceInfo());
             }
 
             final ListenableFuture<Void> onClusterRoleChange = deviceContext.onClusterRoleChange(null, newRole);
             Futures.addCallback(onClusterRoleChange, new FutureCallback<Void>() {
                 @Override
                 public void onSuccess(@Nullable final Void aVoid) {
-                    LOG.info("Starting/Stopping services for node {} was successful", nodeId);
-                    if (newRole.equals(OfpRole.BECOMESLAVE)) notifyServiceChangeListeners(nodeId, true);
+                    LOG.info("Starting/Stopping services for node {} was successful", deviceInfo);
+                    if (newRole.equals(OfpRole.BECOMESLAVE)) notifyServiceChangeListeners(deviceInfo, true);
                 }
 
                 @Override
                 public void onFailure(final Throwable throwable) {
-                    LOG.warn("Starting/Stopping services for node {} was NOT successful, closing connection", nodeId);
-                    closeConnection(nodeId);
+                    LOG.warn("Starting/Stopping services for node {} was NOT successful, closing connection", deviceInfo);
+                    closeConnection(deviceInfo);
                 }
             });
         }
@@ -152,43 +164,45 @@ public final class LifecycleConductorImpl implements LifecycleConductor, RoleCha
     }
 
     @Override
-    public DeviceContext getDeviceContext(final NodeId nodeId){
-         return deviceManager.getDeviceContextFromNodeId(nodeId);
+    public DeviceContext getDeviceContext(DeviceInfo deviceInfo){
+         return deviceManager.getDeviceContextFromNodeId(deviceInfo);
     }
 
-    public Short gainVersionSafely(final NodeId nodeId) {
-        return (null != getDeviceContext(nodeId)) ? getDeviceContext(nodeId).getPrimaryConnectionContext().getFeatures().getVersion() : null;
+    @Override
+    public Short gainVersionSafely(final DeviceInfo deviceInfo) {
+        return (null != getDeviceContext(deviceInfo)) ? getDeviceContext(deviceInfo).getPrimaryConnectionContext().getFeatures().getVersion() : null;
     }
 
     public Timeout newTimeout(@Nonnull TimerTask task, long delay, @Nonnull TimeUnit unit) {
         return hashedWheelTimer.newTimeout(task, delay, unit);
     }
 
-    public ConnectionContext.CONNECTION_STATE gainConnectionStateSafely(final NodeId nodeId){
-        return (null != getDeviceContext(nodeId)) ? getDeviceContext(nodeId).getPrimaryConnectionContext().getConnectionState() : null;
+    ConnectionContext.CONNECTION_STATE gainConnectionStateSafely(final DeviceInfo deviceInfo){
+        return (null != getDeviceContext(deviceInfo)) ? getDeviceContext(deviceInfo).getPrimaryConnectionContext().getConnectionState() : null;
     }
 
-    public Long reserveXidForDeviceMessage(final NodeId nodeId){
-        return null != getDeviceContext(nodeId) ? getDeviceContext(nodeId).reserveXidForDeviceMessage() : null;
+    @Override
+    public Long reserveXidForDeviceMessage(final DeviceInfo deviceInfo){
+        return null != getDeviceContext(deviceInfo) ? getDeviceContext(deviceInfo).reserveXidForDeviceMessage() : null;
     }
 
     @Override
-    public void deviceStartInitializationDone(final NodeId nodeId, final boolean success) {
+    public void deviceStartInitializationDone(final DeviceInfo deviceInfo, final boolean success) {
         if (!success) {
-            LOG.warn("Initialization phase for node {} in device context was NOT successful, closing connection.", nodeId);
-            closeConnection(nodeId);
+            LOG.warn("Initialization phase for node {} in device context was NOT successful, closing connection.", deviceInfo);
+            closeConnection(deviceInfo);
         } else {
-            LOG.info("initialization phase for node {} in device context was successful. Continuing to next context.", nodeId);
+            LOG.info("initialization phase for node {} in device context was successful. Continuing to next context.", deviceInfo);
         }
     }
 
     @Override
-    public void deviceInitializationDone(final NodeId nodeId, final boolean success) {
+    public void deviceInitializationDone(final DeviceInfo deviceInfo, final boolean success) {
         if (!success) {
-            LOG.warn("Initialization phase for node {} in device context was NOT successful, closing connection.", nodeId);
-            closeConnection(nodeId);
+            LOG.warn("Initialization phase for node {} in device context was NOT successful, closing connection.", deviceInfo);
+            closeConnection(deviceInfo);
         } else {
-            LOG.info("initialization phase for node {} in device context was successful. All phases initialized OK.", nodeId);
+            LOG.info("initialization phase for node {} in device context was successful. All phases initialized OK.", deviceInfo);
         }
     }