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