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