blob: 6a40f43f3e415749a1491089a8b36001cb9758d5 [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 Harchol5c9d6f42013-08-01 11:09:20 -07006public class VlanVid implements OFValueType<VlanVid> {
Yotam Harchol161a5d52013-07-25 17:17:48 -07007
8 private static final short VALIDATION_MASK = 0x0FFF;
9 final static int LENGTH = 2;
10
Yotam Harcholff8f85e2013-08-21 10:02:23 -070011 public static final VlanVid NO_MASK = VlanVid.of((short)0xFFFF);
12 public static final VlanVid FULL_MASK = VlanVid.of((short)0x0);
Yotam Harcholc2813602013-08-20 12:14:22 -070013
Yotam Harchol161a5d52013-07-25 17:17:48 -070014 private final short vid;
15
16 private VlanVid(short vid) {
17 this.vid = vid;
18 }
19
20 public static VlanVid of(short vid) {
21 if ((vid & VALIDATION_MASK) != vid)
22 throw new IllegalArgumentException("Illegal VLAN VID value: " + vid);
23 return new VlanVid(vid);
24 }
25
26 @Override
27 public boolean equals(Object obj) {
28 if (!(obj instanceof VlanVid))
29 return false;
30 VlanVid other = (VlanVid)obj;
31 if (other.vid != this.vid)
32 return false;
33 return true;
34 }
35
36 @Override
37 public int hashCode() {
38 int prime = 13873;
39 return this.vid * prime;
40 }
41
42 @Override
43 public String toString() {
44 return "0x" + Integer.toHexString(vid);
45 }
46
47 public short getValue() {
48 return vid;
49 }
50
51 @Override
52 public int getLength() {
53 return LENGTH;
54 }
55
56
57 volatile byte[] bytesCache = null;
58
59 public byte[] getBytes() {
60 if (bytesCache == null) {
61 synchronized (this) {
62 if (bytesCache == null) {
63 bytesCache =
64 new byte[] { (byte) ((vid >>> 8) & 0xFF),
65 (byte) ((vid >>> 0) & 0xFF) };
66 }
67 }
68 }
69 return bytesCache;
70 }
71
Yotam Harchold7b84202013-07-26 16:08:10 -070072 public void write2Bytes(ChannelBuffer c) {
73 c.writeShort(this.vid);
74 }
Yotam Harchol161a5d52013-07-25 17:17:48 -070075
Yotam Harchol6fccde62013-08-15 12:04:52 -070076 public static VlanVid read2Bytes(ChannelBuffer c) throws OFParseError {
Yotam Harchold7b84202013-07-26 16:08:10 -070077 return VlanVid.of(c.readShort());
Yotam Harchol161a5d52013-07-25 17:17:48 -070078 }
Yotam Harchol5c9d6f42013-08-01 11:09:20 -070079
80 @Override
81 public VlanVid applyMask(VlanVid mask) {
82 return VlanVid.of((short)(this.vid & mask.vid));
83 }
Yotam Harchol161a5d52013-07-25 17:17:48 -070084
Andreas Wundsam40e14f72013-05-06 14:49:08 -070085}