blob: fb359a4711dde5ddc884de15b617bda376cb85ae [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() {
143 final int valueLength = LLDPOrganizationalTLV.OUI_LENGTH
144 + LLDPOrganizationalTLV.SUBTYPE_LENGTH + this.infoString.length;
145 this.value = new byte[valueLength];
146 final ByteBuffer bb = ByteBuffer.wrap(this.value);
147 bb.put(this.oui);
148 bb.put(this.subType);
149 bb.put(this.infoString);
150 return super.serialize();
151 }
152
153 @Override
154 public LLDPTLV deserialize(final ByteBuffer bb) {
155 super.deserialize(bb);
156 final ByteBuffer optionalField = ByteBuffer.wrap(this.value);
157
158 final byte[] oui = new byte[LLDPOrganizationalTLV.OUI_LENGTH];
159 optionalField.get(oui);
160 this.setOUI(oui);
161
162 this.setSubType(optionalField.get());
163
164 final byte[] infoString = new byte[this.getLength()
alshabib638dc712014-09-05 18:03:45 -0700165 - LLDPOrganizationalTLV.OUI_LENGTH
166 - LLDPOrganizationalTLV.SUBTYPE_LENGTH];
alshabibc4901cd2014-09-05 16:50:40 -0700167 optionalField.get(infoString);
168 this.setInfoString(infoString);
169 return this;
170 }
171
172 @Override
173 public int hashCode() {
174 final int prime = 1423;
175 int result = 1;
176 result = prime * result + this.type;
177 result = prime * result + this.length;
178 result = prime * result + Arrays.hashCode(this.oui);
179 result = prime * result + this.subType;
180 result = prime * result + Arrays.hashCode(this.infoString);
181 return result;
182 }
183
184 @Override
185 public boolean equals(final Object o) {
186 if (o == this) {
187 return true;
188 }
189
190 if (!(o instanceof LLDPOrganizationalTLV)) {
191 return false;
192 }
193
194 final LLDPOrganizationalTLV other = (LLDPOrganizationalTLV) o;
195 if (this.type != other.type) {
196 return false;
197 }
198 if (this.length != other.length) {
199 return false;
200 }
201 if (!Arrays.equals(this.oui, other.oui)) {
202 return false;
203 }
204 if (this.subType != other.subType) {
205 return false;
206 }
207 if (!Arrays.equals(this.infoString, other.infoString)) {
208 return false;
209 }
210
211 return true;
212 }
213}