blob: 4d4e0a46ca404fb3a3a911ff36ee1b17e25d405e [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 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License. You may obtain
19 * a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
26 * License for the specific language governing permissions and limitations
27 * under the License.
28 **/
29
30package org.onlab.packet;
31
32import java.nio.ByteBuffer;
33import java.nio.charset.Charset;
34import java.util.Arrays;
35
36/**
37 * The class representing LLDP Organizationally Specific TLV.
38 *
alshabibc4901cd2014-09-05 16:50:40 -070039 */
40public class LLDPOrganizationalTLV extends LLDPTLV {
41 public static final int OUI_LENGTH = 3;
42 public static final int SUBTYPE_LENGTH = 1;
43 public static final byte ORGANIZATIONAL_TLV_TYPE = 127;
44 public static final int MAX_INFOSTRING_LENGTH = 507;
45
46 protected byte[] oui;
47 protected byte subType;
48 private byte[] infoString;
49
50 public LLDPOrganizationalTLV() {
51 this.type = LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE;
52 }
53
54 /**
55 * Set the value of OUI.
56 *
57 * @param oui
58 * The value of OUI to be set.
59 * @return This LLDP Organizationally Specific TLV.
60 */
61 public LLDPOrganizationalTLV setOUI(final byte[] oui) {
62 if (oui.length != LLDPOrganizationalTLV.OUI_LENGTH) {
63 throw new IllegalArgumentException("The length of OUI must be "
64 + LLDPOrganizationalTLV.OUI_LENGTH + ", but it is "
65 + oui.length);
66 }
67 this.oui = Arrays.copyOf(oui, oui.length);
68 return this;
69 }
70
71 /**
72 * Returns the value of the OUI.
73 *
74 * @return The value of the OUI .
75 */
76 public byte[] getOUI() {
77 return Arrays.copyOf(this.oui, this.oui.length);
78 }
79
80 /**
81 * Set the value of sub type.
82 *
83 * @param subType
84 * The value of sub type to be set.
85 * @return This LLDP Organizationally Specific TLV.
86 */
87 public LLDPOrganizationalTLV setSubType(final byte subType) {
88 this.subType = subType;
89 return this;
90 }
91
92 /**
93 * Returns the value of the sub type.
94 *
95 * @return The value of the sub type.
96 */
97 public byte getSubType() {
98 return this.subType;
99 }
100
101 /**
102 * Set the value of information string.
103 *
104 * @param infoString
105 * the byte array of the value of information string.
106 * @return This LLDP Organizationally Specific TLV.
107 */
108 public LLDPOrganizationalTLV setInfoString(final byte[] infoString) {
109 if (infoString.length > LLDPOrganizationalTLV.MAX_INFOSTRING_LENGTH) {
110 throw new IllegalArgumentException(
111 "The length of infoString cannot exceed "
112 + LLDPOrganizationalTLV.MAX_INFOSTRING_LENGTH);
113 }
114 this.infoString = Arrays.copyOf(infoString, infoString.length);
115 return this;
116 }
117
118 /**
119 * Set the value of information string. The String value is automatically
120 * converted into byte array with UTF-8 encoding.
121 *
122 * @param infoString
123 * the String value of information string.
124 * @return This LLDP Organizationally Specific TLV.
125 */
126 public LLDPOrganizationalTLV setInfoString(final String infoString) {
127 final byte[] infoStringBytes = infoString.getBytes(Charset
128 .forName("UTF-8"));
129 return this.setInfoString(infoStringBytes);
130 }
131
132 /**
133 * Returns the value of information string.
134 *
135 * @return the value of information string.
136 */
137 public byte[] getInfoString() {
138 return Arrays.copyOf(this.infoString, this.infoString.length);
139 }
140
141 @Override
142 public byte[] serialize() {
alshabib7911a052014-10-16 17:49:37 -0700143 if (this.type != LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE) {
144 return super.serialize();
145 }
alshabibc4901cd2014-09-05 16:50:40 -0700146 final int valueLength = LLDPOrganizationalTLV.OUI_LENGTH
147 + LLDPOrganizationalTLV.SUBTYPE_LENGTH + this.infoString.length;
148 this.value = new byte[valueLength];
149 final ByteBuffer bb = ByteBuffer.wrap(this.value);
150 bb.put(this.oui);
151 bb.put(this.subType);
152 bb.put(this.infoString);
153 return super.serialize();
154 }
155
156 @Override
157 public LLDPTLV deserialize(final ByteBuffer bb) {
alshabib7911a052014-10-16 17:49:37 -0700158 LLDPTLV tlv = super.deserialize(bb);
159 if (tlv.getType() != LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE) {
160 return tlv;
161 }
162
alshabibc4901cd2014-09-05 16:50:40 -0700163 final ByteBuffer optionalField = ByteBuffer.wrap(this.value);
164
165 final byte[] oui = new byte[LLDPOrganizationalTLV.OUI_LENGTH];
166 optionalField.get(oui);
167 this.setOUI(oui);
168
169 this.setSubType(optionalField.get());
170
171 final byte[] infoString = new byte[this.getLength()
alshabib638dc712014-09-05 18:03:45 -0700172 - LLDPOrganizationalTLV.OUI_LENGTH
173 - LLDPOrganizationalTLV.SUBTYPE_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -0700174 optionalField.get(infoString);
175 this.setInfoString(infoString);
176 return this;
177 }
178
179 @Override
180 public int hashCode() {
181 final int prime = 1423;
182 int result = 1;
183 result = prime * result + this.type;
184 result = prime * result + this.length;
185 result = prime * result + Arrays.hashCode(this.oui);
186 result = prime * result + this.subType;
187 result = prime * result + Arrays.hashCode(this.infoString);
188 return result;
189 }
190
191 @Override
192 public boolean equals(final Object o) {
193 if (o == this) {
194 return true;
195 }
196
197 if (!(o instanceof LLDPOrganizationalTLV)) {
198 return false;
199 }
200
201 final LLDPOrganizationalTLV other = (LLDPOrganizationalTLV) o;
202 if (this.type != other.type) {
203 return false;
204 }
205 if (this.length != other.length) {
206 return false;
207 }
208 if (!Arrays.equals(this.oui, other.oui)) {
209 return false;
210 }
211 if (this.subType != other.subType) {
212 return false;
213 }
214 if (!Arrays.equals(this.infoString, other.infoString)) {
215 return false;
216 }
217
218 return true;
219 }
220}