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 | c9bacee | 2014-04-11 19:02:17 -0700 | [diff] [blame^] | 23 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 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 | */ |
Pavlin Radoslavov | c9bacee | 2014-04-11 19:02:17 -0700 | [diff] [blame^] | 66 | @SuppressFBWarnings(value = "EI_EXPOSE_REP", |
| 67 | justification = "TODO: Return a copy of the object?") |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 68 | public byte[] getValue() { |
| 69 | return value; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param value the value to set |
| 74 | */ |
Pavlin Radoslavov | c9bacee | 2014-04-11 19:02:17 -0700 | [diff] [blame^] | 75 | @SuppressFBWarnings(value = "EI_EXPOSE_REP2", |
| 76 | justification = "TODO: Store a copy of the object?") |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 77 | public LLDPTLV setValue(byte[] value) { |
| 78 | this.value = value; |
| 79 | return this; |
| 80 | } |
| 81 | |
| 82 | public byte[] serialize() { |
| 83 | // type = 7 bits |
| 84 | // info string length 9 bits, each value == byte |
| 85 | // info string |
| 86 | short scratch = (short) (((0x7f & this.type) << 9) | (0x1ff & this.length)); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 87 | byte[] data = new byte[2 + this.length]; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 88 | ByteBuffer bb = ByteBuffer.wrap(data); |
| 89 | bb.putShort(scratch); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 90 | if (this.value != null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 91 | bb.put(this.value); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 92 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 93 | return data; |
| 94 | } |
| 95 | |
| 96 | public LLDPTLV deserialize(ByteBuffer bb) { |
| 97 | short sscratch; |
| 98 | sscratch = bb.getShort(); |
| 99 | this.type = (byte) ((sscratch >> 9) & 0x7f); |
| 100 | this.length = (short) (sscratch & 0x1ff); |
| 101 | if (this.length > 0) { |
| 102 | this.value = new byte[this.length]; |
| 103 | |
| 104 | // if there is an underrun just toss the TLV |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 105 | if (bb.remaining() < this.length) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 106 | return null; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 107 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 108 | bb.get(this.value); |
| 109 | } |
| 110 | return this; |
| 111 | } |
| 112 | |
| 113 | /* (non-Javadoc) |
| 114 | * @see java.lang.Object#hashCode() |
| 115 | */ |
| 116 | @Override |
| 117 | public int hashCode() { |
| 118 | final int prime = 1423; |
| 119 | int result = 1; |
| 120 | result = prime * result + length; |
| 121 | result = prime * result + type; |
| 122 | result = prime * result + Arrays.hashCode(value); |
| 123 | return result; |
| 124 | } |
| 125 | |
| 126 | /* (non-Javadoc) |
| 127 | * @see java.lang.Object#equals(java.lang.Object) |
| 128 | */ |
| 129 | @Override |
| 130 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 131 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 132 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 133 | } |
| 134 | if (obj == null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 135 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 136 | } |
Pavlin Radoslavov | adc3ec1 | 2014-04-11 16:08:53 -0700 | [diff] [blame] | 137 | // |
| 138 | // NOTE: Subclasses are are considered as change of identity, hence |
| 139 | // equals() will return false if the class type doesn't match. |
| 140 | // |
| 141 | // The implication is that two instances - base class and derived class |
| 142 | // will be different even if they have same bits on the wire. |
| 143 | // We use this assumption to address the fundamental |
| 144 | // "equivalence relation" issue with class inheritance and "equals()": |
| 145 | // http://www.artima.com/lejava/articles/equality.html |
| 146 | // http://www.angelikalanger.com/Articles/JavaSolutions/SecretsOfEquals/Equals.html |
| 147 | // |
| 148 | // Based on existing code, we don't mix the usage of based and derived |
| 149 | // class instances. |
| 150 | // |
| 151 | // Note that the fix below is different from the Floodlight fix. |
| 152 | // The Floodlight code uses "if (!(obj instanceof LLDPTLV))", but |
| 153 | // that statement breaks the "equivalence relation". |
| 154 | // |
| 155 | if (getClass() != obj.getClass()) { |
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 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 158 | LLDPTLV other = (LLDPTLV) obj; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 159 | if (length != other.length) { |
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 (type != other.type) { |
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 | } |
| 165 | if (!Arrays.equals(value, other.value)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 166 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 167 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 168 | return true; |
| 169 | } |
| 170 | } |