blob: 3928068010bff271ee6c8ad0df33184b17cac753 [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
51 */
52 public LLDP setChassisId(final LLDPTLV chassis) {
53 this.chassisId = chassis;
54 return this;
55 }
56
57 /**
58 * @return the portId
59 */
60 public LLDPTLV getPortId() {
61 return this.portId;
62 }
63
64 /**
65 * @param portId
66 * the portId to set
67 */
68 public LLDP setPortId(final LLDPTLV portId) {
69 this.portId = portId;
70 return this;
71 }
72
73 /**
74 * @return the ttl
75 */
76 public LLDPTLV getTtl() {
77 return this.ttl;
78 }
79
80 /**
81 * @param ttl
82 * the ttl to set
83 */
84 public LLDP setTtl(final LLDPTLV ttl) {
85 this.ttl = ttl;
86 return this;
87 }
88
89 /**
90 * @return the optionalTLVList
91 */
92 public List<LLDPTLV> getOptionalTLVList() {
93 return this.optionalTLVList;
94 }
95
96 /**
97 * @param optionalTLVList
98 * the optionalTLVList to set
99 */
100 public LLDP setOptionalTLVList(final List<LLDPTLV> optionalTLVList) {
101 this.optionalTLVList = optionalTLVList;
102 return this;
103 }
104
105 @Override
106 public byte[] serialize() {
107 int length = 2 + this.chassisId.getLength() + 2
108 + this.portId.getLength() + 2 + this.ttl.getLength() + 2;
109 for (final LLDPTLV tlv : this.optionalTLVList) {
110 length += 2 + tlv.getLength();
111 }
112
113 final byte[] data = new byte[length];
114 final ByteBuffer bb = ByteBuffer.wrap(data);
115 bb.put(this.chassisId.serialize());
116 bb.put(this.portId.serialize());
117 bb.put(this.ttl.serialize());
118 for (final LLDPTLV tlv : this.optionalTLVList) {
119 bb.put(tlv.serialize());
120 }
121 bb.putShort((short) 0); // End of LLDPDU
122
123 /*
124 * if (this.parent != null && this.parent instanceof Ethernet) {
125 * ((Ethernet) this.parent).setEtherType(this.ethType); }
126 */
127
128 return data;
129 }
130
131 @Override
132 public IPacket deserialize(final byte[] data, final int offset,
133 final int length) {
134 final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
135 LLDPTLV tlv;
136 do {
alshabib7911a052014-10-16 17:49:37 -0700137 tlv = new LLDPOrganizationalTLV().deserialize(bb);
alshabibc4901cd2014-09-05 16:50:40 -0700138
139 // if there was a failure to deserialize stop processing TLVs
140 if (tlv == null) {
141 break;
142 }
143 switch (tlv.getType()) {
144 case 0x0:
145 // can throw this one away, its just an end delimiter
146 break;
147 case 0x1:
148 this.chassisId = tlv;
149 break;
150 case 0x2:
151 this.portId = tlv;
152 break;
153 case 0x3:
154 this.ttl = tlv;
155 break;
alshabib7911a052014-10-16 17:49:37 -0700156
alshabibc4901cd2014-09-05 16:50:40 -0700157 default:
158 this.optionalTLVList.add(tlv);
159 break;
160 }
161 } while (tlv.getType() != 0 && bb.hasRemaining());
162 return this;
163 }
164
165 /*
166 * (non-Javadoc)
167 *
168 * @see java.lang.Object#hashCode()
169 */
170 @Override
171 public int hashCode() {
172 final int prime = 883;
173 int result = super.hashCode();
174 result = prime * result
175 + (this.chassisId == null ? 0 : this.chassisId.hashCode());
176 result = prime * result + this.optionalTLVList.hashCode();
177 result = prime * result
178 + (this.portId == null ? 0 : this.portId.hashCode());
179 result = prime * result + (this.ttl == null ? 0 : this.ttl.hashCode());
180 return result;
181 }
182
183 /*
184 * (non-Javadoc)
185 *
186 * @see java.lang.Object#equals(java.lang.Object)
187 */
188 @Override
189 public boolean equals(final Object obj) {
190 if (this == obj) {
191 return true;
192 }
193 if (!super.equals(obj)) {
194 return false;
195 }
196 if (!(obj instanceof LLDP)) {
197 return false;
198 }
199 final LLDP other = (LLDP) obj;
200 if (this.chassisId == null) {
201 if (other.chassisId != null) {
202 return false;
203 }
204 } else if (!this.chassisId.equals(other.chassisId)) {
205 return false;
206 }
207 if (!this.optionalTLVList.equals(other.optionalTLVList)) {
208 return false;
209 }
210 if (this.portId == null) {
211 if (other.portId != null) {
212 return false;
213 }
214 } else if (!this.portId.equals(other.portId)) {
215 return false;
216 }
217 if (this.ttl == null) {
218 if (other.ttl != null) {
219 return false;
220 }
221 } else if (!this.ttl.equals(other.ttl)) {
222 return false;
223 }
224 return true;
225 }
226}