BUG-770: NumberFormatException for datapathId
[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
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;
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         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));
86         }
87
88         public  void removeNodeConnector(
89                         InstanceIdentifier<NodeConnector> nodeConnectorInstanceId,
90                         NodeConnector nodeConnector) {
91                 nodeConnectorMap.remove(nodeConnectorInstanceId);               
92         }
93         
94         private  byte[] lldpDataFrom(InstanceIdentifier<Node> nodeInstanceId,InstanceIdentifier<NodeConnector> nodeConnectorInstanceId,MacAddress src) {
95                 
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);
102                 
103         // Create LLDP ChassisID TLV
104         BigInteger dataPathId = InventoryDataServiceUtil.dataPathIdFromNodeId(nodeId);
105         byte[] cidValue = LLDPTLV.createChassisIDTLVValue(
106                 colonize(InventoryDataServiceUtil.bigIntegerToPaddedHex(dataPathId)));
107         LLDPTLV chassisIdTlv = new LLDPTLV();
108         chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue());
109         chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue()).setLength((short) cidValue.length)
110                 .setValue(cidValue);
111
112         // Create LLDP SystemName TLV
113         byte[] snValue = LLDPTLV.createSystemNameTLVValue(nodeId.getValue());
114         LLDPTLV systemNameTlv = new LLDPTLV();
115         systemNameTlv.setType(LLDPTLV.TLVType.SystemName.getValue());
116         systemNameTlv.setType(LLDPTLV.TLVType.SystemName.getValue()).setLength((short) snValue.length)
117                 .setValue(snValue);
118
119         // Create LLDP PortID TL
120         Long portNo = InventoryDataServiceUtil.portNumberfromNodeConnectorId(nodeConnectorId);
121         String hexString = Long.toHexString(portNo);
122         byte[] pidValue = LLDPTLV.createPortIDTLVValue(hexString);
123         LLDPTLV portIdTlv = new LLDPTLV();
124         portIdTlv.setType(LLDPTLV.TLVType.PortID.getValue()).setLength((short) pidValue.length).setValue(pidValue);
125         portIdTlv.setType(LLDPTLV.TLVType.PortID.getValue());
126
127         // Create LLDP Custom TLV
128         byte[] customValue = LLDPTLV.createCustomTLVValue(nodeConnectorId.getValue());
129         LLDPTLV customTlv = new LLDPTLV();
130         customTlv.setType(LLDPTLV.TLVType.Custom.getValue()).setLength((short) customValue.length)
131                 .setValue(customValue);
132
133         // Create LLDP Custom Option list
134         List<LLDPTLV> customList = Collections.singletonList(customTlv);
135
136         // Create discovery pkt
137         LLDP discoveryPkt = new LLDP();
138         discoveryPkt.setChassisId(chassisIdTlv).setPortId(portIdTlv).setTtl(ttlTlv).setSystemNameId(systemNameTlv)
139                 .setOptionalTLVList(customList);
140
141         // Create ethernet pkt
142         byte[] sourceMac = HexEncode.bytesFromHexString(src.getValue());
143         Ethernet ethPkt = new Ethernet();
144         ethPkt.setSourceMACAddress(sourceMac).setDestinationMACAddress(LLDP.LLDPMulticastMac)
145                 .setEtherType(EtherTypes.LLDP.shortValue()).setPayload(discoveryPkt);
146
147         try {
148             byte[] data = ethPkt.serialize();
149             return data;
150         } catch (PacketException e) {
151             LOG.error("Error creating LLDP packet",e);
152         }
153         return null;
154         }
155
156     private class LLDPSpeakerTask extends TimerTask {
157
158         @Override
159         public void run() {
160             for (InstanceIdentifier<NodeConnector> nodeConnectorInstanceId : nodeConnectorMap.keySet()) {
161                 InstanceIdentifier<Node> nodeInstanceId = nodeConnectorInstanceId.firstIdentifierOf(Node.class);
162                 ModelDrivenSwitch md = nodeMap.get(nodeInstanceId);
163                 md.transmitPacket(nodeConnectorMap.get(nodeConnectorInstanceId));
164             }
165             
166         }
167             
168         }
169         
170         private String colonize(String orig) {
171             return orig.replaceAll("(?<=..)(..)", ":$1");
172         }
173 }