xinwu | c1b011d | 2013-09-18 15:24:04 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
xinwu | 3203443 | 2013-09-18 17:17:50 -0700 | [diff] [blame] | 3 | import javax.annotation.concurrent.Immutable; |
xinwu | c1b011d | 2013-09-18 15:24:04 -0700 | [diff] [blame] | 4 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 5 | import org.jboss.netty.buffer.ChannelBuffer; |
| 6 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 7 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 8 | import com.google.common.primitives.UnsignedInts; |
| 9 | |
xinwu | c1b011d | 2013-09-18 15:24:04 -0700 | [diff] [blame] | 10 | @Immutable |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 11 | public class LagId implements OFValueType<LagId> { |
xinwu | c1b011d | 2013-09-18 15:24:04 -0700 | [diff] [blame] | 12 | static final int LENGTH = 4; |
| 13 | private final int rawValue; |
| 14 | |
Rich Lane | 376cafe | 2013-10-27 21:49:14 -0700 | [diff] [blame] | 15 | private final static int NONE_VAL = 0; |
| 16 | public final static LagId NONE = new LagId(NONE_VAL); |
| 17 | |
Andreas Wundsam | 14ae8c6 | 2013-11-05 17:15:35 -0800 | [diff] [blame] | 18 | 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 | |
xinwu | c1b011d | 2013-09-18 15:24:04 -0700 | [diff] [blame] | 22 | private LagId(final int rawValue) { |
| 23 | this.rawValue = rawValue; |
| 24 | } |
| 25 | |
| 26 | public static LagId of(final int raw) { |
Andreas Wundsam | 14ae8c6 | 2013-11-05 17:15:35 -0800 | [diff] [blame] | 27 | if(raw == NONE_VAL) |
| 28 | return NONE; |
| 29 | else if (raw == NO_MASK_VAL) |
| 30 | return NO_MASK; |
xinwu | c1b011d | 2013-09-18 15:24:04 -0700 | [diff] [blame] | 31 | return new LagId(raw); |
| 32 | } |
| 33 | |
| 34 | public int getInt() { |
| 35 | return rawValue; |
| 36 | } |
| 37 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 38 | @Override |
xinwu | c1b011d | 2013-09-18 15:24:04 -0700 | [diff] [blame] | 39 | 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 | |
xinwu | c1b011d | 2013-09-18 15:24:04 -0700 | [diff] [blame] | 70 | 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 Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 77 | |
| 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 Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 87 | |
| 88 | @Override |
| 89 | public void putTo(PrimitiveSink sink) { |
| 90 | sink.putInt(rawValue); |
| 91 | } |
xinwu | c1b011d | 2013-09-18 15:24:04 -0700 | [diff] [blame] | 92 | } |