blob: 105a9f38bc330192f3224fc3f7f0a69a5606e020 [file] [log] [blame]
alshabibc4901cd2014-09-05 16:50:40 -07001/*******************************************************************************
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ******************************************************************************/
16/**
17 * Copyright 2011, Big Switch Networks, Inc.
18 * Originally created by David Erickson, Stanford University
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 * not use this file except in compliance with the License. You may obtain
22 * a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 * License for the specific language governing permissions and limitations
30 * under the License.
31 **/
32
33/**
34 *
35 */
36package org.onlab.packet;
37
38import java.nio.ByteBuffer;
39import java.util.ArrayList;
40import java.util.List;
41
42/**
alshabibc4901cd2014-09-05 16:50:40 -070043 *
44 */
45public class LLDP extends BasePacket {
46 protected LLDPTLV chassisId;
47 protected LLDPTLV portId;
48 protected LLDPTLV ttl;
49 protected List<LLDPTLV> optionalTLVList;
50 protected short ethType;
51
52 public LLDP() {
53 this.optionalTLVList = new ArrayList<LLDPTLV>();
54 this.ethType = Ethernet.TYPE_LLDP;
55 }
56
57 /**
58 * @return the chassisId
59 */
60 public LLDPTLV getChassisId() {
61 return this.chassisId;
62 }
63
64 /**
tom5f18cf32014-09-13 14:10:57 -070065 * @param chassis
alshabibc4901cd2014-09-05 16:50:40 -070066 * the chassisId to set
67 */
68 public LLDP setChassisId(final LLDPTLV chassis) {
69 this.chassisId = chassis;
70 return this;
71 }
72
73 /**
74 * @return the portId
75 */
76 public LLDPTLV getPortId() {
77 return this.portId;
78 }
79
80 /**
81 * @param portId
82 * the portId to set
83 */
84 public LLDP setPortId(final LLDPTLV portId) {
85 this.portId = portId;
86 return this;
87 }
88
89 /**
90 * @return the ttl
91 */
92 public LLDPTLV getTtl() {
93 return this.ttl;
94 }
95
96 /**
97 * @param ttl
98 * the ttl to set
99 */
100 public LLDP setTtl(final LLDPTLV ttl) {
101 this.ttl = ttl;
102 return this;
103 }
104
105 /**
106 * @return the optionalTLVList
107 */
108 public List<LLDPTLV> getOptionalTLVList() {
109 return this.optionalTLVList;
110 }
111
112 /**
113 * @param optionalTLVList
114 * the optionalTLVList to set
115 */
116 public LLDP setOptionalTLVList(final List<LLDPTLV> optionalTLVList) {
117 this.optionalTLVList = optionalTLVList;
118 return this;
119 }
120
121 @Override
122 public byte[] serialize() {
123 int length = 2 + this.chassisId.getLength() + 2
124 + this.portId.getLength() + 2 + this.ttl.getLength() + 2;
125 for (final LLDPTLV tlv : this.optionalTLVList) {
126 length += 2 + tlv.getLength();
127 }
128
129 final byte[] data = new byte[length];
130 final ByteBuffer bb = ByteBuffer.wrap(data);
131 bb.put(this.chassisId.serialize());
132 bb.put(this.portId.serialize());
133 bb.put(this.ttl.serialize());
134 for (final LLDPTLV tlv : this.optionalTLVList) {
135 bb.put(tlv.serialize());
136 }
137 bb.putShort((short) 0); // End of LLDPDU
138
139 /*
140 * if (this.parent != null && this.parent instanceof Ethernet) {
141 * ((Ethernet) this.parent).setEtherType(this.ethType); }
142 */
143
144 return data;
145 }
146
147 @Override
148 public IPacket deserialize(final byte[] data, final int offset,
149 final int length) {
150 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
151 LLDPTLV tlv;
152 do {
153 tlv = new LLDPTLV().deserialize(bb);
154
155 // if there was a failure to deserialize stop processing TLVs
156 if (tlv == null) {
157 break;
158 }
159 switch (tlv.getType()) {
160 case 0x0:
161 // can throw this one away, its just an end delimiter
162 break;
163 case 0x1:
164 this.chassisId = tlv;
165 break;
166 case 0x2:
167 this.portId = tlv;
168 break;
169 case 0x3:
170 this.ttl = tlv;
171 break;
172 default:
173 this.optionalTLVList.add(tlv);
174 break;
175 }
176 } while (tlv.getType() != 0 && bb.hasRemaining());
177 return this;
178 }
179
180 /*
181 * (non-Javadoc)
182 *
183 * @see java.lang.Object#hashCode()
184 */
185 @Override
186 public int hashCode() {
187 final int prime = 883;
188 int result = super.hashCode();
189 result = prime * result
190 + (this.chassisId == null ? 0 : this.chassisId.hashCode());
191 result = prime * result + this.optionalTLVList.hashCode();
192 result = prime * result
193 + (this.portId == null ? 0 : this.portId.hashCode());
194 result = prime * result + (this.ttl == null ? 0 : this.ttl.hashCode());
195 return result;
196 }
197
198 /*
199 * (non-Javadoc)
200 *
201 * @see java.lang.Object#equals(java.lang.Object)
202 */
203 @Override
204 public boolean equals(final Object obj) {
205 if (this == obj) {
206 return true;
207 }
208 if (!super.equals(obj)) {
209 return false;
210 }
211 if (!(obj instanceof LLDP)) {
212 return false;
213 }
214 final LLDP other = (LLDP) obj;
215 if (this.chassisId == null) {
216 if (other.chassisId != null) {
217 return false;
218 }
219 } else if (!this.chassisId.equals(other.chassisId)) {
220 return false;
221 }
222 if (!this.optionalTLVList.equals(other.optionalTLVList)) {
223 return false;
224 }
225 if (this.portId == null) {
226 if (other.portId != null) {
227 return false;
228 }
229 } else if (!this.portId.equals(other.portId)) {
230 return false;
231 }
232 if (this.ttl == null) {
233 if (other.ttl != null) {
234 return false;
235 }
236 } else if (!this.ttl.equals(other.ttl)) {
237 return false;
238 }
239 return true;
240 }
241}