2766e169eefd77445d8f5e6cbb7814453f99a8fe
[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.opendaylight.mdsal.binding.api.NotificationPublishService;
13 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
14 import org.opendaylight.openflowplugin.applications.topology.lldp.utils.LLDPDiscoveryUtils;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscoveredBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 @Singleton
26 public class LLDPDiscoveryListener implements PacketProcessingListener {
27     private static final Logger LOG = LoggerFactory.getLogger(LLDPDiscoveryListener.class);
28
29     private final LLDPLinkAger lldpLinkAger;
30     private final NotificationPublishService notificationService;
31     private final EntityOwnershipService eos;
32
33     @Inject
34     public LLDPDiscoveryListener(final NotificationPublishService notificationService,
35             final LLDPLinkAger lldpLinkAger, final EntityOwnershipService entityOwnershipService) {
36         this.notificationService = notificationService;
37         this.lldpLinkAger = lldpLinkAger;
38         this.eos = entityOwnershipService;
39     }
40
41     @Override
42     public void onPacketReceived(final PacketReceived lldp) {
43         NodeConnectorRef src = LLDPDiscoveryUtils.lldpToNodeConnectorRef(lldp.getPayload(), true);
44         if (src != null) {
45             final NodeKey nodeKey = lldp.getIngress().getValue().firstKeyOf(Node.class);
46             LOG.debug("LLDP packet received for destination node {}", nodeKey);
47             if (nodeKey != null) {
48                 final LinkDiscoveredBuilder ldb = new LinkDiscoveredBuilder();
49                 ldb.setDestination(lldp.getIngress());
50                 ldb.setSource(new NodeConnectorRef(src));
51                 final LinkDiscovered ld = ldb.build();
52                 final boolean linkWasPresent = lldpLinkAger.isLinkPresent(ld);
53                 lldpLinkAger.put(ld);
54                 if (LLDPDiscoveryUtils.isEntityOwned(this.eos, nodeKey.getId().getValue())) {
55                     if (linkWasPresent) {
56                         LOG.trace("Link {} already present in the cache, skip publishing the notification.", ld);
57                     } else {
58                         LOG.debug("Publish add event for link {}", ld);
59                         try {
60                             notificationService.putNotification(ld);
61                         } catch (InterruptedException e) {
62                             LOG.warn("Interrupted while publishing notification {}", ld, e);
63                         }
64                     }
65                 } else {
66                     LOG.trace("Skip publishing the add event for link because controller is non-owner of the "
67                             + "node {}. Link : {}", nodeKey.getId().getValue(), ld);
68                 }
69             } else {
70                 LOG.debug("LLDP packet ignored. Unable to extract node-key from packet-in ingress.");
71             }
72         }
73     }
74 }