blob: 2cea470176244a7f5bda3d70695c38f6f1e395f7 [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
Ray Milkeyb29e6262014-04-09 16:02:14 -0700120 if (this.parent != null && this.parent instanceof Ethernet) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700121 ((Ethernet) this.parent).setEtherType(ethType);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700122 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800123
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
Ray Milkeyb29e6262014-04-09 16:02:14 -0700135 if (tlv == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800136 break;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700137 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800138 switch (tlv.getType()) {
139 case 0x0:
140 // can throw this one away, its just an end delimiter
141 break;
142 case 0x1:
143 this.chassisId = tlv;
144 break;
145 case 0x2:
146 this.portId = tlv;
147 break;
148 case 0x3:
149 this.ttl = tlv;
150 break;
151 default:
152 this.optionalTLVList.add(tlv);
153 break;
154 }
155 } while (tlv.getType() != 0 && bb.hasRemaining());
156 return this;
157 }
158
159 /* (non-Javadoc)
160 * @see java.lang.Object#hashCode()
161 */
162 @Override
163 public int hashCode() {
164 final int prime = 883;
165 int result = super.hashCode();
166 result = prime * result
167 + ((chassisId == null) ? 0 : chassisId.hashCode());
168 result = prime * result + (optionalTLVList.hashCode());
169 result = prime * result + ((portId == null) ? 0 : portId.hashCode());
170 result = prime * result + ((ttl == null) ? 0 : ttl.hashCode());
171 return result;
172 }
173
174 /* (non-Javadoc)
175 * @see java.lang.Object#equals(java.lang.Object)
176 */
177 @Override
178 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700179 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800180 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700181 }
182 if (!super.equals(obj)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800183 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700184 }
185 if (!(obj instanceof LLDP)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800186 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700187 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800188 LLDP other = (LLDP) obj;
189 if (chassisId == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700190 if (other.chassisId != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800191 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700192 }
193 } else if (!chassisId.equals(other.chassisId)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800194 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700195 }
196 if (!optionalTLVList.equals(other.optionalTLVList)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800197 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700198 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800199 if (portId == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700200 if (other.portId != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800201 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700202 }
203 } else if (!portId.equals(other.portId)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800204 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700205 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800206 if (ttl == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700207 if (other.ttl != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800208 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700209 }
210 } else if (!ttl.equals(other.ttl)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800211 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700212 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800213 return true;
214 }
215}