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