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.openflowplugin.libraries.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.
19 public class LLDP extends Packet {
20 private static final String CHASSISID = "ChassisId";
21 private static final String SYSTEMNAMEID = "SystemNameID";
22 private static final String PORTID = "PortId";
23 private static final String TTL = "TTL";
24 private static final int LLDP_DEFAULT_TLVS = 3;
25 private static final LLDPTLV EMPTY_TLV = new LLDPTLV().setLength((short) 0).setType((byte) 0);
26 public static final byte[] LLDP_MULTICAST_MAC = { 1, (byte) 0x80, (byte) 0xc2, 0, 0, (byte) 0xe };
28 private Map<Byte, LLDPTLV> mandatoryTLVs;
29 private Map<Byte, LLDPTLV> optionalTLVs;
30 private Map<CustomTLVKey, LLDPTLV> customTLVs;
33 * Default constructor that creates the tlvList LinkedHashMap.
40 * Constructor that creates the tlvList LinkedHashMap and sets the write access for the same.
42 public LLDP(final boolean writeAccess) {
48 mandatoryTLVs = new LinkedHashMap<>(LLDP_DEFAULT_TLVS);
49 optionalTLVs = new LinkedHashMap<>();
50 customTLVs = new LinkedHashMap<>();
54 * Returns the TLV byte type.
56 * @param String 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);
92 * Gets the full LLDPTLV.
94 * @param type description of the type of TLV
95 * @return LLDPTLV - full TLV
97 public LLDPTLV getTLV(final String type) {
98 return getFromTLVs(getType(type));
101 public LLDPTLV getCustomTLV(final CustomTLVKey key) {
102 return customTLVs.get(key);
106 * Sets the LLDPTLV for a type.
108 * @param type description of the type of TLV
109 * @param tlv tlv to set
111 public void setTLV(final String type, final LLDPTLV tlv) {
112 putToTLVs(getType(type), tlv);
116 * Returns the chassisId TLV.
118 public LLDPTLV getChassisId() {
119 return getTLV(CHASSISID);
122 public LLDP setChassisId(final LLDPTLV chassisId) {
123 setTLV(CHASSISID, chassisId);
128 * Returns the SystemName TLV.
130 public LLDPTLV getSystemNameId() {
131 return getTLV(SYSTEMNAMEID);
134 public LLDP setSystemNameId(final LLDPTLV systemNameId) {
135 setTLV(SYSTEMNAMEID, systemNameId);
140 * Returns the portId TLV.
142 public LLDPTLV getPortId() {
143 return getTLV(PORTID);
146 public LLDP setPortId(final LLDPTLV portId) {
147 setTLV(PORTID, portId);
152 * Return the ttl TLV.
154 public LLDPTLV getTtl() {
158 public LLDP setTtl(final LLDPTLV ttl) {
164 * Returns the optionalTLVList.
166 public Iterable<LLDPTLV> getOptionalTLVList() {
167 return optionalTLVs.values();
171 * Returns the customTlvList.
173 public Iterable<LLDPTLV> getCustomTlvList() {
174 return customTLVs.values();
177 public LLDP setOptionalTLVList(final List<LLDPTLV> optionalTLVList) {
178 for (LLDPTLV tlv : optionalTLVList) {
179 optionalTLVs.put(tlv.getType(), tlv);
184 public LLDP addCustomTLV(final LLDPTLV customTLV) {
185 CustomTLVKey key = new CustomTLVKey(LLDPTLV.extractCustomOUI(customTLV),
186 LLDPTLV.extractCustomSubtype(customTLV));
187 customTLVs.put(key, customTLV);
193 public Packet deserialize(final byte[] data, final int bitOffset, final int size) throws PacketException {
194 int lldpOffset = bitOffset; // LLDP start
195 int lldpSize = size; // LLDP size
197 if (LOG.isTraceEnabled()) {
198 LOG.trace("LLDP: {} (offset {} bitsize {})", new Object[] { HexEncode.bytesToHexString(data),
199 lldpOffset, lldpSize });
202 * Deserialize the TLVs until we reach the end of the packet
204 while (lldpSize > 0) {
205 LLDPTLV tlv = new LLDPTLV();
206 tlv.deserialize(data, lldpOffset, lldpSize);
207 if (tlv.getType() == 0 && tlv.getLength() == 0) {
210 int tlvSize = tlv.getTLVSize(); // Size of current TLV in bits
211 lldpOffset += tlvSize;
213 if (tlv.getType() == LLDPTLV.TLVType.Custom.getValue()) {
216 this.putToTLVs(tlv.getType(), tlv);
223 public byte[] serialize() throws PacketException {
225 byte[] serializedBytes = new byte[getLLDPPacketLength()];
227 final Iterable<LLDPTLV> allTlvs = Iterables.concat(mandatoryTLVs.values(), optionalTLVs.values(),
228 customTLVs.values());
229 for (LLDPTLV tlv : allTlvs) {
230 int numBits = tlv.getTLVSize();
232 BitBufferHelper.setBytes(serializedBytes, tlv.serialize(), startOffset, numBits);
233 } catch (final BufferException e) {
234 throw new PacketException("Error from setBytes", e);
236 startOffset += numBits;
238 // Now add the empty LLDPTLV at the end
240 BitBufferHelper.setBytes(serializedBytes, LLDP.EMPTY_TLV.serialize(), startOffset,
241 LLDP.EMPTY_TLV.getTLVSize());
242 } catch (final BufferException e) {
243 throw new PacketException("Error from setBytes", e);
246 if (LOG.isTraceEnabled()) {
247 LOG.trace("LLDP: serialized: {}", HexEncode.bytesToHexString(serializedBytes));
249 return serializedBytes;
253 * Returns the size of LLDP packet in bytes.
255 * @return int - LLDP Packet size in bytes
257 private int getLLDPPacketLength() {
260 for (LLDPTLV lldptlv : Iterables.concat(mandatoryTLVs.values(), optionalTLVs.values(), customTLVs.values())) {
261 len += lldptlv.getTLVSize();
264 len += LLDP.EMPTY_TLV.getTLVSize();
266 return len / NetUtils.NUM_BITS_IN_A_BYTE;