blob: c8e04d2afbefb9e6c0fc404ca0630a895f06dd41 [file] [log] [blame]
Sovietaced92603b02014-01-28 18:23:24 -08001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4import org.projectfloodlight.openflow.exceptions.OFParseError;
5
Sovietaced0f9dddf2014-01-28 19:45:07 -08006import com.google.common.hash.PrimitiveSink;
Sovietaced92603b02014-01-28 18:23:24 -08007import com.google.common.primitives.Shorts;
8
Sovietaced0f9dddf2014-01-28 19:45:07 -08009public class OFAuxId implements Comparable<OFAuxId>, PrimitiveSinkable {
Sovietaced92603b02014-01-28 18:23:24 -080010
11 private static final short VALIDATION_MASK = 0xFF;
12
13 private static final short MAIN_VAL = 0x0000;
14
Sovietaced0f9dddf2014-01-28 19:45:07 -080015 public static final OFAuxId MAIN = new OFAuxId(MAIN_VAL);
Sovietaced92603b02014-01-28 18:23:24 -080016
17 private final short id;
18
Sovietaced0f9dddf2014-01-28 19:45:07 -080019 private OFAuxId(short id) {
Sovietaced92603b02014-01-28 18:23:24 -080020 this.id = id;
21 }
22
Sovietaced0f9dddf2014-01-28 19:45:07 -080023 public static OFAuxId of(short id) {
Sovietaced92603b02014-01-28 18:23:24 -080024 switch(id) {
25 case MAIN_VAL:
26 return MAIN;
27 default:
28 if ((id & VALIDATION_MASK) != id)
29 throw new IllegalArgumentException("Illegal Aux id value: " + id);
Sovietaced0f9dddf2014-01-28 19:45:07 -080030 return new OFAuxId(id);
Sovietaced92603b02014-01-28 18:23:24 -080031 }
32 }
33
Sovietaced0f9dddf2014-01-28 19:45:07 -080034 public static OFAuxId of(int id) {
Sovietaced92603b02014-01-28 18:23:24 -080035 if((id & VALIDATION_MASK) != id)
36 throw new IllegalArgumentException("Illegal Aux id value: "+id);
37 return of((short) id);
38 }
39
40 @Override
41 public String toString() {
42 return "0x" + Integer.toHexString(id);
43 }
44
Sovietacedc4a5f502014-01-29 19:25:42 -080045 @Override
46 public int hashCode() {
47 final int prime = 31;
48 int result = 1;
49 result = prime * result + id;
50 return result;
51 }
52
53 @Override
54 public boolean equals(Object obj) {
55 if (this == obj) return true;
56 if (obj == null) return false;
57 if (getClass() != obj.getClass()) return false;
58 OFAuxId other = (OFAuxId) obj;
59 if (id != other.id) return false;
60 return true;
61 }
62
Sovietaced92603b02014-01-28 18:23:24 -080063 public short getValue() {
64 return id;
65 }
66
67 public void writeByte(ChannelBuffer c) {
68 c.writeByte(this.id);
69 }
70
Sovietaced0f9dddf2014-01-28 19:45:07 -080071 public static OFAuxId readByte(ChannelBuffer c) throws OFParseError {
72 return OFAuxId.of(c.readUnsignedByte());
Sovietaced92603b02014-01-28 18:23:24 -080073 }
74
Sovietaced92603b02014-01-28 18:23:24 -080075
76 @Override
Sovietaced0f9dddf2014-01-28 19:45:07 -080077 public int compareTo(OFAuxId other) {
Sovietaced92603b02014-01-28 18:23:24 -080078 return Shorts.compare(this.id, other.id);
79 }
80
Sovietaced0f9dddf2014-01-28 19:45:07 -080081 @Override
82 public void putTo(PrimitiveSink sink) {
83 sink.putByte((byte) id);
84 }
85
Sovietaced92603b02014-01-28 18:23:24 -080086}