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