blob: 0a7b7c5e22460fc27babf54912d3d3eb1335aba6 [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 Harchol161a5d52013-07-25 17:17:48 -07006public class VlanPcp implements OFValueType {
7
8 private static final byte VALIDATION_MASK = 0x07;
9 static final int LENGTH = 1;
10
11 private final byte pcp;
12
13 private VlanPcp(byte pcp) {
14 this.pcp = pcp;
15 }
16
17 public static VlanPcp of(byte pcp) {
18 if ((pcp & VALIDATION_MASK) != pcp)
19 throw new IllegalArgumentException("Illegal VLAN PCP value: " + pcp);
20 return new VlanPcp(pcp);
21 }
22
23 @Override
24 public boolean equals(Object obj) {
25 if (!(obj instanceof VlanPcp))
26 return false;
27 VlanPcp other = (VlanPcp)obj;
28 if (other.pcp != this.pcp)
29 return false;
30 return true;
31 }
32
33 @Override
34 public int hashCode() {
35 int prime = 20173;
36 return this.pcp * prime;
37 }
38
39 @Override
40 public String toString() {
41 return "0x" + Integer.toHexString(pcp);
42 }
43
44 public byte getValue() {
45 return pcp;
46 }
47
48 @Override
49 public int getLength() {
50 return LENGTH;
51 }
52
53 volatile byte[] bytesCache = null;
54
55 public byte[] getBytes() {
56 if (bytesCache == null) {
57 synchronized (this) {
58 if (bytesCache == null) {
59 bytesCache =
60 new byte[] { pcp };
61 }
62 }
63 }
64 return bytesCache;
65 }
66
67 public static final Serializer<VlanPcp> SERIALIZER_V10 = new SerializerV10();
68 public static final Serializer<VlanPcp> SERIALIZER_V11 = SERIALIZER_V10;
69 public static final Serializer<VlanPcp> SERIALIZER_V12 = SERIALIZER_V10;
70 public static final Serializer<VlanPcp> SERIALIZER_V13 = SERIALIZER_V10;
71
72 private static class SerializerV10 implements OFValueType.Serializer<VlanPcp> {
73
74 @Override
75 public void writeTo(VlanPcp value, ChannelBuffer c) {
76 c.writeShort(value.pcp);
77 }
78
79 @Override
80 public VlanPcp readFrom(ChannelBuffer c) throws OFParseError {
81 return VlanPcp.of((byte)(c.readUnsignedByte() & 0xFF));
82 }
83
84 }
85
Andreas Wundsam40e14f72013-05-06 14:49:08 -070086}