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 com.google.common.collect.Iterables;
12 import java.util.LinkedHashMap;
13 import java.util.List;
17 * Class that represents the LLDP frame objects
20 public class LLDP extends Packet {
21 private static final String CHASSISID = "ChassisId";
22 private static final String SYSTEMNAMEID = "SystemNameID";
23 private static final String PORTID = "PortId";
24 private static final String TTL = "TTL";
25 private static final int LLDPDefaultTlvs = 3;
26 private static final LLDPTLV emptyTLV = new LLDPTLV().setLength((short) 0).setType((byte) 0);
27 public static final byte[] LLDPMulticastMac = { 1, (byte) 0x80, (byte) 0xc2, 0, 0, (byte) 0xe };
29 private Map<Byte, LLDPTLV> mandatoryTLVs;
30 private Map<Byte, LLDPTLV> optionalTLVs;
31 private Map<CustomTLVKey, LLDPTLV> customTLVs;
34 * Default constructor that creates the tlvList LinkedHashMap
42 * Constructor that creates the tlvList LinkedHashMap and sets the write access for the same
44 public LLDP(boolean writeAccess) {
50 mandatoryTLVs = new LinkedHashMap<>(LLDPDefaultTlvs);
51 optionalTLVs = new LinkedHashMap<>();
52 customTLVs = new LinkedHashMap<>();
57 * - description of the type of TLV
58 * @return byte - type of TLV
60 private byte getType(String typeDesc) {
61 if (typeDesc.equals(CHASSISID)) {
62 return LLDPTLV.TLVType.ChassisID.getValue();
63 } else if (typeDesc.equals(PORTID)) {
64 return LLDPTLV.TLVType.PortID.getValue();
65 } else if (typeDesc.equals(TTL)) {
66 return LLDPTLV.TLVType.TTL.getValue();
67 } else if (typeDesc.equals(SYSTEMNAMEID)) {
68 return LLDPTLV.TLVType.SystemName.getValue();
70 return LLDPTLV.TLVType.Unknown.getValue();
74 private LLDPTLV getFromTLVs(Byte type) {
76 tlv = mandatoryTLVs.get(type);
78 tlv = optionalTLVs.get(type);
83 private void putToTLVs(final Byte type, final LLDPTLV tlv) {
84 if (type == LLDPTLV.TLVType.ChassisID.getValue() || type == LLDPTLV.TLVType.PortID.getValue()
85 || type == LLDPTLV.TLVType.TTL.getValue()) {
86 mandatoryTLVs.put(type, tlv);
87 } else if (type != LLDPTLV.TLVType.Custom.getValue()) {
88 optionalTLVs.put(type, tlv);
94 * - description of the type of TLV
95 * @return LLDPTLV - full TLV
97 public LLDPTLV getTLV(String type) {
98 return getFromTLVs(getType(type));
101 public LLDPTLV getCustomTLV(CustomTLVKey key) {
102 return customTLVs.get(key);
107 * - description of the type of TLV
111 public void setTLV(String type, LLDPTLV tlv) {
112 putToTLVs(getType(type), tlv);
116 * @return the chassisId TLV
118 public LLDPTLV getChassisId() {
119 return getTLV(CHASSISID);
124 * - the chassisId to set
126 public LLDP setChassisId(LLDPTLV chassisId) {
127 setTLV(CHASSISID, chassisId);
132 * @return the SystemName TLV
134 public LLDPTLV getSystemNameId() {
135 return getTLV(SYSTEMNAMEID);
139 * @param systemNameId
140 * - the systemNameId to set
142 public LLDP setSystemNameId(LLDPTLV systemNameId) {
143 setTLV(SYSTEMNAMEID, systemNameId);
148 * @return LLDPTLV - the portId TLV
150 public LLDPTLV getPortId() {
151 return getTLV(PORTID);
156 * - the portId to set
159 public LLDP setPortId(LLDPTLV portId) {
160 setTLV(PORTID, portId);
165 * @return LLDPTLV - the ttl TLV
167 public LLDPTLV getTtl() {
176 public LLDP setTtl(LLDPTLV ttl) {
182 * @return the optionalTLVList
184 public Iterable<LLDPTLV> getOptionalTLVList() {
185 return optionalTLVs.values();
189 * @return the customTlvList
191 public Iterable<LLDPTLV> getCustomTlvList() {
192 return customTLVs.values();
196 * @param optionalTLVList
197 * the optionalTLVList to set
200 public LLDP setOptionalTLVList(List<LLDPTLV> optionalTLVList) {
201 for (LLDPTLV tlv : optionalTLVList) {
202 optionalTLVs.put(tlv.getType(), tlv);
209 * the list of custom TLVs to set
212 public LLDP addCustomTLV(final LLDPTLV customTLV) {
213 CustomTLVKey key = new CustomTLVKey(LLDPTLV.extractCustomOUI(customTLV),
214 LLDPTLV.extractCustomSubtype(customTLV));
215 customTLVs.put(key, customTLV);
221 public Packet deserialize(byte[] data, int bitOffset, int size) throws PacketException {
222 int lldpOffset = bitOffset; // LLDP start
223 int lldpSize = size; // LLDP size
225 if (logger.isTraceEnabled()) {
226 logger.trace("LLDP: {} (offset {} bitsize {})", new Object[] { HexEncode.bytesToHexString(data),
227 lldpOffset, lldpSize });
230 * Deserialize the TLVs until we reach the end of the packet
232 while (lldpSize > 0) {
233 LLDPTLV tlv = new LLDPTLV();
234 tlv.deserialize(data, lldpOffset, lldpSize);
235 if (tlv.getType() == 0 && tlv.getLength() == 0) {
238 int tlvSize = tlv.getTLVSize(); // Size of current TLV in bits
239 lldpOffset += tlvSize;
241 if (tlv.getType() == LLDPTLV.TLVType.Custom.getValue()) {
244 this.putToTLVs(tlv.getType(), tlv);
251 public byte[] serialize() throws PacketException {
253 byte[] serializedBytes = new byte[getLLDPPacketLength()];
255 final Iterable<LLDPTLV> allTlvs = Iterables.concat(mandatoryTLVs.values(), optionalTLVs.values(), customTLVs.values());
256 for (LLDPTLV tlv : allTlvs) {
257 int numBits = tlv.getTLVSize();
259 BitBufferHelper.setBytes(serializedBytes, tlv.serialize(), startOffset, numBits);
260 } catch (BufferException e) {
261 throw new PacketException(e.getMessage());
263 startOffset += numBits;
265 // Now add the empty LLDPTLV at the end
267 BitBufferHelper.setBytes(serializedBytes, LLDP.emptyTLV.serialize(), startOffset,
268 LLDP.emptyTLV.getTLVSize());
269 } catch (BufferException e) {
270 throw new PacketException(e.getMessage());
273 if (logger.isTraceEnabled()) {
274 logger.trace("LLDP: serialized: {}", HexEncode.bytesToHexString(serializedBytes));
276 return serializedBytes;
280 * Returns the size of LLDP packet in bytes
282 * @return int - LLDP Packet size in bytes
284 private int getLLDPPacketLength() {
287 for (LLDPTLV lldptlv : Iterables.concat(mandatoryTLVs.values(), optionalTLVs.values(), customTLVs.values())) {
288 len += lldptlv.getTLVSize();
291 len += LLDP.emptyTLV.getTLVSize();
293 return len / NetUtils.NumBitsInAByte;