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