blob: 21bd55fa017fe268f9ebca258ee1cfc11cac24d4 [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
43 */
44 public LLDPTLV setType(final byte type) {
45 this.type = type;
46 return this;
47 }
48
49 /**
50 * @return the length
51 */
52 public short getLength() {
53 return this.length;
54 }
55
56 /**
57 * @param length
58 * the length to set
59 */
60 public LLDPTLV setLength(final short length) {
61 this.length = length;
62 return this;
63 }
64
65 /**
66 * @return the value
67 */
68 public byte[] getValue() {
69 return this.value;
70 }
71
72 /**
73 * @param value
74 * the value to set
75 */
76 public LLDPTLV setValue(final byte[] value) {
77 this.value = value;
78 return this;
79 }
80
81 public byte[] serialize() {
82 // type = 7 bits
83 // info string length 9 bits, each value == byte
84 // info string
85 final short scratch = (short) ((0x7f & this.type) << 9 | 0x1ff & this.length);
86 final byte[] data = new byte[2 + this.length];
87 final ByteBuffer bb = ByteBuffer.wrap(data);
88 bb.putShort(scratch);
89 if (this.value != null) {
90 bb.put(this.value);
91 }
92 return data;
93 }
94
95 public LLDPTLV deserialize(final ByteBuffer bb) {
96 short sscratch;
97 sscratch = bb.getShort();
98 this.type = (byte) (sscratch >> 9 & 0x7f);
99 this.length = (short) (sscratch & 0x1ff);
alshabib7911a052014-10-16 17:49:37 -0700100
alshabibc4901cd2014-09-05 16:50:40 -0700101 if (this.length > 0) {
102 this.value = new byte[this.length];
103
104 // if there is an underrun just toss the TLV
105 if (bb.remaining() < this.length) {
106 return null;
107 }
108 bb.get(this.value);
109 }
alshabib7911a052014-10-16 17:49:37 -0700110
alshabibc4901cd2014-09-05 16:50:40 -0700111 return this;
112 }
113
114 /*
115 * (non-Javadoc)
116 *
117 * @see java.lang.Object#hashCode()
118 */
119 @Override
120 public int hashCode() {
121 final int prime = 1423;
122 int result = 1;
123 result = prime * result + this.length;
124 result = prime * result + this.type;
125 result = prime * result + Arrays.hashCode(this.value);
126 return result;
127 }
128
129 /*
130 * (non-Javadoc)
131 *
132 * @see java.lang.Object#equals(java.lang.Object)
133 */
134 @Override
135 public boolean equals(final Object obj) {
136 if (this == obj) {
137 return true;
138 }
139 if (obj == null) {
140 return false;
141 }
142 if (!(obj instanceof LLDPTLV)) {
143 return false;
144 }
145 final LLDPTLV other = (LLDPTLV) obj;
146 if (this.length != other.length) {
147 return false;
148 }
149 if (this.type != other.type) {
150 return false;
151 }
152 if (!Arrays.equals(this.value, other.value)) {
153 return false;
154 }
155 return true;
156 }
157}