Merge "Bug 3367 - StatManagerImpl scalable statPolling per stat type on device"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / SalRegistrationManager.java
index 84079a8bf915db352bc9e2734006c2d61bcc31b2..25e29aba26632fdf1c2e0c752f9616de86afd392 100644 (file)
@@ -7,21 +7,25 @@
  */
 package org.opendaylight.openflowplugin.openflow.md.core.sal;
 
+import com.google.common.base.Preconditions;
 import java.math.BigInteger;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
+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.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.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.openflowplugin.api.openflow.md.ModelDrivenSwitch;
+import org.opendaylight.openflowplugin.api.openflow.md.core.NotificationQueueWrapper;
+import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
+import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionListener;
+import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionManager;
+import org.opendaylight.openflowplugin.api.openflow.md.core.session.SwitchSessionKeyOF;
 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.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;
@@ -35,6 +39,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.N
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
 import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
 import org.slf4j.Logger;
@@ -45,16 +50,18 @@ import org.slf4j.LoggerFactory;
  */
 public class SalRegistrationManager implements SessionListener, AutoCloseable {
 
-    private final static Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
-
-    private ProviderContext providerContext;
+    private static final Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
 
     private NotificationProviderService publishService;
 
-    private DataProviderService dataService;
-    
+    private DataBroker dataService;
+
+    private RpcProviderRegistry rpcProviderRegistry;
+
     private SwitchFeaturesUtil swFeaturesUtil;
-    
+
+    private ListenerRegistration<SessionListener> sessionListenerRegistration;
+
     public SalRegistrationManager() {
         swFeaturesUtil = SwitchFeaturesUtil.getInstance();
     }
@@ -67,37 +74,40 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
         this.publishService = publishService;
     }
 
-    public ProviderContext getProviderContext() {
-        return providerContext;
+    public void setDataService(DataBroker dataService) {
+        this.dataService = dataService;
+    }
+
+    public void setRpcProviderRegistry(RpcProviderRegistry rpcProviderRegistry) {
+        this.rpcProviderRegistry = rpcProviderRegistry;
     }
 
-    public void onSessionInitiated(ProviderContext session) {
-        LOG.debug("onSessionInitiated");
-        this.providerContext = session;
-        this.publishService = session.getSALService(NotificationProviderService.class);
-        this.dataService = session.getSALService(DataProviderService.class);
-        // We register as listener for Session Manager
-        getSessionManager().registerSessionListener(this);
+    public void init() {
+        LOG.debug("init..");
+        sessionListenerRegistration = 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);
         NodeRef nodeRef = new NodeRef(identifier);
         NodeId nodeId = nodeIdFromDatapathId(datapathId);
         ModelDrivenSwitchImpl ofSwitch = new ModelDrivenSwitchImpl(nodeId, identifier, context);
-        LLDPSpeaker.getInstance().addModelDrivenSwitch(identifier, ofSwitch);
-        CompositeObjectRegistration<ModelDrivenSwitch> registration = ofSwitch.register(providerContext);
+        CompositeObjectRegistration<ModelDrivenSwitch> registration =
+                ofSwitch.register(rpcProviderRegistry);
         context.setProviderRegistration(registration);
 
-        LOG.debug("ModelDrivenSwitch for {} registered to MD-SAL.", datapathId.toString());
+        LOG.debug("ModelDrivenSwitch for {} registered to MD-SAL.", datapathId);
 
-        publishService.publish(nodeAdded(ofSwitch, features,nodeRef));
+        NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
+                nodeAdded(ofSwitch, features, nodeRef),
+                context.getFeatures().getVersion());
+        context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
     }
 
     @Override
@@ -107,26 +117,62 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
         NodeRef nodeRef = new NodeRef(identifier);
         NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
-        LLDPSpeaker.getInstance().removeModelDrivenSwitch(identifier);
+
         CompositeObjectRegistration<ModelDrivenSwitch> registration = context.getProviderRegistration();
-        registration.close();
-        
-        LOG.debug("ModelDrivenSwitch for {} unregistered from MD-SAL.", datapathId.toString());
-        publishService.publish(nodeRemoved);
+        if (null != registration) {
+            registration.close();
+            context.setProviderRegistration(null);
+        }
+        LOG.debug("ModelDrivenSwitch for {} unregistered from MD-SAL.", datapathId);
+
+        NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
+                nodeRemoved, context.getFeatures().getVersion());
+        context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
     }
 
     private NodeUpdated nodeAdded(ModelDrivenSwitch sw, GetFeaturesOutput features, NodeRef nodeRef) {
         NodeUpdatedBuilder builder = new NodeUpdatedBuilder();
         builder.setId(sw.getNodeId());
         builder.setNodeRef(nodeRef);
-        
+
         FlowCapableNodeUpdatedBuilder builder2 = new FlowCapableNodeUpdatedBuilder();
+        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 static IpAddress getIpAddressOf(ModelDrivenSwitch sw) {
+        SessionContext sessionContext = sw.getSessionContext();
+        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) {
+            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,8 +181,8 @@ 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();
+        InstanceIdentifierBuilder<Node> builder = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey);
+        return builder.build();
     }
 
     public static NodeKey nodeKeyFromDatapathId(BigInteger datapathId) {
@@ -145,19 +191,22 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
 
     public static NodeId nodeIdFromDatapathId(BigInteger datapathId) {
         // FIXME: Convert to textual representation of datapathID
-        String current = datapathId.toString();
+        String current = String.valueOf(datapathId);
         return new NodeId("openflow:" + current);
     }
 
     public SessionManager getSessionManager() {
         return OFSessionUtil.getSessionManager();
     }
-    
+
     @Override
     public void close() {
         LOG.debug("close");
         dataService = null;
-        providerContext = null;
+        rpcProviderRegistry = null;
         publishService = null;
+        if (sessionListenerRegistration != null) {
+            sessionListenerRegistration.close();
+        }
     }
 }