blob: 698bc8e686cf2e236f77f71f2189c341c9d43710 [file] [log] [blame]
Andreas Wundsam37e0fb12013-09-28 18:57:57 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4import org.projectfloodlight.openflow.exceptions.OFParseError;
5
Andreas Wundsam85c961f2013-09-29 21:22:12 -07006import com.google.common.primitives.Shorts;
7
8public class TableId implements OFValueType<TableId>, Comparable<TableId> {
Andreas Wundsam37e0fb12013-09-28 18:57:57 -07009
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 Wundsam45c95f82013-10-08 15:04:23 -070017
Andreas Wundsam37e0fb12013-09-28 18:57:57 -070018 public static final TableId ALL = new TableId(ALL_VAL);
Andreas Wundsam45c95f82013-10-08 15:04:23 -070019 public static final TableId ZERO = NONE;
Andreas Wundsam37e0fb12013-09-28 18:57:57 -070020
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 Wundsam37e0fb12013-09-28 18:57:57 -070047 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 Wundsam85c961f2013-09-29 21:22:12 -070073 @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 Wundsam37e0fb12013-09-28 18:57:57 -070093
94}