blob: 5c23779a7ec5c37727ca1d8be40c64e0f52cc0f1 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4import org.projectfloodlight.openflow.exceptions.OFParseError;
5
6public class VlanVid implements OFValueType<VlanVid> {
7
8 private static final short VALIDATION_MASK = 0x0FFF;
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -07009 private static final short NONE_VAL = 0x0000;
Yotam Harcholf3f11152013-09-05 16:47:16 -070010 final static int LENGTH = 2;
11
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070012 public static final VlanVid NONE = new VlanVid(NONE_VAL);
Yotam Harchol329f2b02013-09-11 15:05:26 -070013 public static final VlanVid NO_MASK = new VlanVid((short)0xFFFF);
Yotam Harcholf3f11152013-09-05 16:47:16 -070014 public static final VlanVid FULL_MASK = VlanVid.of((short)0x0);
15
16 private final short vid;
17
18 private VlanVid(short vid) {
19 this.vid = vid;
20 }
21
22 public static VlanVid of(short vid) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070023 if(vid == NONE_VAL)
24 return NONE;
25 else if ((vid & VALIDATION_MASK) != vid)
Yotam Harcholf3f11152013-09-05 16:47:16 -070026 throw new IllegalArgumentException("Illegal VLAN VID value: " + vid);
27 return new VlanVid(vid);
28 }
29
30 @Override
31 public boolean equals(Object obj) {
32 if (!(obj instanceof VlanVid))
33 return false;
34 VlanVid other = (VlanVid)obj;
35 if (other.vid != this.vid)
36 return false;
37 return true;
38 }
39
40 @Override
41 public int hashCode() {
42 int prime = 13873;
43 return this.vid * prime;
44 }
45
46 @Override
47 public String toString() {
48 return "0x" + Integer.toHexString(vid);
49 }
50
51 public short getValue() {
52 return vid;
53 }
54
55 @Override
56 public int getLength() {
57 return LENGTH;
58 }
59
60
61 volatile byte[] bytesCache = null;
62
63 public byte[] getBytes() {
64 if (bytesCache == null) {
65 synchronized (this) {
66 if (bytesCache == null) {
67 bytesCache =
68 new byte[] { (byte) ((vid >>> 8) & 0xFF),
69 (byte) ((vid >>> 0) & 0xFF) };
70 }
71 }
72 }
73 return bytesCache;
74 }
75
76 public void write2Bytes(ChannelBuffer c) {
77 c.writeShort(this.vid);
78 }
79
80 public static VlanVid read2Bytes(ChannelBuffer c) throws OFParseError {
81 return VlanVid.of(c.readShort());
82 }
83
84 @Override
85 public VlanVid applyMask(VlanVid mask) {
86 return VlanVid.of((short)(this.vid & mask.vid));
87 }
88
89}