blob: 8de0d8a94a57dc3e2559655849f79a78f6e35910 [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
23/**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080024 * @author David Erickson (daviderickson@cs.stanford.edu)
25 */
26public class LLDPTLV {
27 protected byte type;
28 protected short length;
29 protected byte[] value;
30
31 /**
32 * @return the type
33 */
34 public byte getType() {
35 return type;
36 }
37
38 /**
39 * @param type the type to set
40 */
41 public LLDPTLV setType(byte type) {
42 this.type = type;
43 return this;
44 }
45
46 /**
47 * @return the length
48 */
49 public short getLength() {
50 return length;
51 }
52
53 /**
54 * @param length the length to set
55 */
56 public LLDPTLV setLength(short length) {
57 this.length = length;
58 return this;
59 }
60
61 /**
62 * @return the value
63 */
64 public byte[] getValue() {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070065 if (this.value == null) {
66 return null;
67 }
68 return this.value.clone();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080069 }
70
71 /**
72 * @param value the value to set
73 */
74 public LLDPTLV setValue(byte[] value) {
Pavlin Radoslavov0a9d5c32014-04-15 17:45:23 -070075 if (value == null) {
76 this.value = null;
77 } else {
78 this.value = value.clone();
79 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080080 return this;
81 }
82
83 public byte[] serialize() {
84 // type = 7 bits
85 // info string length 9 bits, each value == byte
86 // info string
87 short scratch = (short) (((0x7f & this.type) << 9) | (0x1ff & this.length));
Ray Milkey269ffb92014-04-03 14:43:30 -070088 byte[] data = new byte[2 + this.length];
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080089 ByteBuffer bb = ByteBuffer.wrap(data);
90 bb.putShort(scratch);
Ray Milkeyb29e6262014-04-09 16:02:14 -070091 if (this.value != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080092 bb.put(this.value);
Ray Milkeyb29e6262014-04-09 16:02:14 -070093 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080094 return data;
95 }
96
97 public LLDPTLV deserialize(ByteBuffer bb) {
98 short sscratch;
99 sscratch = bb.getShort();
100 this.type = (byte) ((sscratch >> 9) & 0x7f);
101 this.length = (short) (sscratch & 0x1ff);
102 if (this.length > 0) {
103 this.value = new byte[this.length];
104
105 // if there is an underrun just toss the TLV
Ray Milkeyb29e6262014-04-09 16:02:14 -0700106 if (bb.remaining() < this.length) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800107 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700108 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800109 bb.get(this.value);
110 }
111 return this;
112 }
113
114 /* (non-Javadoc)
115 * @see java.lang.Object#hashCode()
116 */
117 @Override
118 public int hashCode() {
119 final int prime = 1423;
120 int result = 1;
121 result = prime * result + length;
122 result = prime * result + type;
123 result = prime * result + Arrays.hashCode(value);
124 return result;
125 }
126
127 /* (non-Javadoc)
128 * @see java.lang.Object#equals(java.lang.Object)
129 */
130 @Override
131 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700132 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800133 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700134 }
135 if (obj == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800136 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700137 }
Pavlin Radoslavovadc3ec12014-04-11 16:08:53 -0700138 //
139 // NOTE: Subclasses are are considered as change of identity, hence
140 // equals() will return false if the class type doesn't match.
141 //
142 // The implication is that two instances - base class and derived class
143 // will be different even if they have same bits on the wire.
144 // We use this assumption to address the fundamental
145 // "equivalence relation" issue with class inheritance and "equals()":
146 // http://www.artima.com/lejava/articles/equality.html
147 // http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals.html
148 //
149 // Based on existing code, we don't mix the usage of based and derived
150 // class instances.
151 //
152 // Note that the fix below is different from the Floodlight fix.
153 // The Floodlight code uses "if (!(obj instanceof LLDPTLV))", but
154 // that statement breaks the "equivalence relation".
155 //
156 if (getClass() != obj.getClass()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800157 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700158 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800159 LLDPTLV other = (LLDPTLV) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700160 if (length != other.length) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800161 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700162 }
163 if (type != other.type) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800164 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700165 }
166 if (!Arrays.equals(value, other.value)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800167 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700168 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800169 return true;
170 }
171}