Merge "Bug 6366 - of-switch-config-pusher - DTCL instead of DTL"
[openflowplugin.git] / applications / lldp-speaker / src / main / java / org / opendaylight / openflowplugin / applications / lldpspeaker / LLDPSpeaker.java
1 /*
2  * Copyright (c) 2014 Pacnet 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
9 package org.opendaylight.openflowplugin.applications.lldpspeaker;
10
11 import java.util.Map;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.Executors;
14 import java.util.concurrent.ScheduledExecutorService;
15 import java.util.concurrent.ScheduledFuture;
16 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketProcessingService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.applications.lldp.speaker.rev141023.OperStatus;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Objects of this class send LLDP frames over all flow-capable ports that can
35  * be discovered through inventory.
36  */
37 public class LLDPSpeaker implements AutoCloseable, NodeConnectorEventsObserver, Runnable {
38     private static final Logger LOG = LoggerFactory.getLogger(LLDPSpeaker.class);
39     private static final long LLDP_FLOOD_PERIOD = 5;
40
41     private final PacketProcessingService packetProcessingService;
42     private final ScheduledExecutorService scheduledExecutorService;
43     private final Map<InstanceIdentifier<NodeConnector>, TransmitPacketInput> nodeConnectorMap =
44             new ConcurrentHashMap<>();
45     private final ScheduledFuture<?> scheduledSpeakerTask;
46     private final MacAddress addressDestionation;
47     private volatile OperStatus operationalStatus = OperStatus.RUN;
48
49     public LLDPSpeaker(final PacketProcessingService packetProcessingService, final MacAddress addressDestionation) {
50         this(packetProcessingService, Executors.newSingleThreadScheduledExecutor(), addressDestionation);
51     }
52
53     public void setOperationalStatus(final OperStatus operationalStatus) {
54         LOG.info("Setting operational status to {}", operationalStatus);
55         this.operationalStatus = operationalStatus;
56         if (operationalStatus.equals(OperStatus.STANDBY)) {
57             nodeConnectorMap.clear();
58         }
59     }
60
61     public OperStatus getOperationalStatus() {
62         return operationalStatus;
63     }
64
65     public LLDPSpeaker(final PacketProcessingService packetProcessingService,
66                        final ScheduledExecutorService scheduledExecutorService,
67                        final MacAddress addressDestionation) {
68         this.addressDestionation = addressDestionation;
69         this.scheduledExecutorService = scheduledExecutorService;
70         scheduledSpeakerTask = this.scheduledExecutorService
71                 .scheduleAtFixedRate(this, LLDP_FLOOD_PERIOD,LLDP_FLOOD_PERIOD, TimeUnit.SECONDS);
72         this.packetProcessingService = packetProcessingService;
73         LOG.info("LLDPSpeaker started, it will send LLDP frames each {} seconds", LLDP_FLOOD_PERIOD);
74     }
75
76     /**
77      * Closes this resource, relinquishing any underlying resources.
78      */
79     @Override
80     public void close() {
81         nodeConnectorMap.clear();
82         scheduledExecutorService.shutdown();
83         scheduledSpeakerTask.cancel(true);
84         LOG.trace("LLDPSpeaker stopped sending LLDP frames.");
85     }
86
87     /**
88      * Send LLDPDU frames to all known openflow switch ports.
89      */
90     @Override
91     public void run() {
92         if (OperStatus.RUN.equals(operationalStatus)) {
93             LOG.debug("Sending LLDP frames to {} ports...", nodeConnectorMap.keySet().size());
94             for (InstanceIdentifier<NodeConnector> nodeConnectorInstanceId : nodeConnectorMap.keySet()) {
95                 NodeConnectorId nodeConnectorId = InstanceIdentifier.keyOf(nodeConnectorInstanceId).getId();
96                 LOG.trace("Sending LLDP through port {}", nodeConnectorId.getValue());
97                 packetProcessingService.transmitPacket(nodeConnectorMap.get(nodeConnectorInstanceId));
98             }
99         }
100     }
101
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public void nodeConnectorAdded(final InstanceIdentifier<NodeConnector> nodeConnectorInstanceId,
107                                    final FlowCapableNodeConnector flowConnector) {
108         NodeConnectorId nodeConnectorId = InstanceIdentifier.keyOf(nodeConnectorInstanceId).getId();
109
110         // nodeConnectorAdded can be called even if we already sending LLDP
111         // frames to
112         // port, so first we check if we actually need to perform any action
113         if (nodeConnectorMap.containsKey(nodeConnectorInstanceId)) {
114             LOG.trace(
115                     "Port {} already in LLDPSpeaker.nodeConnectorMap, no need for additional processing",
116                     nodeConnectorId.getValue());
117             return;
118         }
119
120         // Prepare to build LLDP payload
121         InstanceIdentifier<Node> nodeInstanceId = nodeConnectorInstanceId.firstIdentifierOf(Node.class);
122         NodeId nodeId = InstanceIdentifier.keyOf(nodeInstanceId).getId();
123         MacAddress srcMacAddress = flowConnector.getHardwareAddress();
124         Long outputPortNo = flowConnector.getPortNumber().getUint32();
125
126         // No need to send LLDP frames on local ports
127         if (outputPortNo == null) {
128             LOG.trace("Port {} is local, not sending LLDP frames through it", nodeConnectorId.getValue());
129             return;
130         }
131
132         // Generate packet with destination switch and port
133         TransmitPacketInput packet = new TransmitPacketInputBuilder()
134                 .setEgress(new NodeConnectorRef(nodeConnectorInstanceId))
135                 .setNode(new NodeRef(nodeInstanceId))
136                 .setPayload(LLDPUtil
137                         .buildLldpFrame(nodeId, nodeConnectorId, srcMacAddress, outputPortNo, addressDestionation))
138                 .build();
139
140         // Save packet to node connector id -> packet map to transmit it every 5
141         // seconds
142         nodeConnectorMap.put(nodeConnectorInstanceId, packet);
143         LOG.trace("Port {} added to LLDPSpeaker.nodeConnectorMap", nodeConnectorId.getValue());
144
145         // Transmit packet for first time immediately
146         packetProcessingService.transmitPacket(packet);
147     }
148
149     /**
150      * {@inheritDoc}
151      */
152     @Override
153     public void nodeConnectorRemoved(final InstanceIdentifier<NodeConnector> nodeConnectorInstanceId) {
154         nodeConnectorMap.remove(nodeConnectorInstanceId);
155         NodeConnectorId nodeConnectorId = InstanceIdentifier.keyOf(nodeConnectorInstanceId).getId();
156         LOG.trace("Port {} removed from LLDPSpeaker.nodeConnectorMap", nodeConnectorId.getValue());
157     }
158
159 }