Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 1 | package org.openflow.types; |
| 2 | |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame^] | 3 | import org.jboss.netty.buffer.ChannelBuffer; |
| 4 | import org.openflow.exceptions.OFParseError; |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 5 | |
Yotam Harchol | 161a5d5 | 2013-07-25 17:17:48 -0700 | [diff] [blame^] | 6 | public class VlanVid implements OFValueType { |
| 7 | |
| 8 | private static final short VALIDATION_MASK = 0x0FFF; |
| 9 | final static int LENGTH = 2; |
| 10 | |
| 11 | private final short vid; |
| 12 | |
| 13 | private VlanVid(short vid) { |
| 14 | this.vid = vid; |
| 15 | } |
| 16 | |
| 17 | public static VlanVid of(short vid) { |
| 18 | if ((vid & VALIDATION_MASK) != vid) |
| 19 | throw new IllegalArgumentException("Illegal VLAN VID value: " + vid); |
| 20 | return new VlanVid(vid); |
| 21 | } |
| 22 | |
| 23 | @Override |
| 24 | public boolean equals(Object obj) { |
| 25 | if (!(obj instanceof VlanVid)) |
| 26 | return false; |
| 27 | VlanVid other = (VlanVid)obj; |
| 28 | if (other.vid != this.vid) |
| 29 | return false; |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | @Override |
| 34 | public int hashCode() { |
| 35 | int prime = 13873; |
| 36 | return this.vid * prime; |
| 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public String toString() { |
| 41 | return "0x" + Integer.toHexString(vid); |
| 42 | } |
| 43 | |
| 44 | public short getValue() { |
| 45 | return vid; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public int getLength() { |
| 50 | return LENGTH; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | volatile byte[] bytesCache = null; |
| 55 | |
| 56 | public byte[] getBytes() { |
| 57 | if (bytesCache == null) { |
| 58 | synchronized (this) { |
| 59 | if (bytesCache == null) { |
| 60 | bytesCache = |
| 61 | new byte[] { (byte) ((vid >>> 8) & 0xFF), |
| 62 | (byte) ((vid >>> 0) & 0xFF) }; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | return bytesCache; |
| 67 | } |
| 68 | |
| 69 | public static final Serializer<VlanVid> SERIALIZER_V10 = new SerializerV10(); |
| 70 | public static final Serializer<VlanVid> SERIALIZER_V11 = SERIALIZER_V10; |
| 71 | public static final Serializer<VlanVid> SERIALIZER_V12 = SERIALIZER_V10; |
| 72 | public static final Serializer<VlanVid> SERIALIZER_V13 = SERIALIZER_V10; |
| 73 | |
| 74 | private static class SerializerV10 implements OFValueType.Serializer<VlanVid> { |
| 75 | |
| 76 | @Override |
| 77 | public void writeTo(VlanVid value, ChannelBuffer c) { |
| 78 | c.writeShort(value.vid); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public VlanVid readFrom(ChannelBuffer c) throws OFParseError { |
| 83 | return VlanVid.of(c.readShort()); |
| 84 | } |
| 85 | |
| 86 | } |
| 87 | |
Andreas Wundsam | 40e14f7 | 2013-05-06 14:49:08 -0700 | [diff] [blame] | 88 | } |