blob: b63e4dceaf24ab4f3be4d182bb074136c44babdb [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
Andreas Wundsam22ba3af2013-10-04 16:00:30 -07006import com.google.common.hash.PrimitiveSink;
Andreas Wundsam85c961f2013-09-29 21:22:12 -07007import com.google.common.primitives.Shorts;
8
Andreas Wundsam98a18632013-11-05 11:34:24 -08009/** Represents an 802.1Q Vlan VID (12 bits).
Andreas Wundsam55b71ce2013-10-01 19:26:46 -070010 *
11 * @author Andreas Wundsam <andreas.wundsam@bigswitch.com>
12 *
13 */
Yotam Harcholf3f11152013-09-05 16:47:16 -070014public class VlanVid implements OFValueType<VlanVid> {
15
Andreas Wundsam98a18632013-11-05 11:34:24 -080016 private static final short VALIDATION_MASK = 0x0FFF;
17 private static final short ZERO_VAL = 0x0000;
Yotam Harcholf3f11152013-09-05 16:47:16 -070018 final static int LENGTH = 2;
19
Andreas Wundsam55b71ce2013-10-01 19:26:46 -070020 /** this value means 'not set' in OF1.0 (e.g., in a match). not used elsewhere */
Andreas Wundsam98a18632013-11-05 11:34:24 -080021 public static final VlanVid ZERO = new VlanVid(ZERO_VAL);
Andreas Wundsam55b71ce2013-10-01 19:26:46 -070022
23 /** for use with masking operations */
Yotam Harchol329f2b02013-09-11 15:05:26 -070024 public static final VlanVid NO_MASK = new VlanVid((short)0xFFFF);
Andreas Wundsam98a18632013-11-05 11:34:24 -080025 public static final VlanVid FULL_MASK = ZERO;
Yotam Harcholf3f11152013-09-05 16:47:16 -070026
27 private final short vid;
28
29 private VlanVid(short vid) {
30 this.vid = vid;
31 }
32
Andreas Wundsam98a18632013-11-05 11:34:24 -080033 public static VlanVid ofVlan(short vid) {
34 if ((vid & VALIDATION_MASK) != vid)
Andreas Wundsam55b71ce2013-10-01 19:26:46 -070035 throw new IllegalArgumentException(String.format("Illegal VLAN value: %x", vid));
Yotam Harcholf3f11152013-09-05 16:47:16 -070036 return new VlanVid(vid);
37 }
38
Andreas Wundsam55b71ce2013-10-01 19:26:46 -070039 /** @return the actual VLAN tag this vid identifies */
40 public short getVlan() {
Andreas Wundsam98a18632013-11-05 11:34:24 -080041 return vid;
Andreas Wundsam55b71ce2013-10-01 19:26:46 -070042 }
43
Yotam Harcholf3f11152013-09-05 16:47:16 -070044 @Override
45 public boolean equals(Object obj) {
46 if (!(obj instanceof VlanVid))
47 return false;
48 VlanVid other = (VlanVid)obj;
49 if (other.vid != this.vid)
50 return false;
51 return true;
52 }
53
54 @Override
55 public int hashCode() {
56 int prime = 13873;
57 return this.vid * prime;
58 }
59
60 @Override
61 public String toString() {
62 return "0x" + Integer.toHexString(vid);
63 }
64
Yotam Harcholf3f11152013-09-05 16:47:16 -070065 @Override
66 public int getLength() {
67 return LENGTH;
68 }
69
Yotam Harcholf3f11152013-09-05 16:47:16 -070070 volatile byte[] bytesCache = null;
71
72 public byte[] getBytes() {
73 if (bytesCache == null) {
74 synchronized (this) {
75 if (bytesCache == null) {
76 bytesCache =
77 new byte[] { (byte) ((vid >>> 8) & 0xFF),
78 (byte) ((vid >>> 0) & 0xFF) };
79 }
80 }
81 }
82 return bytesCache;
83 }
84
85 public void write2Bytes(ChannelBuffer c) {
86 c.writeShort(this.vid);
87 }
88
Andreas Wundsam55b71ce2013-10-01 19:26:46 -070089 public void write2BytesOF10(ChannelBuffer c) {
90 c.writeShort(this.getVlan());
91 }
92
Yotam Harcholf3f11152013-09-05 16:47:16 -070093 public static VlanVid read2Bytes(ChannelBuffer c) throws OFParseError {
Andreas Wundsam98a18632013-11-05 11:34:24 -080094 return VlanVid.ofVlan(c.readShort());
Yotam Harcholf3f11152013-09-05 16:47:16 -070095 }
96
97 @Override
98 public VlanVid applyMask(VlanVid mask) {
Andreas Wundsam98a18632013-11-05 11:34:24 -080099 return VlanVid.ofVlan((short)(this.vid & mask.vid));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700100 }
101
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700102 @Override
103 public int compareTo(VlanVid o) {
104 return Shorts.compare(vid, o.vid);
105 }
Andreas Wundsam98a18632013-11-05 11:34:24 -0800106
Andreas Wundsam22ba3af2013-10-04 16:00:30 -0700107 @Override
108 public void putTo(PrimitiveSink sink) {
109 sink.putShort(vid);
110 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700111}