edgeUpdate API enhancement and related changes.
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / packet / LLDP.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.sal.packet;
10
11 import java.util.ArrayList;
12 import java.util.LinkedHashMap;
13 import java.util.List;
14 import java.util.Map;
15 import org.opendaylight.controller.sal.utils.NetUtils;
16
17 /**
18  * Class that represents the LLDP frame objects
19  */
20
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)
27             .setType((byte) 0);
28     public static final byte[] LLDPMulticastMac = { 1, (byte) 0x80,
29             (byte) 0xc2, 0, 0, (byte) 0xe };
30     private Map<Byte, LLDPTLV> tlvList;
31
32     /**
33      * Default constructor that creates the tlvList LinkedHashMap
34      */
35     public LLDP() {
36         super();
37         tlvList = new LinkedHashMap<Byte, LLDPTLV>(LLDPDefaultTlvs);
38     }
39
40     /**
41      * Constructor that creates the tlvList LinkedHashMap and sets the write
42      * access for the same
43      */
44     public LLDP(boolean writeAccess) {
45         super(writeAccess);
46         tlvList = new LinkedHashMap<Byte, LLDPTLV>(LLDPDefaultTlvs); // Mandatory
47                                                                      // TLVs
48     }
49
50     /**
51      * @param String
52      *            - description of the type of TLV
53      * @return byte - type of TLV
54      */
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();
62         } else {
63             return LLDPTLV.TLVType.Unknown.getValue();
64         }
65     }
66
67     /**
68      * @param String
69      *            - description of the type of TLV
70      * @return LLDPTLV - full TLV
71      */
72     public LLDPTLV getTLV(String type) {
73         return tlvList.get(getType(type));
74     }
75
76     /**
77      * @param String
78      *            - description of the type of TLV
79      * @param LLDPTLV
80      *            - tlv to set
81      * @return void
82      */
83     public void setTLV(String type, LLDPTLV tlv) {
84         tlvList.put(getType(type), tlv);
85     }
86
87     /**
88      * @return the chassisId TLV
89      */
90     public LLDPTLV getChassisId() {
91         return getTLV(CHASSISID);
92     }
93
94     /**
95      * @param LLDPTLV
96      *            - the chassisId to set
97      */
98     public LLDP setChassisId(LLDPTLV chassisId) {
99         tlvList.put(getType(CHASSISID), chassisId);
100         return this;
101     }
102
103     /**
104      * @return LLDPTLV - the portId TLV
105      */
106     public LLDPTLV getPortId() {
107         return tlvList.get(getType(PORTID));
108     }
109
110     /**
111      * @param LLDPTLV
112      *            - the portId to set
113      * @return LLDP
114      */
115     public LLDP setPortId(LLDPTLV portId) {
116         tlvList.put(getType(PORTID), portId);
117         return this;
118     }
119
120     /**
121      * @return LLDPTLV - the ttl TLV
122      */
123     public LLDPTLV getTtl() {
124         return tlvList.get(getType(TTL));
125     }
126
127     /**
128      * @param LLDPTLV
129      *            - the ttl to set
130      * @return LLDP
131      */
132     public LLDP setTtl(LLDPTLV ttl) {
133         tlvList.put(getType(TTL), ttl);
134         return this;
135     }
136
137     /**
138      * @return the optionalTLVList
139      */
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())) {
147                 continue;
148             } else {
149                 list.add(entry.getValue());
150             }
151         }
152         return list;
153     }
154
155     /**
156      * @param optionalTLVList
157      *            the optionalTLVList to set
158      * @return LLDP
159      */
160     public LLDP setOptionalTLVList(List<LLDPTLV> optionalTLVList) {
161         for (LLDPTLV tlv : optionalTLVList) {
162             tlvList.put(tlv.getType(), tlv);
163         }
164         return this;
165     }
166
167     @Override
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
172
173         /*
174          * Deserialize the TLVs until we reach the end of the packet
175          */
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;
181             lldpSize -= tlvSize;
182             this.tlvList.put(tlv.getType(), tlv);
183         }
184         return this;
185     }
186
187     @Override
188     public byte[] serialize() throws PacketException {
189         int startOffset = 0;
190         byte[] serializedBytes = new byte[getLLDPPacketLength()];
191
192         for (Map.Entry<Byte, LLDPTLV> entry : tlvList.entrySet()) {
193             LLDPTLV tlv = entry.getValue();
194             int numBits = tlv.getTLVSize();
195             try {
196                 BitBufferHelper.setBytes(serializedBytes, tlv.serialize(),
197                         startOffset, numBits);
198             } catch (BufferException e) {
199                 throw new PacketException(e.getMessage());
200             }
201             startOffset += numBits;
202         }
203         // Now add the empty LLDPTLV at the end
204         try {
205             BitBufferHelper.setBytes(serializedBytes,
206                     LLDP.emptyTLV.serialize(), startOffset,
207                     LLDP.emptyTLV.getTLVSize());
208         } catch (BufferException e) {
209             throw new PacketException(e.getMessage());
210         }
211
212         return serializedBytes;
213     }
214
215     /**
216      * Returns the size of LLDP packet in bytes
217      * 
218      * @return int - LLDP Packet size in bytes
219      */
220     private int getLLDPPacketLength() {
221         int len = 0;
222         LLDPTLV tlv;
223
224         for (Map.Entry<Byte, LLDPTLV> entry : this.tlvList.entrySet()) {
225             tlv = entry.getValue();
226             len += tlv.getTLVSize();
227         }
228         len += LLDP.emptyTLV.getTLVSize();
229
230         return len / NetUtils.NumBitsInAByte;
231     }
232 }