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.util.Collections;
12 import com.google.common.collect.Iterables;
13 import java.util.ArrayList;
14 import java.util.LinkedHashMap;
15 import java.util.List;
19 * Class that represents the LLDP frame objects
22 public class LLDP extends Packet {
23 private static final String CHASSISID = "ChassisId";
24 private static final String SYSTEMNAMEID = "SystemNameID";
25 private static final String PORTID = "PortId";
26 private static final String TTL = "TTL";
27 private static final int LLDPDefaultTlvs = 4;
28 private static LLDPTLV emptyTLV = new LLDPTLV().setLength((short) 0)
30 public static final byte[] LLDPMulticastMac = { 1, (byte) 0x80,
31 (byte) 0xc2, 0, 0, (byte) 0xe };
32 private Map<Byte, LLDPTLV> tlvList;
34 private List<LLDPTLV> customTlvList = Collections.emptyList();
37 * Default constructor that creates the tlvList LinkedHashMap
41 tlvList = new LinkedHashMap<Byte, LLDPTLV>(LLDPDefaultTlvs);
45 * Constructor that creates the tlvList LinkedHashMap and sets the write
48 public LLDP(boolean writeAccess) {
50 tlvList = new LinkedHashMap<Byte, LLDPTLV>(LLDPDefaultTlvs); // Mandatory
56 * - description of the type of TLV
57 * @return byte - type of TLV
59 private byte getType(String typeDesc) {
60 if (typeDesc.equals(CHASSISID)) {
61 return LLDPTLV.TLVType.ChassisID.getValue();
62 } else if (typeDesc.equals(PORTID)) {
63 return LLDPTLV.TLVType.PortID.getValue();
64 } else if (typeDesc.equals(TTL)) {
65 return LLDPTLV.TLVType.TTL.getValue();
67 return LLDPTLV.TLVType.Unknown.getValue();
73 * - description of the type of TLV
74 * @return LLDPTLV - full TLV
76 public LLDPTLV getTLV(String type) {
77 return tlvList.get(getType(type));
82 * - description of the type of TLV
87 public void setTLV(String type, LLDPTLV tlv) {
88 tlvList.put(getType(type), tlv);
92 * @return the chassisId TLV
94 public LLDPTLV getChassisId() {
95 return getTLV(CHASSISID);
100 * - the chassisId to set
102 public LLDP setChassisId(LLDPTLV chassisId) {
103 tlvList.put(getType(CHASSISID), chassisId);
108 * @return the SystemName TLV
110 public LLDPTLV getSystemNameId() {
111 return getTLV(SYSTEMNAMEID);
116 * - the chassisId to set
118 public LLDP setSystemNameId(LLDPTLV systemNameId) {
119 tlvList.put(getType(SYSTEMNAMEID), systemNameId);
124 * @return LLDPTLV - the portId TLV
126 public LLDPTLV getPortId() {
127 return tlvList.get(getType(PORTID));
132 * - the portId to set
135 public LLDP setPortId(LLDPTLV portId) {
136 tlvList.put(getType(PORTID), portId);
141 * @return LLDPTLV - the ttl TLV
143 public LLDPTLV getTtl() {
144 return tlvList.get(getType(TTL));
152 public LLDP setTtl(LLDPTLV ttl) {
153 tlvList.put(getType(TTL), ttl);
158 * @return the optionalTLVList
160 public List<LLDPTLV> getOptionalTLVList() {
161 List<LLDPTLV> list = new ArrayList<LLDPTLV>();
162 for (Map.Entry<Byte, LLDPTLV> entry : tlvList.entrySet()) {
163 byte type = entry.getKey();
164 if ((type == LLDPTLV.TLVType.ChassisID.getValue())
165 || (type == LLDPTLV.TLVType.PortID.getValue())
166 || (type == LLDPTLV.TLVType.TTL.getValue())) {
169 list.add(entry.getValue());
176 * @param optionalTLVList
177 * the optionalTLVList to set
180 public LLDP setOptionalTLVList(List<LLDPTLV> optionalTLVList) {
181 for (LLDPTLV tlv : optionalTLVList) {
182 tlvList.put(tlv.getType(), tlv);
188 * @param customTLVList
189 * the list of custom TLVs to set
192 public LLDP setCustomTLVList(final List<LLDPTLV> customTLVList) {
193 this.customTlvList = new ArrayList<>(customTLVList);
198 public Packet deserialize(byte[] data, int bitOffset, int size)
199 throws PacketException {
200 int lldpOffset = bitOffset; // LLDP start
201 int lldpSize = size; // LLDP size
203 if (logger.isTraceEnabled()) {
204 logger.trace("LLDP: {} (offset {} bitsize {})", new Object[] {
205 HexEncode.bytesToHexString(data), lldpOffset, lldpSize });
208 * Deserialize the TLVs until we reach the end of the packet
210 while (lldpSize > 0) {
211 LLDPTLV tlv = new LLDPTLV();
212 tlv.deserialize(data, lldpOffset, lldpSize);
213 if (tlv.getType() == 0 && tlv.getLength() == 0) {
216 int tlvSize = tlv.getTLVSize(); // Size of current TLV in bits
217 lldpOffset += tlvSize;
219 this.tlvList.put(tlv.getType(), tlv);
225 public byte[] serialize() throws PacketException {
227 byte[] serializedBytes = new byte[getLLDPPacketLength()];
229 final Iterable<LLDPTLV> allTlvs = Iterables.concat(tlvList.values(), customTlvList);
230 for (LLDPTLV tlv : allTlvs) {
231 int numBits = tlv.getTLVSize();
233 BitBufferHelper.setBytes(serializedBytes, tlv.serialize(),
234 startOffset, numBits);
235 } catch (BufferException e) {
236 throw new PacketException(e.getMessage());
238 startOffset += numBits;
240 // Now add the empty LLDPTLV at the end
242 BitBufferHelper.setBytes(serializedBytes,
243 LLDP.emptyTLV.serialize(), startOffset,
244 LLDP.emptyTLV.getTLVSize());
245 } catch (BufferException e) {
246 throw new PacketException(e.getMessage());
249 if (logger.isTraceEnabled()) {
250 logger.trace("LLDP: serialized: {}",
251 HexEncode.bytesToHexString(serializedBytes));
253 return serializedBytes;
257 * Returns the size of LLDP packet in bytes
259 * @return int - LLDP Packet size in bytes
261 private int getLLDPPacketLength() {
265 for (Map.Entry<Byte, LLDPTLV> entry : this.tlvList.entrySet()) {
266 tlv = entry.getValue();
267 len += tlv.getTLVSize();
270 for (LLDPTLV customTlv : this.customTlvList) {
271 len += customTlv.getTLVSize();
274 len += LLDP.emptyTLV.getTLVSize();
276 return len / NetUtils.NumBitsInAByte;