blob: b3f187f8847b026f1637af542ba911730fa59e14 [file] [log] [blame]
xinwuc1b011d2013-09-18 15:24:04 -07001package org.projectfloodlight.openflow.types;
2
xinwu32034432013-09-18 17:17:50 -07003import javax.annotation.concurrent.Immutable;
xinwuc1b011d2013-09-18 15:24:04 -07004
Andreas Wundsam85c961f2013-09-29 21:22:12 -07005import org.jboss.netty.buffer.ChannelBuffer;
6
Andreas Wundsam22ba3af2013-10-04 16:00:30 -07007import com.google.common.hash.PrimitiveSink;
Andreas Wundsam85c961f2013-09-29 21:22:12 -07008import com.google.common.primitives.UnsignedInts;
9
xinwuc1b011d2013-09-18 15:24:04 -070010@Immutable
11public class VRF implements OFValueType<VRF> {
12 static final int LENGTH = 4;
13 private final int rawValue;
14
15 public static final VRF NO_MASK = VRF.of(0xFFFFFFFF);
16 public static final VRF FULL_MASK = VRF.of(0x00000000);
17
18 private VRF(final int rawValue) {
19 this.rawValue = rawValue;
20 }
21
22 public static VRF of(final int raw) {
23 return new VRF(raw);
24 }
25
26 public int getInt() {
27 return rawValue;
28 }
29
30 @Override
31 public int getLength() {
32 return LENGTH;
33 }
34
35 @Override
36 public int hashCode() {
37 final int prime = 31;
38 int result = 1;
39 result = prime * result + rawValue;
40 return result;
41 }
42
43 @Override
44 public boolean equals(Object obj) {
45 if (this == obj)
46 return true;
47 if (obj == null)
48 return false;
49 if (getClass() != obj.getClass())
50 return false;
51 VRF other = (VRF) obj;
52 if (rawValue != other.rawValue)
53 return false;
54 return true;
55 }
56
57 @Override
58 public String toString() {
59 return Integer.toString(rawValue);
60 }
61
62 public void write4Bytes(ChannelBuffer c) {
63 c.writeInt(rawValue);
64 }
65
66 public static VRF read4Bytes(ChannelBuffer c) {
67 return VRF.of(c.readInt());
68 }
69
70 @Override
71 public VRF applyMask(VRF mask) {
72 return VRF.of(this.rawValue & mask.rawValue);
73 }
Andreas Wundsam85c961f2013-09-29 21:22:12 -070074
75 @Override
76 public int compareTo(VRF o) {
77 return UnsignedInts.compare(rawValue, o.rawValue);
78 }
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070079
80 @Override
81 public void putTo(PrimitiveSink sink) {
82 sink.putInt(rawValue);
83 }
xinwuc1b011d2013-09-18 15:24:04 -070084}