blob: 62f2e7a218a834c9a3b1795579c918369206c3e7 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
alshabibc4901cd2014-09-05 16:50:40 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
alshabibc4901cd2014-09-05 16:50:40 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
16
17
alshabibc4901cd2014-09-05 16:50:40 -070018
19package org.onlab.packet;
20
21import java.nio.ByteBuffer;
22import java.util.Arrays;
23
24/**
25 *
26 *
alshabibc4901cd2014-09-05 16:50:40 -070027 */
28public class LLDPTLV {
jaegonkim47b1b4a2017-12-04 14:08:26 +090029
30 public static final int MAX_LENGTH = 0x1ff;
31
alshabibc4901cd2014-09-05 16:50:40 -070032 protected byte type;
33 protected short length;
34 protected byte[] value;
35
Jian Li5fc14292015-12-04 11:30:46 -080036 @Override
37 public String toString() {
38 StringBuilder sb = new StringBuilder();
39 sb.append("[");
40 sb.append("type= ");
41 sb.append(type);
42 sb.append("length= ");
43 sb.append(length);
44 sb.append("value= ");
45 sb.append(Arrays.toString(value));
46 sb.append("]");
47 return sb.toString();
48 }
49
alshabibc4901cd2014-09-05 16:50:40 -070050 /**
51 * @return the type
52 */
53 public byte getType() {
54 return this.type;
55 }
56
57 /**
58 * @param type
59 * the type to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080060 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070061 */
62 public LLDPTLV setType(final byte type) {
63 this.type = type;
64 return this;
65 }
66
67 /**
68 * @return the length
69 */
70 public short getLength() {
71 return this.length;
72 }
73
74 /**
75 * @param length
76 * the length to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080077 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070078 */
79 public LLDPTLV setLength(final short length) {
80 this.length = length;
81 return this;
82 }
83
84 /**
85 * @return the value
86 */
87 public byte[] getValue() {
88 return this.value;
89 }
90
91 /**
92 * @param value
93 * the value to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080094 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070095 */
96 public LLDPTLV setValue(final byte[] value) {
97 this.value = value;
98 return this;
99 }
100
101 public byte[] serialize() {
102 // type = 7 bits
103 // info string length 9 bits, each value == byte
104 // info string
105 final short scratch = (short) ((0x7f & this.type) << 9 | 0x1ff & this.length);
106 final byte[] data = new byte[2 + this.length];
107 final ByteBuffer bb = ByteBuffer.wrap(data);
108 bb.putShort(scratch);
109 if (this.value != null) {
110 bb.put(this.value);
111 }
112 return data;
113 }
114
Jonathan Hart2a655752015-04-07 16:46:33 -0700115 public LLDPTLV deserialize(final ByteBuffer bb) throws DeserializationException {
116 if (bb.remaining() < 2) {
117 throw new DeserializationException(
118 "Not enough bytes to deserialize TLV type and length");
119 }
120 short typeLength;
121 typeLength = bb.getShort();
122 this.type = (byte) (typeLength >> 9 & 0x7f);
123 this.length = (short) (typeLength & 0x1ff);
alshabib7911a052014-10-16 17:49:37 -0700124
alshabibc4901cd2014-09-05 16:50:40 -0700125 if (this.length > 0) {
126 this.value = new byte[this.length];
127
128 // if there is an underrun just toss the TLV
129 if (bb.remaining() < this.length) {
Jonathan Hart2a655752015-04-07 16:46:33 -0700130 throw new DeserializationException(
131 "Remaining bytes are less then the length of the TLV");
alshabibc4901cd2014-09-05 16:50:40 -0700132 }
133 bb.get(this.value);
134 }
alshabib7911a052014-10-16 17:49:37 -0700135
alshabibc4901cd2014-09-05 16:50:40 -0700136 return this;
137 }
138
139 /*
140 * (non-Javadoc)
141 *
142 * @see java.lang.Object#hashCode()
143 */
144 @Override
145 public int hashCode() {
146 final int prime = 1423;
147 int result = 1;
148 result = prime * result + this.length;
149 result = prime * result + this.type;
150 result = prime * result + Arrays.hashCode(this.value);
151 return result;
152 }
153
154 /*
155 * (non-Javadoc)
156 *
157 * @see java.lang.Object#equals(java.lang.Object)
158 */
159 @Override
160 public boolean equals(final Object obj) {
161 if (this == obj) {
162 return true;
163 }
164 if (obj == null) {
165 return false;
166 }
167 if (!(obj instanceof LLDPTLV)) {
168 return false;
169 }
170 final LLDPTLV other = (LLDPTLV) obj;
171 if (this.length != other.length) {
172 return false;
173 }
174 if (this.type != other.type) {
175 return false;
176 }
177 if (!Arrays.equals(this.value, other.value)) {
178 return false;
179 }
180 return true;
181 }
182}