blob: 950087ddb0adb87af412cf918af9910d7d6290ec [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4import org.projectfloodlight.openflow.exceptions.OFParseError;
5
6import com.google.common.hash.PrimitiveSink;
7import com.google.common.primitives.Shorts;
8
9public class TableId implements OFValueType<TableId>, Comparable<TableId> {
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);
18
19 public static final TableId ALL = new TableId(ALL_VAL);
20 public static final TableId ZERO = NONE;
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
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
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 }
94
95 @Override
96 public void putTo(PrimitiveSink sink) {
97 sink.putByte((byte) id);
98 }
99
100}