blob: e23ce1159e57dd7b42c1d5e1f17d9acd7d1bb97b [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * 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 Krishnaswamy345ee992012-12-13 20:29:48 -080017
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070018package net.onrc.onos.core.packet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019
20import java.nio.ByteBuffer;
21import java.util.Arrays;
22
23/**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080024 * @author David Erickson (daviderickson@cs.stanford.edu)
25 */
26public 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() {
65 return value;
66 }
67
68 /**
69 * @param value the value to set
70 */
71 public LLDPTLV setValue(byte[] value) {
72 this.value = value;
73 return this;
74 }
75
76 public byte[] serialize() {
77 // type = 7 bits
78 // info string length 9 bits, each value == byte
79 // info string
80 short scratch = (short) (((0x7f & this.type) << 9) | (0x1ff & this.length));
Ray Milkey269ffb92014-04-03 14:43:30 -070081 byte[] data = new byte[2 + this.length];
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080082 ByteBuffer bb = ByteBuffer.wrap(data);
83 bb.putShort(scratch);
Ray Milkeyb29e6262014-04-09 16:02:14 -070084 if (this.value != null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080085 bb.put(this.value);
Ray Milkeyb29e6262014-04-09 16:02:14 -070086 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080087 return data;
88 }
89
90 public LLDPTLV deserialize(ByteBuffer bb) {
91 short sscratch;
92 sscratch = bb.getShort();
93 this.type = (byte) ((sscratch >> 9) & 0x7f);
94 this.length = (short) (sscratch & 0x1ff);
95 if (this.length > 0) {
96 this.value = new byte[this.length];
97
98 // if there is an underrun just toss the TLV
Ray Milkeyb29e6262014-04-09 16:02:14 -070099 if (bb.remaining() < this.length) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800100 return null;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700101 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800102 bb.get(this.value);
103 }
104 return this;
105 }
106
107 /* (non-Javadoc)
108 * @see java.lang.Object#hashCode()
109 */
110 @Override
111 public int hashCode() {
112 final int prime = 1423;
113 int result = 1;
114 result = prime * result + length;
115 result = prime * result + type;
116 result = prime * result + Arrays.hashCode(value);
117 return result;
118 }
119
120 /* (non-Javadoc)
121 * @see java.lang.Object#equals(java.lang.Object)
122 */
123 @Override
124 public boolean equals(Object obj) {
Ray Milkeyb29e6262014-04-09 16:02:14 -0700125 if (this == obj) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800126 return true;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700127 }
128 if (obj == null) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800129 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700130 }
131 if (!(obj instanceof LLDPTLV)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800132 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700133 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800134 LLDPTLV other = (LLDPTLV) obj;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700135 if (length != other.length) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800136 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700137 }
138 if (type != other.type) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800139 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700140 }
141 if (!Arrays.equals(value, other.value)) {
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800142 return false;
Ray Milkeyb29e6262014-04-09 16:02:14 -0700143 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800144 return true;
145 }
146}