blob: a0930bd0fcf73c977ae778ed6dcaf53cbbe15c24 [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
15package net.floodlightcontroller.packet;
16
17import java.nio.ByteBuffer;
18import java.nio.charset.Charset;
19import java.util.Arrays;
20
21/**
22 * The class representing LLDP Organizationally Specific TLV.
23 *
24 * @author Sho Shimizu (sho.shimizu@gmail.com)
25 */
26public class LLDPOrganizationalTLV extends LLDPTLV {
27 public static final int OUI_LENGTH = 3;
28 public static final int SUBTYPE_LENGTH = 1;
29 public static final byte ORGANIZATIONAL_TLV_TYPE = 127;
30 public static final int MAX_INFOSTRING_LENGTH = 507;
31
32 protected byte[] oui;
33 protected byte subType;
34 private byte[] infoString;
35
36 public LLDPOrganizationalTLV() {
37 type = ORGANIZATIONAL_TLV_TYPE;
38 }
39
40 /**
41 * Set the value of OUI.
42 * @param oui The value of OUI to be set.
43 * @return This LLDP Organizationally Specific TLV.
44 */
45 public LLDPOrganizationalTLV setOUI(byte[] oui) {
46 if (oui.length != OUI_LENGTH) {
47 throw new IllegalArgumentException("The length of OUI must be " + OUI_LENGTH +
48 ", but it is " + oui.length);
49 }
50 this.oui = Arrays.copyOf(oui, oui.length);
51 return this;
52 }
53
54 /**
55 * Returns the value of the OUI.
56 * @return The value of the OUI .
57 */
58 public byte[] getOUI() {
59 return Arrays.copyOf(oui, oui.length);
60 }
61
62 /**
63 * Set the value of sub type.
64 * @param subType The value of sub type to be set.
65 * @return This LLDP Organizationally Specific TLV.
66 */
67 public LLDPOrganizationalTLV setSubType(byte subType) {
68 this.subType = subType;
69 return this;
70 }
71
72 /**
73 * Returns the value of the sub type.
74 * @return The value of the sub type.
75 */
76 public byte getSubType() {
77 return subType;
78 }
79
80 /**
81 * Set the value of information string.
82 * @param infoString the byte array of the value of information string.
83 * @return This LLDP Organizationally Specific TLV.
84 */
85 public LLDPOrganizationalTLV setInfoString(byte[] infoString) {
86 if (infoString.length > MAX_INFOSTRING_LENGTH) {
87 throw new IllegalArgumentException("The length of infoString cannot exceed " + MAX_INFOSTRING_LENGTH);
88 }
89 this.infoString = Arrays.copyOf(infoString, infoString.length);
90 return this;
91 }
92
93 /**
94 * Set the value of information string.
95 * The String value is automatically converted into byte array with UTF-8 encoding.
96 * @param infoString the String value of information string.
97 * @return This LLDP Organizationally Specific TLV.
98 */
99 public LLDPOrganizationalTLV setInfoString(String infoString) {
100 byte[] infoStringBytes = infoString.getBytes(Charset.forName("UTF-8"));
101 return setInfoString(infoStringBytes);
102 }
103
104 /**
105 * Returns the value of information string.
106 * @return the value of information string.
107 */
108 public byte[] getInfoString() {
109 return Arrays.copyOf(infoString, infoString.length);
110 }
111
112 @Override
113 public byte[] serialize() {
114 int valueLength = OUI_LENGTH + SUBTYPE_LENGTH + infoString.length;
115 value = new byte[valueLength];
116 ByteBuffer bb = ByteBuffer.wrap(value);
117 bb.put(oui);
118 bb.put(subType);
119 bb.put(infoString);
120 return super.serialize();
121 }
122
123 @Override
124 public LLDPTLV deserialize(ByteBuffer bb) {
125 super.deserialize(bb);
126 ByteBuffer optionalField = ByteBuffer.wrap(value);
127
128 byte[] oui = new byte[OUI_LENGTH];
129 optionalField.get(oui);
130 setOUI(oui);
131
132 setSubType(optionalField.get());
133
134 byte[] infoString = new byte[getLength() - OUI_LENGTH - SUBTYPE_LENGTH];
135 optionalField.get(infoString);
136 setInfoString(infoString);
137 return this;
138 }
139
140 @Override
141 public int hashCode() {
142 final int prime = 1423;
143 int result = 1;
144 result = prime * result + type;
145 result = prime * result + length;
146 result = prime * result + Arrays.hashCode(oui);
147 result = prime * result + subType;
148 result = prime * result + Arrays.hashCode(infoString);
149 return result;
150 }
151
152 @Override
153 public boolean equals(Object o) {
154 if (o == this) {
155 return true;
156 }
157
158 if (!(o instanceof LLDPOrganizationalTLV)) {
159 return false;
160 }
161
162 LLDPOrganizationalTLV other = (LLDPOrganizationalTLV)o;
163 if (this.type != other.type) {
164 return false;
165 }
166 if (this.length != other.length) {
167 return false;
168 }
169 if (!Arrays.equals(this.oui, other.oui)) {
170 return false;
171 }
172 if (this.subType != other.subType) {
173 return false;
174 }
175 if (!Arrays.equals(this.infoString, other.infoString)) {
176 return false;
177 }
178
179 return true;
180 }
181}