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 | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 9 | /** Represents an OpenFlow Vlan VID, as specified by the OpenFlow 1.3 spec. |
| 10 | * |
| 11 | * <b> Note: this is not just the 12-bit vlan tag. OpenFlow defines |
| 12 | * the additional mask bits 0x1000 to represent the presence of a vlan |
| 13 | * tag. This additional bit will be stripped when writing a OF1.0 value |
| 14 | * tag. |
| 15 | * </b> |
| 16 | * |
| 17 | * |
| 18 | * @author Andreas Wundsam <andreas.wundsam@bigswitch.com> |
| 19 | * |
| 20 | */ |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 21 | public class VlanVid implements OFValueType<VlanVid> { |
| 22 | |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 23 | private static final short VALIDATION_MASK = 0x1FFF; |
| 24 | private static final short PRESENT_VAL = 0x1000; |
| 25 | private static final short VLAN_MASK = 0x0FFF; |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 26 | private static final short NONE_VAL = 0x0000; |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 27 | private static final short UNTAGGED_VAL_OF13 = (short) 0x0000; |
| 28 | private static final short UNTAGGED_VAL_OF10 = (short) 0xFFFF; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 29 | final static int LENGTH = 2; |
| 30 | |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 31 | /** presence of a VLAN tag is idicated by the presence of bit 0x1000 */ |
| 32 | public static final VlanVid PRESENT = new VlanVid(PRESENT_VAL); |
| 33 | |
| 34 | /** this value means 'not set' in OF1.0 (e.g., in a match). not used elsewhere */ |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 35 | public static final VlanVid NONE = new VlanVid(NONE_VAL); |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 36 | |
| 37 | /** for use with masking operations */ |
Yotam Harchol | 329f2b0 | 2013-09-11 15:05:26 -0700 | [diff] [blame] | 38 | public static final VlanVid NO_MASK = new VlanVid((short)0xFFFF); |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 39 | public static final VlanVid FULL_MASK = NONE; |
| 40 | |
| 41 | /** an untagged packet is specified as 0000 in OF 1.0, but 0xFFFF in OF1.0. Special case that. */ |
| 42 | public static final VlanVid UNTAGGED = new VlanVid(NONE_VAL) { |
| 43 | @Override |
| 44 | public void write2BytesOF10(ChannelBuffer c) { |
| 45 | c.writeShort(UNTAGGED_VAL_OF10); |
| 46 | } |
| 47 | }; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 48 | |
| 49 | private final short vid; |
| 50 | |
| 51 | private VlanVid(short vid) { |
| 52 | this.vid = vid; |
| 53 | } |
| 54 | |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 55 | public static VlanVid ofRawVid(short vid) { |
| 56 | if(vid == UNTAGGED_VAL_OF13) |
| 57 | return UNTAGGED; |
| 58 | else if(vid == PRESENT_VAL) |
| 59 | return PRESENT; |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 60 | else if ((vid & VALIDATION_MASK) != vid) |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 61 | throw new IllegalArgumentException(String.format("Illegal VLAN value: %x", vid)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 62 | return new VlanVid(vid); |
| 63 | } |
| 64 | |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 65 | public static VlanVid ofVlan(int vlan) { |
| 66 | if( (vlan & VLAN_MASK) != vlan) |
| 67 | throw new IllegalArgumentException(String.format("Illegal VLAN value: %x", vlan)); |
| 68 | return ofRawVid( (short) (vlan | PRESENT_VAL)); |
| 69 | } |
| 70 | |
| 71 | public static VlanVid ofVlanOF10(short of10vlan) { |
| 72 | if(of10vlan == NONE_VAL) { |
| 73 | return NONE; |
| 74 | } else if(of10vlan == UNTAGGED_VAL_OF10) { |
| 75 | return UNTAGGED; |
| 76 | } else { |
| 77 | return ofVlan(of10vlan); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** @return whether or not this VlanId has the present (0x1000) bit set */ |
| 82 | public boolean isPresentBitSet() { |
| 83 | return (vid & PRESENT_VAL) != 0; |
| 84 | } |
| 85 | |
| 86 | /** @return the actual VLAN tag this vid identifies */ |
| 87 | public short getVlan() { |
| 88 | return (short) (vid & VLAN_MASK); |
| 89 | } |
| 90 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 91 | @Override |
| 92 | public boolean equals(Object obj) { |
| 93 | if (!(obj instanceof VlanVid)) |
| 94 | return false; |
| 95 | VlanVid other = (VlanVid)obj; |
| 96 | if (other.vid != this.vid) |
| 97 | return false; |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public int hashCode() { |
| 103 | int prime = 13873; |
| 104 | return this.vid * prime; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public String toString() { |
| 109 | return "0x" + Integer.toHexString(vid); |
| 110 | } |
| 111 | |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 112 | public short getRawVid() { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 113 | return vid; |
| 114 | } |
| 115 | |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 116 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 117 | @Override |
| 118 | public int getLength() { |
| 119 | return LENGTH; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | volatile byte[] bytesCache = null; |
| 124 | |
| 125 | public byte[] getBytes() { |
| 126 | if (bytesCache == null) { |
| 127 | synchronized (this) { |
| 128 | if (bytesCache == null) { |
| 129 | bytesCache = |
| 130 | new byte[] { (byte) ((vid >>> 8) & 0xFF), |
| 131 | (byte) ((vid >>> 0) & 0xFF) }; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | return bytesCache; |
| 136 | } |
| 137 | |
| 138 | public void write2Bytes(ChannelBuffer c) { |
| 139 | c.writeShort(this.vid); |
| 140 | } |
| 141 | |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 142 | public void write2BytesOF10(ChannelBuffer c) { |
| 143 | c.writeShort(this.getVlan()); |
| 144 | } |
| 145 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 146 | public static VlanVid read2Bytes(ChannelBuffer c) throws OFParseError { |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 147 | return VlanVid.ofRawVid(c.readShort()); |
| 148 | } |
| 149 | |
| 150 | public static VlanVid read2BytesOF10(ChannelBuffer c) throws OFParseError { |
| 151 | return VlanVid.ofVlanOF10(c.readShort()); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public VlanVid applyMask(VlanVid mask) { |
Andreas Wundsam | 55b71ce | 2013-10-01 19:26:46 -0700 | [diff] [blame] | 156 | return VlanVid.ofRawVid((short)(this.vid & mask.vid)); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 159 | @Override |
| 160 | public int compareTo(VlanVid o) { |
| 161 | return Shorts.compare(vid, o.vid); |
| 162 | } |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 163 | @Override |
| 164 | public void putTo(PrimitiveSink sink) { |
| 165 | sink.putShort(vid); |
| 166 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 167 | } |