blob: 262cb246d8e277c9c477372b4693a0a36988a05a [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
Yuta HIGUCHIaa132f52014-06-26 10:18:39 -070025// CHECKSTYLE IGNORE WriteTag FOR NEXT 2 LINES
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080026/**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080027 * @author David Erickson (daviderickson@cs.stanford.edu)
28 */
29public class LLDPTLV {
30 protected byte type;
31 protected short length;
32 protected byte[] value;
33
34 /**
35 * @return the type
36 */
37 public byte getType() {
38 return type;
39 }
40
41 /**
42 * @param type the type to set
43 */
44 public LLDPTLV setType(byte type) {
45 this.type = type;
46 return this;
47 }
48
49 /**
50 * @return the length
51 */
52 public short getLength() {
53 return length;
54 }
55
56 /**
57 * @param length the length to set
58 */
59 public LLDPTLV setLength(short length) {
60 this.length = length;
61 return this;
62 }
63
64 /**
65 * @return the value
66 */
67 public byte[] getValue() {
Pavlin Radoslavov1d595c02014-04-16 14:11:54 -070068 return ArrayUtils.clone(this.value);
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 Radoslavov1d595c02014-04-16 14:11:54 -070075 this.value = ArrayUtils.clone(value);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080076 return this;
77 }
78
79 public byte[] serialize() {
80 // type = 7 bits
81 // info string length 9 bits, each value == byte
82 // info string
83 short scratch = (short) (((0x7f & this.type) << 9) | (0x1ff & this.length));
Ray Milkey269ffb92014-04-03 14:43:30 -070084 byte[] data = new byte[2 + this.length];
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080085 ByteBuffer bb = ByteBuffer.wrap(data);
86 bb.putShort(scratch);
Ray Milkeyb29e6262014-04-09 16:02:14 -070087 if (this.value != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080088 bb.put(this.value);
Ray Milkeyb29e6262014-04-09 16:02:14 -070089 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080090 return data;
91 }
92
93 public LLDPTLV deserialize(ByteBuffer bb) {
94 short sscratch;
95 sscratch = bb.getShort();
96 this.type = (byte) ((sscratch >> 9) & 0x7f);
97 this.length = (short) (sscratch & 0x1ff);
98 if (this.length > 0) {
99 this.value = new byte[this.length];
100
101 // if there is an underrun just toss the TLV
Ray Milkeyb29e6262014-04-09 16:02:14 -0700102 if (bb.remaining() < this.length) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800103 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700104 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800105 bb.get(this.value);
106 }
107 return this;
108 }
109
110 /* (non-Javadoc)
111 * @see java.lang.Object#hashCode()
112 */
113 @Override
114 public int hashCode() {
115 final int prime = 1423;
116 int result = 1;
117 result = prime * result + length;
118 result = prime * result + type;
119 result = prime * result + Arrays.hashCode(value);
120 return result;
121 }
122
123 /* (non-Javadoc)
124 * @see java.lang.Object#equals(java.lang.Object)
125 */
126 @Override
127 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700128 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800129 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700130 }
131 if (obj == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800132 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700133 }
Pavlin Radoslavovadc3ec12014-04-11 16:08:53 -0700134 //
135 // NOTE: Subclasses are are considered as change of identity, hence
136 // equals() will return false if the class type doesn't match.
137 //
138 // The implication is that two instances - base class and derived class
139 // will be different even if they have same bits on the wire.
140 // We use this assumption to address the fundamental
141 // "equivalence relation" issue with class inheritance and "equals()":
142 // http://www.artima.com/lejava/articles/equality.html
143 // http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals.html
144 //
145 // Based on existing code, we don't mix the usage of based and derived
146 // class instances.
147 //
148 // Note that the fix below is different from the Floodlight fix.
149 // The Floodlight code uses "if (!(obj instanceof LLDPTLV))", but
150 // that statement breaks the "equivalence relation".
151 //
152 if (getClass() != obj.getClass()) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800153 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700154 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800155 LLDPTLV other = (LLDPTLV) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700156 if (length != other.length) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800157 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700158 }
159 if (type != other.type) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800160 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700161 }
162 if (!Arrays.equals(value, other.value)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800163 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700164 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800165 return true;
166 }
167}