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 | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 5 | import com.google.common.hash.PrimitiveSink; |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 6 | import com.google.common.primitives.Shorts; |
| 7 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 8 | /** |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 9 | * |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 10 | * @author Yotam Harchol (yotam.harchol@bigswitch.com) |
| 11 | * |
| 12 | */ |
| 13 | public 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 Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 19 | |
| 20 | private static final short NONE_VAL = 0; |
| 21 | public static final ICMPv4Code NONE = new ICMPv4Code(NONE_VAL); |
| 22 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 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) { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 31 | if(code == NONE_VAL) |
| 32 | return NONE; |
| 33 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 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 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 43 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 44 | public short getCode() { |
| 45 | return code; |
| 46 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 47 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 48 | public void writeByte(ChannelBuffer c) { |
| 49 | c.writeByte(this.code); |
| 50 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 51 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 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 | |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 61 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 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 | } |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 88 | |
| 89 | @Override |
| 90 | public void putTo(PrimitiveSink sink) { |
| 91 | sink.putShort(code); |
| 92 | } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 93 | } |