Sovietaced | 92603b0 | 2014-01-28 18:23:24 -0800 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import org.jboss.netty.buffer.ChannelBuffer; |
| 4 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 5 | |
Sovietaced | 0f9dddf | 2014-01-28 19:45:07 -0800 | [diff] [blame] | 6 | import com.google.common.hash.PrimitiveSink; |
Sovietaced | 92603b0 | 2014-01-28 18:23:24 -0800 | [diff] [blame] | 7 | import com.google.common.primitives.Shorts; |
| 8 | |
Sovietaced | 0f9dddf | 2014-01-28 19:45:07 -0800 | [diff] [blame] | 9 | public class OFAuxId implements Comparable<OFAuxId>, PrimitiveSinkable { |
Sovietaced | 92603b0 | 2014-01-28 18:23:24 -0800 | [diff] [blame] | 10 | |
| 11 | private static final short VALIDATION_MASK = 0xFF; |
| 12 | |
| 13 | private static final short MAIN_VAL = 0x0000; |
| 14 | |
Sovietaced | 0f9dddf | 2014-01-28 19:45:07 -0800 | [diff] [blame] | 15 | public static final OFAuxId MAIN = new OFAuxId(MAIN_VAL); |
Sovietaced | 92603b0 | 2014-01-28 18:23:24 -0800 | [diff] [blame] | 16 | |
| 17 | private final short id; |
| 18 | |
Sovietaced | 0f9dddf | 2014-01-28 19:45:07 -0800 | [diff] [blame] | 19 | private OFAuxId(short id) { |
Sovietaced | 92603b0 | 2014-01-28 18:23:24 -0800 | [diff] [blame] | 20 | this.id = id; |
| 21 | } |
| 22 | |
Sovietaced | 0f9dddf | 2014-01-28 19:45:07 -0800 | [diff] [blame] | 23 | public static OFAuxId of(short id) { |
Sovietaced | 92603b0 | 2014-01-28 18:23:24 -0800 | [diff] [blame] | 24 | 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); |
Sovietaced | 0f9dddf | 2014-01-28 19:45:07 -0800 | [diff] [blame] | 30 | return new OFAuxId(id); |
Sovietaced | 92603b0 | 2014-01-28 18:23:24 -0800 | [diff] [blame] | 31 | } |
| 32 | } |
| 33 | |
Sovietaced | 0f9dddf | 2014-01-28 19:45:07 -0800 | [diff] [blame] | 34 | public static OFAuxId of(int id) { |
Sovietaced | 92603b0 | 2014-01-28 18:23:24 -0800 | [diff] [blame] | 35 | 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 | |
Sovietaced | c4a5f50 | 2014-01-29 19:25:42 -0800 | [diff] [blame] | 45 | @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 | |
Sovietaced | 92603b0 | 2014-01-28 18:23:24 -0800 | [diff] [blame] | 63 | public short getValue() { |
| 64 | return id; |
| 65 | } |
| 66 | |
| 67 | public void writeByte(ChannelBuffer c) { |
| 68 | c.writeByte(this.id); |
| 69 | } |
| 70 | |
Sovietaced | 0f9dddf | 2014-01-28 19:45:07 -0800 | [diff] [blame] | 71 | public static OFAuxId readByte(ChannelBuffer c) throws OFParseError { |
| 72 | return OFAuxId.of(c.readUnsignedByte()); |
Sovietaced | 92603b0 | 2014-01-28 18:23:24 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Sovietaced | 92603b0 | 2014-01-28 18:23:24 -0800 | [diff] [blame] | 75 | |
| 76 | @Override |
Sovietaced | 0f9dddf | 2014-01-28 19:45:07 -0800 | [diff] [blame] | 77 | public int compareTo(OFAuxId other) { |
Sovietaced | 92603b0 | 2014-01-28 18:23:24 -0800 | [diff] [blame] | 78 | return Shorts.compare(this.id, other.id); |
| 79 | } |
| 80 | |
Sovietaced | 0f9dddf | 2014-01-28 19:45:07 -0800 | [diff] [blame] | 81 | @Override |
| 82 | public void putTo(PrimitiveSink sink) { |
| 83 | sink.putByte((byte) id); |
| 84 | } |
| 85 | |
Sovietaced | 92603b0 | 2014-01-28 18:23:24 -0800 | [diff] [blame] | 86 | } |