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