blob: 950087ddb0adb87af412cf918af9910d7d6290ec [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 Wundsam22ba3af2013-10-04 16:00:30 -07006import com.google.common.hash.PrimitiveSink;
Andreas Wundsam85c961f2013-09-29 21:22:12 -07007import com.google.common.primitives.Shorts;
8
9public class TableId implements OFValueType<TableId>, Comparable<TableId> {
Andreas Wundsam37e0fb12013-09-28 18:57:57 -070010
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 Wundsam45c95f82013-10-08 15:04:23 -070018
Andreas Wundsam37e0fb12013-09-28 18:57:57 -070019 public static final TableId ALL = new TableId(ALL_VAL);
Andreas Wundsam45c95f82013-10-08 15:04:23 -070020 public static final TableId ZERO = NONE;
Andreas Wundsam37e0fb12013-09-28 18:57:57 -070021
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 Wundsam37e0fb12013-09-28 18:57:57 -070048 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 Wundsam85c961f2013-09-29 21:22:12 -070074 @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 Wundsam37e0fb12013-09-28 18:57:57 -070094
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070095 @Override
96 public void putTo(PrimitiveSink sink) {
97 sink.putByte((byte) id);
98 }
99
Andreas Wundsam37e0fb12013-09-28 18:57:57 -0700100}