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