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.NetUtils;
18 * Class that represents the LLDP frame objects
21 public class LLDP extends Packet {
22 private static final String CHASSISID = "ChassisId";
23 private static final String PORTID = "PortId";
24 private static final String TTL = "TTL";
25 private static final int LLDPDefaultTlvs = 3;
26 private static LLDPTLV emptyTLV = new LLDPTLV().setLength((short) 0)
28 public static final byte[] LLDPMulticastMac = { 1, (byte) 0x80,
29 (byte) 0xc2, 0, 0, (byte) 0xe };
30 private Map<Byte, LLDPTLV> tlvList;
33 * Default constructor that creates the tlvList LinkedHashMap
37 tlvList = new LinkedHashMap<Byte, LLDPTLV>(LLDPDefaultTlvs);
41 * Constructor that creates the tlvList LinkedHashMap and sets the write
44 public LLDP(boolean writeAccess) {
46 tlvList = new LinkedHashMap<Byte, LLDPTLV>(LLDPDefaultTlvs); // Mandatory
52 * - description of the type of TLV
53 * @return byte - type of TLV
55 private byte getType(String typeDesc) {
56 if (typeDesc.equals(CHASSISID)) {
57 return LLDPTLV.TLVType.ChassisID.getValue();
58 } else if (typeDesc.equals(PORTID)) {
59 return LLDPTLV.TLVType.PortID.getValue();
60 } else if (typeDesc.equals(TTL)) {
61 return LLDPTLV.TLVType.TTL.getValue();
63 return LLDPTLV.TLVType.Unknown.getValue();
69 * - description of the type of TLV
70 * @return LLDPTLV - full TLV
72 public LLDPTLV getTLV(String type) {
73 return tlvList.get(getType(type));
78 * - description of the type of TLV
83 public void setTLV(String type, LLDPTLV tlv) {
84 tlvList.put(getType(type), tlv);
88 * @return the chassisId TLV
90 public LLDPTLV getChassisId() {
91 return getTLV(CHASSISID);
96 * - the chassisId to set
98 public LLDP setChassisId(LLDPTLV chassisId) {
99 tlvList.put(getType(CHASSISID), chassisId);
104 * @return LLDPTLV - the portId TLV
106 public LLDPTLV getPortId() {
107 return tlvList.get(getType(PORTID));
112 * - the portId to set
115 public LLDP setPortId(LLDPTLV portId) {
116 tlvList.put(getType(PORTID), portId);
121 * @return LLDPTLV - the ttl TLV
123 public LLDPTLV getTtl() {
124 return tlvList.get(getType(TTL));
132 public LLDP setTtl(LLDPTLV ttl) {
133 tlvList.put(getType(TTL), ttl);
138 * @return the optionalTLVList
140 public List<LLDPTLV> getOptionalTLVList() {
141 List<LLDPTLV> list = new ArrayList<LLDPTLV>();
142 for (Map.Entry<Byte, LLDPTLV> entry : tlvList.entrySet()) {
143 byte type = entry.getKey();
144 if ((type == LLDPTLV.TLVType.ChassisID.getValue())
145 || (type == LLDPTLV.TLVType.PortID.getValue())
146 || (type == LLDPTLV.TLVType.TTL.getValue())) {
149 list.add(entry.getValue());
156 * @param optionalTLVList
157 * the optionalTLVList to set
160 public LLDP setOptionalTLVList(List<LLDPTLV> optionalTLVList) {
161 for (LLDPTLV tlv : optionalTLVList) {
162 tlvList.put(tlv.getType(), tlv);
168 public Packet deserialize(byte[] data, int bitOffset, int size)
169 throws PacketException {
170 int lldpOffset = bitOffset; // LLDP start
171 int lldpSize = size; // LLDP size
174 * Deserialize the TLVs until we reach the end of the packet
176 while (lldpSize > 0) {
177 LLDPTLV tlv = new LLDPTLV();
178 tlv.deserialize(data, lldpOffset, lldpSize);
179 int tlvSize = tlv.getTLVSize(); // Size of current TLV in bits
180 lldpOffset += tlvSize;
182 this.tlvList.put(tlv.getType(), tlv);
188 public byte[] serialize() throws PacketException {
190 byte[] serializedBytes = new byte[getLLDPPacketLength()];
192 for (Map.Entry<Byte, LLDPTLV> entry : tlvList.entrySet()) {
193 LLDPTLV tlv = entry.getValue();
194 int numBits = tlv.getTLVSize();
196 BitBufferHelper.setBytes(serializedBytes, tlv.serialize(),
197 startOffset, numBits);
198 } catch (BufferException e) {
199 throw new PacketException(e.getMessage());
201 startOffset += numBits;
203 // Now add the empty LLDPTLV at the end
205 BitBufferHelper.setBytes(serializedBytes,
206 LLDP.emptyTLV.serialize(), startOffset,
207 LLDP.emptyTLV.getTLVSize());
208 } catch (BufferException e) {
209 throw new PacketException(e.getMessage());
212 return serializedBytes;
216 * Returns the size of LLDP packet in bytes
218 * @return int - LLDP Packet size in bytes
220 private int getLLDPPacketLength() {
224 for (Map.Entry<Byte, LLDPTLV> entry : this.tlvList.entrySet()) {
225 tlv = entry.getValue();
226 len += tlv.getTLVSize();
228 len += LLDP.emptyTLV.getTLVSize();
230 return len / NetUtils.NumBitsInAByte;