blob: 7acef720329d8a42575ec54f5269eb58cacffda4 [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
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070018package net.onrc.onos.core.packet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019
20import java.nio.ByteBuffer;
21import java.util.Arrays;
22
Pavlin Radoslavovc9bacee2014-04-11 19:02:17 -070023import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
24
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080025/**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080026 * @author David Erickson (daviderickson@cs.stanford.edu)
27 */
28public class LLDPTLV {
29 protected byte type;
30 protected short length;
31 protected byte[] value;
32
33 /**
34 * @return the type
35 */
36 public byte getType() {
37 return type;
38 }
39
40 /**
41 * @param type the type to set
42 */
43 public LLDPTLV setType(byte type) {
44 this.type = type;
45 return this;
46 }
47
48 /**
49 * @return the length
50 */
51 public short getLength() {
52 return length;
53 }
54
55 /**
56 * @param length the length to set
57 */
58 public LLDPTLV setLength(short length) {
59 this.length = length;
60 return this;
61 }
62
63 /**
64 * @return the value
65 */
Pavlin Radoslavovc9bacee2014-04-11 19:02:17 -070066 @SuppressFBWarnings(value = "EI_EXPOSE_REP",
67 justification = "TODO: Return a copy of the object?")
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080068 public byte[] getValue() {
69 return value;
70 }
71
72 /**
73 * @param value the value to set
74 */
Pavlin Radoslavovc9bacee2014-04-11 19:02:17 -070075 @SuppressFBWarnings(value = "EI_EXPOSE_REP2",
76 justification = "TODO: Store a copy of the object?")
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080077 public LLDPTLV setValue(byte[] value) {
78 this.value = value;
79 return this;
80 }
81
82 public byte[] serialize() {
83 // type = 7 bits
84 // info string length 9 bits, each value == byte
85 // info string
86 short scratch = (short) (((0x7f & this.type) << 9) | (0x1ff & this.length));
Ray Milkey269ffb92014-04-03 14:43:30 -070087 byte[] data = new byte[2 + this.length];
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080088 ByteBuffer bb = ByteBuffer.wrap(data);
89 bb.putShort(scratch);
Ray Milkeyb29e6262014-04-09 16:02:14 -070090 if (this.value != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080091 bb.put(this.value);
Ray Milkeyb29e6262014-04-09 16:02:14 -070092 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080093 return data;
94 }
95
96 public LLDPTLV deserialize(ByteBuffer bb) {
97 short sscratch;
98 sscratch = bb.getShort();
99 this.type = (byte) ((sscratch >> 9) & 0x7f);
100 this.length = (short) (sscratch & 0x1ff);
101 if (this.length > 0) {
102 this.value = new byte[this.length];
103
104 // if there is an underrun just toss the TLV
Ray Milkeyb29e6262014-04-09 16:02:14 -0700105 if (bb.remaining() < this.length) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800106 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700107 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800108 bb.get(this.value);
109 }
110 return this;
111 }
112
113 /* (non-Javadoc)
114 * @see java.lang.Object#hashCode()
115 */
116 @Override
117 public int hashCode() {
118 final int prime = 1423;
119 int result = 1;
120 result = prime * result + length;
121 result = prime * result + type;
122 result = prime * result + Arrays.hashCode(value);
123 return result;
124 }
125
126 /* (non-Javadoc)
127 * @see java.lang.Object#equals(java.lang.Object)
128 */
129 @Override
130 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700131 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800132 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700133 }
134 if (obj == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800135 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700136 }
Pavlin Radoslavovadc3ec12014-04-11 16:08:53 -0700137 //
138 // NOTE: Subclasses are are considered as change of identity, hence
139 // equals() will return false if the class type doesn't match.
140 //
141 // The implication is that two instances - base class and derived class
142 // will be different even if they have same bits on the wire.
143 // We use this assumption to address the fundamental
144 // "equivalence relation" issue with class inheritance and "equals()":
145 // http://www.artima.com/lejava/articles/equality.html
146 // http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals.html
147 //
148 // Based on existing code, we don't mix the usage of based and derived
149 // class instances.
150 //
151 // Note that the fix below is different from the Floodlight fix.
152 // The Floodlight code uses "if (!(obj instanceof LLDPTLV))", but
153 // that statement breaks the "equivalence relation".
154 //
155 if (getClass() != obj.getClass()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800156 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700157 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800158 LLDPTLV other = (LLDPTLV) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700159 if (length != other.length) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800160 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700161 }
162 if (type != other.type) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800163 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700164 }
165 if (!Arrays.equals(value, other.value)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800166 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700167 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800168 return true;
169 }
170}