blob: 7277cdabf5949f048d91b5c3bb3f2900d5b68afd [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 {
alshabib7911a052014-10-16 17:49:37 -0700153 tlv = new LLDPOrganizationalTLV().deserialize(bb);
alshabibc4901cd2014-09-05 16:50:40 -0700154
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;
alshabib7911a052014-10-16 17:49:37 -0700172
alshabibc4901cd2014-09-05 16:50:40 -0700173 default:
174 this.optionalTLVList.add(tlv);
175 break;
176 }
177 } while (tlv.getType() != 0 && bb.hasRemaining());
178 return this;
179 }
180
181 /*
182 * (non-Javadoc)
183 *
184 * @see java.lang.Object#hashCode()
185 */
186 @Override
187 public int hashCode() {
188 final int prime = 883;
189 int result = super.hashCode();
190 result = prime * result
191 + (this.chassisId == null ? 0 : this.chassisId.hashCode());
192 result = prime * result + this.optionalTLVList.hashCode();
193 result = prime * result
194 + (this.portId == null ? 0 : this.portId.hashCode());
195 result = prime * result + (this.ttl == null ? 0 : this.ttl.hashCode());
196 return result;
197 }
198
199 /*
200 * (non-Javadoc)
201 *
202 * @see java.lang.Object#equals(java.lang.Object)
203 */
204 @Override
205 public boolean equals(final Object obj) {
206 if (this == obj) {
207 return true;
208 }
209 if (!super.equals(obj)) {
210 return false;
211 }
212 if (!(obj instanceof LLDP)) {
213 return false;
214 }
215 final LLDP other = (LLDP) obj;
216 if (this.chassisId == null) {
217 if (other.chassisId != null) {
218 return false;
219 }
220 } else if (!this.chassisId.equals(other.chassisId)) {
221 return false;
222 }
223 if (!this.optionalTLVList.equals(other.optionalTLVList)) {
224 return false;
225 }
226 if (this.portId == null) {
227 if (other.portId != null) {
228 return false;
229 }
230 } else if (!this.portId.equals(other.portId)) {
231 return false;
232 }
233 if (this.ttl == null) {
234 if (other.ttl != null) {
235 return false;
236 }
237 } else if (!this.ttl.equals(other.ttl)) {
238 return false;
239 }
240 return true;
241 }
242}