blob: c66486f3b919c1db3b88167542aaf3a3fec70103 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4
Andreas Wundsam85c961f2013-09-29 21:22:12 -07005import com.google.common.primitives.Shorts;
6
Yotam Harcholf3f11152013-09-05 16:47:16 -07007/**
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -07008 *
Yotam Harcholf3f11152013-09-05 16:47:16 -07009 * @author Yotam Harchol (yotam.harchol@bigswitch.com)
10 *
11 */
12public class ICMPv4Code implements OFValueType<ICMPv4Code> {
13
14 final static int LENGTH = 1;
15 final static short MAX_CODE = 0xFF;
16
17 private final short code;
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070018
19 private static final short NONE_VAL = 0;
20 public static final ICMPv4Code NONE = new ICMPv4Code(NONE_VAL);
21
Yotam Harcholf3f11152013-09-05 16:47:16 -070022 public static final ICMPv4Code NO_MASK = new ICMPv4Code((short)0xFFFF);
23 public static final ICMPv4Code FULL_MASK = new ICMPv4Code((short)0x0000);
24
25 private ICMPv4Code(short code) {
26 this.code = code;
27 }
28
29 public static ICMPv4Code of(short code) {
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070030 if(code == NONE_VAL)
31 return NONE;
32
Yotam Harcholf3f11152013-09-05 16:47:16 -070033 if (code > MAX_CODE || code < 0)
34 throw new IllegalArgumentException("Illegal ICMPv4 code: " + code);
35 return new ICMPv4Code(code);
36 }
37
38 @Override
39 public int getLength() {
40 return LENGTH;
41 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070042
Yotam Harcholf3f11152013-09-05 16:47:16 -070043 public short getCode() {
44 return code;
45 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070046
Yotam Harcholf3f11152013-09-05 16:47:16 -070047 public void writeByte(ChannelBuffer c) {
48 c.writeByte(this.code);
49 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070050
Yotam Harcholf3f11152013-09-05 16:47:16 -070051 public static ICMPv4Code readByte(ChannelBuffer c) {
52 return ICMPv4Code.of(c.readUnsignedByte());
53 }
54
55 @Override
56 public ICMPv4Code applyMask(ICMPv4Code mask) {
57 return ICMPv4Code.of((short)(this.code & mask.code));
58 }
59
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -070060
Andreas Wundsam85c961f2013-09-29 21:22:12 -070061 @Override
62 public int hashCode() {
63 final int prime = 31;
64 int result = 1;
65 result = prime * result + code;
66 return result;
67 }
68
69 @Override
70 public boolean equals(Object obj) {
71 if (this == obj)
72 return true;
73 if (obj == null)
74 return false;
75 if (getClass() != obj.getClass())
76 return false;
77 ICMPv4Code other = (ICMPv4Code) obj;
78 if (code != other.code)
79 return false;
80 return true;
81 }
82
83 @Override
84 public int compareTo(ICMPv4Code o) {
85 return Shorts.compare(code, o.code);
86 }
Yotam Harcholf3f11152013-09-05 16:47:16 -070087}