blob: 055232165d2c2590b6234dab5901b9bad3c4886b [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
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**/
17
18package net.floodlightcontroller.packet;
19
20import java.nio.ByteBuffer;
21import java.util.Arrays;
22
23/**
24 *
25 *
26 * @author David Erickson (daviderickson@cs.stanford.edu)
27 */
28public 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() {
67 return value;
68 }
69
70 /**
71 * @param value the value to set
72 */
73 public LLDPTLV setValue(byte[] value) {
74 this.value = value;
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));
83 byte[] data = new byte[2+this.length];
84 ByteBuffer bb = ByteBuffer.wrap(data);
85 bb.putShort(scratch);
86 if (this.value != null)
87 bb.put(this.value);
88 return data;
89 }
90
91 public LLDPTLV deserialize(ByteBuffer bb) {
92 short sscratch;
93 sscratch = bb.getShort();
94 this.type = (byte) ((sscratch >> 9) & 0x7f);
95 this.length = (short) (sscratch & 0x1ff);
96 if (this.length > 0) {
97 this.value = new byte[this.length];
98
99 // if there is an underrun just toss the TLV
100 if (bb.remaining() < this.length)
101 return null;
102 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) {
125 if (this == obj)
126 return true;
127 if (obj == null)
128 return false;
129 if (!(obj instanceof LLDPTLV))
130 return false;
131 LLDPTLV other = (LLDPTLV) obj;
132 if (length != other.length)
133 return false;
134 if (type != other.type)
135 return false;
136 if (!Arrays.equals(value, other.value))
137 return false;
138 return true;
139 }
140}