blob: 6466eeea23fef1eb14c35157e5953e2172c7f89e [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4
Andreas Wundsam22ba3af2013-10-04 16:00:30 -07005import com.google.common.hash.PrimitiveSink;
Andreas Wundsam85c961f2013-09-29 21:22:12 -07006import com.google.common.primitives.Shorts;
7
Yotam Harcholf3f11152013-09-05 16:47:16 -07008/**
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -07009 *
Yotam Harcholf3f11152013-09-05 16:47:16 -070010 * @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;
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070019
20 private static final short NONE_VAL = 0;
21 public static final ICMPv4Code NONE = new ICMPv4Code(NONE_VAL);
22
Yotam Harcholf3f11152013-09-05 16:47:16 -070023 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) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070031 if(code == NONE_VAL)
32 return NONE;
33
Yotam Harcholf3f11152013-09-05 16:47:16 -070034 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 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070043
Yotam Harcholf3f11152013-09-05 16:47:16 -070044 public short getCode() {
45 return code;
46 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070047
Yotam Harcholf3f11152013-09-05 16:47:16 -070048 public void writeByte(ChannelBuffer c) {
49 c.writeByte(this.code);
50 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070051
Yotam Harcholf3f11152013-09-05 16:47:16 -070052 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
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070061
Andreas Wundsam85c961f2013-09-29 21:22:12 -070062 @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 }
Andreas Wundsam22ba3af2013-10-04 16:00:30 -070088
89 @Override
90 public void putTo(PrimitiveSink sink) {
91 sink.putShort(code);
92 }
Yotam Harcholf3f11152013-09-05 16:47:16 -070093}