blob: 6d7db3c34f3285b02134e78606ae76a72959dfe5 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
alshabibc4901cd2014-09-05 16:50:40 -07009 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
Thomas Vachuska24c849c2014-10-27 09:53:05 -070012 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
alshabibc4901cd2014-09-05 16:50:40 -070017 * under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070018 */
19
20
alshabibc4901cd2014-09-05 16:50:40 -070021
22package org.onlab.packet;
23
24import java.nio.ByteBuffer;
25import java.util.Arrays;
26
27/**
28 *
29 *
alshabibc4901cd2014-09-05 16:50:40 -070030 */
31public class LLDPTLV {
32 protected byte type;
33 protected short length;
34 protected byte[] value;
35
36 /**
37 * @return the type
38 */
39 public byte getType() {
40 return this.type;
41 }
42
43 /**
44 * @param type
45 * the type to set
46 */
47 public LLDPTLV setType(final byte type) {
48 this.type = type;
49 return this;
50 }
51
52 /**
53 * @return the length
54 */
55 public short getLength() {
56 return this.length;
57 }
58
59 /**
60 * @param length
61 * the length to set
62 */
63 public LLDPTLV setLength(final short length) {
64 this.length = length;
65 return this;
66 }
67
68 /**
69 * @return the value
70 */
71 public byte[] getValue() {
72 return this.value;
73 }
74
75 /**
76 * @param value
77 * the value to set
78 */
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}