Bug 1254 - added basic functionality test for LLDPSpeakerPopListener
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / lldp / LLDPSpeaker.java
1 /**
2  * Copyright (c) 2013 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.openflow.md.lldp;
9
10 import java.math.BigInteger;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Timer;
15 import java.util.TimerTask;
16 import java.util.concurrent.ConcurrentHashMap;
17 import org.opendaylight.controller.liblldp.EtherTypes;
18 import org.opendaylight.controller.liblldp.Ethernet;
19 import org.opendaylight.controller.liblldp.HexEncode;
20 import org.opendaylight.controller.liblldp.LLDP;
21 import org.opendaylight.controller.liblldp.LLDPTLV;
22 import org.opendaylight.controller.liblldp.PacketException;
23 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
24 import org.opendaylight.openflowplugin.openflow.md.ModelDrivenSwitch;
25 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInputBuilder;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41
42 public class LLDPSpeaker {
43     private static Logger LOG = LoggerFactory.getLogger(LLDPSpeaker.class);
44
45     private final Map<InstanceIdentifier<NodeConnector>, TransmitPacketInput> nodeConnectorMap = new ConcurrentHashMap<InstanceIdentifier<NodeConnector>, TransmitPacketInput>();
46     private final Map<InstanceIdentifier<Node>, ModelDrivenSwitch> nodeMap = new ConcurrentHashMap<InstanceIdentifier<Node>, ModelDrivenSwitch>();
47     private static final LLDPSpeaker instance = new LLDPSpeaker();
48     private Timer timer = new Timer();
49     private static final int DELAY = 0;
50     private static final int PERIOD = 1000 * 5;
51
52     private LLDPSpeaker() {
53         timer.schedule(new LLDPSpeakerTask(), DELAY, PERIOD);
54     }
55
56     public static LLDPSpeaker getInstance() {
57         return instance;
58     }
59
60     public void addModelDrivenSwitch(InstanceIdentifier<Node> nodeInstanceId, ModelDrivenSwitch sw) {
61         nodeMap.put(nodeInstanceId, sw);
62     }
63
64     public void removeModelDrivenSwitch(InstanceIdentifier<Node> nodeInstanceId) {
65         nodeMap.remove(nodeInstanceId);
66         for (InstanceIdentifier<NodeConnector> nodeConnectorInstanceId : nodeConnectorMap.keySet()) {
67             if (nodeInstanceId.equals(nodeConnectorInstanceId.firstIdentifierOf(Node.class))) {
68                 nodeConnectorMap.remove(nodeConnectorInstanceId);
69             }
70         }
71     }
72
73     public void addNodeConnector(InstanceIdentifier<NodeConnector> nodeConnectorInstanceId, NodeConnector nodeConnector) {
74         InstanceIdentifier<Node> nodeInstanceId = nodeConnectorInstanceId.firstIdentifierOf(Node.class);
75         ModelDrivenSwitch md = nodeMap.get(nodeInstanceId);
76
77         NodeKey nodeKey = InstanceIdentifier.keyOf(nodeInstanceId);
78         NodeId nodeId = nodeKey.getId();
79         NodeConnectorId nodeConnectorId = nodeConnector.getId();
80         FlowCapableNodeConnector flowConnector = nodeConnector.<FlowCapableNodeConnector>getAugmentation(FlowCapableNodeConnector.class);
81         TransmitPacketInputBuilder tpib = new TransmitPacketInputBuilder();
82         tpib.setEgress(new NodeConnectorRef(nodeConnectorInstanceId));
83         tpib.setNode(new NodeRef(nodeInstanceId));
84         tpib.setPayload(lldpDataFrom(nodeInstanceId, nodeConnectorInstanceId, flowConnector.getHardwareAddress(),
85                 md.getSessionContext().getPrimaryConductor().getVersion()));
86         nodeConnectorMap.put(nodeConnectorInstanceId, tpib.build());
87
88         md.transmitPacket(nodeConnectorMap.get(nodeConnectorInstanceId));
89     }
90
91     public void removeNodeConnector(
92             InstanceIdentifier<NodeConnector> nodeConnectorInstanceId,
93             NodeConnector nodeConnector) {
94         nodeConnectorMap.remove(nodeConnectorInstanceId);
95     }
96
97     private byte[] lldpDataFrom(InstanceIdentifier<Node> nodeInstanceId, InstanceIdentifier<NodeConnector> nodeConnectorInstanceId, MacAddress src,
98                                 Short version) {
99
100         NodeId nodeId = InstanceIdentifier.keyOf(nodeInstanceId).getId();
101         NodeConnectorId nodeConnectorId = InstanceIdentifier.keyOf(nodeConnectorInstanceId).getId();
102         // Create LLDP TTL TLV
103         byte[] ttl = new byte[]{(byte) 0, (byte) 120};
104         LLDPTLV ttlTlv = new LLDPTLV();
105         ttlTlv.setType(LLDPTLV.TLVType.TTL.getValue()).setLength((short) ttl.length).setValue(ttl);
106
107         // Create LLDP ChassisID TLV
108         BigInteger dataPathId = InventoryDataServiceUtil.dataPathIdFromNodeId(nodeId);
109         byte[] cidValue = LLDPTLV.createChassisIDTLVValue(
110                 colonize(InventoryDataServiceUtil.bigIntegerToPaddedHex(dataPathId)));
111         LLDPTLV chassisIdTlv = new LLDPTLV();
112         chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue());
113         chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue()).setLength((short) cidValue.length)
114                 .setValue(cidValue);
115
116         // Create LLDP SystemName TLV
117         byte[] snValue = LLDPTLV.createSystemNameTLVValue(nodeId.getValue());
118         LLDPTLV systemNameTlv = new LLDPTLV();
119         systemNameTlv.setType(LLDPTLV.TLVType.SystemName.getValue());
120         systemNameTlv.setType(LLDPTLV.TLVType.SystemName.getValue()).setLength((short) snValue.length)
121                 .setValue(snValue);
122
123         // Create LLDP PortID TL
124         Long portNo = InventoryDataServiceUtil.portNumberfromNodeConnectorId(OpenflowVersion.get(version), nodeConnectorId);
125
126         String hexString = Long.toHexString(portNo);
127         byte[] pidValue = LLDPTLV.createPortIDTLVValue(hexString);
128         LLDPTLV portIdTlv = new LLDPTLV();
129         portIdTlv.setType(LLDPTLV.TLVType.PortID.getValue()).setLength((short) pidValue.length).setValue(pidValue);
130         portIdTlv.setType(LLDPTLV.TLVType.PortID.getValue());
131
132         // Create LLDP Custom TLV
133         byte[] customValue = LLDPTLV.createCustomTLVValue(nodeConnectorId.getValue());
134         LLDPTLV customTlv = new LLDPTLV();
135         customTlv.setType(LLDPTLV.TLVType.Custom.getValue()).setLength((short) customValue.length)
136                 .setValue(customValue);
137
138         // Create LLDP Custom Option list
139         List<LLDPTLV> customList = Collections.singletonList(customTlv);
140
141         // Create discovery pkt
142         LLDP discoveryPkt = new LLDP();
143         discoveryPkt.setChassisId(chassisIdTlv).setPortId(portIdTlv).setTtl(ttlTlv).setSystemNameId(systemNameTlv)
144                 .setOptionalTLVList(customList);
145
146         // Create ethernet pkt
147         byte[] sourceMac = HexEncode.bytesFromHexString(src.getValue());
148         Ethernet ethPkt = new Ethernet();
149         ethPkt.setSourceMACAddress(sourceMac).setDestinationMACAddress(LLDP.LLDPMulticastMac)
150                 .setEtherType(EtherTypes.LLDP.shortValue()).setPayload(discoveryPkt);
151
152         try {
153             byte[] data = ethPkt.serialize();
154             return data;
155         } catch (PacketException e) {
156             LOG.error("Error creating LLDP packet", e);
157         }
158         return null;
159     }
160
161     private class LLDPSpeakerTask extends TimerTask {
162
163         @Override
164         public void run() {
165             for (InstanceIdentifier<NodeConnector> nodeConnectorInstanceId : nodeConnectorMap.keySet()) {
166                 InstanceIdentifier<Node> nodeInstanceId = nodeConnectorInstanceId.firstIdentifierOf(Node.class);
167                 ModelDrivenSwitch md = nodeMap.get(nodeInstanceId);
168                 md.transmitPacket(nodeConnectorMap.get(nodeConnectorInstanceId));
169             }
170
171         }
172
173     }
174
175     private String colonize(String orig) {
176         return orig.replaceAll("(?<=..)(..)", ":$1");
177     }
178 }