Andreas Wundsam | 37e0fb1 | 2013-09-28 18:57: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 | |
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 | |
| 9 | public class TableId implements OFValueType<TableId>, Comparable<TableId> { |
Andreas Wundsam | 37e0fb1 | 2013-09-28 18:57:57 -0700 | [diff] [blame] | 10 | |
| 11 | final static int LENGTH = 1; |
| 12 | |
| 13 | private static final short VALIDATION_MASK = 0x00FF; |
| 14 | |
| 15 | private static final short ALL_VAL = 0x00FF; |
| 16 | private static final short NONE_VAL = 0x0000; |
| 17 | public static final TableId NONE = new TableId(NONE_VAL); |
Andreas Wundsam | 45c95f8 | 2013-10-08 15:04:23 -0700 | [diff] [blame] | 18 | |
Andreas Wundsam | 37e0fb1 | 2013-09-28 18:57:57 -0700 | [diff] [blame] | 19 | public static final TableId ALL = new TableId(ALL_VAL); |
Andreas Wundsam | 45c95f8 | 2013-10-08 15:04:23 -0700 | [diff] [blame] | 20 | public static final TableId ZERO = NONE; |
Andreas Wundsam | 37e0fb1 | 2013-09-28 18:57:57 -0700 | [diff] [blame] | 21 | |
| 22 | private final short id; |
| 23 | |
| 24 | private TableId(short id) { |
| 25 | this.id = id; |
| 26 | } |
| 27 | |
| 28 | public static TableId of(short id) { |
| 29 | switch(id) { |
| 30 | case NONE_VAL: |
| 31 | return NONE; |
| 32 | case ALL_VAL: |
| 33 | return ALL; |
| 34 | default: |
| 35 | if ((id & VALIDATION_MASK) != id) |
| 36 | throw new IllegalArgumentException("Illegal Table id value: " + id); |
| 37 | return new TableId(id); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | public static TableId of(int id) { |
| 42 | if((id & VALIDATION_MASK) != id) |
| 43 | throw new IllegalArgumentException("Illegal Table id value: "+id); |
| 44 | return of((short) id); |
| 45 | } |
| 46 | |
| 47 | @Override |
Andreas Wundsam | 37e0fb1 | 2013-09-28 18:57:57 -0700 | [diff] [blame] | 48 | public String toString() { |
| 49 | return "0x" + Integer.toHexString(id); |
| 50 | } |
| 51 | |
| 52 | public short getValue() { |
| 53 | return id; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public int getLength() { |
| 58 | return LENGTH; |
| 59 | } |
| 60 | |
| 61 | public void writeByte(ChannelBuffer c) { |
| 62 | c.writeByte(this.id); |
| 63 | } |
| 64 | |
| 65 | public static TableId readByte(ChannelBuffer c) throws OFParseError { |
| 66 | return TableId.of(c.readUnsignedByte()); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public TableId applyMask(TableId mask) { |
| 71 | return TableId.of((short)(this.id & mask.id)); |
| 72 | } |
| 73 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 74 | @Override |
| 75 | public boolean equals(Object obj) { |
| 76 | if (!(obj instanceof TableId)) |
| 77 | return false; |
| 78 | TableId other = (TableId)obj; |
| 79 | if (other.id != this.id) |
| 80 | return false; |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int hashCode() { |
| 86 | int prime = 13873; |
| 87 | return this.id * prime; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public int compareTo(TableId other) { |
| 92 | return Shorts.compare(this.id, other.id); |
| 93 | } |
Andreas Wundsam | 37e0fb1 | 2013-09-28 18:57:57 -0700 | [diff] [blame] | 94 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 95 | @Override |
| 96 | public void putTo(PrimitiveSink sink) { |
| 97 | sink.putByte((byte) id); |
| 98 | } |
| 99 | |
Andreas Wundsam | 37e0fb1 | 2013-09-28 18:57:57 -0700 | [diff] [blame] | 100 | } |