alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | ******************************************************************************/ |
| 16 | /** |
| 17 | * Copyright 2011, Big Switch Networks, Inc. |
| 18 | * Originally created by David Erickson, Stanford University |
| 19 | * |
| 20 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 21 | * not use this file except in compliance with the License. You may obtain |
| 22 | * a copy of the License at |
| 23 | * |
| 24 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 25 | * |
| 26 | * Unless required by applicable law or agreed to in writing, software |
| 27 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 28 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 29 | * License for the specific language governing permissions and limitations |
| 30 | * under the License. |
| 31 | **/ |
| 32 | |
| 33 | package org.onlab.packet; |
| 34 | |
| 35 | import java.nio.ByteBuffer; |
| 36 | import java.util.Arrays; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * |
alshabib | c4901cd | 2014-09-05 16:50:40 -0700 | [diff] [blame] | 41 | */ |
| 42 | public class LLDPTLV { |
| 43 | protected byte type; |
| 44 | protected short length; |
| 45 | protected byte[] value; |
| 46 | |
| 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 |
| 57 | */ |
| 58 | public LLDPTLV setType(final byte type) { |
| 59 | this.type = type; |
| 60 | return this; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return the length |
| 65 | */ |
| 66 | public short getLength() { |
| 67 | return this.length; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param length |
| 72 | * the length to set |
| 73 | */ |
| 74 | public LLDPTLV setLength(final short length) { |
| 75 | this.length = length; |
| 76 | return this; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @return the value |
| 81 | */ |
| 82 | public byte[] getValue() { |
| 83 | return this.value; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param value |
| 88 | * the value to set |
| 89 | */ |
| 90 | public LLDPTLV setValue(final byte[] value) { |
| 91 | this.value = value; |
| 92 | return this; |
| 93 | } |
| 94 | |
| 95 | public byte[] serialize() { |
| 96 | // type = 7 bits |
| 97 | // info string length 9 bits, each value == byte |
| 98 | // info string |
| 99 | final short scratch = (short) ((0x7f & this.type) << 9 | 0x1ff & this.length); |
| 100 | final byte[] data = new byte[2 + this.length]; |
| 101 | final ByteBuffer bb = ByteBuffer.wrap(data); |
| 102 | bb.putShort(scratch); |
| 103 | if (this.value != null) { |
| 104 | bb.put(this.value); |
| 105 | } |
| 106 | return data; |
| 107 | } |
| 108 | |
| 109 | public LLDPTLV deserialize(final ByteBuffer bb) { |
| 110 | short sscratch; |
| 111 | sscratch = bb.getShort(); |
| 112 | this.type = (byte) (sscratch >> 9 & 0x7f); |
| 113 | this.length = (short) (sscratch & 0x1ff); |
| 114 | if (this.length > 0) { |
| 115 | this.value = new byte[this.length]; |
| 116 | |
| 117 | // if there is an underrun just toss the TLV |
| 118 | if (bb.remaining() < this.length) { |
| 119 | return null; |
| 120 | } |
| 121 | bb.get(this.value); |
| 122 | } |
| 123 | return this; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * (non-Javadoc) |
| 128 | * |
| 129 | * @see java.lang.Object#hashCode() |
| 130 | */ |
| 131 | @Override |
| 132 | public int hashCode() { |
| 133 | final int prime = 1423; |
| 134 | int result = 1; |
| 135 | result = prime * result + this.length; |
| 136 | result = prime * result + this.type; |
| 137 | result = prime * result + Arrays.hashCode(this.value); |
| 138 | return result; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * (non-Javadoc) |
| 143 | * |
| 144 | * @see java.lang.Object#equals(java.lang.Object) |
| 145 | */ |
| 146 | @Override |
| 147 | public boolean equals(final Object obj) { |
| 148 | if (this == obj) { |
| 149 | return true; |
| 150 | } |
| 151 | if (obj == null) { |
| 152 | return false; |
| 153 | } |
| 154 | if (!(obj instanceof LLDPTLV)) { |
| 155 | return false; |
| 156 | } |
| 157 | final LLDPTLV other = (LLDPTLV) obj; |
| 158 | if (this.length != other.length) { |
| 159 | return false; |
| 160 | } |
| 161 | if (this.type != other.type) { |
| 162 | return false; |
| 163 | } |
| 164 | if (!Arrays.equals(this.value, other.value)) { |
| 165 | return false; |
| 166 | } |
| 167 | return true; |
| 168 | } |
| 169 | } |