blob: ccbaa6300a908e3b26d90522bac661899fdc9949 [file] [log] [blame]
Andreas Wundsam40e14f72013-05-06 14:49:08 -07001package org.openflow.types;
2
Yotam Harchol161a5d52013-07-25 17:17:48 -07003import org.jboss.netty.buffer.ChannelBuffer;
4import org.openflow.exceptions.OFParseError;
Andreas Wundsam40e14f72013-05-06 14:49:08 -07005
Yotam Harchol5c9d6f42013-08-01 11:09:20 -07006public class VlanPcp implements OFValueType<VlanPcp> {
Yotam Harchol161a5d52013-07-25 17:17:48 -07007
8 private static final byte VALIDATION_MASK = 0x07;
9 static final int LENGTH = 1;
10
11 private final byte pcp;
12
Yotam Harcholff8f85e2013-08-21 10:02:23 -070013 public static final VlanPcp NO_MASK = VlanPcp.of((byte)0xFF);
14 public static final VlanPcp FULL_MASK = VlanPcp.of((byte)0x0);
Yotam Harcholc2813602013-08-20 12:14:22 -070015
Yotam Harchol161a5d52013-07-25 17:17:48 -070016 private VlanPcp(byte pcp) {
17 this.pcp = pcp;
18 }
19
20 public static VlanPcp of(byte pcp) {
21 if ((pcp & VALIDATION_MASK) != pcp)
22 throw new IllegalArgumentException("Illegal VLAN PCP value: " + pcp);
23 return new VlanPcp(pcp);
24 }
25
26 @Override
27 public boolean equals(Object obj) {
28 if (!(obj instanceof VlanPcp))
29 return false;
30 VlanPcp other = (VlanPcp)obj;
31 if (other.pcp != this.pcp)
32 return false;
33 return true;
34 }
35
36 @Override
37 public int hashCode() {
38 int prime = 20173;
39 return this.pcp * prime;
40 }
41
42 @Override
43 public String toString() {
44 return "0x" + Integer.toHexString(pcp);
45 }
46
47 public byte getValue() {
48 return pcp;
49 }
50
51 @Override
52 public int getLength() {
53 return LENGTH;
54 }
Yotam Harchold7b84202013-07-26 16:08:10 -070055
56 public void writeByte(ChannelBuffer c) {
57 c.writeShort(this.pcp);
Yotam Harchol161a5d52013-07-25 17:17:48 -070058 }
Yotam Harchol161a5d52013-07-25 17:17:48 -070059
Yotam Harchold7b84202013-07-26 16:08:10 -070060 public static VlanPcp readByte(ChannelBuffer c) throws OFParseError {
61 return VlanPcp.of((byte)(c.readUnsignedByte() & 0xFF));
Yotam Harchol161a5d52013-07-25 17:17:48 -070062 }
Yotam Harchol5c9d6f42013-08-01 11:09:20 -070063
64 @Override
65 public VlanPcp applyMask(VlanPcp mask) {
66 return VlanPcp.of((byte)(this.pcp & mask.pcp));
67 }
Yotam Harchol161a5d52013-07-25 17:17:48 -070068
Andreas Wundsam40e14f72013-05-06 14:49:08 -070069}