blob: b5fe83313bcaa39c9239c2760a9155d3274ee124 [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
98 public LLDPTLV deserialize(final ByteBuffer bb) {
99 short sscratch;
100 sscratch = bb.getShort();
101 this.type = (byte) (sscratch >> 9 & 0x7f);
102 this.length = (short) (sscratch & 0x1ff);
alshabib7911a052014-10-16 17:49:37 -0700103
alshabibc4901cd2014-09-05 16:50:40 -0700104 if (this.length > 0) {
105 this.value = new byte[this.length];
106
107 // if there is an underrun just toss the TLV
108 if (bb.remaining() < this.length) {
109 return null;
110 }
111 bb.get(this.value);
112 }
alshabib7911a052014-10-16 17:49:37 -0700113
alshabibc4901cd2014-09-05 16:50:40 -0700114 return this;
115 }
116
117 /*
118 * (non-Javadoc)
119 *
120 * @see java.lang.Object#hashCode()
121 */
122 @Override
123 public int hashCode() {
124 final int prime = 1423;
125 int result = 1;
126 result = prime * result + this.length;
127 result = prime * result + this.type;
128 result = prime * result + Arrays.hashCode(this.value);
129 return result;
130 }
131
132 /*
133 * (non-Javadoc)
134 *
135 * @see java.lang.Object#equals(java.lang.Object)
136 */
137 @Override
138 public boolean equals(final Object obj) {
139 if (this == obj) {
140 return true;
141 }
142 if (obj == null) {
143 return false;
144 }
145 if (!(obj instanceof LLDPTLV)) {
146 return false;
147 }
148 final LLDPTLV other = (LLDPTLV) obj;
149 if (this.length != other.length) {
150 return false;
151 }
152 if (this.type != other.type) {
153 return false;
154 }
155 if (!Arrays.equals(this.value, other.value)) {
156 return false;
157 }
158 return true;
159 }
160}