blob: 6f995c517e1d70175ffcbd25320e70a52a161ae0 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2 * Licensed under the Apache License, Version 2.0 (the "License"); you may
3 * not use this file except in compliance with the License. You may obtain
4 * a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 * License for the specific language governing permissions and limitations
12 * under the License.
13 **/
14
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070015package net.onrc.onos.core.packet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080016
17import java.nio.ByteBuffer;
18import java.nio.charset.Charset;
19import java.util.Arrays;
20
21/**
22 * The class representing LLDP Organizationally Specific TLV.
23 *
Yuta HIGUCHIaa132f52014-06-26 10:18:39 -070024 * <!-- CHECKSTYLE IGNORE WriteTag FOR NEXT 1 LINES -->
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080025 * @author Sho Shimizu (sho.shimizu@gmail.com)
26 */
27public class LLDPOrganizationalTLV extends LLDPTLV {
28 public static final int OUI_LENGTH = 3;
29 public static final int SUBTYPE_LENGTH = 1;
30 public static final byte ORGANIZATIONAL_TLV_TYPE = 127;
31 public static final int MAX_INFOSTRING_LENGTH = 507;
32
33 protected byte[] oui;
34 protected byte subType;
35 private byte[] infoString;
36
37 public LLDPOrganizationalTLV() {
38 type = ORGANIZATIONAL_TLV_TYPE;
39 }
40
41 /**
42 * Set the value of OUI.
Ray Milkey269ffb92014-04-03 14:43:30 -070043 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080044 * @param oui The value of OUI to be set.
45 * @return This LLDP Organizationally Specific TLV.
46 */
47 public LLDPOrganizationalTLV setOUI(byte[] oui) {
48 if (oui.length != OUI_LENGTH) {
49 throw new IllegalArgumentException("The length of OUI must be " + OUI_LENGTH +
Ray Milkey269ffb92014-04-03 14:43:30 -070050 ", but it is " + oui.length);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080051 }
52 this.oui = Arrays.copyOf(oui, oui.length);
53 return this;
54 }
55
56 /**
57 * Returns the value of the OUI.
Ray Milkey269ffb92014-04-03 14:43:30 -070058 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080059 * @return The value of the OUI .
60 */
61 public byte[] getOUI() {
62 return Arrays.copyOf(oui, oui.length);
63 }
64
65 /**
66 * Set the value of sub type.
Ray Milkey269ffb92014-04-03 14:43:30 -070067 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080068 * @param subType The value of sub type to be set.
69 * @return This LLDP Organizationally Specific TLV.
70 */
71 public LLDPOrganizationalTLV setSubType(byte subType) {
72 this.subType = subType;
73 return this;
74 }
75
76 /**
77 * Returns the value of the sub type.
Ray Milkey269ffb92014-04-03 14:43:30 -070078 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080079 * @return The value of the sub type.
80 */
81 public byte getSubType() {
82 return subType;
83 }
84
85 /**
86 * Set the value of information string.
Ray Milkey269ffb92014-04-03 14:43:30 -070087 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080088 * @param infoString the byte array of the value of information string.
89 * @return This LLDP Organizationally Specific TLV.
90 */
91 public LLDPOrganizationalTLV setInfoString(byte[] infoString) {
92 if (infoString.length > MAX_INFOSTRING_LENGTH) {
93 throw new IllegalArgumentException("The length of infoString cannot exceed " + MAX_INFOSTRING_LENGTH);
94 }
95 this.infoString = Arrays.copyOf(infoString, infoString.length);
96 return this;
97 }
98
99 /**
100 * Set the value of information string.
101 * The String value is automatically converted into byte array with UTF-8 encoding.
Ray Milkey269ffb92014-04-03 14:43:30 -0700102 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800103 * @param infoString the String value of information string.
104 * @return This LLDP Organizationally Specific TLV.
105 */
106 public LLDPOrganizationalTLV setInfoString(String infoString) {
107 byte[] infoStringBytes = infoString.getBytes(Charset.forName("UTF-8"));
108 return setInfoString(infoStringBytes);
109 }
110
111 /**
112 * Returns the value of information string.
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800114 * @return the value of information string.
115 */
116 public byte[] getInfoString() {
117 return Arrays.copyOf(infoString, infoString.length);
118 }
119
120 @Override
121 public byte[] serialize() {
122 int valueLength = OUI_LENGTH + SUBTYPE_LENGTH + infoString.length;
123 value = new byte[valueLength];
124 ByteBuffer bb = ByteBuffer.wrap(value);
125 bb.put(oui);
126 bb.put(subType);
127 bb.put(infoString);
128 return super.serialize();
129 }
130
131 @Override
132 public LLDPTLV deserialize(ByteBuffer bb) {
133 super.deserialize(bb);
134 ByteBuffer optionalField = ByteBuffer.wrap(value);
135
136 byte[] oui = new byte[OUI_LENGTH];
137 optionalField.get(oui);
138 setOUI(oui);
139
140 setSubType(optionalField.get());
141
142 byte[] infoString = new byte[getLength() - OUI_LENGTH - SUBTYPE_LENGTH];
143 optionalField.get(infoString);
144 setInfoString(infoString);
145 return this;
146 }
147
148 @Override
149 public int hashCode() {
150 final int prime = 1423;
151 int result = 1;
152 result = prime * result + type;
153 result = prime * result + length;
154 result = prime * result + Arrays.hashCode(oui);
155 result = prime * result + subType;
156 result = prime * result + Arrays.hashCode(infoString);
157 return result;
158 }
159
160 @Override
161 public boolean equals(Object o) {
162 if (o == this) {
163 return true;
164 }
Pavlin Radoslavovadc3ec12014-04-11 16:08:53 -0700165 if (!super.equals(o)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800166 return false;
167 }
Pavlin Radoslavovadc3ec12014-04-11 16:08:53 -0700168 //
169 // NOTE: Subclasses are are considered as change of identity, hence
170 // equals() will return false if the class type doesn't match.
171 //
172 if (getClass() != o.getClass()) {
173 return false;
174 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700175 LLDPOrganizationalTLV other = (LLDPOrganizationalTLV) o;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800176 if (this.type != other.type) {
177 return false;
178 }
179 if (this.length != other.length) {
180 return false;
181 }
182 if (!Arrays.equals(this.oui, other.oui)) {
183 return false;
184 }
185 if (this.subType != other.subType) {
186 return false;
187 }
188 if (!Arrays.equals(this.infoString, other.infoString)) {
189 return false;
190 }
191
192 return true;
193 }
194}