2 * Copyright (c) 2014 Pacnet 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
9 package org.opendaylight.openflowplugin.applications.lldpspeaker;
11 import static org.opendaylight.controller.liblldp.LLDPTLV.CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC;
12 import static org.opendaylight.openflowplugin.applications.topology.lldp.utils.LLDPDiscoveryUtils.getValueForLLDPPacketIntegrityEnsuring;
14 import java.math.BigInteger;
15 import java.security.NoSuchAlgorithmException;
16 import org.apache.commons.lang3.StringUtils;
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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Utility class for dealing with LLDP packets.
32 public final class LLDPUtil {
33 private static final Logger LOG = LoggerFactory.getLogger(LLDPUtil.class);
34 private static final String OF_URI_PREFIX = "openflow:";
36 static byte[] buildLldpFrame(final NodeId nodeId,
37 final NodeConnectorId nodeConnectorId,
40 final MacAddress destinationAddress) {
41 // Create discovery pkt
42 LLDP discoveryPkt = new LLDP();
44 // Create LLDP ChassisID TLV
45 BigInteger dataPathId = dataPathIdFromNodeId(nodeId);
46 byte[] cidValue = LLDPTLV.createChassisIDTLVValue(colonize(bigIntegerToPaddedHex(dataPathId)));
47 LLDPTLV chassisIdTlv = new LLDPTLV();
48 chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue());
49 chassisIdTlv.setType(LLDPTLV.TLVType.ChassisID.getValue())
50 .setLength((short) cidValue.length)
52 discoveryPkt.setChassisId(chassisIdTlv);
54 // Create LLDP PortID TL
55 String hexString = Long.toHexString(outPortNo);
56 byte[] pidValue = LLDPTLV.createPortIDTLVValue(hexString);
57 LLDPTLV portIdTlv = new LLDPTLV();
58 portIdTlv.setType(LLDPTLV.TLVType.PortID.getValue())
59 .setLength((short) pidValue.length)
61 portIdTlv.setType(LLDPTLV.TLVType.PortID.getValue());
62 discoveryPkt.setPortId(portIdTlv);
64 // Create LLDP TTL TLV
65 byte[] ttl = new byte[] { (byte) 0x13, (byte) 0x37 };
66 LLDPTLV ttlTlv = new LLDPTLV();
67 ttlTlv.setType(LLDPTLV.TLVType.TTL.getValue()).setLength((short) ttl.length).setValue(ttl);
68 discoveryPkt.setTtl(ttlTlv);
70 // Create LLDP SystemName TLV
71 byte[] snValue = LLDPTLV.createSystemNameTLVValue(nodeId.getValue());
72 LLDPTLV systemNameTlv = new LLDPTLV();
73 systemNameTlv.setType(LLDPTLV.TLVType.SystemName.getValue());
74 systemNameTlv.setType(LLDPTLV.TLVType.SystemName.getValue())
75 .setLength((short) snValue.length)
77 discoveryPkt.setSystemNameId(systemNameTlv);
79 // Create LLDP Custom TLV
80 byte[] customValue = LLDPTLV.createCustomTLVValue(nodeConnectorId.getValue());
81 LLDPTLV customTlv = new LLDPTLV();
82 customTlv.setType(LLDPTLV.TLVType.Custom.getValue())
83 .setLength((short) customValue.length)
84 .setValue(customValue);
85 discoveryPkt.addCustomTLV(customTlv);
87 //Create LLDP CustomSec TLV
88 byte[] pureValue = new byte[1];
90 pureValue = getValueForLLDPPacketIntegrityEnsuring(nodeConnectorId);
91 byte[] customSecValue = LLDPTLV.createCustomTLVValue(CUSTOM_TLV_SUB_TYPE_CUSTOM_SEC, pureValue);
92 LLDPTLV customSecTlv = new LLDPTLV();
93 customSecTlv.setType(LLDPTLV.TLVType.Custom.getValue())
94 .setLength((short)customSecValue.length)
95 .setValue(customSecValue);
96 discoveryPkt.addCustomTLV(customSecTlv);
97 } catch (NoSuchAlgorithmException e1) {
98 LOG.info("LLDP extra authenticator creation failed: {}", e1.getMessage());
99 LOG.debug("Reason why LLDP extra authenticator creation failed: ", e1);
103 // Create ethernet pkt
104 byte[] sourceMac = HexEncode.bytesFromHexString(src.getValue());
105 Ethernet ethPkt = new Ethernet();
106 ethPkt.setSourceMACAddress(sourceMac)
107 .setEtherType(EtherTypes.LLDP.shortValue())
108 .setPayload(discoveryPkt);
109 if (destinationAddress == null) {
110 ethPkt.setDestinationMACAddress(LLDP.LLDPMulticastMac);
112 ethPkt.setDestinationMACAddress(HexEncode.bytesFromHexString(destinationAddress.getValue()));
116 return ethPkt.serialize();
117 } catch (PacketException e) {
118 LOG.warn("Error creating LLDP packet: {}", e.getMessage());
119 LOG.debug("Error creating LLDP packet.. ", e);
124 private static String colonize(final String orig) {
125 return orig.replaceAll("(?<=..)(..)", ":$1");
128 private static BigInteger dataPathIdFromNodeId(final NodeId nodeId) {
129 String dpids = nodeId.getValue().replace(OF_URI_PREFIX, "");
130 return new BigInteger(dpids);
133 private static String bigIntegerToPaddedHex(final BigInteger dataPathId) {
134 return StringUtils.leftPad(dataPathId.toString(16), 16, "0");
137 static byte[] buildLldpFrame(final NodeId nodeId,
138 final NodeConnectorId nodeConnectorId,
139 final MacAddress srcMacAddress,
140 final Long outputPortNo) {
141 return buildLldpFrame(nodeId, nodeConnectorId, srcMacAddress, outputPortNo, null);