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.UnsignedInts; |
| 7 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 8 | public class ArpOpcode implements OFValueType<ArpOpcode> { |
| 9 | |
| 10 | final static int LENGTH = 2; |
| 11 | |
Andreas Wundsam | 520c880 | 2013-10-08 22:05:23 -0700 | [diff] [blame] | 12 | private static final int VAL_REQUEST = 1; |
| 13 | private static final int VAL_REPLY = 2; |
| 14 | private static final int VAL_REQUEST_REVERSE = 3; |
| 15 | private static final int VAL_REPLY_REVERSE = 4; |
| 16 | private static final int VAL_DRARP_REQUEST = 5; |
| 17 | private static final int VAL_DRARP_REPLY = 6; |
| 18 | private static final int VAL_DRARP_ERROR = 7; |
| 19 | private static final int VAL_INARP_REQUEST = 8; |
| 20 | private static final int VAL_INARP_REPLY = 9; |
| 21 | private static final int VAL_ARP_NAK = 10; |
| 22 | private static final int VAL_MARS_REQUEST = 11; |
| 23 | private static final int VAL_MARS_MULTI = 12; |
| 24 | private static final int VAL_MARS_MSERV = 13; |
| 25 | private static final int VAL_MARS_JOIN = 14; |
| 26 | private static final int VAL_MARS_LEAVE = 15; |
| 27 | private static final int VAL_MARS_NAK = 16; |
| 28 | private static final int VAL_MARS_UNSERV = 17; |
| 29 | private static final int VAL_MARS_SJOIN = 18; |
| 30 | private static final int VAL_MARS_SLEAVE = 19; |
| 31 | private static final int VAL_MARS_GROUPLIST_REQUEST = 20; |
| 32 | private static final int VAL_MARS_GROUPLIST_REPLY = 21; |
| 33 | private static final int VAL_MARS_REDIRECT_MAP = 22; |
| 34 | private static final int VAL_MAPOS_UNARP = 23; |
| 35 | private static final int VAL_OP_EXP1 = 24; |
| 36 | private static final int VAL_OP_EXP2 = 25; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 37 | |
Andreas Wundsam | 520c880 | 2013-10-08 22:05:23 -0700 | [diff] [blame] | 38 | public static final ArpOpcode REQUEST = new ArpOpcode(VAL_REQUEST); |
| 39 | public static final ArpOpcode REPLY = new ArpOpcode(VAL_REPLY); |
| 40 | public static final ArpOpcode REQUEST_REVERSE = new ArpOpcode(VAL_REQUEST_REVERSE); |
| 41 | public static final ArpOpcode REPLY_REVERSE = new ArpOpcode(VAL_REPLY_REVERSE); |
| 42 | public static final ArpOpcode DRARP_REQUEST = new ArpOpcode(VAL_DRARP_REQUEST); |
| 43 | public static final ArpOpcode DRARP_REPLY = new ArpOpcode(VAL_DRARP_REPLY); |
| 44 | public static final ArpOpcode DRARP_ERROR = new ArpOpcode(VAL_DRARP_ERROR); |
| 45 | public static final ArpOpcode INARP_REQUEST = new ArpOpcode(VAL_INARP_REQUEST); |
| 46 | public static final ArpOpcode INARP_REPLY = new ArpOpcode(VAL_INARP_REPLY); |
| 47 | public static final ArpOpcode ARP_NAK = new ArpOpcode(VAL_ARP_NAK); |
| 48 | public static final ArpOpcode MARS_REQUEST = new ArpOpcode(VAL_MARS_REQUEST); |
| 49 | public static final ArpOpcode MARS_MULTI = new ArpOpcode(VAL_MARS_MULTI); |
| 50 | public static final ArpOpcode MARS_MSERV = new ArpOpcode(VAL_MARS_MSERV); |
| 51 | public static final ArpOpcode MARS_JOIN = new ArpOpcode(VAL_MARS_JOIN); |
| 52 | public static final ArpOpcode MARS_LEAVE = new ArpOpcode(VAL_MARS_LEAVE); |
| 53 | public static final ArpOpcode MARS_NAK = new ArpOpcode(VAL_MARS_NAK); |
| 54 | public static final ArpOpcode MARS_UNSERV = new ArpOpcode(VAL_MARS_UNSERV); |
| 55 | public static final ArpOpcode MARS_SJOIN = new ArpOpcode(VAL_MARS_SJOIN); |
| 56 | public static final ArpOpcode MARS_SLEAVE = new ArpOpcode(VAL_MARS_SLEAVE); |
| 57 | public static final ArpOpcode MARS_GROUPLIST_REQUEST = new ArpOpcode(VAL_MARS_GROUPLIST_REQUEST); |
| 58 | public static final ArpOpcode MARS_GROUPLIST_REPLY = new ArpOpcode(VAL_MARS_GROUPLIST_REPLY); |
| 59 | public static final ArpOpcode MARS_REDIRECT_MAP = new ArpOpcode(VAL_MARS_REDIRECT_MAP); |
| 60 | public static final ArpOpcode MAPOS_UNARP = new ArpOpcode(VAL_MAPOS_UNARP); |
| 61 | public static final ArpOpcode OP_EXP1 = new ArpOpcode(VAL_OP_EXP1); |
| 62 | public static final ArpOpcode OP_EXP2 = new ArpOpcode(VAL_OP_EXP2); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 63 | |
| 64 | private static final int MIN_OPCODE = 0; |
| 65 | private static final int MAX_OPCODE = 0xFFFF; |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 66 | |
| 67 | private static final int NONE_VAL = 0; |
| 68 | public static final ArpOpcode NONE = new ArpOpcode(NONE_VAL); |
| 69 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 70 | public static final ArpOpcode NO_MASK = new ArpOpcode(0xFFFFFFFF); |
| 71 | public static final ArpOpcode FULL_MASK = new ArpOpcode(0x00000000); |
| 72 | |
| 73 | private final int opcode; |
| 74 | |
| 75 | private ArpOpcode(int opcode) { |
| 76 | this.opcode = opcode; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public int getLength() { |
| 81 | return LENGTH; |
| 82 | } |
| 83 | |
| 84 | public int getOpcode() { |
| 85 | return this.opcode; |
| 86 | } |
| 87 | |
| 88 | public static ArpOpcode of(int opcode) { |
| 89 | if (opcode < MIN_OPCODE || opcode > MAX_OPCODE) |
| 90 | throw new IllegalArgumentException("Invalid ARP opcode: " + opcode); |
| 91 | switch (opcode) { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 92 | case NONE_VAL: |
| 93 | return NONE; |
Andreas Wundsam | 520c880 | 2013-10-08 22:05:23 -0700 | [diff] [blame] | 94 | case VAL_REQUEST: |
| 95 | return REQUEST; |
| 96 | case VAL_REPLY: |
| 97 | return REPLY; |
| 98 | case VAL_REQUEST_REVERSE: |
| 99 | return REQUEST_REVERSE; |
| 100 | case VAL_REPLY_REVERSE: |
| 101 | return REPLY_REVERSE; |
| 102 | case VAL_DRARP_REQUEST: |
| 103 | return DRARP_REQUEST; |
| 104 | case VAL_DRARP_REPLY: |
| 105 | return DRARP_REPLY; |
| 106 | case VAL_DRARP_ERROR: |
| 107 | return DRARP_ERROR; |
| 108 | case VAL_INARP_REQUEST: |
| 109 | return INARP_REQUEST; |
| 110 | case VAL_INARP_REPLY: |
| 111 | return INARP_REPLY; |
| 112 | case VAL_ARP_NAK: |
| 113 | return ARP_NAK; |
| 114 | case VAL_MARS_REQUEST: |
| 115 | return MARS_REQUEST; |
| 116 | case VAL_MARS_MULTI: |
| 117 | return MARS_MULTI; |
| 118 | case VAL_MARS_MSERV: |
| 119 | return MARS_MSERV; |
| 120 | case VAL_MARS_JOIN: |
| 121 | return MARS_JOIN; |
| 122 | case VAL_MARS_LEAVE: |
| 123 | return MARS_LEAVE; |
| 124 | case VAL_MARS_NAK: |
| 125 | return MARS_NAK; |
| 126 | case VAL_MARS_UNSERV: |
| 127 | return MARS_UNSERV; |
| 128 | case VAL_MARS_SJOIN: |
| 129 | return MARS_SJOIN; |
| 130 | case VAL_MARS_SLEAVE: |
| 131 | return MARS_SLEAVE; |
| 132 | case VAL_MARS_GROUPLIST_REQUEST: |
| 133 | return MARS_GROUPLIST_REQUEST; |
| 134 | case VAL_MARS_GROUPLIST_REPLY: |
| 135 | return MARS_GROUPLIST_REPLY; |
| 136 | case VAL_MARS_REDIRECT_MAP: |
| 137 | return MARS_REDIRECT_MAP; |
| 138 | case VAL_MAPOS_UNARP: |
| 139 | return MAPOS_UNARP; |
| 140 | case VAL_OP_EXP1: |
| 141 | return OP_EXP1; |
| 142 | case VAL_OP_EXP2: |
| 143 | return OP_EXP2; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 144 | default: |
| 145 | return new ArpOpcode(opcode); |
| 146 | } |
| 147 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 148 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 149 | public void write2Bytes(ChannelBuffer c) { |
| 150 | c.writeShort(this.opcode); |
| 151 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 152 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 153 | public static ArpOpcode read2Bytes(ChannelBuffer c) { |
| 154 | return ArpOpcode.of(c.readUnsignedShort()); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public ArpOpcode applyMask(ArpOpcode mask) { |
| 159 | return ArpOpcode.of(this.opcode & mask.opcode); |
| 160 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 161 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 162 | @Override |
| 163 | public int hashCode() { |
| 164 | final int prime = 31; |
| 165 | int result = 1; |
| 166 | result = prime * result + opcode; |
| 167 | return result; |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public boolean equals(Object obj) { |
| 172 | if (this == obj) |
| 173 | return true; |
| 174 | if (obj == null) |
| 175 | return false; |
| 176 | if (getClass() != obj.getClass()) |
| 177 | return false; |
| 178 | ArpOpcode other = (ArpOpcode) obj; |
| 179 | if (opcode != other.opcode) |
| 180 | return false; |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public int compareTo(ArpOpcode o) { |
| 186 | return UnsignedInts.compare(opcode, o.opcode); |
| 187 | } |
| 188 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 189 | @Override |
| 190 | public void putTo(PrimitiveSink sink) { |
| 191 | sink.putShort((short) this.opcode); |
| 192 | } |
| 193 | |
Byungjoon Lee | e539689 | 2014-03-14 15:00:53 +0900 | [diff] [blame] | 194 | @Override |
| 195 | public String toString() { |
| 196 | return String.valueOf(this.opcode); |
| 197 | } |
| 198 | } |