blob: 77efe1b719b1e516e6cc7f8c3cd66ad7a86de0d7 [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
33 /**
34 * @return the type
35 */
36 public byte getType() {
37 return this.type;
38 }
39
40 /**
41 * @param type
42 * the type to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080043 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070044 */
45 public LLDPTLV setType(final byte type) {
46 this.type = type;
47 return this;
48 }
49
50 /**
51 * @return the length
52 */
53 public short getLength() {
54 return this.length;
55 }
56
57 /**
58 * @param length
59 * the length to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080060 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070061 */
62 public LLDPTLV setLength(final short length) {
63 this.length = length;
64 return this;
65 }
66
67 /**
68 * @return the value
69 */
70 public byte[] getValue() {
71 return this.value;
72 }
73
74 /**
75 * @param value
76 * the value to set
Yuta HIGUCHI2281b3f2014-11-04 00:20:48 -080077 * @return this
alshabibc4901cd2014-09-05 16:50:40 -070078 */
79 public LLDPTLV setValue(final byte[] value) {
80 this.value = value;
81 return this;
82 }
83
84 public byte[] serialize() {
85 // type = 7 bits
86 // info string length 9 bits, each value == byte
87 // info string
88 final short scratch = (short) ((0x7f & this.type) << 9 | 0x1ff & this.length);
89 final byte[] data = new byte[2 + this.length];
90 final ByteBuffer bb = ByteBuffer.wrap(data);
91 bb.putShort(scratch);
92 if (this.value != null) {
93 bb.put(this.value);
94 }
95 return data;
96 }
97
Jonathan Hart2a655752015-04-07 16:46:33 -070098 public LLDPTLV deserialize(final ByteBuffer bb) throws DeserializationException {
99 if (bb.remaining() < 2) {
100 throw new DeserializationException(
101 "Not enough bytes to deserialize TLV type and length");
102 }
103 short typeLength;
104 typeLength = bb.getShort();
105 this.type = (byte) (typeLength >> 9 & 0x7f);
106 this.length = (short) (typeLength & 0x1ff);
alshabib7911a052014-10-16 17:49:37 -0700107
alshabibc4901cd2014-09-05 16:50:40 -0700108 if (this.length > 0) {
109 this.value = new byte[this.length];
110
111 // if there is an underrun just toss the TLV
112 if (bb.remaining() < this.length) {
Jonathan Hart2a655752015-04-07 16:46:33 -0700113 throw new DeserializationException(
114 "Remaining bytes are less then the length of the TLV");
alshabibc4901cd2014-09-05 16:50:40 -0700115 }
116 bb.get(this.value);
117 }
alshabib7911a052014-10-16 17:49:37 -0700118
alshabibc4901cd2014-09-05 16:50:40 -0700119 return this;
120 }
121
122 /*
123 * (non-Javadoc)
124 *
125 * @see java.lang.Object#hashCode()
126 */
127 @Override
128 public int hashCode() {
129 final int prime = 1423;
130 int result = 1;
131 result = prime * result + this.length;
132 result = prime * result + this.type;
133 result = prime * result + Arrays.hashCode(this.value);
134 return result;
135 }
136
137 /*
138 * (non-Javadoc)
139 *
140 * @see java.lang.Object#equals(java.lang.Object)
141 */
142 @Override
143 public boolean equals(final Object obj) {
144 if (this == obj) {
145 return true;
146 }
147 if (obj == null) {
148 return false;
149 }
150 if (!(obj instanceof LLDPTLV)) {
151 return false;
152 }
153 final LLDPTLV other = (LLDPTLV) obj;
154 if (this.length != other.length) {
155 return false;
156 }
157 if (this.type != other.type) {
158 return false;
159 }
160 if (!Arrays.equals(this.value, other.value)) {
161 return false;
162 }
163 return true;
164 }
165}