Merge "BUG-86: Fixed echo response processing"
[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 org.opendaylight.controller.sal.binding.api.NotificationProviderService;
11 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
12 import org.opendaylight.openflowplugin.applications.topology.lldp.utils.LLDPDiscoveryUtils;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscovered;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.topology.discovery.rev130819.LinkDiscoveredBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingListener;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class LLDPDiscoveryListener implements PacketProcessingListener {
24     private static final Logger LOG = LoggerFactory.getLogger(LLDPDiscoveryListener.class);
25
26     private final LLDPLinkAger lldpLinkAger;
27     private final NotificationProviderService notificationService;
28     private final EntityOwnershipService eos;
29
30
31     public LLDPDiscoveryListener(final NotificationProviderService notificationService, final LLDPLinkAger lldpLinkAger,
32             final EntityOwnershipService entityOwnershipService) {
33         this.notificationService = notificationService;
34         this.lldpLinkAger = lldpLinkAger;
35         this.eos = entityOwnershipService;
36     }
37
38     @Override
39     public void onPacketReceived(PacketReceived lldp) {
40         NodeConnectorRef src = LLDPDiscoveryUtils.lldpToNodeConnectorRef(lldp.getPayload(), true);
41         if (src != null) {
42             NodeKey nodeKey = src.getValue().firstKeyOf(Node.class);
43             LOG.debug("LLDP packet received for node {}", nodeKey);
44             if (nodeKey != null) {
45                 LinkDiscoveredBuilder ldb = new LinkDiscoveredBuilder();
46                 ldb.setDestination(lldp.getIngress());
47                 ldb.setSource(new NodeConnectorRef(src));
48                 LinkDiscovered ld = ldb.build();
49
50                 lldpLinkAger.put(ld);
51                 if (LLDPDiscoveryUtils.isEntityOwned(this.eos, nodeKey.getId().getValue())) {
52                     LOG.debug("Publish add event for link {}", ld);
53                     notificationService.publish(ld);
54                 } else {
55                     LOG.trace("Skip publishing the add event for link because controller is non-owner of the " +
56                             "node {}. Link : {}", nodeKey.getId().getValue(), ld);
57                 }
58             } else {
59                 LOG.debug("LLDP packet ignored. Unable to extract node-key from source node-connector reference.");
60             }
61         }
62     }
63 }