blob: 0b0f9ec0be35b7dea3682544dc79c72c53234f7e [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * 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 **/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017
18/**
Ray Milkey269ffb92014-04-03 14:43:30 -070019 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080020 */
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070021package net.onrc.onos.core.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)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080029 */
30public class LLDP extends BasePacket {
31 protected LLDPTLV chassisId;
32 protected LLDPTLV portId;
33 protected LLDPTLV ttl;
34 protected List<LLDPTLV> optionalTLVList;
35 protected short ethType;
36
37 public LLDP() {
38 this.optionalTLVList = new ArrayList<LLDPTLV>();
39 this.ethType = Ethernet.TYPE_LLDP;
40 }
41
42 /**
43 * @return the chassisId
44 */
45 public LLDPTLV getChassisId() {
46 return chassisId;
47 }
48
49 /**
50 * @param chassisId the chassisId to set
51 */
52 public LLDP setChassisId(LLDPTLV chassisId) {
53 this.chassisId = chassisId;
54 return this;
55 }
56
57 /**
58 * @return the portId
59 */
60 public LLDPTLV getPortId() {
61 return portId;
62 }
63
64 /**
65 * @param portId the portId to set
66 */
67 public LLDP setPortId(LLDPTLV portId) {
68 this.portId = portId;
69 return this;
70 }
71
72 /**
73 * @return the ttl
74 */
75 public LLDPTLV getTtl() {
76 return ttl;
77 }
78
79 /**
80 * @param ttl the ttl to set
81 */
82 public LLDP setTtl(LLDPTLV ttl) {
83 this.ttl = ttl;
84 return this;
85 }
86
87 /**
88 * @return the optionalTLVList
89 */
90 public List<LLDPTLV> getOptionalTLVList() {
91 return optionalTLVList;
92 }
93
94 /**
95 * @param optionalTLVList the optionalTLVList to set
96 */
97 public LLDP setOptionalTLVList(List<LLDPTLV> optionalTLVList) {
98 this.optionalTLVList = optionalTLVList;
99 return this;
100 }
101
102 @Override
103 public byte[] serialize() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700104 int length = 2 + this.chassisId.getLength() + 2 + this.portId.getLength() +
105 2 + this.ttl.getLength() + 2;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800106 for (LLDPTLV tlv : this.optionalTLVList) {
107 length += 2 + tlv.getLength();
108 }
109
110 byte[] data = new byte[length];
111 ByteBuffer bb = ByteBuffer.wrap(data);
112 bb.put(this.chassisId.serialize());
113 bb.put(this.portId.serialize());
114 bb.put(this.ttl.serialize());
115 for (LLDPTLV tlv : this.optionalTLVList) {
116 bb.put(tlv.serialize());
117 }
118 bb.putShort((short) 0); // End of LLDPDU
119
120 if (this.parent != null && this.parent instanceof Ethernet)
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 ((Ethernet) this.parent).setEtherType(ethType);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800122
123 return data;
124 }
125
126 @Override
127 public IPacket deserialize(byte[] data, int offset, int length) {
128 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
129 LLDPTLV tlv;
130 do {
131 tlv = new LLDPTLV().deserialize(bb);
132
133 // if there was a failure to deserialize stop processing TLVs
134 if (tlv == null)
135 break;
136 switch (tlv.getType()) {
137 case 0x0:
138 // can throw this one away, its just an end delimiter
139 break;
140 case 0x1:
141 this.chassisId = tlv;
142 break;
143 case 0x2:
144 this.portId = tlv;
145 break;
146 case 0x3:
147 this.ttl = tlv;
148 break;
149 default:
150 this.optionalTLVList.add(tlv);
151 break;
152 }
153 } while (tlv.getType() != 0 && bb.hasRemaining());
154 return this;
155 }
156
157 /* (non-Javadoc)
158 * @see java.lang.Object#hashCode()
159 */
160 @Override
161 public int hashCode() {
162 final int prime = 883;
163 int result = super.hashCode();
164 result = prime * result
165 + ((chassisId == null) ? 0 : chassisId.hashCode());
166 result = prime * result + (optionalTLVList.hashCode());
167 result = prime * result + ((portId == null) ? 0 : portId.hashCode());
168 result = prime * result + ((ttl == null) ? 0 : ttl.hashCode());
169 return result;
170 }
171
172 /* (non-Javadoc)
173 * @see java.lang.Object#equals(java.lang.Object)
174 */
175 @Override
176 public boolean equals(Object obj) {
177 if (this == obj)
178 return true;
179 if (!super.equals(obj))
180 return false;
181 if (!(obj instanceof LLDP))
182 return false;
183 LLDP other = (LLDP) obj;
184 if (chassisId == null) {
185 if (other.chassisId != null)
186 return false;
187 } else if (!chassisId.equals(other.chassisId))
188 return false;
189 if (!optionalTLVList.equals(other.optionalTLVList))
190 return false;
191 if (portId == null) {
192 if (other.portId != null)
193 return false;
194 } else if (!portId.equals(other.portId))
195 return false;
196 if (ttl == null) {
197 if (other.ttl != null)
198 return false;
199 } else if (!ttl.equals(other.ttl))
200 return false;
201 return true;
202 }
203}