blob: cbb7004ea97fe7c8d67d3ac7502ffe2f21acb820 [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4import org.projectfloodlight.openflow.exceptions.OFParseError;
5
6import com.google.common.hash.PrimitiveSink;
7import com.google.common.primitives.UnsignedBytes;
8
9public class VlanPcp implements OFValueType<VlanPcp> {
10
11 private static final byte VALIDATION_MASK = 0x07;
12 private static final byte NONE_VAL = 0x00;
13 static final int LENGTH = 1;
14
15 private final byte pcp;
16
17 public static final VlanPcp NONE = new VlanPcp(NONE_VAL);
18 public static final VlanPcp NO_MASK = new VlanPcp((byte)0xFF);
19 public static final VlanPcp FULL_MASK = VlanPcp.of((byte)0x0);
20
21 private VlanPcp(byte pcp) {
22 this.pcp = pcp;
23 }
24
25 public static VlanPcp of(byte pcp) {
26 if ((pcp & VALIDATION_MASK) != pcp)
27 throw new IllegalArgumentException("Illegal VLAN PCP value: " + pcp);
28 return new VlanPcp(pcp);
29 }
30
31 @Override
32 public boolean equals(Object obj) {
33 if (!(obj instanceof VlanPcp))
34 return false;
35 VlanPcp other = (VlanPcp)obj;
36 if (other.pcp != this.pcp)
37 return false;
38 return true;
39 }
40
41 @Override
42 public int hashCode() {
43 int prime = 20173;
44 return this.pcp * prime;
45 }
46
47 @Override
48 public String toString() {
49 return "0x" + Integer.toHexString(pcp);
50 }
51
52 public byte getValue() {
53 return pcp;
54 }
55
56 @Override
57 public int getLength() {
58 return LENGTH;
59 }
60
61 public void writeByte(ChannelBuffer c) {
62 c.writeByte(this.pcp);
63 }
64
65 public static VlanPcp readByte(ChannelBuffer c) throws OFParseError {
66 return VlanPcp.of((byte)(c.readUnsignedByte() & 0xFF));
67 }
68
69 @Override
70 public VlanPcp applyMask(VlanPcp mask) {
71 return VlanPcp.of((byte)(this.pcp & mask.pcp));
72 }
73
74 @Override
75 public int compareTo(VlanPcp o) {
76 return UnsignedBytes.compare(pcp, o.pcp);
77 }
78 @Override
79 public void putTo(PrimitiveSink sink) {
80 sink.putByte(pcp);
81 }
82}