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
9 package org.opendaylight.controller.liblldp;
11 import java.io.UnsupportedEncodingException;
12 import java.nio.charset.Charset;
13 import java.util.Arrays;
14 import java.util.HashMap;
15 import java.util.LinkedHashMap;
18 import org.apache.commons.lang3.tuple.MutablePair;
19 import org.apache.commons.lang3.tuple.Pair;
22 * Class that represents the LLDPTLV objects
25 public class LLDPTLV extends Packet {
26 private static final String TYPE = "Type";
27 private static final String LENGTH = "Length";
28 private static final String VALUE = "Value";
29 private static final int LLDPTLVFields = 3;
30 public static final byte[] OFOUI = new byte[] { (byte) 0x00, (byte) 0x26,
31 (byte) 0xe1 }; // OpenFlow OUI
32 public static final byte[] customTlvSubType = new byte[] { 0 };
33 public static final int customTlvOffset = OFOUI.length
34 + customTlvSubType.length;
35 public static final byte chassisIDSubType[] = new byte[] { 4 }; // MAC address for the system
36 public static final byte portIDSubType[] = new byte[] { 7 }; // locally assigned
39 Unknown((byte) 0), ChassisID((byte) 1), PortID((byte) 2), TTL((byte) 3), PortDesc(
40 (byte) 4), SystemName((byte) 5), SystemDesc((byte) 6), Custom(
45 private TLVType(byte value) {
49 public byte getValue() {
54 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
55 private static final long serialVersionUID = 1L;
58 put(TYPE, new MutablePair<Integer, Integer>(0, 7));
59 put(LENGTH, new MutablePair<Integer, Integer>(7, 9));
60 put(VALUE, new MutablePair<Integer, Integer>(16, 0));
64 protected Map<String, byte[]> fieldValues;
67 * Default constructor that creates and sets the hash map values and sets
72 fieldValues = new HashMap<String, byte[]>(LLDPTLVFields);
73 hdrFieldCoordMap = fieldCoordinates;
74 hdrFieldsMap = fieldValues;
78 * Constructor that writes the passed LLDPTLV values to the hdrFieldsMap
80 public LLDPTLV(LLDPTLV other) {
81 for (Map.Entry<String, byte[]> entry : other.hdrFieldsMap.entrySet()) {
82 this.hdrFieldsMap.put(entry.getKey(), entry.getValue());
87 * @return int - the length of TLV
89 public int getLength() {
90 return (int) BitBufferHelper.toNumber(fieldValues.get(LENGTH),
91 fieldCoordinates.get(LENGTH).getRight().intValue());
95 * @return byte - the type of TLV
97 public byte getType() {
98 return BitBufferHelper.getByte(fieldValues.get(TYPE));
102 * @return byte[] - the value field of TLV
104 public byte[] getValue() {
105 return fieldValues.get(VALUE);
109 * @param byte - the type to set
112 public LLDPTLV setType(byte type) {
113 byte[] lldpTLVtype = { type };
114 fieldValues.put(TYPE, lldpTLVtype);
119 * @param short - the length to set
122 public LLDPTLV setLength(short length) {
123 fieldValues.put(LENGTH, BitBufferHelper.toByteArray(length));
128 * @param byte[] - the value to set
131 public LLDPTLV setValue(byte[] value) {
132 fieldValues.put(VALUE, value);
137 public void setHeaderField(String headerField, byte[] readValue) {
138 hdrFieldsMap.put(headerField, readValue);
142 public int hashCode() {
143 final int prime = 31;
144 int result = super.hashCode();
145 result = prime * result
146 + ((fieldValues == null) ? 0 : fieldValues.hashCode());
151 public boolean equals(Object obj) {
155 if (!super.equals(obj)) {
158 if (getClass() != obj.getClass()) {
161 LLDPTLV other = (LLDPTLV) obj;
162 if (fieldValues == null) {
163 if (other.fieldValues != null) {
166 } else if (!fieldValues.equals(other.fieldValues)) {
173 public int getfieldnumBits(String fieldName) {
174 if (fieldName.equals(VALUE)) {
175 return (NetUtils.NumBitsInAByte * BitBufferHelper.getShort(
176 fieldValues.get(LENGTH), fieldCoordinates.get(LENGTH)
177 .getRight().intValue()));
179 return fieldCoordinates.get(fieldName).getRight();
183 * Returns the size in bits of the whole TLV
185 * @return int - size in bits of full TLV
187 public int getTLVSize() {
188 return (LLDPTLV.fieldCoordinates.get(TYPE).getRight() + // static
189 LLDPTLV.fieldCoordinates.get(LENGTH).getRight() + // static
190 getfieldnumBits(VALUE)); // variable
194 * Creates the SystemName TLV value
197 * node identifier string
198 * @return the SystemName TLV value in byte array
200 static public byte[] createSystemNameTLVValue(String nodeId) {
201 byte[] nid = nodeId.getBytes();
206 * Creates the ChassisID TLV value including the subtype and ChassisID
210 * node identifier string
211 * @return the ChassisID TLV value in byte array
213 static public byte[] createChassisIDTLVValue(String nodeId) {
214 byte[] nid = HexEncode.bytesFromHexString(nodeId);
215 byte[] cid = new byte[6];
216 int srcPos = 0, dstPos = 0;
218 if (nid.length > cid.length) {
219 srcPos = nid.length - cid.length;
221 dstPos = cid.length - nid.length;
223 System.arraycopy(nid, srcPos, cid, dstPos, cid.length);
225 byte[] cidValue = new byte[cid.length + chassisIDSubType.length];
227 System.arraycopy(chassisIDSubType, 0, cidValue, 0,
228 chassisIDSubType.length);
229 System.arraycopy(cid, 0, cidValue, chassisIDSubType.length, cid.length);
235 * Creates the PortID TLV value including the subtype and PortID string
238 * port identifier string
239 * @return the PortID TLV value in byte array
241 static public byte[] createPortIDTLVValue(String portId) {
242 byte[] pid = portId.getBytes(Charset.defaultCharset());
243 byte[] pidValue = new byte[pid.length + portIDSubType.length];
245 System.arraycopy(portIDSubType, 0, pidValue, 0, portIDSubType.length);
246 System.arraycopy(pid, 0, pidValue, portIDSubType.length, pid.length);
252 * Creates the custom TLV value including OUI, subtype and custom string
255 * port identifier string
256 * @return the custom TLV value in byte array
258 static public byte[] createCustomTLVValue(String customString) {
259 byte[] customArray = customString.getBytes(Charset.defaultCharset());
260 byte[] customValue = new byte[customTlvOffset + customArray.length];
262 System.arraycopy(OFOUI, 0, customValue, 0, OFOUI.length);
263 System.arraycopy(customTlvSubType, 0, customValue, OFOUI.length,
264 customTlvSubType.length);
265 System.arraycopy(customArray, 0, customValue, customTlvOffset,
272 * Retrieves the string from TLV value and returns it in HexString format
278 * @return the HexString
280 static public String getHexStringValue(byte[] tlvValue, int tlvLen) {
281 byte[] cidBytes = new byte[tlvLen - chassisIDSubType.length];
282 System.arraycopy(tlvValue, chassisIDSubType.length, cidBytes, 0,
284 return HexEncode.bytesToHexStringFormat(cidBytes);
288 * Retrieves the string from TLV value
296 static public String getStringValue(byte[] tlvValue, int tlvLen) {
297 byte[] pidSubType = new byte[portIDSubType.length];
298 byte[] pidBytes = new byte[tlvLen - portIDSubType.length];
299 System.arraycopy(tlvValue, 0, pidSubType, 0,
301 System.arraycopy(tlvValue, portIDSubType.length, pidBytes, 0,
303 if (pidSubType[0] == (byte) 0x3) {
304 return HexEncode.bytesToHexStringFormat(pidBytes);
306 return (new String(pidBytes, Charset.defaultCharset()));
311 * Retrieves the custom string from the Custom TLV value which includes OUI,
312 * subtype and custom string
314 * @param customTlvValue
315 * the custom TLV value
316 * @param customTlvLen
317 * the custom TLV length
318 * @return the custom string
320 static public String getCustomString(byte[] customTlvValue, int customTlvLen) {
321 String customString = "";
322 byte[] vendor = new byte[3];
323 System.arraycopy(customTlvValue, 0, vendor, 0, vendor.length);
324 if (Arrays.equals(vendor, LLDPTLV.OFOUI)) {
325 int customArrayLength = customTlvLen - customTlvOffset;
326 byte[] customArray = new byte[customArrayLength];
327 System.arraycopy(customTlvValue, customTlvOffset, customArray, 0,
330 customString = new String(customArray, "UTF-8");
331 } catch (UnsupportedEncodingException e) {