alshabib | 1f44e8e | 2014-08-14 15:19:57 -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 | |
| 6 | import com.google.common.hash.PrimitiveSink; |
| 7 | import com.google.common.primitives.UnsignedInts; |
| 8 | |
| 9 | public class GenTableId implements OFValueType<GenTableId>, Comparable<GenTableId> { |
| 10 | final static int LENGTH = 2; |
| 11 | |
| 12 | private static final int VALIDATION_MASK = 0xFFFF; |
| 13 | |
| 14 | private static final int ALL_VAL = 0xFFFF; |
| 15 | private static final int NONE_VAL = 0x0000; |
| 16 | public static final GenTableId NONE = new GenTableId(NONE_VAL); |
| 17 | |
| 18 | public static final GenTableId ALL = new GenTableId(ALL_VAL); |
| 19 | public static final GenTableId ZERO = NONE; |
| 20 | |
| 21 | private final int id; |
| 22 | |
| 23 | private GenTableId(int id) { |
| 24 | this.id = id; |
| 25 | } |
| 26 | |
| 27 | public static GenTableId of(int id) { |
| 28 | switch(id) { |
| 29 | case NONE_VAL: |
| 30 | return NONE; |
| 31 | case ALL_VAL: |
| 32 | return ALL; |
| 33 | default: |
| 34 | if ((id & VALIDATION_MASK) != id) |
| 35 | throw new IllegalArgumentException("Illegal Table id value: " + id); |
| 36 | return new GenTableId(id); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public String toString() { |
| 42 | return "0x" + Integer.toHexString(id); |
| 43 | } |
| 44 | |
| 45 | public int getValue() { |
| 46 | return id; |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public int getLength() { |
| 51 | return LENGTH; |
| 52 | } |
| 53 | |
| 54 | public void write2Bytes(ChannelBuffer c) { |
| 55 | c.writeShort(this.id); |
| 56 | } |
| 57 | |
| 58 | public static GenTableId read2Bytes(ChannelBuffer c) throws OFParseError { |
| 59 | return GenTableId.of(c.readUnsignedShort()); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public GenTableId applyMask(GenTableId mask) { |
| 64 | return GenTableId.of(this.id & mask.id); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public boolean equals(Object obj) { |
| 69 | if (!(obj instanceof GenTableId)) |
| 70 | return false; |
| 71 | GenTableId other = (GenTableId)obj; |
| 72 | if (other.id != this.id) |
| 73 | return false; |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public int hashCode() { |
| 79 | int prime = 13873; |
| 80 | return this.id * prime; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public int compareTo(GenTableId other) { |
| 85 | return UnsignedInts.compare(this.id, other.id); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void putTo(PrimitiveSink sink) { |
| 90 | sink.putShort((byte) id); |
| 91 | } |
| 92 | |
| 93 | } |