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