Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 18 | |
| 19 | package org.onlab.packet; |
| 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | import java.util.Arrays; |
| 23 | |
| 24 | /** |
| 25 | * |
| 26 | * |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 27 | */ |
| 28 | public class LLDPTLV { |
jaegonkim | 47b1b4a | 2017-12-04 14:08:26 +0900 | [diff] [blame] | 29 | |
| 30 | public static final int MAX_LENGTH = 0x1ff; |
| 31 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 32 | protected byte type; |
| 33 | protected short length; |
| 34 | protected byte[] value; |
| 35 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 36 | @Override |
| 37 | public String toString() { |
| 38 | StringBuilder sb = new StringBuilder(); |
| 39 | sb.append("["); |
| 40 | sb.append("type= "); |
| 41 | sb.append(type); |
| 42 | sb.append("length= "); |
| 43 | sb.append(length); |
| 44 | sb.append("value= "); |
| 45 | sb.append(Arrays.toString(value)); |
| 46 | sb.append("]"); |
| 47 | return sb.toString(); |
| 48 | } |
| 49 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 50 | /** |
| 51 | * @return the type |
| 52 | */ |
| 53 | public byte getType() { |
| 54 | return this.type; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param type |
| 59 | * the type to set |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 60 | * @return this |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 61 | */ |
| 62 | public LLDPTLV setType(final byte type) { |
| 63 | this.type = type; |
| 64 | return this; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @return the length |
| 69 | */ |
| 70 | public short getLength() { |
| 71 | return this.length; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param length |
| 76 | * the length to set |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 77 | * @return this |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 78 | */ |
| 79 | public LLDPTLV setLength(final short length) { |
| 80 | this.length = length; |
| 81 | return this; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @return the value |
| 86 | */ |
| 87 | public byte[] getValue() { |
| 88 | return this.value; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @param value |
| 93 | * the value to set |
Yuta HIGUCHI | 2281b3f | 2014-11-04 00:20:48 -0800 | [diff] [blame] | 94 | * @return this |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 95 | */ |
| 96 | public LLDPTLV setValue(final byte[] value) { |
| 97 | this.value = value; |
| 98 | return this; |
| 99 | } |
| 100 | |
| 101 | public byte[] serialize() { |
| 102 | // type = 7 bits |
| 103 | // info string length 9 bits, each value == byte |
| 104 | // info string |
| 105 | final short scratch = (short) ((0x7f & this.type) << 9 | 0x1ff & this.length); |
| 106 | final byte[] data = new byte[2 + this.length]; |
| 107 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 108 | bb.putShort(scratch); |
| 109 | if (this.value != null) { |
| 110 | bb.put(this.value); |
| 111 | } |
| 112 | return data; |
| 113 | } |
| 114 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 115 | public LLDPTLV deserialize(final ByteBuffer bb) throws DeserializationException { |
| 116 | if (bb.remaining() < 2) { |
| 117 | throw new DeserializationException( |
| 118 | "Not enough bytes to deserialize TLV type and length"); |
| 119 | } |
| 120 | short typeLength; |
| 121 | typeLength = bb.getShort(); |
| 122 | this.type = (byte) (typeLength >> 9 & 0x7f); |
| 123 | this.length = (short) (typeLength & 0x1ff); |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 124 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 125 | if (this.length > 0) { |
| 126 | this.value = new byte[this.length]; |
| 127 | |
| 128 | // if there is an underrun just toss the TLV |
| 129 | if (bb.remaining() < this.length) { |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 130 | throw new DeserializationException( |
| 131 | "Remaining bytes are less then the length of the TLV"); |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 132 | } |
| 133 | bb.get(this.value); |
| 134 | } |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 135 | |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 136 | return this; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * (non-Javadoc) |
| 141 | * |
| 142 | * @see java.lang.Object#hashCode() |
| 143 | */ |
| 144 | @Override |
| 145 | public int hashCode() { |
| 146 | final int prime = 1423; |
| 147 | int result = 1; |
| 148 | result = prime * result + this.length; |
| 149 | result = prime * result + this.type; |
| 150 | result = prime * result + Arrays.hashCode(this.value); |
| 151 | return result; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * (non-Javadoc) |
| 156 | * |
| 157 | * @see java.lang.Object#equals(java.lang.Object) |
| 158 | */ |
| 159 | @Override |
| 160 | public boolean equals(final Object obj) { |
| 161 | if (this == obj) { |
| 162 | return true; |
| 163 | } |
| 164 | if (obj == null) { |
| 165 | return false; |
| 166 | } |
| 167 | if (!(obj instanceof LLDPTLV)) { |
| 168 | return false; |
| 169 | } |
| 170 | final LLDPTLV other = (LLDPTLV) obj; |
| 171 | if (this.length != other.length) { |
| 172 | return false; |
| 173 | } |
| 174 | if (this.type != other.type) { |
| 175 | return false; |
| 176 | } |
| 177 | if (!Arrays.equals(this.value, other.value)) { |
| 178 | return false; |
| 179 | } |
| 180 | return true; |
| 181 | } |
| 182 | } |