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 | |
Pavlin Radoslavov | 1d595c0 | 2014-04-16 14:11:54 -0700 | [diff] [blame] | 23 | import org.apache.commons.lang.ArrayUtils; |
| 24 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 25 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 26 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 27 | */ |
| 28 | public class LLDPTLV { |
| 29 | protected byte type; |
| 30 | protected short length; |
| 31 | protected byte[] value; |
| 32 | |
| 33 | /** |
| 34 | * @return the type |
| 35 | */ |
| 36 | public byte getType() { |
| 37 | return type; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @param type the type to set |
| 42 | */ |
| 43 | public LLDPTLV setType(byte type) { |
| 44 | this.type = type; |
| 45 | return this; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @return the length |
| 50 | */ |
| 51 | public short getLength() { |
| 52 | return length; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param length the length to set |
| 57 | */ |
| 58 | public LLDPTLV setLength(short length) { |
| 59 | this.length = length; |
| 60 | return this; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @return the value |
| 65 | */ |
| 66 | public byte[] getValue() { |
Pavlin Radoslavov | 1d595c0 | 2014-04-16 14:11:54 -0700 | [diff] [blame] | 67 | return ArrayUtils.clone(this.value); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param value the value to set |
| 72 | */ |
| 73 | public LLDPTLV setValue(byte[] value) { |
Pavlin Radoslavov | 1d595c0 | 2014-04-16 14:11:54 -0700 | [diff] [blame] | 74 | this.value = ArrayUtils.clone(value); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 75 | return this; |
| 76 | } |
| 77 | |
| 78 | public byte[] serialize() { |
| 79 | // type = 7 bits |
| 80 | // info string length 9 bits, each value == byte |
| 81 | // info string |
| 82 | short scratch = (short) (((0x7f & this.type) << 9) | (0x1ff & this.length)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 83 | byte[] data = new byte[2 + this.length]; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 84 | ByteBuffer bb = ByteBuffer.wrap(data); |
| 85 | bb.putShort(scratch); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 86 | if (this.value != null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 87 | bb.put(this.value); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 88 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 89 | return data; |
| 90 | } |
| 91 | |
| 92 | public LLDPTLV deserialize(ByteBuffer bb) { |
| 93 | short sscratch; |
| 94 | sscratch = bb.getShort(); |
| 95 | this.type = (byte) ((sscratch >> 9) & 0x7f); |
| 96 | this.length = (short) (sscratch & 0x1ff); |
| 97 | if (this.length > 0) { |
| 98 | this.value = new byte[this.length]; |
| 99 | |
| 100 | // if there is an underrun just toss the TLV |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 101 | if (bb.remaining() < this.length) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 102 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 103 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 104 | bb.get(this.value); |
| 105 | } |
| 106 | return this; |
| 107 | } |
| 108 | |
| 109 | /* (non-Javadoc) |
| 110 | * @see java.lang.Object#hashCode() |
| 111 | */ |
| 112 | @Override |
| 113 | public int hashCode() { |
| 114 | final int prime = 1423; |
| 115 | int result = 1; |
| 116 | result = prime * result + length; |
| 117 | result = prime * result + type; |
| 118 | result = prime * result + Arrays.hashCode(value); |
| 119 | return result; |
| 120 | } |
| 121 | |
| 122 | /* (non-Javadoc) |
| 123 | * @see java.lang.Object#equals(java.lang.Object) |
| 124 | */ |
| 125 | @Override |
| 126 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 127 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 128 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 129 | } |
| 130 | if (obj == null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 131 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 132 | } |
Pavlin Radoslavov | adc3ec1 | 2014-04-11 16:08:53 -0700 | [diff] [blame] | 133 | // |
| 134 | // NOTE: Subclasses are are considered as change of identity, hence |
| 135 | // equals() will return false if the class type doesn't match. |
| 136 | // |
| 137 | // The implication is that two instances - base class and derived class |
| 138 | // will be different even if they have same bits on the wire. |
| 139 | // We use this assumption to address the fundamental |
| 140 | // "equivalence relation" issue with class inheritance and "equals()": |
| 141 | // http://www.artima.com/lejava/articles/equality.html |
| 142 | // http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals.html |
| 143 | // |
| 144 | // Based on existing code, we don't mix the usage of based and derived |
| 145 | // class instances. |
| 146 | // |
| 147 | // Note that the fix below is different from the Floodlight fix. |
| 148 | // The Floodlight code uses "if (!(obj instanceof LLDPTLV))", but |
| 149 | // that statement breaks the "equivalence relation". |
| 150 | // |
| 151 | if (getClass() != obj.getClass()) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 152 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 153 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 154 | LLDPTLV other = (LLDPTLV) obj; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 155 | if (length != other.length) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 156 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 157 | } |
| 158 | if (type != other.type) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 159 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 160 | } |
| 161 | if (!Arrays.equals(value, other.value)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 162 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 163 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | } |