blob: 1c51456639136e83546be815c1912c904c0379bb [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
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 {
29 protected byte type;
30 protected short length;
31 protected byte[] value;
32
Jian Li5fc14292015-12-04 11:30:46 -080033 @Override
34 public String toString() {
35 StringBuilder sb = new StringBuilder();
36 sb.append("[");
37 sb.append("type= ");
38 sb.append(type);
39 sb.append("length= ");
40 sb.append(length);
41 sb.append("value= ");
42 sb.append(Arrays.toString(value));
43 sb.append("]");
44 return sb.toString();
45 }
46
alshabibc4901cd2014-09-05 16:50:40 -070047 /**
48 * @return the type
49 */
50 public byte getType() {
51 return this.type;
52 }
53
54 /**
55 * @param type
56 * the type to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080057 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070058 */
59 public LLDPTLV setType(final byte type) {
60 this.type = type;
61 return this;
62 }
63
64 /**
65 * @return the length
66 */
67 public short getLength() {
68 return this.length;
69 }
70
71 /**
72 * @param length
73 * the length to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080074 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070075 */
76 public LLDPTLV setLength(final short length) {
77 this.length = length;
78 return this;
79 }
80
81 /**
82 * @return the value
83 */
84 public byte[] getValue() {
85 return this.value;
86 }
87
88 /**
89 * @param value
90 * the value to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080091 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070092 */
93 public LLDPTLV setValue(final byte[] value) {
94 this.value = value;
95 return this;
96 }
97
98 public byte[] serialize() {
99 // type = 7 bits
100 // info string length 9 bits, each value == byte
101 // info string
102 final short scratch = (short) ((0x7f & this.type) << 9 | 0x1ff & this.length);
103 final byte[] data = new byte[2 + this.length];
104 final ByteBuffer bb = ByteBuffer.wrap(data);
105 bb.putShort(scratch);
106 if (this.value != null) {
107 bb.put(this.value);
108 }
109 return data;
110 }
111
Jonathan Hart2a655752015-04-07 16:46:33 -0700112 public LLDPTLV deserialize(final ByteBuffer bb) throws DeserializationException {
113 if (bb.remaining() < 2) {
114 throw new DeserializationException(
115 "Not enough bytes to deserialize TLV type and length");
116 }
117 short typeLength;
118 typeLength = bb.getShort();
119 this.type = (byte) (typeLength >> 9 & 0x7f);
120 this.length = (short) (typeLength & 0x1ff);
alshabib7911a052014-10-16 17:49:37 -0700121
alshabibc4901cd2014-09-05 16:50:40 -0700122 if (this.length > 0) {
123 this.value = new byte[this.length];
124
125 // if there is an underrun just toss the TLV
126 if (bb.remaining() < this.length) {
Jonathan Hart2a655752015-04-07 16:46:33 -0700127 throw new DeserializationException(
128 "Remaining bytes are less then the length of the TLV");
alshabibc4901cd2014-09-05 16:50:40 -0700129 }
130 bb.get(this.value);
131 }
alshabib7911a052014-10-16 17:49:37 -0700132
alshabibc4901cd2014-09-05 16:50:40 -0700133 return this;
134 }
135
136 /*
137 * (non-Javadoc)
138 *
139 * @see java.lang.Object#hashCode()
140 */
141 @Override
142 public int hashCode() {
143 final int prime = 1423;
144 int result = 1;
145 result = prime * result + this.length;
146 result = prime * result + this.type;
147 result = prime * result + Arrays.hashCode(this.value);
148 return result;
149 }
150
151 /*
152 * (non-Javadoc)
153 *
154 * @see java.lang.Object#equals(java.lang.Object)
155 */
156 @Override
157 public boolean equals(final Object obj) {
158 if (this == obj) {
159 return true;
160 }
161 if (obj == null) {
162 return false;
163 }
164 if (!(obj instanceof LLDPTLV)) {
165 return false;
166 }
167 final LLDPTLV other = (LLDPTLV) obj;
168 if (this.length != other.length) {
169 return false;
170 }
171 if (this.type != other.type) {
172 return false;
173 }
174 if (!Arrays.equals(this.value, other.value)) {
175 return false;
176 }
177 return true;
178 }
179}