Revert "Bug-2827: role switch proposal"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / SalRegistrationManager.java
index ec62918fc5a06e69a57626c2d60a140d5cb41ea9..02d60ac1109a5def49f06e7510dfb185bb0e707f 100644 (file)
@@ -7,12 +7,12 @@
  */
 package org.opendaylight.openflowplugin.openflow.md.core.sal;
 
+import com.google.common.base.Preconditions;
 import java.math.BigInteger;
 import java.net.Inet4Address;
 import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
-
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
@@ -50,7 +50,7 @@ import org.slf4j.LoggerFactory;
  */
 public class SalRegistrationManager implements SessionListener, AutoCloseable {
 
-    private final static Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
+    private static final Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
 
     private ProviderContext providerContext;
 
@@ -104,7 +104,7 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
         LOG.debug("ModelDrivenSwitch for {} registered to MD-SAL.", datapathId.toString());
 
         NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
-                nodeAdded(ofSwitch, features, nodeRef), 
+                nodeAdded(ofSwitch, features, nodeRef),
                 context.getFeatures().getVersion());
         context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
     }
@@ -116,13 +116,14 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
         NodeRef nodeRef = new NodeRef(identifier);
         NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
-        if (context.isValid()) {
-            CompositeObjectRegistration<ModelDrivenSwitch> registration = context.getProviderRegistration();
+
+        CompositeObjectRegistration<ModelDrivenSwitch> registration = context.getProviderRegistration();
+        if (null != registration) {
             registration.close();
+            context.setProviderRegistration(null);
         }
-
         LOG.debug("ModelDrivenSwitch for {} unregistered from MD-SAL.", datapathId.toString());
-        
+
         NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
                 nodeRemoved, context.getFeatures().getVersion());
         context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
@@ -134,19 +135,23 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
         builder.setNodeRef(nodeRef);
 
         FlowCapableNodeUpdatedBuilder builder2 = new FlowCapableNodeUpdatedBuilder();
-        builder2.setIpAddress(getIpAddressOf(sw));
+        try {
+            builder2.setIpAddress(getIpAddressOf(sw));
+        } catch (Exception e) {
+            LOG.warn("IP address of the node {} cannot be obtained.", sw.getNodeId(), e);
+        }
         builder2.setSwitchFeatures(swFeaturesUtil.buildSwitchFeatures(features));
         builder.addAugmentation(FlowCapableNodeUpdated.class, builder2.build());
 
         return builder.build();
     }
 
-    private IpAddress getIpAddressOf(ModelDrivenSwitch sw) {
+    private static IpAddress getIpAddressOf(ModelDrivenSwitch sw) {
         SessionContext sessionContext = sw.getSessionContext();
-        if (!sessionContext.isValid()) {
-            LOG.warn("IP address of the node {} cannot be obtained. Session is not valid.", sw.getNodeId());
-            return null;
-        }
+        Preconditions.checkNotNull(sessionContext.getPrimaryConductor(),
+                "primary conductor must not be NULL -> " + sw.getNodeId());
+        Preconditions.checkNotNull(sessionContext.getPrimaryConductor().getConnectionAdapter(),
+                "connection adapter of primary conductor must not be NULL -> " + sw.getNodeId());
         InetSocketAddress remoteAddress = sessionContext.getPrimaryConductor().getConnectionAdapter()
                 .getRemoteAddress();
         if (remoteAddress == null) {
@@ -176,7 +181,7 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
     public static InstanceIdentifier<Node> identifierFromDatapathId(BigInteger datapathId) {
         NodeKey nodeKey = nodeKeyFromDatapathId(datapathId);
         InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
-        return builder.toInstance();
+        return builder.build();
     }
 
     public static NodeKey nodeKeyFromDatapathId(BigInteger datapathId) {
@@ -203,4 +208,11 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
             sessionListenerRegistration.close();
         }
     }
+
+    /**
+     * @param providerContext the providerContext to set
+     */
+    public void setProviderContext(ProviderContext providerContext) {
+        this.providerContext = providerContext;
+    }
 }