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
41 * Constructor that creates the tlvList LinkedHashMap and sets the write access for the same
43 public LLDP(final boolean writeAccess) {
49 mandatoryTLVs = new LinkedHashMap<>(LLDPDefaultTlvs);
50 optionalTLVs = new LinkedHashMap<>();
51 customTLVs = new LinkedHashMap<>();
56 * - description of the type of TLV
57 * @return byte - type of TLV
59 private byte getType(final 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();
66 } else if (typeDesc.equals(SYSTEMNAMEID)) {
67 return LLDPTLV.TLVType.SystemName.getValue();
69 return LLDPTLV.TLVType.Unknown.getValue();
73 private LLDPTLV getFromTLVs(final Byte type) {
75 tlv = mandatoryTLVs.get(type);
77 tlv = optionalTLVs.get(type);
82 private void putToTLVs(final Byte type, final LLDPTLV tlv) {
83 if (type == LLDPTLV.TLVType.ChassisID.getValue() || type == LLDPTLV.TLVType.PortID.getValue()
84 || type == LLDPTLV.TLVType.TTL.getValue()) {
85 mandatoryTLVs.put(type, tlv);
86 } else if (type != LLDPTLV.TLVType.Custom.getValue()) {
87 optionalTLVs.put(type, tlv);
93 * - description of the type of TLV
94 * @return LLDPTLV - full TLV
96 public LLDPTLV getTLV(final String type) {
97 return getFromTLVs(getType(type));
100 public LLDPTLV getCustomTLV(final CustomTLVKey key) {
101 return customTLVs.get(key);
106 * - description of the type of TLV
110 public void setTLV(final String type, final LLDPTLV tlv) {
111 putToTLVs(getType(type), tlv);
115 * @return the chassisId TLV
117 public LLDPTLV getChassisId() {
118 return getTLV(CHASSISID);
123 * - the chassisId to set
125 public LLDP setChassisId(final LLDPTLV chassisId) {
126 setTLV(CHASSISID, chassisId);
131 * @return the SystemName TLV
133 public LLDPTLV getSystemNameId() {
134 return getTLV(SYSTEMNAMEID);
138 * @param systemNameId
139 * - the systemNameId to set
141 public LLDP setSystemNameId(final LLDPTLV systemNameId) {
142 setTLV(SYSTEMNAMEID, systemNameId);
147 * @return LLDPTLV - the portId TLV
149 public LLDPTLV getPortId() {
150 return getTLV(PORTID);
155 * - the portId to set
158 public LLDP setPortId(final LLDPTLV portId) {
159 setTLV(PORTID, portId);
164 * @return LLDPTLV - the ttl TLV
166 public LLDPTLV getTtl() {
175 public LLDP setTtl(final LLDPTLV ttl) {
181 * @return the optionalTLVList
183 public Iterable<LLDPTLV> getOptionalTLVList() {
184 return optionalTLVs.values();
188 * @return the customTlvList
190 public Iterable<LLDPTLV> getCustomTlvList() {
191 return customTLVs.values();
195 * @param optionalTLVList
196 * the optionalTLVList to set
199 public LLDP setOptionalTLVList(final List<LLDPTLV> optionalTLVList) {
200 for (LLDPTLV tlv : optionalTLVList) {
201 optionalTLVs.put(tlv.getType(), tlv);
208 * the list of custom TLVs to set
211 public LLDP addCustomTLV(final LLDPTLV customTLV) {
212 CustomTLVKey key = new CustomTLVKey(LLDPTLV.extractCustomOUI(customTLV),
213 LLDPTLV.extractCustomSubtype(customTLV));
214 customTLVs.put(key, customTLV);
220 public Packet deserialize(final byte[] data, final int bitOffset, final int size) throws PacketException {
221 int lldpOffset = bitOffset; // LLDP start
222 int lldpSize = size; // LLDP size
224 if (LOG.isTraceEnabled()) {
225 LOG.trace("LLDP: {} (offset {} bitsize {})", new Object[] { HexEncode.bytesToHexString(data),
226 lldpOffset, lldpSize });
229 * Deserialize the TLVs until we reach the end of the packet
231 while (lldpSize > 0) {
232 LLDPTLV tlv = new LLDPTLV();
233 tlv.deserialize(data, lldpOffset, lldpSize);
234 if (tlv.getType() == 0 && tlv.getLength() == 0) {
237 int tlvSize = tlv.getTLVSize(); // Size of current TLV in bits
238 lldpOffset += tlvSize;
240 if (tlv.getType() == LLDPTLV.TLVType.Custom.getValue()) {
243 this.putToTLVs(tlv.getType(), tlv);
250 public byte[] serialize() throws PacketException {
252 byte[] serializedBytes = new byte[getLLDPPacketLength()];
254 final Iterable<LLDPTLV> allTlvs = Iterables.concat(mandatoryTLVs.values(), optionalTLVs.values(), customTLVs.values());
255 for (LLDPTLV tlv : allTlvs) {
256 int numBits = tlv.getTLVSize();
258 BitBufferHelper.setBytes(serializedBytes, tlv.serialize(), startOffset, numBits);
259 } catch (final BufferException e) {
260 throw new PacketException(e.getMessage());
262 startOffset += numBits;
264 // Now add the empty LLDPTLV at the end
266 BitBufferHelper.setBytes(serializedBytes, LLDP.emptyTLV.serialize(), startOffset,
267 LLDP.emptyTLV.getTLVSize());
268 } catch (final BufferException e) {
269 throw new PacketException(e.getMessage());
272 if (LOG.isTraceEnabled()) {
273 LOG.trace("LLDP: serialized: {}", HexEncode.bytesToHexString(serializedBytes));
275 return serializedBytes;
279 * Returns the size of LLDP packet in bytes
281 * @return int - LLDP Packet size in bytes
283 private int getLLDPPacketLength() {
286 for (LLDPTLV lldptlv : Iterables.concat(mandatoryTLVs.values(), optionalTLVs.values(), customTLVs.values())) {
287 len += lldptlv.getTLVSize();
290 len += LLDP.emptyTLV.getTLVSize();
292 return len / NetUtils.NumBitsInAByte;