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.Shorts; |
| 8 | |
Andreas Wundsam | 98a1863 | 2013-11-05 11:34:24 -0800 | [diff] [blame] | 9 | /** Represents an 802.1Q Vlan VID (12 bits). |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 10 | * |
| 11 | * @author Andreas Wundsam <andreas.wundsam@bigswitch.com> |
| 12 | * |
| 13 | */ |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 14 | public class VlanVid implements OFValueType<VlanVid> { |
| 15 | |
Andreas Wundsam | 98a1863 | 2013-11-05 11:34:24 -0800 | [diff] [blame] | 16 | private static final short VALIDATION_MASK = 0x0FFF; |
| 17 | private static final short ZERO_VAL = 0x0000; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 18 | final static int LENGTH = 2; |
| 19 | |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 20 | /** this value means 'not set' in OF1.0 (e.g., in a match). not used elsewhere */ |
Andreas Wundsam | 98a1863 | 2013-11-05 11:34:24 -0800 | [diff] [blame] | 21 | public static final VlanVid ZERO = new VlanVid(ZERO_VAL); |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 22 | |
| 23 | /** for use with masking operations */ |
Yotam Harchol | 329f2b0 | 2013-09-11 15:05:26 -0700 | [diff] [blame] | 24 | public static final VlanVid NO_MASK = new VlanVid((short)0xFFFF); |
Andreas Wundsam | 98a1863 | 2013-11-05 11:34:24 -0800 | [diff] [blame] | 25 | public static final VlanVid FULL_MASK = ZERO; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 26 | |
| 27 | private final short vid; |
| 28 | |
| 29 | private VlanVid(short vid) { |
| 30 | this.vid = vid; |
| 31 | } |
| 32 | |
Andreas Wundsam | 98a1863 | 2013-11-05 11:34:24 -0800 | [diff] [blame] | 33 | public static VlanVid ofVlan(short vid) { |
| 34 | if ((vid & VALIDATION_MASK) != vid) |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 35 | throw new IllegalArgumentException(String.format("Illegal VLAN value: %x", vid)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 36 | return new VlanVid(vid); |
| 37 | } |
| 38 | |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 39 | /** @return the actual VLAN tag this vid identifies */ |
| 40 | public short getVlan() { |
Andreas Wundsam | 98a1863 | 2013-11-05 11:34:24 -0800 | [diff] [blame] | 41 | return vid; |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 44 | @Override |
| 45 | public boolean equals(Object obj) { |
| 46 | if (!(obj instanceof VlanVid)) |
| 47 | return false; |
| 48 | VlanVid other = (VlanVid)obj; |
| 49 | if (other.vid != this.vid) |
| 50 | return false; |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public int hashCode() { |
| 56 | int prime = 13873; |
| 57 | return this.vid * prime; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public String toString() { |
| 62 | return "0x" + Integer.toHexString(vid); |
| 63 | } |
| 64 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 65 | @Override |
| 66 | public int getLength() { |
| 67 | return LENGTH; |
| 68 | } |
| 69 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 70 | volatile byte[] bytesCache = null; |
| 71 | |
| 72 | public byte[] getBytes() { |
| 73 | if (bytesCache == null) { |
| 74 | synchronized (this) { |
| 75 | if (bytesCache == null) { |
| 76 | bytesCache = |
| 77 | new byte[] { (byte) ((vid >>> 8) & 0xFF), |
| 78 | (byte) ((vid >>> 0) & 0xFF) }; |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | return bytesCache; |
| 83 | } |
| 84 | |
| 85 | public void write2Bytes(ChannelBuffer c) { |
| 86 | c.writeShort(this.vid); |
| 87 | } |
| 88 | |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 89 | public void write2BytesOF10(ChannelBuffer c) { |
| 90 | c.writeShort(this.getVlan()); |
| 91 | } |
| 92 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 93 | public static VlanVid read2Bytes(ChannelBuffer c) throws OFParseError { |
Andreas Wundsam | 98a1863 | 2013-11-05 11:34:24 -0800 | [diff] [blame] | 94 | return VlanVid.ofVlan(c.readShort()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public VlanVid applyMask(VlanVid mask) { |
Andreas Wundsam | 98a1863 | 2013-11-05 11:34:24 -0800 | [diff] [blame] | 99 | return VlanVid.ofVlan((short)(this.vid & mask.vid)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 102 | @Override |
| 103 | public int compareTo(VlanVid o) { |
| 104 | return Shorts.compare(vid, o.vid); |
| 105 | } |
Andreas Wundsam | 98a1863 | 2013-11-05 11:34:24 -0800 | [diff] [blame] | 106 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 107 | @Override |
| 108 | public void putTo(PrimitiveSink sink) { |
| 109 | sink.putShort(vid); |
| 110 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 111 | } |