Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import org.jboss.netty.buffer.ChannelBuffer; |
| 4 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 5 | import com.google.common.primitives.Shorts; |
| 6 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 7 | /** |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 8 | * |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 9 | * @author Yotam Harchol (yotam.harchol@bigswitch.com) |
| 10 | * |
| 11 | */ |
| 12 | public 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 Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 18 | |
| 19 | private static final short NONE_VAL = 0; |
| 20 | public static final ICMPv4Code NONE = new ICMPv4Code(NONE_VAL); |
| 21 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 22 | 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 Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 30 | if(code == NONE_VAL) |
| 31 | return NONE; |
| 32 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 33 | 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 Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 42 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 43 | public short getCode() { |
| 44 | return code; |
| 45 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 46 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 47 | public void writeByte(ChannelBuffer c) { |
| 48 | c.writeByte(this.code); |
| 49 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 50 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 51 | 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 Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 60 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 61 | @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 Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 87 | } |