blob: d41b08887279aac1ed6a564347dfa4738d2b5a9c [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 Radoslavov1d595c02014-04-16 14:11:54 -070023import org.apache.commons.lang.ArrayUtils;
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 */
66 public byte[] getValue() {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070067 return ArrayUtils.clone(this.value);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080068 }
69
70 /**
71 * @param value the value to set
72 */
73 public LLDPTLV setValue(byte[] value) {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070074 this.value = ArrayUtils.clone(value);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080075 return this;
76 }
77
78 public byte[] serialize() {
79 // type = 7 bits
80 // info string length 9 bits, each value == byte
81 // info string
82 short scratch = (short) (((0x7f & this.type) << 9) | (0x1ff & this.length));
Ray Milkey269ffb92014-04-03 14:43:30 -070083 byte[] data = new byte[2 + this.length];
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080084 ByteBuffer bb = ByteBuffer.wrap(data);
85 bb.putShort(scratch);
Ray Milkeyb29e6262014-04-09 16:02:14 -070086 if (this.value != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080087 bb.put(this.value);
Ray Milkeyb29e6262014-04-09 16:02:14 -070088 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080089 return data;
90 }
91
92 public LLDPTLV deserialize(ByteBuffer bb) {
93 short sscratch;
94 sscratch = bb.getShort();
95 this.type = (byte) ((sscratch >> 9) & 0x7f);
96 this.length = (short) (sscratch & 0x1ff);
97 if (this.length > 0) {
98 this.value = new byte[this.length];
99
100 // if there is an underrun just toss the TLV
Ray Milkeyb29e6262014-04-09 16:02:14 -0700101 if (bb.remaining() < this.length) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800102 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700103 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800104 bb.get(this.value);
105 }
106 return this;
107 }
108
109 /* (non-Javadoc)
110 * @see java.lang.Object#hashCode()
111 */
112 @Override
113 public int hashCode() {
114 final int prime = 1423;
115 int result = 1;
116 result = prime * result + length;
117 result = prime * result + type;
118 result = prime * result + Arrays.hashCode(value);
119 return result;
120 }
121
122 /* (non-Javadoc)
123 * @see java.lang.Object#equals(java.lang.Object)
124 */
125 @Override
126 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700127 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800128 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700129 }
130 if (obj == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800131 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700132 }
Pavlin Radoslavovadc3ec12014-04-11 16:08:53 -0700133 //
134 // NOTE: Subclasses are are considered as change of identity, hence
135 // equals() will return false if the class type doesn't match.
136 //
137 // The implication is that two instances - base class and derived class
138 // will be different even if they have same bits on the wire.
139 // We use this assumption to address the fundamental
140 // "equivalence relation" issue with class inheritance and "equals()":
141 // http://www.artima.com/lejava/articles/equality.html
142 // http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals.html
143 //
144 // Based on existing code, we don't mix the usage of based and derived
145 // class instances.
146 //
147 // Note that the fix below is different from the Floodlight fix.
148 // The Floodlight code uses "if (!(obj instanceof LLDPTLV))", but
149 // that statement breaks the "equivalence relation".
150 //
151 if (getClass() != obj.getClass()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800152 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700153 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800154 LLDPTLV other = (LLDPTLV) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700155 if (length != other.length) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800156 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700157 }
158 if (type != other.type) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800159 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700160 }
161 if (!Arrays.equals(value, other.value)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800162 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700163 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800164 return true;
165 }
166}