Open Kilda Java Documentation
VerificationPacket.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.floodlight.pathverification;
17 
18 import net.floodlightcontroller.packet.BasePacket;
19 import net.floodlightcontroller.packet.Data;
20 import net.floodlightcontroller.packet.IPacket;
21 import net.floodlightcontroller.packet.LLDPTLV;
22 import org.projectfloodlight.openflow.types.EthType;
23 
24 import java.nio.ByteBuffer;
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 public class VerificationPacket extends BasePacket {
29  protected LLDPTLV chassisId;
30  protected LLDPTLV portId;
31  protected LLDPTLV ttl;
32  protected List<LLDPTLV> optionalTLVList;
33  protected EthType ethType;
34 
35  public VerificationPacket() {
36  this.optionalTLVList = new ArrayList<LLDPTLV>();
37  }
38 
39  public VerificationPacket(Data data) {
40  this.optionalTLVList = new ArrayList<LLDPTLV>();
41  deserialize(data.getData(), 0, data.getData().length);
42  }
43 
47  public LLDPTLV getChassisId() {
48  return chassisId;
49  }
50 
55  this.chassisId = chassisId;
56  return this;
57  }
58 
62  public LLDPTLV getPortId() {
63  return portId;
64  }
65 
70  this.portId = portId;
71  return this;
72  }
73 
77  public LLDPTLV getTtl() {
78  return ttl;
79  }
80 
84  public VerificationPacket setTtl(LLDPTLV ttl) {
85  this.ttl = ttl;
86  return this;
87  }
88 
92  public List<LLDPTLV> getOptionalTLVList() {
93  return optionalTLVList;
94  }
95 
100  this.optionalTLVList = optionalTLVList;
101  return this;
102  }
103 
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  if (tlv != null)
109  length += 2 + tlv.getLength();
110  }
111 
112  byte[] data = new byte[length];
113  ByteBuffer bb = ByteBuffer.wrap(data);
114  bb.put(this.chassisId.serialize());
115  bb.put(this.portId.serialize());
116  bb.put(this.ttl.serialize());
117  for (LLDPTLV tlv : this.optionalTLVList) {
118  if (tlv != null) bb.put(tlv.serialize());
119  }
120  bb.putShort((short) 0); // End of LLDPDU
121 
122  return data;
123  }
124 
125  public IPacket deserialize(byte[] data, int offset, int length) {
126  ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
127  LLDPTLV tlv;
128  do {
129  tlv = new LLDPTLV().deserialize(bb);
130 
131  // if there was a failure to deserialize stop processing TLVs
132  if (tlv == null)
133  break;
134  switch (tlv.getType()) {
135  case 0x0:
136  // can throw this one away, its just an end delimiter
137  break;
138  case 0x1:
139  this.chassisId = tlv;
140  break;
141  case 0x2:
142  this.portId = tlv;
143  break;
144  case 0x3:
145  this.ttl = tlv;
146  break;
147  default:
148  this.optionalTLVList.add(tlv);
149  break;
150  }
151  } while (tlv.getType() != 0 && bb.hasRemaining());
152  return this;
153  }
154 
155  /* (non-Javadoc)
156  * @see java.lang.Object#hashCode()
157  */
158  @Override
159  public int hashCode() {
160  final int prime = 883;
161  int result = super.hashCode();
162  result = prime * result
163  + ((chassisId == null) ? 0 : chassisId.hashCode());
164  result = prime * result + (optionalTLVList.hashCode());
165  result = prime * result + ((portId == null) ? 0 : portId.hashCode());
166  result = prime * result + ((ttl == null) ? 0 : ttl.hashCode());
167  return result;
168  }
169 
170  /* (non-Javadoc)
171  * @see java.lang.Object#equals(java.lang.Object)
172  */
173  @Override
174  public boolean equals(Object obj) {
175  if (this == obj)
176  return true;
177  if (!super.equals(obj))
178  return false;
179  if (!(obj instanceof VerificationPacket))
180  return false;
182  if (chassisId == null) {
183  if (other.chassisId != null)
184  return false;
185  } else if (!chassisId.equals(other.chassisId))
186  return false;
187  if (!optionalTLVList.equals(other.optionalTLVList))
188  return false;
189  if (portId == null) {
190  if (other.portId != null)
191  return false;
192  } else if (!portId.equals(other.portId))
193  return false;
194  if (ttl == null) {
195  if (other.ttl != null)
196  return false;
197  } else if (!ttl.equals(other.ttl))
198  return false;
199  return true;
200  }
201 
202  @Override
203  public String toString() {
204  String str = "";
205  str += "chassisId=" + ((this.chassisId == null) ? "null" : this.chassisId.toString());
206  str += " portId=" + ((this.portId == null) ? "null" : this.portId.toString());
207  str += " ttl=" + ((this.ttl == null) ? "null" : this.ttl.toString());
208  str += " etherType=" + ethType.toString();
209  str += " optionalTlvList=[";
210  if (this.optionalTLVList != null) {
211  for (LLDPTLV l : optionalTLVList) str += l.toString() + ", ";
212  }
213  str += "]";
214  return str;
215  }
216 }
VerificationPacket setOptionalTLVList(List< LLDPTLV > optionalTLVList)
list result
Definition: plan-d.py:72
IPacket deserialize(byte[] data, int offset, int length)
net
Definition: plan-b.py:46