Bug 1985 - NPE when running groupbasedpolicy POC demo
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / SalRegistrationManager.java
index 84079a8bf915db352bc9e2734006c2d61bcc31b2..7a2d0c7121748919bb12244ba652b37964595f8e 100644 (file)
@@ -8,20 +8,23 @@
 package org.opendaylight.openflowplugin.openflow.md.core.sal;
 
 import java.math.BigInteger;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
+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;
-import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
 import org.opendaylight.openflowplugin.openflow.md.ModelDrivenSwitch;
-import org.opendaylight.openflowplugin.openflow.md.SwitchInventory;
-import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionListener;
 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionManager;
+import org.opendaylight.openflowplugin.openflow.md.core.session.SwitchSessionKeyOF;
 import org.opendaylight.openflowplugin.openflow.md.lldp.LLDPSpeaker;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdatedBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
@@ -51,10 +54,10 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
 
     private NotificationProviderService publishService;
 
-    private DataProviderService dataService;
-    
+    private DataBroker dataService;
+
     private SwitchFeaturesUtil swFeaturesUtil;
-    
+
     public SalRegistrationManager() {
         swFeaturesUtil = SwitchFeaturesUtil.getInstance();
     }
@@ -75,16 +78,16 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
         LOG.debug("onSessionInitiated");
         this.providerContext = session;
         this.publishService = session.getSALService(NotificationProviderService.class);
-        this.dataService = session.getSALService(DataProviderService.class);
+        this.dataService = session.getSALService(DataBroker.class);
         // We register as listener for Session Manager
         getSessionManager().registerSessionListener(this);
         getSessionManager().setNotificationProviderService(publishService);
-        getSessionManager().setDataProviderService(dataService);
+        getSessionManager().setDataBroker(dataService);
         LOG.debug("SalRegistrationManager initialized");
     }
 
     @Override
-    public void onSessionAdded(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
+    public void onSessionAdded(SwitchSessionKeyOF sessionKey, SessionContext context) {
         GetFeaturesOutput features = context.getFeatures();
         BigInteger datapathId = features.getDatapathId();
         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
@@ -97,7 +100,7 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
 
         LOG.debug("ModelDrivenSwitch for {} registered to MD-SAL.", datapathId.toString());
 
-        publishService.publish(nodeAdded(ofSwitch, features,nodeRef));
+        publishService.publish(nodeAdded(ofSwitch, features, nodeRef));
     }
 
     @Override
@@ -108,9 +111,11 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
         NodeRef nodeRef = new NodeRef(identifier);
         NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
         LLDPSpeaker.getInstance().removeModelDrivenSwitch(identifier);
-        CompositeObjectRegistration<ModelDrivenSwitch> registration = context.getProviderRegistration();
-        registration.close();
-        
+        if (context.isValid()) {
+            CompositeObjectRegistration<ModelDrivenSwitch> registration = context.getProviderRegistration();
+            registration.close();
+        }
+
         LOG.debug("ModelDrivenSwitch for {} unregistered from MD-SAL.", datapathId.toString());
         publishService.publish(nodeRemoved);
     }
@@ -119,14 +124,41 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
         NodeUpdatedBuilder builder = new NodeUpdatedBuilder();
         builder.setId(sw.getNodeId());
         builder.setNodeRef(nodeRef);
-        
+
         FlowCapableNodeUpdatedBuilder builder2 = new FlowCapableNodeUpdatedBuilder();
+        builder2.setIpAddress(getIpAddressOf(sw));
         builder2.setSwitchFeatures(swFeaturesUtil.buildSwitchFeatures(features));
         builder.addAugmentation(FlowCapableNodeUpdated.class, builder2.build());
-        
+
         return builder.build();
     }
 
+    private 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;
+        }
+        InetSocketAddress remoteAddress = sessionContext.getPrimaryConductor().getConnectionAdapter()
+                .getRemoteAddress();
+        if (remoteAddress == null) {
+            LOG.warn("IP address of the node {} cannot be obtained. No connection with switch.", sw.getNodeId());
+            return null;
+        }
+        return resolveIpAddress(remoteAddress.getAddress());
+    }
+
+    private static IpAddress resolveIpAddress(InetAddress address) {
+        String hostAddress = address.getHostAddress();
+        if (address instanceof Inet4Address) {
+            return new IpAddress(new Ipv4Address(hostAddress));
+        }
+        if (address instanceof Inet6Address) {
+            return new IpAddress(new Ipv6Address(hostAddress));
+        }
+        throw new IllegalArgumentException("Unsupported IP address type!");
+    }
+
     private NodeRemoved nodeRemoved(NodeRef nodeRef) {
         NodeRemovedBuilder builder = new NodeRemovedBuilder();
         builder.setNodeRef(nodeRef);
@@ -135,7 +167,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);
+        InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
         return builder.toInstance();
     }
 
@@ -152,7 +184,7 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
     public SessionManager getSessionManager() {
         return OFSessionUtil.getSessionManager();
     }
-    
+
     @Override
     public void close() {
         LOG.debug("close");