Switch to MD-SAL APIs
[openflowplugin.git] / applications / topology-lldp-discovery / src / main / java / org / opendaylight / openflowplugin / applications / topology / lldp / LLDPDiscoveryListener.java
index 097d0edc4ebc0089c11375afa782ad9577ccb387..aba6fd942a5305146b90a49002204461c32e9269 100644 (file)
@@ -7,38 +7,65 @@
  */
 package org.opendaylight.openflowplugin.applications.topology.lldp;
 
-import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.apache.aries.blueprint.annotation.service.Reference;
+import org.opendaylight.mdsal.binding.api.NotificationPublishService;
+import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
 import org.opendaylight.openflowplugin.applications.topology.lldp.utils.LLDPDiscoveryUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscoveredBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@Singleton
 public class LLDPDiscoveryListener implements PacketProcessingListener {
     private static final Logger LOG = LoggerFactory.getLogger(LLDPDiscoveryListener.class);
 
     private final LLDPLinkAger lldpLinkAger;
-    private final NotificationProviderService notificationService;
+    private final NotificationPublishService notificationService;
+    private final EntityOwnershipService eos;
 
-    public LLDPDiscoveryListener(NotificationProviderService notificationService, LLDPLinkAger lldpLinkAger) {
+    @Inject
+    public LLDPDiscoveryListener(@Reference final NotificationPublishService notificationService,
+            final LLDPLinkAger lldpLinkAger, @Reference final EntityOwnershipService entityOwnershipService) {
         this.notificationService = notificationService;
         this.lldpLinkAger = lldpLinkAger;
+        this.eos = entityOwnershipService;
     }
 
     @Override
     public void onPacketReceived(PacketReceived lldp) {
         NodeConnectorRef src = LLDPDiscoveryUtils.lldpToNodeConnectorRef(lldp.getPayload(), true);
-        if(src != null) {
-            LinkDiscoveredBuilder ldb = new LinkDiscoveredBuilder();
-            ldb.setDestination(lldp.getIngress());
-            ldb.setSource(new NodeConnectorRef(src));
-            LinkDiscovered ld = ldb.build();
+        if (src != null) {
+            final NodeKey nodeKey = lldp.getIngress().getValue().firstKeyOf(Node.class);
+            LOG.debug("LLDP packet received for destination node {}", nodeKey);
+            if (nodeKey != null) {
+                LinkDiscoveredBuilder ldb = new LinkDiscoveredBuilder();
+                ldb.setDestination(lldp.getIngress());
+                ldb.setSource(new NodeConnectorRef(src));
+                LinkDiscovered ld = ldb.build();
 
-            notificationService.publish(ld);
-            lldpLinkAger.put(ld);
+                lldpLinkAger.put(ld);
+                if (LLDPDiscoveryUtils.isEntityOwned(this.eos, nodeKey.getId().getValue())) {
+                    LOG.debug("Publish add event for link {}", ld);
+                    try {
+                        notificationService.putNotification(ld);
+                    } catch (InterruptedException e) {
+                        LOG.warn("Interrupted while publishing notification {}", ld, e);
+                    }
+                } else {
+                    LOG.trace("Skip publishing the add event for link because controller is non-owner of the "
+                            + "node {}. Link : {}", nodeKey.getId().getValue(), ld);
+                }
+            } else {
+                LOG.debug("LLDP packet ignored. Unable to extract node-key from packet-in ingress.");
+            }
         }
     }
-}
\ No newline at end of file
+}