Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import org.jboss.netty.buffer.ChannelBuffer; |
| 4 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 5 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 6 | import com.google.common.primitives.UnsignedBytes; |
| 7 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 8 | public class VlanPcp implements OFValueType<VlanPcp> { |
| 9 | |
| 10 | private static final byte VALIDATION_MASK = 0x07; |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 11 | private static final byte NONE_VAL = 0x00; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 12 | static final int LENGTH = 1; |
| 13 | |
| 14 | private final byte pcp; |
| 15 | |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 16 | public static final VlanPcp NONE = new VlanPcp(NONE_VAL); |
Yotam Harchol | 329f2b0 | 2013-09-11 15:05:26 -0700 | [diff] [blame] | 17 | public static final VlanPcp NO_MASK = new VlanPcp((byte)0xFF); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 18 | 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 Harchol | 234fd1e | 2013-09-16 09:57:30 -0700 | [diff] [blame] | 61 | c.writeByte(this.pcp); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 62 | } |
| 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 Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 73 | @Override |
| 74 | public int compareTo(VlanPcp o) { |
| 75 | return UnsignedBytes.compare(pcp, o.pcp); |
| 76 | } |
| 77 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 78 | } |