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