blob: ee605de8df031fccee0caeb17d03fde25a94ef2d [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.projectfloodlight.openflow.types;
2
3import java.util.Arrays;
4
5import org.jboss.netty.buffer.ChannelBuffer;
6import org.projectfloodlight.openflow.exceptions.OFParseError;
7
8import com.google.common.hash.PrimitiveSink;
9import com.google.common.primitives.Shorts;
10
11/** Represents an 802.1Q Vlan VID (12 bits).
12 *
13 * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
14 *
15 */
16public class VlanVid implements OFValueType<VlanVid> {
17
18 private static final short VALIDATION_MASK = 0x0FFF;
19 private static final short ZERO_VAL = 0x0000;
20 final static int LENGTH = 2;
21
22 /** this value means 'not set' in OF1.0 (e.g., in a match). not used elsewhere */
23 public static final VlanVid ZERO = new VlanVid(ZERO_VAL);
24
25 /** for use with masking operations */
26 public static final VlanVid NO_MASK = new VlanVid((short)0xFFFF);
27 public static final VlanVid FULL_MASK = ZERO;
28
29 private final short vid;
30
31 private VlanVid(short vid) {
32 this.vid = vid;
33 }
34
35 public static VlanVid ofVlan(int vid) {
36 if (vid == NO_MASK.vid)
37 return NO_MASK;
38 if ((vid & VALIDATION_MASK) != vid)
39 throw new IllegalArgumentException(String.format("Illegal VLAN value: %x", vid));
40 return new VlanVid((short) vid);
41 }
42
43 /** @return the actual VLAN tag this vid identifies */
44 public short getVlan() {
45 return vid;
46 }
47
48 @Override
49 public boolean equals(Object obj) {
50 if (!(obj instanceof VlanVid))
51 return false;
52 VlanVid other = (VlanVid)obj;
53 if (other.vid != this.vid)
54 return false;
55 return true;
56 }
57
58 @Override
59 public int hashCode() {
60 int prime = 13873;
61 return this.vid * prime;
62 }
63
64 @Override
65 public String toString() {
66 return "0x" + Integer.toHexString(vid);
67 }
68
69 @Override
70 public int getLength() {
71 return LENGTH;
72 }
73
74 private volatile byte[] bytesCache = null;
75
76 public byte[] getBytes() {
77 if (bytesCache == null) {
78 synchronized (this) {
79 if (bytesCache == null) {
80 bytesCache =
81 new byte[] { (byte) ((vid >>> 8) & 0xFF),
82 (byte) ((vid >>> 0) & 0xFF) };
83 }
84 }
85 }
86 return Arrays.copyOf(bytesCache, bytesCache.length);
87 }
88
89 public void write2Bytes(ChannelBuffer c) {
90 c.writeShort(this.vid);
91 }
92
93 public void write2BytesOF10(ChannelBuffer c) {
94 c.writeShort(this.getVlan());
95 }
96
97 public static VlanVid read2Bytes(ChannelBuffer c) throws OFParseError {
98 return VlanVid.ofVlan(c.readShort());
99 }
100
101 @Override
102 public VlanVid applyMask(VlanVid mask) {
103 return VlanVid.ofVlan((short)(this.vid & mask.vid));
104 }
105
106 @Override
107 public int compareTo(VlanVid o) {
108 return Shorts.compare(vid, o.vid);
109 }
110
111 @Override
112 public void putTo(PrimitiveSink sink) {
113 sink.putShort(vid);
114 }
115}