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.sal.packet;
11 import java.util.ArrayList;
12 import java.util.LinkedHashMap;
13 import java.util.List;
15 import org.opendaylight.controller.sal.utils.HexEncode;
16 import org.opendaylight.controller.sal.utils.NetUtils;
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;
35 * Default constructor that creates the tlvList LinkedHashMap
39 tlvList = new LinkedHashMap<Byte, LLDPTLV>(LLDPDefaultTlvs);
43 * Constructor that creates the tlvList LinkedHashMap and sets the write
46 public LLDP(boolean writeAccess) {
48 tlvList = new LinkedHashMap<Byte, LLDPTLV>(LLDPDefaultTlvs); // Mandatory
54 * - description of the type of TLV
55 * @return byte - type of TLV
57 private byte getType(String typeDesc) {
58 if (typeDesc.equals(CHASSISID)) {
59 return LLDPTLV.TLVType.ChassisID.getValue();
60 } else if (typeDesc.equals(PORTID)) {
61 return LLDPTLV.TLVType.PortID.getValue();
62 } else if (typeDesc.equals(TTL)) {
63 return LLDPTLV.TLVType.TTL.getValue();
65 return LLDPTLV.TLVType.Unknown.getValue();
71 * - description of the type of TLV
72 * @return LLDPTLV - full TLV
74 public LLDPTLV getTLV(String type) {
75 return tlvList.get(getType(type));
80 * - description of the type of TLV
85 public void setTLV(String type, LLDPTLV tlv) {
86 tlvList.put(getType(type), tlv);
90 * @return the chassisId TLV
92 public LLDPTLV getChassisId() {
93 return getTLV(CHASSISID);
98 * - the chassisId to set
100 public LLDP setChassisId(LLDPTLV chassisId) {
101 tlvList.put(getType(CHASSISID), chassisId);
106 * @return the SystemName TLV
108 public LLDPTLV getSystemNameId() {
109 return getTLV(SYSTEMNAMEID);
114 * - the chassisId to set
116 public LLDP setSystemNameId(LLDPTLV systemNameId) {
117 tlvList.put(getType(SYSTEMNAMEID), systemNameId);
122 * @return LLDPTLV - the portId TLV
124 public LLDPTLV getPortId() {
125 return tlvList.get(getType(PORTID));
130 * - the portId to set
133 public LLDP setPortId(LLDPTLV portId) {
134 tlvList.put(getType(PORTID), portId);
139 * @return LLDPTLV - the ttl TLV
141 public LLDPTLV getTtl() {
142 return tlvList.get(getType(TTL));
150 public LLDP setTtl(LLDPTLV ttl) {
151 tlvList.put(getType(TTL), ttl);
156 * @return the optionalTLVList
158 public List<LLDPTLV> getOptionalTLVList() {
159 List<LLDPTLV> list = new ArrayList<LLDPTLV>();
160 for (Map.Entry<Byte, LLDPTLV> entry : tlvList.entrySet()) {
161 byte type = entry.getKey();
162 if ((type == LLDPTLV.TLVType.ChassisID.getValue())
163 || (type == LLDPTLV.TLVType.PortID.getValue())
164 || (type == LLDPTLV.TLVType.TTL.getValue())) {
167 list.add(entry.getValue());
174 * @param optionalTLVList
175 * the optionalTLVList to set
178 public LLDP setOptionalTLVList(List<LLDPTLV> optionalTLVList) {
179 for (LLDPTLV tlv : optionalTLVList) {
180 tlvList.put(tlv.getType(), tlv);
186 public Packet deserialize(byte[] data, int bitOffset, int size)
187 throws PacketException {
188 int lldpOffset = bitOffset; // LLDP start
189 int lldpSize = size; // LLDP size
191 if (logger.isTraceEnabled()) {
192 logger.trace("LLDP: {} (offset {} bitsize {})", new Object[] {
193 HexEncode.bytesToHexString(data), lldpOffset, lldpSize });
196 * Deserialize the TLVs until we reach the end of the packet
198 while (lldpSize > 0) {
199 LLDPTLV tlv = new LLDPTLV();
200 tlv.deserialize(data, lldpOffset, lldpSize);
201 if (tlv.getType() == 0 && tlv.getLength() == 0) {
204 int tlvSize = tlv.getTLVSize(); // Size of current TLV in bits
205 lldpOffset += tlvSize;
207 this.tlvList.put(tlv.getType(), tlv);
213 public byte[] serialize() throws PacketException {
215 byte[] serializedBytes = new byte[getLLDPPacketLength()];
217 for (Map.Entry<Byte, LLDPTLV> entry : tlvList.entrySet()) {
218 LLDPTLV tlv = entry.getValue();
219 int numBits = tlv.getTLVSize();
221 BitBufferHelper.setBytes(serializedBytes, tlv.serialize(),
222 startOffset, numBits);
223 } catch (BufferException e) {
224 throw new PacketException(e.getMessage());
226 startOffset += numBits;
228 // Now add the empty LLDPTLV at the end
230 BitBufferHelper.setBytes(serializedBytes,
231 LLDP.emptyTLV.serialize(), startOffset,
232 LLDP.emptyTLV.getTLVSize());
233 } catch (BufferException e) {
234 throw new PacketException(e.getMessage());
237 if (logger.isTraceEnabled()) {
238 logger.trace("LLDP: serialized: {}",
239 HexEncode.bytesToHexString(serializedBytes));
241 return serializedBytes;
245 * Returns the size of LLDP packet in bytes
247 * @return int - LLDP Packet size in bytes
249 private int getLLDPPacketLength() {
253 for (Map.Entry<Byte, LLDPTLV> entry : this.tlvList.entrySet()) {
254 tlv = entry.getValue();
255 len += tlv.getTLVSize();
257 len += LLDP.emptyTLV.getTLVSize();
259 return len / NetUtils.NumBitsInAByte;