Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 18 | package net.onrc.onos.core.packet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | |
| 20 | import java.nio.ByteBuffer; |
| 21 | import java.util.Arrays; |
| 22 | |
| 23 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 24 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 25 | */ |
| 26 | public class LLDPTLV { |
| 27 | protected byte type; |
| 28 | protected short length; |
| 29 | protected byte[] value; |
| 30 | |
| 31 | /** |
| 32 | * @return the type |
| 33 | */ |
| 34 | public byte getType() { |
| 35 | return type; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @param type the type to set |
| 40 | */ |
| 41 | public LLDPTLV setType(byte type) { |
| 42 | this.type = type; |
| 43 | return this; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @return the length |
| 48 | */ |
| 49 | public short getLength() { |
| 50 | return length; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @param length the length to set |
| 55 | */ |
| 56 | public LLDPTLV setLength(short length) { |
| 57 | this.length = length; |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @return the value |
| 63 | */ |
| 64 | public byte[] getValue() { |
Pavlin Radoslavov | 0a9d5c3 | 2014-04-15 17:45:23 -0700 | [diff] [blame] | 65 | if (this.value == null) { |
| 66 | return null; |
| 67 | } |
| 68 | return this.value.clone(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param value the value to set |
| 73 | */ |
| 74 | public LLDPTLV setValue(byte[] value) { |
Pavlin Radoslavov | 0a9d5c3 | 2014-04-15 17:45:23 -0700 | [diff] [blame] | 75 | if (value == null) { |
| 76 | this.value = null; |
| 77 | } else { |
| 78 | this.value = value.clone(); |
| 79 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 80 | return this; |
| 81 | } |
| 82 | |
| 83 | public byte[] serialize() { |
| 84 | // type = 7 bits |
| 85 | // info string length 9 bits, each value == byte |
| 86 | // info string |
| 87 | short scratch = (short) (((0x7f & this.type) << 9) | (0x1ff & this.length)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | byte[] data = new byte[2 + this.length]; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 89 | ByteBuffer bb = ByteBuffer.wrap(data); |
| 90 | bb.putShort(scratch); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 91 | if (this.value != null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 92 | bb.put(this.value); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 93 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 94 | return data; |
| 95 | } |
| 96 | |
| 97 | public LLDPTLV deserialize(ByteBuffer bb) { |
| 98 | short sscratch; |
| 99 | sscratch = bb.getShort(); |
| 100 | this.type = (byte) ((sscratch >> 9) & 0x7f); |
| 101 | this.length = (short) (sscratch & 0x1ff); |
| 102 | if (this.length > 0) { |
| 103 | this.value = new byte[this.length]; |
| 104 | |
| 105 | // if there is an underrun just toss the TLV |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 106 | if (bb.remaining() < this.length) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 107 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 108 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 109 | bb.get(this.value); |
| 110 | } |
| 111 | return this; |
| 112 | } |
| 113 | |
| 114 | /* (non-Javadoc) |
| 115 | * @see java.lang.Object#hashCode() |
| 116 | */ |
| 117 | @Override |
| 118 | public int hashCode() { |
| 119 | final int prime = 1423; |
| 120 | int result = 1; |
| 121 | result = prime * result + length; |
| 122 | result = prime * result + type; |
| 123 | result = prime * result + Arrays.hashCode(value); |
| 124 | return result; |
| 125 | } |
| 126 | |
| 127 | /* (non-Javadoc) |
| 128 | * @see java.lang.Object#equals(java.lang.Object) |
| 129 | */ |
| 130 | @Override |
| 131 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 132 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 133 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 134 | } |
| 135 | if (obj == null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 136 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 137 | } |
Pavlin Radoslavov | adc3ec1 | 2014-04-11 16:08:53 -0700 | [diff] [blame] | 138 | // |
| 139 | // NOTE: Subclasses are are considered as change of identity, hence |
| 140 | // equals() will return false if the class type doesn't match. |
| 141 | // |
| 142 | // The implication is that two instances - base class and derived class |
| 143 | // will be different even if they have same bits on the wire. |
| 144 | // We use this assumption to address the fundamental |
| 145 | // "equivalence relation" issue with class inheritance and "equals()": |
| 146 | // http://www.artima.com/lejava/articles/equality.html |
| 147 | // http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals.html |
| 148 | // |
| 149 | // Based on existing code, we don't mix the usage of based and derived |
| 150 | // class instances. |
| 151 | // |
| 152 | // Note that the fix below is different from the Floodlight fix. |
| 153 | // The Floodlight code uses "if (!(obj instanceof LLDPTLV))", but |
| 154 | // that statement breaks the "equivalence relation". |
| 155 | // |
| 156 | if (getClass() != obj.getClass()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 157 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 158 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 159 | LLDPTLV other = (LLDPTLV) obj; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 160 | if (length != other.length) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 161 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 162 | } |
| 163 | if (type != other.type) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 164 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 165 | } |
| 166 | if (!Arrays.equals(value, other.value)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 167 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 168 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 169 | return true; |
| 170 | } |
| 171 | } |