blob: dcc7d60e54fe4520963581aff9a9858e35e37a59 [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 Wundsam85c961f2013-09-29 21:22:12 -07006import com.google.common.primitives.UnsignedBytes;
7
Yotam Harcholf3f11152013-09-05 16:47:16 -07008public class VlanPcp implements OFValueType<VlanPcp> {
9
10 private static final byte VALIDATION_MASK = 0x07;
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070011 private static final byte NONE_VAL = 0x00;
Yotam Harcholf3f11152013-09-05 16:47:16 -070012 static final int LENGTH = 1;
13
14 private final byte pcp;
15
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070016 public static final VlanPcp NONE = new VlanPcp(NONE_VAL);
Yotam Harchol329f2b02013-09-11 15:05:26 -070017 public static final VlanPcp NO_MASK = new VlanPcp((byte)0xFF);
Yotam Harcholf3f11152013-09-05 16:47:16 -070018 public static final VlanPcp FULL_MASK = VlanPcp.of((byte)0x0);
19
20 private VlanPcp(byte pcp) {
21 this.pcp = pcp;
22 }
23
24 public static VlanPcp of(byte pcp) {
25 if ((pcp & VALIDATION_MASK) != pcp)
26 throw new IllegalArgumentException("Illegal VLAN PCP value: " + pcp);
27 return new VlanPcp(pcp);
28 }
29
30 @Override
31 public boolean equals(Object obj) {
32 if (!(obj instanceof VlanPcp))
33 return false;
34 VlanPcp other = (VlanPcp)obj;
35 if (other.pcp != this.pcp)
36 return false;
37 return true;
38 }
39
40 @Override
41 public int hashCode() {
42 int prime = 20173;
43 return this.pcp * prime;
44 }
45
46 @Override
47 public String toString() {
48 return "0x" + Integer.toHexString(pcp);
49 }
50
51 public byte getValue() {
52 return pcp;
53 }
54
55 @Override
56 public int getLength() {
57 return LENGTH;
58 }
59
60 public void writeByte(ChannelBuffer c) {
Yotam Harchol234fd1e2013-09-16 09:57:30 -070061 c.writeByte(this.pcp);
Yotam Harcholf3f11152013-09-05 16:47:16 -070062 }
63
64 public static VlanPcp readByte(ChannelBuffer c) throws OFParseError {
65 return VlanPcp.of((byte)(c.readUnsignedByte() & 0xFF));
66 }
67
68 @Override
69 public VlanPcp applyMask(VlanPcp mask) {
70 return VlanPcp.of((byte)(this.pcp & mask.pcp));
71 }
72
Andreas Wundsam85c961f2013-09-29 21:22:12 -070073 @Override
74 public int compareTo(VlanPcp o) {
75 return UnsignedBytes.compare(pcp, o.pcp);
76 }
77
Yotam Harcholf3f11152013-09-05 16:47:16 -070078}