blob: ced5737534630ddf38e2b87a948fc3cd6e03c169 [file] [log] [blame]
tom0eb04ca2014-08-25 14:34:51 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4
5import com.google.common.hash.PrimitiveSink;
6import com.google.common.primitives.Shorts;
7
8/**
9 *
10 * @author Yotam Harchol (yotam.harchol@bigswitch.com)
11 *
12 */
13public class ICMPv4Code implements OFValueType<ICMPv4Code> {
14
15 final static int LENGTH = 1;
16 final static short MAX_CODE = 0xFF;
17
18 private final short code;
19
20 private static final short NONE_VAL = 0;
21 public static final ICMPv4Code NONE = new ICMPv4Code(NONE_VAL);
22
23 public static final ICMPv4Code NO_MASK = new ICMPv4Code((short)0xFFFF);
24 public static final ICMPv4Code FULL_MASK = new ICMPv4Code((short)0x0000);
25
26 private ICMPv4Code(short code) {
27 this.code = code;
28 }
29
30 public static ICMPv4Code of(short code) {
31 if(code == NONE_VAL)
32 return NONE;
33
34 if (code > MAX_CODE || code < 0)
35 throw new IllegalArgumentException("Illegal ICMPv4 code: " + code);
36 return new ICMPv4Code(code);
37 }
38
39 @Override
40 public int getLength() {
41 return LENGTH;
42 }
43
44 public short getCode() {
45 return code;
46 }
47
48 public void writeByte(ChannelBuffer c) {
49 c.writeByte(this.code);
50 }
51
52 public static ICMPv4Code readByte(ChannelBuffer c) {
53 return ICMPv4Code.of(c.readUnsignedByte());
54 }
55
56 @Override
57 public ICMPv4Code applyMask(ICMPv4Code mask) {
58 return ICMPv4Code.of((short)(this.code & mask.code));
59 }
60
61
62 @Override
63 public int hashCode() {
64 final int prime = 31;
65 int result = 1;
66 result = prime * result + code;
67 return result;
68 }
69
70 @Override
71 public boolean equals(Object obj) {
72 if (this == obj)
73 return true;
74 if (obj == null)
75 return false;
76 if (getClass() != obj.getClass())
77 return false;
78 ICMPv4Code other = (ICMPv4Code) obj;
79 if (code != other.code)
80 return false;
81 return true;
82 }
83
84 @Override
85 public int compareTo(ICMPv4Code o) {
86 return Shorts.compare(code, o.code);
87 }
88
89 @Override
90 public void putTo(PrimitiveSink sink) {
91 sink.putShort(code);
92 }
93
94 @Override
95 public String toString() {
96 return String.valueOf(this.code);
97 }
98}