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.sal.packet;
11 import java.io.UnsupportedEncodingException;
12 import java.util.HashMap;
14 import java.util.Arrays;
15 import java.util.LinkedHashMap;
17 import org.apache.commons.lang3.builder.EqualsBuilder;
18 import org.apache.commons.lang3.builder.HashCodeBuilder;
19 import org.apache.commons.lang3.tuple.MutablePair;
20 import org.apache.commons.lang3.tuple.Pair;
21 import org.opendaylight.controller.sal.utils.HexEncode;
22 import org.opendaylight.controller.sal.utils.NetUtils;
25 * Class that represents the LLDPTLV objects
28 public class LLDPTLV extends Packet {
29 private static final String TYPE = "Type";
30 private static final String LENGTH = "Length";
31 private static final String VALUE = "Value";
32 private static final int LLDPTLVFields = 3;
33 public static final byte[] OFOUI = new byte[] { (byte) 0x00, (byte) 0x26,
34 (byte) 0xe1 }; // OpenFlow OUI
35 public static final byte[] customTlvSubType = new byte[] { 0 };
36 public static final int customTlvOffset = OFOUI.length
37 + customTlvSubType.length;
38 public static final byte chassisIDSubType[] = new byte[] { 4 }; // MAC address for the system
39 public static final byte portIDSubType[] = new byte[] { 7 }; // locally assigned
42 Unknown((byte) 0), ChassisID((byte) 1), PortID((byte) 2), TTL((byte) 3), PortDesc(
43 (byte) 4), SystemName((byte) 5), SystemDesc((byte) 6), Custom(
48 private TLVType(byte value) {
52 public byte getValue() {
57 private static Map<String, Pair<Integer, Integer>> fieldCoordinates = new LinkedHashMap<String, Pair<Integer, Integer>>() {
58 private static final long serialVersionUID = 1L;
61 put(TYPE, new MutablePair<Integer, Integer>(0, 7));
62 put(LENGTH, new MutablePair<Integer, Integer>(7, 9));
63 put(VALUE, new MutablePair<Integer, Integer>(16, 0));
67 protected Map<String, byte[]> fieldValues;
70 * Default constructor that creates and sets the hash map values and sets
75 fieldValues = new HashMap<String, byte[]>(LLDPTLVFields);
76 hdrFieldCoordMap = fieldCoordinates;
77 hdrFieldsMap = fieldValues;
81 * Constructor that writes the passed LLDPTLV values to the hdrFieldsMap
83 public LLDPTLV(LLDPTLV other) {
84 for (Map.Entry<String, byte[]> entry : other.hdrFieldsMap.entrySet()) {
85 this.hdrFieldsMap.put(entry.getKey(), entry.getValue());
90 * @return int - the length of TLV
92 public int getLength() {
93 return (int) BitBufferHelper.toNumber(fieldValues.get(LENGTH),
94 fieldCoordinates.get(LENGTH).getRight().intValue());
98 * @return byte - the type of TLV
100 public byte getType() {
101 return BitBufferHelper.getByte(fieldValues.get(TYPE));
105 * @return byte[] - the value field of TLV
107 public byte[] getValue() {
108 return fieldValues.get(VALUE);
112 * @param byte - the type to set
115 public LLDPTLV setType(byte type) {
116 byte[] lldpTLVtype = { type };
117 fieldValues.put(TYPE, lldpTLVtype);
122 * @param short - the length to set
125 public LLDPTLV setLength(short length) {
126 fieldValues.put(LENGTH, BitBufferHelper.toByteArray(length));
131 * @param byte[] - the value to set
134 public LLDPTLV setValue(byte[] value) {
135 fieldValues.put(VALUE, value);
140 public void setHeaderField(String headerField, byte[] readValue) {
141 hdrFieldsMap.put(headerField, readValue);
145 public int hashCode() {
146 return HashCodeBuilder.reflectionHashCode(this);
150 public boolean equals(Object obj) {
151 return EqualsBuilder.reflectionEquals(this, obj);
155 public int getfieldnumBits(String fieldName) throws Exception {
156 if (fieldName.equals(VALUE)) {
157 return (NetUtils.NumBitsInAByte * (int) BitBufferHelper.getShort(
158 fieldValues.get(LENGTH), fieldCoordinates.get(LENGTH)
159 .getRight().intValue()));
161 return fieldCoordinates.get(fieldName).getRight();
165 * Returns the size in bits of the whole TLV
167 * @return int - size in bits of full TLV
170 public int getTLVSize() throws Exception {
171 return (LLDPTLV.fieldCoordinates.get(TYPE).getRight() + // static
172 LLDPTLV.fieldCoordinates.get(LENGTH).getRight() + // static
173 getfieldnumBits(VALUE)); // variable
177 * Creates the ChassisID TLV value including the subtype and ChassisID
181 * node identifier string
182 * @return the ChassisID TLV value in byte array
184 static public byte[] createChassisIDTLVValue(String nodeId) {
185 byte[] nid = HexEncode.bytesFromHexString(nodeId);
186 byte[] cid = new byte[6];
187 int srcPos = 0, dstPos = 0;
189 if (nid.length > cid.length) {
190 srcPos = nid.length - cid.length;
192 dstPos = cid.length - nid.length;
194 System.arraycopy(nid, srcPos, cid, dstPos, cid.length);
196 byte[] cidValue = new byte[cid.length + chassisIDSubType.length];
198 System.arraycopy(chassisIDSubType, 0, cidValue, 0,
199 chassisIDSubType.length);
200 System.arraycopy(cid, 0, cidValue, chassisIDSubType.length, cid.length);
206 * Creates the PortID TLV value including the subtype and PortID string
209 * port identifier string
210 * @return the PortID TLV value in byte array
212 static public byte[] createPortIDTLVValue(String portId) {
213 byte[] pid = portId.getBytes();
214 byte[] pidValue = new byte[pid.length + portIDSubType.length];
216 System.arraycopy(portIDSubType, 0, pidValue, 0, portIDSubType.length);
217 System.arraycopy(pid, 0, pidValue, portIDSubType.length, pid.length);
223 * Creates the custom TLV value including OUI, subtype and custom string
226 * port identifier string
227 * @return the custom TLV value in byte array
229 static public byte[] createCustomTLVValue(String customString) {
230 byte[] customArray = customString.getBytes();
231 byte[] customValue = new byte[customTlvOffset + customArray.length];
233 System.arraycopy(OFOUI, 0, customValue, 0, OFOUI.length);
234 System.arraycopy(customTlvSubType, 0, customValue, OFOUI.length,
235 customTlvSubType.length);
236 System.arraycopy(customArray, 0, customValue, customTlvOffset,
243 * Retrieves the string from TLV value and returns it in HexString format
249 * @return the HexString
251 static public String getHexStringValue(byte[] tlvValue, int tlvLen) {
252 byte[] cidBytes = new byte[tlvLen - chassisIDSubType.length];
253 System.arraycopy(tlvValue, chassisIDSubType.length, cidBytes, 0,
255 return HexEncode.bytesToHexStringFormat(cidBytes);
259 * Retrieves the string from TLV value
267 static public String getStringValue(byte[] tlvValue, int tlvLen) {
268 byte[] pidBytes = new byte[tlvLen - portIDSubType.length];
269 System.arraycopy(tlvValue, portIDSubType.length, pidBytes, 0,
271 return (new String(pidBytes));
275 * Retrieves the custom string from the Custom TLV value which includes OUI,
276 * subtype and custom string
278 * @param customTlvValue
279 * the custom TLV value
280 * @param customTlvLen
281 * the custom TLV length
282 * @return the custom string
284 static public String getCustomString(byte[] customTlvValue, int customTlvLen) {
285 String customString = "";
286 byte[] vendor = new byte[3];
287 System.arraycopy(customTlvValue, 0, vendor, 0, vendor.length);
288 if (Arrays.equals(vendor, LLDPTLV.OFOUI)) {
289 int customArrayLength = customTlvLen - customTlvOffset;
290 byte[] customArray = new byte[customArrayLength];
291 System.arraycopy(customTlvValue, customTlvOffset, customArray, 0,
294 customString = new String(customArray, "UTF-8");
295 } catch (UnsupportedEncodingException e) {