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 | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 6 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 7 | import com.google.common.primitives.UnsignedBytes; |
| 8 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 9 | public class VlanPcp implements OFValueType<VlanPcp> { |
| 10 | |
| 11 | private static final byte VALIDATION_MASK = 0x07; |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 12 | private static final byte NONE_VAL = 0x00; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 13 | static final int LENGTH = 1; |
| 14 | |
| 15 | private final byte pcp; |
| 16 | |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 17 | public static final VlanPcp NONE = new VlanPcp(NONE_VAL); |
Yotam Harchol | 329f2b0 | 2013-09-11 15:05:26 -0700 | [diff] [blame] | 18 | public static final VlanPcp NO_MASK = new VlanPcp((byte)0xFF); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 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) { |
Yotam Harchol | 234fd1e | 2013-09-16 09:57:30 -0700 | [diff] [blame] | 62 | c.writeByte(this.pcp); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 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 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 74 | @Override |
| 75 | public int compareTo(VlanPcp o) { |
| 76 | return UnsignedBytes.compare(pcp, o.pcp); |
| 77 | } |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 78 | @Override |
| 79 | public void putTo(PrimitiveSink sink) { |
| 80 | sink.putByte(pcp); |
| 81 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 82 | } |