blob: 7f3eafa75f13f9159a980547af3f450d33801b27 [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
Yuta HIGUCHIaa132f52014-06-26 10:18:39 -070027// CHECKSTYLE IGNORE WriteTag FOR NEXT 2 LINES
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080028/**
29 * @author David Erickson (daviderickson@cs.stanford.edu)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080030 */
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() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 int length = 2 + this.chassisId.getLength() + 2 + this.portId.getLength() +
106 2 + this.ttl.getLength() + 2;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800107 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
Ray Milkeyb29e6262014-04-09 16:02:14 -0700121 if (this.parent != null && this.parent instanceof Ethernet) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700122 ((Ethernet) this.parent).setEtherType(ethType);
Ray Milkeyb29e6262014-04-09 16:02:14 -0700123 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800124
125 return data;
126 }
127
128 @Override
129 public IPacket deserialize(byte[] data, int offset, int length) {
130 ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
131 LLDPTLV tlv;
132 do {
133 tlv = new LLDPTLV().deserialize(bb);
134
135 // if there was a failure to deserialize stop processing TLVs
Ray Milkeyb29e6262014-04-09 16:02:14 -0700136 if (tlv == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800137 break;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700138 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800139 switch (tlv.getType()) {
140 case 0x0:
141 // can throw this one away, its just an end delimiter
142 break;
143 case 0x1:
144 this.chassisId = tlv;
145 break;
146 case 0x2:
147 this.portId = tlv;
148 break;
149 case 0x3:
150 this.ttl = tlv;
151 break;
152 default:
153 this.optionalTLVList.add(tlv);
154 break;
155 }
156 } while (tlv.getType() != 0 && bb.hasRemaining());
157 return this;
158 }
159
160 /* (non-Javadoc)
161 * @see java.lang.Object#hashCode()
162 */
163 @Override
164 public int hashCode() {
165 final int prime = 883;
166 int result = super.hashCode();
167 result = prime * result
168 + ((chassisId == null) ? 0 : chassisId.hashCode());
169 result = prime * result + (optionalTLVList.hashCode());
170 result = prime * result + ((portId == null) ? 0 : portId.hashCode());
171 result = prime * result + ((ttl == null) ? 0 : ttl.hashCode());
172 return result;
173 }
174
175 /* (non-Javadoc)
176 * @see java.lang.Object#equals(java.lang.Object)
177 */
178 @Override
179 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700180 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800181 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700182 }
183 if (!super.equals(obj)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800184 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700185 }
186 if (!(obj instanceof LLDP)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800187 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700188 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800189 LLDP other = (LLDP) obj;
190 if (chassisId == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700191 if (other.chassisId != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800192 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700193 }
194 } else if (!chassisId.equals(other.chassisId)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800195 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700196 }
197 if (!optionalTLVList.equals(other.optionalTLVList)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800198 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700199 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800200 if (portId == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700201 if (other.portId != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800202 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700203 }
204 } else if (!portId.equals(other.portId)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800205 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700206 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800207 if (ttl == null) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700208 if (other.ttl != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800209 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700210 }
211 } else if (!ttl.equals(other.ttl)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800212 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700213 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800214 return true;
215 }
216}