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.UnsignedInts; |
| 6 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 7 | public class ArpOpcode implements OFValueType<ArpOpcode> { |
| 8 | |
| 9 | final static int LENGTH = 2; |
| 10 | |
| 11 | private static final int ARP_OPCODE_VAL_REQUEST = 1; |
| 12 | private static final int ARP_OPCODE_VAL_REPLY = 2; |
| 13 | private static final int ARP_OPCODE_VAL_REQUEST_REVERSE = 3; |
| 14 | private static final int ARP_OPCODE_VAL_REPLY_REVERSE = 4; |
| 15 | private static final int ARP_OPCODE_VAL_DRARP_REQUEST = 5; |
| 16 | private static final int ARP_OPCODE_VAL_DRARP_REPLY = 6; |
| 17 | private static final int ARP_OPCODE_VAL_DRARP_ERROR = 7; |
| 18 | private static final int ARP_OPCODE_VAL_INARP_REQUEST = 8; |
| 19 | private static final int ARP_OPCODE_VAL_INARP_REPLY = 9; |
| 20 | private static final int ARP_OPCODE_VAL_ARP_NAK = 10; |
| 21 | private static final int ARP_OPCODE_VAL_MARS_REQUEST = 11; |
| 22 | private static final int ARP_OPCODE_VAL_MARS_MULTI = 12; |
| 23 | private static final int ARP_OPCODE_VAL_MARS_MSERV = 13; |
| 24 | private static final int ARP_OPCODE_VAL_MARS_JOIN = 14; |
| 25 | private static final int ARP_OPCODE_VAL_MARS_LEAVE = 15; |
| 26 | private static final int ARP_OPCODE_VAL_MARS_NAK = 16; |
| 27 | private static final int ARP_OPCODE_VAL_MARS_UNSERV = 17; |
| 28 | private static final int ARP_OPCODE_VAL_MARS_SJOIN = 18; |
| 29 | private static final int ARP_OPCODE_VAL_MARS_SLEAVE = 19; |
| 30 | private static final int ARP_OPCODE_VAL_MARS_GROUPLIST_REQUEST = 20; |
| 31 | private static final int ARP_OPCODE_VAL_MARS_GROUPLIST_REPLY = 21; |
| 32 | private static final int ARP_OPCODE_VAL_MARS_REDIRECT_MAP = 22; |
| 33 | private static final int ARP_OPCODE_VAL_MAPOS_UNARP = 23; |
| 34 | private static final int ARP_OPCODE_VAL_OP_EXP1 = 24; |
| 35 | private static final int ARP_OPCODE_VAL_OP_EXP2 = 25; |
| 36 | |
| 37 | public static final ArpOpcode ARP_OPCODE_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_REQUEST); |
| 38 | public static final ArpOpcode ARP_OPCODE_REPLY = new ArpOpcode(ARP_OPCODE_VAL_REPLY); |
| 39 | public static final ArpOpcode ARP_OPCODE_REQUEST_REVERSE = new ArpOpcode(ARP_OPCODE_VAL_REQUEST_REVERSE); |
| 40 | public static final ArpOpcode ARP_OPCODE_REPLY_REVERSE = new ArpOpcode(ARP_OPCODE_VAL_REPLY_REVERSE); |
| 41 | public static final ArpOpcode ARP_OPCODE_DRARP_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_DRARP_REQUEST); |
| 42 | public static final ArpOpcode ARP_OPCODE_DRARP_REPLY = new ArpOpcode(ARP_OPCODE_VAL_DRARP_REPLY); |
| 43 | public static final ArpOpcode ARP_OPCODE_DRARP_ERROR = new ArpOpcode(ARP_OPCODE_VAL_DRARP_ERROR); |
| 44 | public static final ArpOpcode ARP_OPCODE_INARP_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_INARP_REQUEST); |
| 45 | public static final ArpOpcode ARP_OPCODE_INARP_REPLY = new ArpOpcode(ARP_OPCODE_VAL_INARP_REPLY); |
| 46 | public static final ArpOpcode ARP_OPCODE_ARP_NAK = new ArpOpcode(ARP_OPCODE_VAL_ARP_NAK); |
| 47 | public static final ArpOpcode ARP_OPCODE_MARS_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_MARS_REQUEST); |
| 48 | public static final ArpOpcode ARP_OPCODE_MARS_MULTI = new ArpOpcode(ARP_OPCODE_VAL_MARS_MULTI); |
| 49 | public static final ArpOpcode ARP_OPCODE_MARS_MSERV = new ArpOpcode(ARP_OPCODE_VAL_MARS_MSERV); |
| 50 | public static final ArpOpcode ARP_OPCODE_MARS_JOIN = new ArpOpcode(ARP_OPCODE_VAL_MARS_JOIN); |
| 51 | public static final ArpOpcode ARP_OPCODE_MARS_LEAVE = new ArpOpcode(ARP_OPCODE_VAL_MARS_LEAVE); |
| 52 | public static final ArpOpcode ARP_OPCODE_MARS_NAK = new ArpOpcode(ARP_OPCODE_VAL_MARS_NAK); |
| 53 | public static final ArpOpcode ARP_OPCODE_MARS_UNSERV = new ArpOpcode(ARP_OPCODE_VAL_MARS_UNSERV); |
| 54 | public static final ArpOpcode ARP_OPCODE_MARS_SJOIN = new ArpOpcode(ARP_OPCODE_VAL_MARS_SJOIN); |
| 55 | public static final ArpOpcode ARP_OPCODE_MARS_SLEAVE = new ArpOpcode(ARP_OPCODE_VAL_MARS_SLEAVE); |
| 56 | public static final ArpOpcode ARP_OPCODE_MARS_GROUPLIST_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_MARS_GROUPLIST_REQUEST); |
| 57 | public static final ArpOpcode ARP_OPCODE_MARS_GROUPLIST_REPLY = new ArpOpcode(ARP_OPCODE_VAL_MARS_GROUPLIST_REPLY); |
| 58 | public static final ArpOpcode ARP_OPCODE_MARS_REDIRECT_MAP = new ArpOpcode(ARP_OPCODE_VAL_MARS_REDIRECT_MAP); |
| 59 | public static final ArpOpcode ARP_OPCODE_MAPOS_UNARP = new ArpOpcode(ARP_OPCODE_VAL_MAPOS_UNARP); |
| 60 | public static final ArpOpcode ARP_OPCODE_OP_EXP1 = new ArpOpcode(ARP_OPCODE_VAL_OP_EXP1); |
| 61 | public static final ArpOpcode ARP_OPCODE_OP_EXP2 = new ArpOpcode(ARP_OPCODE_VAL_OP_EXP2); |
| 62 | |
| 63 | private static final int MIN_OPCODE = 0; |
| 64 | private static final int MAX_OPCODE = 0xFFFF; |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 65 | |
| 66 | private static final int NONE_VAL = 0; |
| 67 | public static final ArpOpcode NONE = new ArpOpcode(NONE_VAL); |
| 68 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 69 | public static final ArpOpcode NO_MASK = new ArpOpcode(0xFFFFFFFF); |
| 70 | public static final ArpOpcode FULL_MASK = new ArpOpcode(0x00000000); |
| 71 | |
| 72 | private final int opcode; |
| 73 | |
| 74 | private ArpOpcode(int opcode) { |
| 75 | this.opcode = opcode; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public int getLength() { |
| 80 | return LENGTH; |
| 81 | } |
| 82 | |
| 83 | public int getOpcode() { |
| 84 | return this.opcode; |
| 85 | } |
| 86 | |
| 87 | public static ArpOpcode of(int opcode) { |
| 88 | if (opcode < MIN_OPCODE || opcode > MAX_OPCODE) |
| 89 | throw new IllegalArgumentException("Invalid ARP opcode: " + opcode); |
| 90 | switch (opcode) { |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 91 | case NONE_VAL: |
| 92 | return NONE; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 93 | case ARP_OPCODE_VAL_REQUEST: |
| 94 | return ARP_OPCODE_REQUEST; |
| 95 | case ARP_OPCODE_VAL_REPLY: |
| 96 | return ARP_OPCODE_REPLY; |
| 97 | case ARP_OPCODE_VAL_REQUEST_REVERSE: |
| 98 | return ARP_OPCODE_REQUEST_REVERSE; |
| 99 | case ARP_OPCODE_VAL_REPLY_REVERSE: |
| 100 | return ARP_OPCODE_REPLY_REVERSE; |
| 101 | case ARP_OPCODE_VAL_DRARP_REQUEST: |
| 102 | return ARP_OPCODE_DRARP_REQUEST; |
| 103 | case ARP_OPCODE_VAL_DRARP_REPLY: |
| 104 | return ARP_OPCODE_DRARP_REPLY; |
| 105 | case ARP_OPCODE_VAL_DRARP_ERROR: |
| 106 | return ARP_OPCODE_DRARP_ERROR; |
| 107 | case ARP_OPCODE_VAL_INARP_REQUEST: |
| 108 | return ARP_OPCODE_INARP_REQUEST; |
| 109 | case ARP_OPCODE_VAL_INARP_REPLY: |
| 110 | return ARP_OPCODE_INARP_REPLY; |
| 111 | case ARP_OPCODE_VAL_ARP_NAK: |
| 112 | return ARP_OPCODE_ARP_NAK; |
| 113 | case ARP_OPCODE_VAL_MARS_REQUEST: |
| 114 | return ARP_OPCODE_MARS_REQUEST; |
| 115 | case ARP_OPCODE_VAL_MARS_MULTI: |
| 116 | return ARP_OPCODE_MARS_MULTI; |
| 117 | case ARP_OPCODE_VAL_MARS_MSERV: |
| 118 | return ARP_OPCODE_MARS_MSERV; |
| 119 | case ARP_OPCODE_VAL_MARS_JOIN: |
| 120 | return ARP_OPCODE_MARS_JOIN; |
| 121 | case ARP_OPCODE_VAL_MARS_LEAVE: |
| 122 | return ARP_OPCODE_MARS_LEAVE; |
| 123 | case ARP_OPCODE_VAL_MARS_NAK: |
| 124 | return ARP_OPCODE_MARS_NAK; |
| 125 | case ARP_OPCODE_VAL_MARS_UNSERV: |
| 126 | return ARP_OPCODE_MARS_UNSERV; |
| 127 | case ARP_OPCODE_VAL_MARS_SJOIN: |
| 128 | return ARP_OPCODE_MARS_SJOIN; |
| 129 | case ARP_OPCODE_VAL_MARS_SLEAVE: |
| 130 | return ARP_OPCODE_MARS_SLEAVE; |
| 131 | case ARP_OPCODE_VAL_MARS_GROUPLIST_REQUEST: |
| 132 | return ARP_OPCODE_MARS_GROUPLIST_REQUEST; |
| 133 | case ARP_OPCODE_VAL_MARS_GROUPLIST_REPLY: |
| 134 | return ARP_OPCODE_MARS_GROUPLIST_REPLY; |
| 135 | case ARP_OPCODE_VAL_MARS_REDIRECT_MAP: |
| 136 | return ARP_OPCODE_MARS_REDIRECT_MAP; |
| 137 | case ARP_OPCODE_VAL_MAPOS_UNARP: |
| 138 | return ARP_OPCODE_MAPOS_UNARP; |
| 139 | case ARP_OPCODE_VAL_OP_EXP1: |
| 140 | return ARP_OPCODE_OP_EXP1; |
| 141 | case ARP_OPCODE_VAL_OP_EXP2: |
| 142 | return ARP_OPCODE_OP_EXP2; |
| 143 | default: |
| 144 | return new ArpOpcode(opcode); |
| 145 | } |
| 146 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 147 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 148 | public void write2Bytes(ChannelBuffer c) { |
| 149 | c.writeShort(this.opcode); |
| 150 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 151 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 152 | public static ArpOpcode read2Bytes(ChannelBuffer c) { |
| 153 | return ArpOpcode.of(c.readUnsignedShort()); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public ArpOpcode applyMask(ArpOpcode mask) { |
| 158 | return ArpOpcode.of(this.opcode & mask.opcode); |
| 159 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 160 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 161 | public int getOpCode() { |
| 162 | return opcode; |
| 163 | } |
Andreas Wundsam | b75c4ad | 2013-09-23 14:45:35 -0700 | [diff] [blame] | 164 | |
Andreas Wundsam | 85c961f | 2013-09-29 21:22:12 -0700 | [diff] [blame] | 165 | @Override |
| 166 | public int hashCode() { |
| 167 | final int prime = 31; |
| 168 | int result = 1; |
| 169 | result = prime * result + opcode; |
| 170 | return result; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public boolean equals(Object obj) { |
| 175 | if (this == obj) |
| 176 | return true; |
| 177 | if (obj == null) |
| 178 | return false; |
| 179 | if (getClass() != obj.getClass()) |
| 180 | return false; |
| 181 | ArpOpcode other = (ArpOpcode) obj; |
| 182 | if (opcode != other.opcode) |
| 183 | return false; |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public int compareTo(ArpOpcode o) { |
| 189 | return UnsignedInts.compare(opcode, o.opcode); |
| 190 | } |
| 191 | |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 192 | } |