blob: ebb196690744688ba35b8e2454c8bf99bbbfdd02 [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);
17 public static final TableId ALL = new TableId(ALL_VAL);
18
19 private final short id;
20
21 private TableId(short id) {
22 this.id = id;
23 }
24
25 public static TableId of(short id) {
26 switch(id) {
27 case NONE_VAL:
28 return NONE;
29 case ALL_VAL:
30 return ALL;
31 default:
32 if ((id & VALIDATION_MASK) != id)
33 throw new IllegalArgumentException("Illegal Table id value: " + id);
34 return new TableId(id);
35 }
36 }
37
38 public static TableId of(int id) {
39 if((id & VALIDATION_MASK) != id)
40 throw new IllegalArgumentException("Illegal Table id value: "+id);
41 return of((short) id);
42 }
43
44 @Override
Andreas Wundsam37e0fb12013-09-28 18:57:57 -070045 public String toString() {
46 return "0x" + Integer.toHexString(id);
47 }
48
49 public short getValue() {
50 return id;
51 }
52
53 @Override
54 public int getLength() {
55 return LENGTH;
56 }
57
58 public void writeByte(ChannelBuffer c) {
59 c.writeByte(this.id);
60 }
61
62 public static TableId readByte(ChannelBuffer c) throws OFParseError {
63 return TableId.of(c.readUnsignedByte());
64 }
65
66 @Override
67 public TableId applyMask(TableId mask) {
68 return TableId.of((short)(this.id & mask.id));
69 }
70
Andreas Wundsam85c961f2013-09-29 21:22:12 -070071 @Override
72 public boolean equals(Object obj) {
73 if (!(obj instanceof TableId))
74 return false;
75 TableId other = (TableId)obj;
76 if (other.id != this.id)
77 return false;
78 return true;
79 }
80
81 @Override
82 public int hashCode() {
83 int prime = 13873;
84 return this.id * prime;
85 }
86
87 @Override
88 public int compareTo(TableId other) {
89 return Shorts.compare(this.id, other.id);
90 }
Andreas Wundsam37e0fb12013-09-28 18:57:57 -070091
92}