2 * Copyright (c) 2015 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.controller.liblldp;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16 import org.junit.Before;
17 import java.util.Iterator;
18 import java.util.ArrayList;
19 import java.util.List;
21 import org.apache.commons.lang3.ArrayUtils;
22 import org.junit.Assert;
23 import org.junit.Test;
24 import org.junit.internal.ArrayComparisonFailure;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.primitives.Bytes;
31 * Test of {@link LLDP} serialization feature (TODO: and deserialization)
33 public class LLDPTest {
35 private static final Logger LOG = LoggerFactory.getLogger(LLDPTest.class);
37 private static final byte[] CHASSIS_ID_VALUE = "chassis".getBytes();
38 private static final short CHASSIS_ID_LENGTH = (short) CHASSIS_ID_VALUE.length;
40 private static final byte[] TTL_VALUE = new byte[] { (byte) 0, (byte) 100 };
41 private static final short TTL_LENGTH = (short) TTL_VALUE.length;
43 private static final byte[] PORT_VALUE = "dummy port id".getBytes();
44 private static final short PORT_LENGTH = (short) PORT_VALUE.length;
46 private static final byte[] SYSTEM_NAME_VALUE = "dummy system name".getBytes();
47 private static final short SYSTEM_NAME_LENGTH = (short) SYSTEM_NAME_VALUE.length;
49 private static final byte SYSTEM_CAPABILITIES_TLV = 8;
50 private static final byte[] SYSTEM_CAPABILITIES_VALUE = "dummy system capabilities".getBytes();
51 private static final short SYSTEM_CAPABILITIES_LENGTH = (short) SYSTEM_CAPABILITIES_VALUE.length;
53 private static final byte[] OUI = LLDPTLV.OFOUI;
55 private static final byte[] OUI_SUBTYPE_A = new byte[] { (byte) 0 };
56 private static final byte[] CUSTOM_SUBTYPE_A_VALUE = "first custom value A".getBytes();
57 private static final short CUSTOM_SUBTYPE_A_LENGTH = (short) (OUI.length + OUI_SUBTYPE_A.length + CUSTOM_SUBTYPE_A_VALUE.length);
59 private static final byte[] OUI_SUBTYPE_B = new byte[] { (byte) 1 };
60 private static final byte[] CUSTOM_SUBTYPE_B_VALUE = "second custom value B".getBytes();
61 private static final short CUSTOM_SUBTYPE_B_LENGTH = (short) (OUI.length + OUI_SUBTYPE_B.length + CUSTOM_SUBTYPE_B_VALUE.length);
63 private static final byte[] BYTES_BEFORE_CUSTOM_A = new byte[] { 0x00, 0x26, (byte) 0xe1, OUI_SUBTYPE_A[0] };
64 private static final byte[] BYTES_BEFORE_CUSTOM_B = new byte[] { 0x00, 0x26, (byte) 0xe1, OUI_SUBTYPE_B[0] };
65 private LLDP lldpBuilder;
69 lldpBuilder = new LLDP();
73 * Tests whether serialization of LLDP packet is correct
75 * @see LLDP#serialize()
76 * @throws PacketException
79 public void testSerialize() throws PacketException {
80 lldpBuilder.setChassisId(dummyTlv(LLDPTLV.TLVType.ChassisID.getValue(), CHASSIS_ID_LENGTH, CHASSIS_ID_VALUE));
81 lldpBuilder.setTtl(dummyTlv(LLDPTLV.TLVType.TTL.getValue(), TTL_LENGTH, TTL_VALUE));
82 lldpBuilder.setPortId(dummyTlv(LLDPTLV.TLVType.PortID.getValue(), PORT_LENGTH, PORT_VALUE));
83 lldpBuilder.setSystemNameId(dummyTlv(LLDPTLV.TLVType.SystemName.getValue(), SYSTEM_NAME_LENGTH,
86 // adding optional TLVs for which doesn't exist special set* methods in LLDP
87 final List<LLDPTLV> optionalTLVs = new ArrayList<>();
88 // System Capabilities TLV (type = 7)
89 optionalTLVs.add(dummyTlv(SYSTEM_CAPABILITIES_TLV, SYSTEM_CAPABILITIES_LENGTH, SYSTEM_CAPABILITIES_VALUE));
90 lldpBuilder.setOptionalTLVList(optionalTLVs);
93 lldpBuilder.addCustomTLV(dummyCustomTlv(LLDPTLV.TLVType.Custom.getValue(), OUI, OUI_SUBTYPE_A,
94 CUSTOM_SUBTYPE_A_LENGTH, CUSTOM_SUBTYPE_A_VALUE));
95 lldpBuilder.addCustomTLV(dummyCustomTlv(LLDPTLV.TLVType.Custom.getValue(), OUI, OUI_SUBTYPE_B,
96 CUSTOM_SUBTYPE_B_LENGTH, CUSTOM_SUBTYPE_B_VALUE));
98 byte[] serialized = lldpBuilder.serialize();
101 offset = checkTLV(serialized, offset, (byte) 0b00000010, "ChassisID", CHASSIS_ID_LENGTH, CHASSIS_ID_VALUE);
102 offset = checkTLV(serialized, offset, (byte) 0b00000110, "TTL", TTL_LENGTH, TTL_VALUE);
103 offset = checkTLV(serialized, offset, (byte) 0b00000100, "PortID", PORT_LENGTH, PORT_VALUE);
104 offset = checkTLV(serialized, offset, (byte) 0b00001010, "SystemName", SYSTEM_NAME_LENGTH, SYSTEM_NAME_VALUE);
105 offset = checkTLV(serialized, offset, (byte) 0b00010000, "System capabilities", SYSTEM_CAPABILITIES_LENGTH,
106 SYSTEM_CAPABILITIES_VALUE);
107 offset = checkTLV(serialized, offset, (byte) 0b11111110, "Custom subtype A", CUSTOM_SUBTYPE_A_LENGTH,
108 CUSTOM_SUBTYPE_A_VALUE, OUI[0], OUI[1], OUI[2], OUI_SUBTYPE_A[0]);
109 offset = checkTLV(serialized, offset, (byte) 0b11111110, "Custom subtype B", CUSTOM_SUBTYPE_B_LENGTH,
110 CUSTOM_SUBTYPE_B_VALUE, OUI[0], OUI[1], OUI[2], OUI_SUBTYPE_B[0]);
115 * Tests whether serialization of LLDP packet is correct
117 * @see LLDP#deserialize(byte[], int, int)
121 public void testDeserialize() throws Exception {
123 byte[] rawLldpTlv = Bytes
124 .concat(awaitedBytes((byte) 0b00000010, CHASSIS_ID_LENGTH, CHASSIS_ID_VALUE, null),
125 awaitedBytes((byte) 0b00000110, TTL_LENGTH, TTL_VALUE, null),
126 awaitedBytes((byte) 0b00000100, PORT_LENGTH, PORT_VALUE, null),
127 awaitedBytes((byte) 0b00001010, SYSTEM_NAME_LENGTH, SYSTEM_NAME_VALUE, null),
128 awaitedBytes((byte) 0b00010010, SYSTEM_CAPABILITIES_LENGTH, SYSTEM_CAPABILITIES_VALUE, null),
129 awaitedBytes((byte) 0b11111110, CUSTOM_SUBTYPE_A_LENGTH, CUSTOM_SUBTYPE_A_VALUE,
130 BYTES_BEFORE_CUSTOM_A),
131 awaitedBytes((byte) 0b11111110, CUSTOM_SUBTYPE_B_LENGTH, CUSTOM_SUBTYPE_B_VALUE,
132 BYTES_BEFORE_CUSTOM_B));
134 lldpBuilder.deserialize(rawLldpTlv, 0, rawLldpTlv.length * NetUtils.NumBitsInAByte);
135 Assert.assertEquals("chassis", new String(lldpBuilder.getChassisId().getValue()));
136 Assert.assertArrayEquals(TTL_VALUE, lldpBuilder.getTtl().getValue());
137 Assert.assertEquals("dummy port id", new String(lldpBuilder.getPortId().getValue()));
138 Assert.assertEquals("dummy system name", new String(lldpBuilder.getSystemNameId().getValue()));
140 // optional items check
141 Iterator<LLDPTLV> iteratorTlvOptional = lldpBuilder.getOptionalTLVList().iterator();
143 assertTrue(iteratorTlvOptional.hasNext());
144 LLDPTLV item0 = iteratorTlvOptional.next();
145 Assert.assertEquals(5, item0.getType());
146 Assert.assertEquals("dummy system name", new String(item0.getValue()));
147 assertTrue(iteratorTlvOptional.hasNext());
149 assertTrue(iteratorTlvOptional.hasNext());
150 LLDPTLV item1 = iteratorTlvOptional.next();
151 Assert.assertEquals(9, item1.getType());
152 Assert.assertEquals("dummy system capabilities", new String(item1.getValue()));
153 assertFalse(iteratorTlvOptional.hasNext());
155 // custom items check
156 Iterable<LLDPTLV> customTlvs = lldpBuilder.getCustomTlvList();
157 Iterator<LLDPTLV> iteratorLLDPTLV = customTlvs.iterator();
158 assertEquals(true, iteratorLLDPTLV.hasNext());
159 checkCustomTlv(iteratorLLDPTLV.next(), "first custom value A");
160 assertEquals(true, iteratorLLDPTLV.hasNext());
161 checkCustomTlv(iteratorLLDPTLV.next(), "second custom value B");
162 assertEquals(false, iteratorLLDPTLV.hasNext());
166 * Test of {@link LLDP#addCustomTLV(LLDPTLV)}
168 * @throws PacketException
171 public void testAddCustomTLV() throws PacketException {
172 byte[] customA = awaitedBytes((byte) 0b11111110, CUSTOM_SUBTYPE_A_LENGTH, CUSTOM_SUBTYPE_A_VALUE,
173 BYTES_BEFORE_CUSTOM_A);
174 byte[] customB = awaitedBytes((byte) 0b11111110, CUSTOM_SUBTYPE_B_LENGTH, CUSTOM_SUBTYPE_B_VALUE,
175 BYTES_BEFORE_CUSTOM_B);
177 Packet lldptlvA = new LLDPTLV().deserialize(customA, 0, customA.length);
178 assertTrue(lldptlvA instanceof LLDPTLV);
179 Packet lldptlvB = new LLDPTLV().deserialize(customB, 0, customB.length);
180 assertTrue(lldptlvB instanceof LLDPTLV);
182 lldpBuilder.addCustomTLV((LLDPTLV) lldptlvA);
183 lldpBuilder.addCustomTLV((LLDPTLV) lldptlvB);
185 Iterator<LLDPTLV> customTLVsIterator = lldpBuilder.getCustomTlvList().iterator();
186 assertTrue(customTLVsIterator.hasNext());
187 customTLVsIterator.next();
188 assertTrue(customTLVsIterator.hasNext());
189 customTLVsIterator.next();
190 assertFalse(customTLVsIterator.hasNext());
194 public void testGetCustomTLV() throws PacketException {
195 int ouiInt = BitBufferHelper.getInt(OUI);
196 CustomTLVKey key = new CustomTLVKey(ouiInt, OUI_SUBTYPE_A[0]);
197 LLDPTLV customTLV = lldpBuilder.getCustomTLV(key);
198 assertNull(customTLV);
200 byte[] customA = awaitedBytes((byte) 0b11111110, CUSTOM_SUBTYPE_A_LENGTH, CUSTOM_SUBTYPE_A_VALUE,
201 BYTES_BEFORE_CUSTOM_A);
202 lldpBuilder.deserialize(customA, 0, customA.length);
204 customTLV = lldpBuilder.getCustomTLV(key);
205 assertNotNull(customTLV);
206 assertEquals(ouiInt, LLDPTLV.extractCustomOUI(customTLV));
207 assertEquals(OUI_SUBTYPE_A[0], LLDPTLV.extractCustomSubtype(customTLV));
212 * @param expectedValue
214 private static void checkCustomTlv(LLDPTLV customItem, String expectedValue) {
215 Assert.assertEquals(127, customItem.getType());
216 LOG.debug("custom TLV1.length: {}", customItem.getLength());
217 Assert.assertEquals(expectedValue,
218 new String(LLDPTLV.getCustomString(customItem.getValue(), customItem.getLength())));
221 private static int checkTLV(byte[] serializedData, int offset, byte typeTLVBits, String typeTLVName,
222 short lengthTLV, byte[] valueTLV, byte... bytesBeforeValue) throws ArrayComparisonFailure {
223 byte[] concreteTlvAwaited = awaitedBytes(typeTLVBits, lengthTLV, valueTLV, bytesBeforeValue);
224 int concreteTlvAwaitLength = concreteTlvAwaited.length;
225 assertArrayEquals("Serialization problem " + typeTLVName, concreteTlvAwaited,
226 ArrayUtils.subarray(serializedData, offset, offset + concreteTlvAwaitLength));
227 return offset + concreteTlvAwaitLength;
230 private static byte[] awaitedBytes(byte typeTLV, short length, byte[] value, byte[] bytesBeforeValue) {
231 byte[] awaited = ArrayUtils.EMPTY_BYTE_ARRAY;
233 // 0 - the less meaning byte (right), 1 most meaning byte (left)
234 byte lengthByte0 = (byte) length;
235 byte lengthByte1 = (byte) (length >> 8);
237 awaited = ArrayUtils.addAll(awaited, (byte) (typeTLV | lengthByte1), lengthByte0);
238 awaited = ArrayUtils.addAll(awaited, bytesBeforeValue);
239 awaited = ArrayUtils.addAll(awaited, value);
243 private static LLDPTLV dummyCustomTlv(final byte tlvType, byte[] oui, byte[] ouiSubtype, short customLength,
244 byte[] subtypeValue) {
245 byte[] fullCustomValue = new byte[0];
246 fullCustomValue = ArrayUtils.addAll(fullCustomValue, oui);
247 fullCustomValue = ArrayUtils.addAll(fullCustomValue, ouiSubtype);
248 fullCustomValue = ArrayUtils.addAll(fullCustomValue, subtypeValue);
249 return dummyTlv(tlvType, customLength, fullCustomValue);
252 private static LLDPTLV dummyTlv(final byte concreteTlv, final short concreteLength, final byte[] concreteValue) {
253 LLDPTLV tlv = new LLDPTLV();
254 tlv.setType(concreteTlv).setLength(concreteLength).setValue(concreteValue);