blob: 51364e1ee64da305a096a7dc1bb9418cbd63c0d3 [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
Andreas Wundsam85c961f2013-09-29 21:22:12 -070011public class LagId implements OFValueType<LagId> {
xinwuc1b011d2013-09-18 15:24:04 -070012 static final int LENGTH = 4;
13 private final int rawValue;
14
Rich Lane376cafe2013-10-27 21:49:14 -070015 private final static int NONE_VAL = 0;
16 public final static LagId NONE = new LagId(NONE_VAL);
17
Andreas Wundsam14ae8c62013-11-05 17:15:35 -080018 private final static int NO_MASK_VAL = 0xFFFFFFFF;
19 public final static LagId NO_MASK = new LagId(NO_MASK_VAL);
20 public final static LagId FULL_MASK = NONE;
21
xinwuc1b011d2013-09-18 15:24:04 -070022 private LagId(final int rawValue) {
23 this.rawValue = rawValue;
24 }
25
26 public static LagId of(final int raw) {
Andreas Wundsam14ae8c62013-11-05 17:15:35 -080027 if(raw == NONE_VAL)
28 return NONE;
29 else if (raw == NO_MASK_VAL)
30 return NO_MASK;
xinwuc1b011d2013-09-18 15:24:04 -070031 return new LagId(raw);
32 }
33
34 public int getInt() {
35 return rawValue;
36 }
37
Andreas Wundsam85c961f2013-09-29 21:22:12 -070038 @Override
xinwuc1b011d2013-09-18 15:24:04 -070039 public int getLength() {
40 return LENGTH;
41 }
42
43 @Override
44 public int hashCode() {
45 final int prime = 31;
46 int result = 1;
47 result = prime * result + rawValue;
48 return result;
49 }
50
51 @Override
52 public String toString() {
53 return Integer.toString(rawValue);
54 }
55
56 @Override
57 public boolean equals(final Object obj) {
58 if (this == obj)
59 return true;
60 if (obj == null)
61 return false;
62 if (getClass() != obj.getClass())
63 return false;
64 LagId other = (LagId) obj;
65 if (rawValue != other.rawValue)
66 return false;
67 return true;
68 }
69
xinwuc1b011d2013-09-18 15:24:04 -070070 public void write4Bytes(ChannelBuffer c) {
71 c.writeInt(rawValue);
72 }
73
74 public static LagId read4Bytes(ChannelBuffer c) {
75 return LagId.of(c.readInt());
76 }
Andreas Wundsam85c961f2013-09-29 21:22:12 -070077
78 @Override
79 public int compareTo(LagId o) {
80 return UnsignedInts.compare(rawValue, o.rawValue);
81 }
82
83 @Override
84 public LagId applyMask(LagId mask) {
85 return LagId.of(rawValue & mask.rawValue);
86 }
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070087
88 @Override
89 public void putTo(PrimitiveSink sink) {
90 sink.putInt(rawValue);
91 }
xinwuc1b011d2013-09-18 15:24:04 -070092}