blob: 16c9e3120c1822b7dfbc219a006091fb187ce74d [file] [log] [blame]
alshabibc4901cd2014-09-05 16:50:40 -07001/*******************************************************************************
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * 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
7 *
8 * 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.
15 ******************************************************************************/
16/**
17 * Copyright 2011, Big Switch Networks, Inc.
18 * Originally created by David Erickson, Stanford University
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 * not use this file except in compliance with the License. You may obtain
22 * a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
29 * License for the specific language governing permissions and limitations
30 * under the License.
31 **/
32
33package org.onlab.packet;
34
35import java.nio.ByteBuffer;
36import java.util.Arrays;
37
38/**
39 *
40 *
alshabibc4901cd2014-09-05 16:50:40 -070041 */
42public class LLDPTLV {
43 protected byte type;
44 protected short length;
45 protected byte[] value;
46
47 /**
48 * @return the type
49 */
50 public byte getType() {
51 return this.type;
52 }
53
54 /**
55 * @param type
56 * the type to set
57 */
58 public LLDPTLV setType(final byte type) {
59 this.type = type;
60 return this;
61 }
62
63 /**
64 * @return the length
65 */
66 public short getLength() {
67 return this.length;
68 }
69
70 /**
71 * @param length
72 * the length to set
73 */
74 public LLDPTLV setLength(final short length) {
75 this.length = length;
76 return this;
77 }
78
79 /**
80 * @return the value
81 */
82 public byte[] getValue() {
83 return this.value;
84 }
85
86 /**
87 * @param value
88 * the value to set
89 */
90 public LLDPTLV setValue(final byte[] value) {
91 this.value = value;
92 return this;
93 }
94
95 public byte[] serialize() {
96 // type = 7 bits
97 // info string length 9 bits, each value == byte
98 // info string
99 final short scratch = (short) ((0x7f & this.type) << 9 | 0x1ff & this.length);
100 final byte[] data = new byte[2 + this.length];
101 final ByteBuffer bb = ByteBuffer.wrap(data);
102 bb.putShort(scratch);
103 if (this.value != null) {
104 bb.put(this.value);
105 }
106 return data;
107 }
108
109 public LLDPTLV deserialize(final ByteBuffer bb) {
110 short sscratch;
111 sscratch = bb.getShort();
112 this.type = (byte) (sscratch >> 9 & 0x7f);
113 this.length = (short) (sscratch & 0x1ff);
alshabib7911a052014-10-16 17:49:37 -0700114
alshabibc4901cd2014-09-05 16:50:40 -0700115 if (this.length > 0) {
116 this.value = new byte[this.length];
117
118 // if there is an underrun just toss the TLV
119 if (bb.remaining() < this.length) {
120 return null;
121 }
122 bb.get(this.value);
123 }
alshabib7911a052014-10-16 17:49:37 -0700124
alshabibc4901cd2014-09-05 16:50:40 -0700125 return this;
126 }
127
128 /*
129 * (non-Javadoc)
130 *
131 * @see java.lang.Object#hashCode()
132 */
133 @Override
134 public int hashCode() {
135 final int prime = 1423;
136 int result = 1;
137 result = prime * result + this.length;
138 result = prime * result + this.type;
139 result = prime * result + Arrays.hashCode(this.value);
140 return result;
141 }
142
143 /*
144 * (non-Javadoc)
145 *
146 * @see java.lang.Object#equals(java.lang.Object)
147 */
148 @Override
149 public boolean equals(final Object obj) {
150 if (this == obj) {
151 return true;
152 }
153 if (obj == null) {
154 return false;
155 }
156 if (!(obj instanceof LLDPTLV)) {
157 return false;
158 }
159 final LLDPTLV other = (LLDPTLV) obj;
160 if (this.length != other.length) {
161 return false;
162 }
163 if (this.type != other.type) {
164 return false;
165 }
166 if (!Arrays.equals(this.value, other.value)) {
167 return false;
168 }
169 return true;
170 }
171}