blob: a2b38b2700eedc7bd4aecd8807162658c98e504e [file] [log] [blame]
Andreas Wundsam40e14f72013-05-06 14:49:08 -07001package org.openflow.types;
2
Yotam Harchol161a5d52013-07-25 17:17:48 -07003import org.jboss.netty.buffer.ChannelBuffer;
4import org.openflow.exceptions.OFParseError;
Andreas Wundsam40e14f72013-05-06 14:49:08 -07005
Yotam Harchol161a5d52013-07-25 17:17:48 -07006public class VlanVid implements OFValueType {
7
8 private static final short VALIDATION_MASK = 0x0FFF;
9 final static int LENGTH = 2;
10
11 private final short vid;
12
13 private VlanVid(short vid) {
14 this.vid = vid;
15 }
16
17 public static VlanVid of(short vid) {
18 if ((vid & VALIDATION_MASK) != vid)
19 throw new IllegalArgumentException("Illegal VLAN VID value: " + vid);
20 return new VlanVid(vid);
21 }
22
23 @Override
24 public boolean equals(Object obj) {
25 if (!(obj instanceof VlanVid))
26 return false;
27 VlanVid other = (VlanVid)obj;
28 if (other.vid != this.vid)
29 return false;
30 return true;
31 }
32
33 @Override
34 public int hashCode() {
35 int prime = 13873;
36 return this.vid * prime;
37 }
38
39 @Override
40 public String toString() {
41 return "0x" + Integer.toHexString(vid);
42 }
43
44 public short getValue() {
45 return vid;
46 }
47
48 @Override
49 public int getLength() {
50 return LENGTH;
51 }
52
53
54 volatile byte[] bytesCache = null;
55
56 public byte[] getBytes() {
57 if (bytesCache == null) {
58 synchronized (this) {
59 if (bytesCache == null) {
60 bytesCache =
61 new byte[] { (byte) ((vid >>> 8) & 0xFF),
62 (byte) ((vid >>> 0) & 0xFF) };
63 }
64 }
65 }
66 return bytesCache;
67 }
68
Yotam Harchold7b84202013-07-26 16:08:10 -070069 public void write2Bytes(ChannelBuffer c) {
70 c.writeShort(this.vid);
71 }
Yotam Harchol161a5d52013-07-25 17:17:48 -070072
Yotam Harchold7b84202013-07-26 16:08:10 -070073 public VlanVid read2Bytes(ChannelBuffer c) throws OFParseError {
74 return VlanVid.of(c.readShort());
Yotam Harchol161a5d52013-07-25 17:17:48 -070075 }
76
Andreas Wundsam40e14f72013-05-06 14:49:08 -070077}