5d5bc18429c265f6ec77788cd51f6952a9187a69
[openflowplugin.git] / applications / topology-lldp-discovery / src / main / java / org / opendaylight / openflowplugin / applications / topology / lldp / LLDPDiscoveryListener.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.applications.topology.lldp;
9
10 import javax.inject.Inject;
11 import javax.inject.Singleton;
12 import org.apache.aries.blueprint.annotation.service.Reference;
13 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
14 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
15 import org.opendaylight.openflowplugin.applications.topology.lldp.utils.LLDPDiscoveryUtils;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscoveredBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 @Singleton
27 public class LLDPDiscoveryListener implements PacketProcessingListener {
28     private static final Logger LOG = LoggerFactory.getLogger(LLDPDiscoveryListener.class);
29
30     private final LLDPLinkAger lldpLinkAger;
31     private final NotificationPublishService notificationService;
32     private final EntityOwnershipService eos;
33
34     @Inject
35     public LLDPDiscoveryListener(@Reference final NotificationPublishService notificationService,
36             final LLDPLinkAger lldpLinkAger, @Reference final EntityOwnershipService entityOwnershipService) {
37         this.notificationService = notificationService;
38         this.lldpLinkAger = lldpLinkAger;
39         this.eos = entityOwnershipService;
40     }
41
42     @Override
43     public void onPacketReceived(final PacketReceived lldp) {
44         NodeConnectorRef src = LLDPDiscoveryUtils.lldpToNodeConnectorRef(lldp.getPayload(), true);
45         if (src != null) {
46             final NodeKey nodeKey = lldp.getIngress().getValue().firstKeyOf(Node.class);
47             LOG.debug("LLDP packet received for destination node {}", nodeKey);
48             if (nodeKey != null) {
49                 final LinkDiscoveredBuilder ldb = new LinkDiscoveredBuilder();
50                 ldb.setDestination(lldp.getIngress());
51                 ldb.setSource(new NodeConnectorRef(src));
52                 final LinkDiscovered ld = ldb.build();
53                 final boolean linkWasPresent = lldpLinkAger.isLinkPresent(ld);
54                 lldpLinkAger.put(ld);
55                 if (LLDPDiscoveryUtils.isEntityOwned(this.eos, nodeKey.getId().getValue())) {
56                     if (linkWasPresent) {
57                         LOG.trace("Link {} already present in the cache, skip publishing the notification.", ld);
58                     } else {
59                         LOG.debug("Publish add event for link {}", ld);
60                         try {
61                             notificationService.putNotification(ld);
62                         } catch (InterruptedException e) {
63                             LOG.warn("Interrupted while publishing notification {}", ld, e);
64                         }
65                     }
66                 } else {
67                     LOG.trace("Skip publishing the add event for link because controller is non-owner of the "
68                             + "node {}. Link : {}", nodeKey.getId().getValue(), ld);
69                 }
70             } else {
71                 LOG.debug("LLDP packet ignored. Unable to extract node-key from packet-in ingress.");
72             }
73         }
74     }
75 }