blob: 339bc3a7a5349e86f500163ca23dfc7a6598cc30 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
alshabibc4901cd2014-09-05 16:50:40 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
alshabibc4901cd2014-09-05 16:50:40 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
alshabibc4901cd2014-09-05 16:50:40 -070016
17/**
18 *
19 */
20package org.onlab.packet;
21
22import java.nio.ByteBuffer;
23import java.util.ArrayList;
24import java.util.List;
25
26/**
alshabibc4901cd2014-09-05 16:50:40 -070027 *
28 */
29public class LLDP extends BasePacket {
30 protected LLDPTLV chassisId;
31 protected LLDPTLV portId;
32 protected LLDPTLV ttl;
33 protected List<LLDPTLV> optionalTLVList;
34 protected short ethType;
35
36 public LLDP() {
37 this.optionalTLVList = new ArrayList<LLDPTLV>();
38 this.ethType = Ethernet.TYPE_LLDP;
39 }
40
41 /**
42 * @return the chassisId
43 */
44 public LLDPTLV getChassisId() {
45 return this.chassisId;
46 }
47
48 /**
tom5f18cf32014-09-13 14:10:57 -070049 * @param chassis
alshabibc4901cd2014-09-05 16:50:40 -070050 * the chassisId to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080051 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070052 */
53 public LLDP setChassisId(final LLDPTLV chassis) {
54 this.chassisId = chassis;
55 return this;
56 }
57
58 /**
59 * @return the portId
60 */
61 public LLDPTLV getPortId() {
62 return this.portId;
63 }
64
65 /**
66 * @param portId
67 * the portId to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080068 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070069 */
70 public LLDP setPortId(final LLDPTLV portId) {
71 this.portId = portId;
72 return this;
73 }
74
75 /**
76 * @return the ttl
77 */
78 public LLDPTLV getTtl() {
79 return this.ttl;
80 }
81
82 /**
83 * @param ttl
84 * the ttl to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080085 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070086 */
87 public LLDP setTtl(final LLDPTLV ttl) {
88 this.ttl = ttl;
89 return this;
90 }
91
92 /**
93 * @return the optionalTLVList
94 */
95 public List<LLDPTLV> getOptionalTLVList() {
96 return this.optionalTLVList;
97 }
98
99 /**
100 * @param optionalTLVList
101 * the optionalTLVList to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -0800102 * @return this
alshabibc4901cd2014-09-05 16:50:40 -0700103 */
104 public LLDP setOptionalTLVList(final List<LLDPTLV> optionalTLVList) {
105 this.optionalTLVList = optionalTLVList;
106 return this;
107 }
108
109 @Override
110 public byte[] serialize() {
111 int length = 2 + this.chassisId.getLength() + 2
112 + this.portId.getLength() + 2 + this.ttl.getLength() + 2;
113 for (final LLDPTLV tlv : this.optionalTLVList) {
114 length += 2 + tlv.getLength();
115 }
116
117 final byte[] data = new byte[length];
118 final ByteBuffer bb = ByteBuffer.wrap(data);
119 bb.put(this.chassisId.serialize());
120 bb.put(this.portId.serialize());
121 bb.put(this.ttl.serialize());
122 for (final LLDPTLV tlv : this.optionalTLVList) {
123 bb.put(tlv.serialize());
124 }
125 bb.putShort((short) 0); // End of LLDPDU
126
127 /*
128 * if (this.parent != null && this.parent instanceof Ethernet) {
129 * ((Ethernet) this.parent).setEtherType(this.ethType); }
130 */
131
132 return data;
133 }
134
135 @Override
136 public IPacket deserialize(final byte[] data, final int offset,
137 final int length) {
138 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
139 LLDPTLV tlv;
140 do {
alshabib7911a052014-10-16 17:49:37 -0700141 tlv = new LLDPOrganizationalTLV().deserialize(bb);
alshabibc4901cd2014-09-05 16:50:40 -0700142
143 // if there was a failure to deserialize stop processing TLVs
144 if (tlv == null) {
145 break;
146 }
147 switch (tlv.getType()) {
148 case 0x0:
149 // can throw this one away, its just an end delimiter
150 break;
151 case 0x1:
152 this.chassisId = tlv;
153 break;
154 case 0x2:
155 this.portId = tlv;
156 break;
157 case 0x3:
158 this.ttl = tlv;
159 break;
alshabib7911a052014-10-16 17:49:37 -0700160
alshabibc4901cd2014-09-05 16:50:40 -0700161 default:
162 this.optionalTLVList.add(tlv);
163 break;
164 }
165 } while (tlv.getType() != 0 && bb.hasRemaining());
166 return this;
167 }
168
169 /*
170 * (non-Javadoc)
171 *
172 * @see java.lang.Object#hashCode()
173 */
174 @Override
175 public int hashCode() {
176 final int prime = 883;
177 int result = super.hashCode();
178 result = prime * result
179 + (this.chassisId == null ? 0 : this.chassisId.hashCode());
180 result = prime * result + this.optionalTLVList.hashCode();
181 result = prime * result
182 + (this.portId == null ? 0 : this.portId.hashCode());
183 result = prime * result + (this.ttl == null ? 0 : this.ttl.hashCode());
184 return result;
185 }
186
187 /*
188 * (non-Javadoc)
189 *
190 * @see java.lang.Object#equals(java.lang.Object)
191 */
192 @Override
193 public boolean equals(final Object obj) {
194 if (this == obj) {
195 return true;
196 }
197 if (!super.equals(obj)) {
198 return false;
199 }
200 if (!(obj instanceof LLDP)) {
201 return false;
202 }
203 final LLDP other = (LLDP) obj;
204 if (this.chassisId == null) {
205 if (other.chassisId != null) {
206 return false;
207 }
208 } else if (!this.chassisId.equals(other.chassisId)) {
209 return false;
210 }
211 if (!this.optionalTLVList.equals(other.optionalTLVList)) {
212 return false;
213 }
214 if (this.portId == null) {
215 if (other.portId != null) {
216 return false;
217 }
218 } else if (!this.portId.equals(other.portId)) {
219 return false;
220 }
221 if (this.ttl == null) {
222 if (other.ttl != null) {
223 return false;
224 }
225 } else if (!this.ttl.equals(other.ttl)) {
226 return false;
227 }
228 return true;
229 }
230}