2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.openflow.md.lldp;
10 import java.util.Collections;
11 import java.util.List;
13 import java.util.Timer;
14 import java.util.TimerTask;
15 import java.util.concurrent.ConcurrentHashMap;
17 import org.apache.commons.lang3.StringUtils;
18 import org.opendaylight.controller.sal.packet.Ethernet;
19 import org.opendaylight.controller.sal.packet.LLDP;
20 import org.opendaylight.controller.sal.packet.LLDPTLV;
21 import org.opendaylight.controller.sal.packet.PacketException;
22 import org.opendaylight.controller.sal.utils.EtherTypes;
23 import org.opendaylight.controller.sal.utils.HexEncode;
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;
42 public class LLDPSpeaker {
43 private static Logger LOG = LoggerFactory.getLogger(LLDPSpeaker.class);
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;
52 private LLDPSpeaker() {
53 timer.schedule(new LLDPSpeakerTask(), DELAY, PERIOD);
56 public static LLDPSpeaker getInstance() {
60 public void addModelDrivenSwitch(InstanceIdentifier<Node> nodeInstanceId, ModelDrivenSwitch sw) {
61 nodeMap.put(nodeInstanceId,sw);
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);
73 public void addNodeConnector(InstanceIdentifier<NodeConnector> nodeConnectorInstanceId, NodeConnector nodeConnector) {
74 InstanceIdentifier<Node> nodeInstanceId = nodeConnectorInstanceId.firstIdentifierOf(Node.class);
75 NodeKey nodeKey = InstanceIdentifier.keyOf(nodeInstanceId);
76 NodeId nodeId = nodeKey.getId();
77 NodeConnectorId nodeConnectorId = nodeConnector.getId();
78 FlowCapableNodeConnector flowConnector = nodeConnector.<FlowCapableNodeConnector>getAugmentation(FlowCapableNodeConnector.class);
79 TransmitPacketInputBuilder tpib = new TransmitPacketInputBuilder();
80 tpib.setEgress(new NodeConnectorRef(nodeConnectorInstanceId));
81 tpib.setNode(new NodeRef(nodeInstanceId));
82 tpib.setPayload(lldpDataFrom(nodeInstanceId,nodeConnectorInstanceId,flowConnector.getHardwareAddress()));
83 nodeConnectorMap.put(nodeConnectorInstanceId, tpib.build());
84 ModelDrivenSwitch md = nodeMap.get(nodeInstanceId);
85 md.transmitPacket(nodeConnectorMap.get(nodeConnectorInstanceId));
88 public void removeNodeConnector(
89 InstanceIdentifier<NodeConnector> nodeConnectorInstanceId,
90 NodeConnector nodeConnector) {
91 nodeConnectorMap.remove(nodeConnectorInstanceId);
94 private byte[] lldpDataFrom(InstanceIdentifier<Node> nodeInstanceId,InstanceIdentifier<NodeConnector> nodeConnectorInstanceId,MacAddress src) {
96 NodeId nodeId = InstanceIdentifier.keyOf(nodeInstanceId).getId();
97 NodeConnectorId nodeConnectorId = InstanceIdentifier.keyOf(nodeConnectorInstanceId).getId();
98 // Create LLDP TTL TLV
99 byte[] ttl = new byte[] { (byte) 0, (byte) 120 };
100 LLDPTLV ttlTlv = new LLDPTLV();
101 ttlTlv.setType(LLDPTLV.TLVType.TTL.getValue()).setLength((short) ttl.length).setValue(ttl);
103 // Create LLDP ChassisID TLV
104 byte[] cidValue = LLDPTLV.createChassisIDTLVValue(colonize(StringUtils.leftPad(Long.toHexString(InventoryDataServiceUtil.dataPathIdFromNodeId(nodeId)),16,"0")));
105 LLDPTLV chassisIdTlv = new LLDPTLV();
106 chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue());
107 chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue()).setLength((short) cidValue.length)
110 // Create LLDP SystemName TLV
111 byte[] snValue = LLDPTLV.createSystemNameTLVValue(nodeId.getValue());
112 LLDPTLV systemNameTlv = new LLDPTLV();
113 systemNameTlv.setType(LLDPTLV.TLVType.SystemName.getValue());
114 systemNameTlv.setType(LLDPTLV.TLVType.SystemName.getValue()).setLength((short) snValue.length)
117 // Create LLDP PortID TL
118 Long portNo = InventoryDataServiceUtil.portNumberfromNodeConnectorId(nodeConnectorId);
119 String hexString = Long.toHexString(portNo);
120 byte[] pidValue = LLDPTLV.createPortIDTLVValue(hexString);
121 LLDPTLV portIdTlv = new LLDPTLV();
122 portIdTlv.setType(LLDPTLV.TLVType.PortID.getValue()).setLength((short) pidValue.length).setValue(pidValue);
123 portIdTlv.setType(LLDPTLV.TLVType.PortID.getValue());
125 // Create LLDP Custom TLV
126 byte[] customValue = LLDPTLV.createCustomTLVValue(nodeConnectorId.getValue());
127 LLDPTLV customTlv = new LLDPTLV();
128 customTlv.setType(LLDPTLV.TLVType.Custom.getValue()).setLength((short) customValue.length)
129 .setValue(customValue);
131 // Create LLDP Custom Option list
132 List<LLDPTLV> customList = Collections.singletonList(customTlv);
134 // Create discovery pkt
135 LLDP discoveryPkt = new LLDP();
136 discoveryPkt.setChassisId(chassisIdTlv).setPortId(portIdTlv).setTtl(ttlTlv).setSystemNameId(systemNameTlv)
137 .setOptionalTLVList(customList);
139 // Create ethernet pkt
140 byte[] sourceMac = HexEncode.bytesFromHexString(src.getValue());
141 Ethernet ethPkt = new Ethernet();
142 ethPkt.setSourceMACAddress(sourceMac).setDestinationMACAddress(LLDP.LLDPMulticastMac)
143 .setEtherType(EtherTypes.LLDP.shortValue()).setPayload(discoveryPkt);
146 byte[] data = ethPkt.serialize();
148 } catch (PacketException e) {
149 LOG.error("Error creating LLDP packet",e);
154 private class LLDPSpeakerTask extends TimerTask {
158 for (InstanceIdentifier<NodeConnector> nodeConnectorInstanceId : nodeConnectorMap.keySet()) {
159 InstanceIdentifier<Node> nodeInstanceId = nodeConnectorInstanceId.firstIdentifierOf(Node.class);
160 ModelDrivenSwitch md = nodeMap.get(nodeInstanceId);
161 md.transmitPacket(nodeConnectorMap.get(nodeConnectorInstanceId));
168 private String colonize(String orig) {
169 return orig.replaceAll("(?<=..)(..)", ":$1");