Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 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 Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 15 | package net.onrc.onos.core.packet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 16 | |
| 17 | import java.nio.ByteBuffer; |
| 18 | import java.nio.charset.Charset; |
| 19 | import java.util.Arrays; |
| 20 | |
| 21 | /** |
| 22 | * The class representing LLDP Organizationally Specific TLV. |
| 23 | * |
| 24 | * @author Sho Shimizu (sho.shimizu@gmail.com) |
| 25 | */ |
| 26 | public 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. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 43 | * @param oui The value of OUI to be set. |
| 44 | * @return This LLDP Organizationally Specific TLV. |
| 45 | */ |
| 46 | public LLDPOrganizationalTLV setOUI(byte[] oui) { |
| 47 | if (oui.length != OUI_LENGTH) { |
| 48 | throw new IllegalArgumentException("The length of OUI must be " + OUI_LENGTH + |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | ", but it is " + oui.length); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 50 | } |
| 51 | this.oui = Arrays.copyOf(oui, oui.length); |
| 52 | return this; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns the value of the OUI. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 57 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 58 | * @return The value of the OUI . |
| 59 | */ |
| 60 | public byte[] getOUI() { |
| 61 | return Arrays.copyOf(oui, oui.length); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Set the value of sub type. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 67 | * @param subType The value of sub type to be set. |
| 68 | * @return This LLDP Organizationally Specific TLV. |
| 69 | */ |
| 70 | public LLDPOrganizationalTLV setSubType(byte subType) { |
| 71 | this.subType = subType; |
| 72 | return this; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the value of the sub type. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 77 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 78 | * @return The value of the sub type. |
| 79 | */ |
| 80 | public byte getSubType() { |
| 81 | return subType; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Set the value of information string. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 87 | * @param infoString the byte array of the value of information string. |
| 88 | * @return This LLDP Organizationally Specific TLV. |
| 89 | */ |
| 90 | public LLDPOrganizationalTLV setInfoString(byte[] infoString) { |
| 91 | if (infoString.length > MAX_INFOSTRING_LENGTH) { |
| 92 | throw new IllegalArgumentException("The length of infoString cannot exceed " + MAX_INFOSTRING_LENGTH); |
| 93 | } |
| 94 | this.infoString = Arrays.copyOf(infoString, infoString.length); |
| 95 | return this; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Set the value of information string. |
| 100 | * The String value is automatically converted into byte array with UTF-8 encoding. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 102 | * @param infoString the String value of information string. |
| 103 | * @return This LLDP Organizationally Specific TLV. |
| 104 | */ |
| 105 | public LLDPOrganizationalTLV setInfoString(String infoString) { |
| 106 | byte[] infoStringBytes = infoString.getBytes(Charset.forName("UTF-8")); |
| 107 | return setInfoString(infoStringBytes); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns the value of information string. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 112 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 113 | * @return the value of information string. |
| 114 | */ |
| 115 | public byte[] getInfoString() { |
| 116 | return Arrays.copyOf(infoString, infoString.length); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public byte[] serialize() { |
| 121 | int valueLength = OUI_LENGTH + SUBTYPE_LENGTH + infoString.length; |
| 122 | value = new byte[valueLength]; |
| 123 | ByteBuffer bb = ByteBuffer.wrap(value); |
| 124 | bb.put(oui); |
| 125 | bb.put(subType); |
| 126 | bb.put(infoString); |
| 127 | return super.serialize(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public LLDPTLV deserialize(ByteBuffer bb) { |
| 132 | super.deserialize(bb); |
| 133 | ByteBuffer optionalField = ByteBuffer.wrap(value); |
| 134 | |
| 135 | byte[] oui = new byte[OUI_LENGTH]; |
| 136 | optionalField.get(oui); |
| 137 | setOUI(oui); |
| 138 | |
| 139 | setSubType(optionalField.get()); |
| 140 | |
| 141 | byte[] infoString = new byte[getLength() - OUI_LENGTH - SUBTYPE_LENGTH]; |
| 142 | optionalField.get(infoString); |
| 143 | setInfoString(infoString); |
| 144 | return this; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public int hashCode() { |
| 149 | final int prime = 1423; |
| 150 | int result = 1; |
| 151 | result = prime * result + type; |
| 152 | result = prime * result + length; |
| 153 | result = prime * result + Arrays.hashCode(oui); |
| 154 | result = prime * result + subType; |
| 155 | result = prime * result + Arrays.hashCode(infoString); |
| 156 | return result; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public boolean equals(Object o) { |
| 161 | if (o == this) { |
| 162 | return true; |
| 163 | } |
Pavlin Radoslavov | adc3ec1 | 2014-04-11 16:08:53 -0700 | [diff] [blame] | 164 | if (!super.equals(o)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 165 | return false; |
| 166 | } |
Pavlin Radoslavov | adc3ec1 | 2014-04-11 16:08:53 -0700 | [diff] [blame] | 167 | // |
| 168 | // NOTE: Subclasses are are considered as change of identity, hence |
| 169 | // equals() will return false if the class type doesn't match. |
| 170 | // |
| 171 | if (getClass() != o.getClass()) { |
| 172 | return false; |
| 173 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 174 | LLDPOrganizationalTLV other = (LLDPOrganizationalTLV) o; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 175 | if (this.type != other.type) { |
| 176 | return false; |
| 177 | } |
| 178 | if (this.length != other.length) { |
| 179 | return false; |
| 180 | } |
| 181 | if (!Arrays.equals(this.oui, other.oui)) { |
| 182 | return false; |
| 183 | } |
| 184 | if (this.subType != other.subType) { |
| 185 | return false; |
| 186 | } |
| 187 | if (!Arrays.equals(this.infoString, other.infoString)) { |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | return true; |
| 192 | } |
| 193 | } |