Yotam Harchol | d7b8420 | 2013-07-26 16:08:10 -0700 | [diff] [blame^] | 1 | package org.openflow.types; |
| 2 | |
| 3 | import org.jboss.netty.buffer.ChannelBuffer; |
| 4 | |
| 5 | public class ArpOpcode implements OFValueType { |
| 6 | |
| 7 | final static int LENGTH = 2; |
| 8 | |
| 9 | private static final int ARP_OPCODE_VAL_REQUEST = 1; |
| 10 | private static final int ARP_OPCODE_VAL_REPLY = 2; |
| 11 | private static final int ARP_OPCODE_VAL_REQUEST_REVERSE = 3; |
| 12 | private static final int ARP_OPCODE_VAL_REPLY_REVERSE = 4; |
| 13 | private static final int ARP_OPCODE_VAL_DRARP_REQUEST = 5; |
| 14 | private static final int ARP_OPCODE_VAL_DRARP_REPLY = 6; |
| 15 | private static final int ARP_OPCODE_VAL_DRARP_ERROR = 7; |
| 16 | private static final int ARP_OPCODE_VAL_INARP_REQUEST = 8; |
| 17 | private static final int ARP_OPCODE_VAL_INARP_REPLY = 9; |
| 18 | private static final int ARP_OPCODE_VAL_ARP_NAK = 10; |
| 19 | private static final int ARP_OPCODE_VAL_MARS_REQUEST = 11; |
| 20 | private static final int ARP_OPCODE_VAL_MARS_MULTI = 12; |
| 21 | private static final int ARP_OPCODE_VAL_MARS_MSERV = 13; |
| 22 | private static final int ARP_OPCODE_VAL_MARS_JOIN = 14; |
| 23 | private static final int ARP_OPCODE_VAL_MARS_LEAVE = 15; |
| 24 | private static final int ARP_OPCODE_VAL_MARS_NAK = 16; |
| 25 | private static final int ARP_OPCODE_VAL_MARS_UNSERV = 17; |
| 26 | private static final int ARP_OPCODE_VAL_MARS_SJOIN = 18; |
| 27 | private static final int ARP_OPCODE_VAL_MARS_SLEAVE = 19; |
| 28 | private static final int ARP_OPCODE_VAL_MARS_GROUPLIST_REQUEST = 20; |
| 29 | private static final int ARP_OPCODE_VAL_MARS_GROUPLIST_REPLY = 21; |
| 30 | private static final int ARP_OPCODE_VAL_MARS_REDIRECT_MAP = 22; |
| 31 | private static final int ARP_OPCODE_VAL_MAPOS_UNARP = 23; |
| 32 | private static final int ARP_OPCODE_VAL_OP_EXP1 = 24; |
| 33 | private static final int ARP_OPCODE_VAL_OP_EXP2 = 25; |
| 34 | |
| 35 | public static ArpOpcode ARP_OPCODE_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_REQUEST); |
| 36 | public static ArpOpcode ARP_OPCODE_REPLY = new ArpOpcode(ARP_OPCODE_VAL_REPLY); |
| 37 | public static ArpOpcode ARP_OPCODE_REQUEST_REVERSE = new ArpOpcode(ARP_OPCODE_VAL_REQUEST_REVERSE); |
| 38 | public static ArpOpcode ARP_OPCODE_REPLY_REVERSE = new ArpOpcode(ARP_OPCODE_VAL_REPLY_REVERSE); |
| 39 | public static ArpOpcode ARP_OPCODE_DRARP_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_DRARP_REQUEST); |
| 40 | public static ArpOpcode ARP_OPCODE_DRARP_REPLY = new ArpOpcode(ARP_OPCODE_VAL_DRARP_REPLY); |
| 41 | public static ArpOpcode ARP_OPCODE_DRARP_ERROR = new ArpOpcode(ARP_OPCODE_VAL_DRARP_ERROR); |
| 42 | public static ArpOpcode ARP_OPCODE_INARP_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_INARP_REQUEST); |
| 43 | public static ArpOpcode ARP_OPCODE_INARP_REPLY = new ArpOpcode(ARP_OPCODE_VAL_INARP_REPLY); |
| 44 | public static ArpOpcode ARP_OPCODE_ARP_NAK = new ArpOpcode(ARP_OPCODE_VAL_ARP_NAK); |
| 45 | public static ArpOpcode ARP_OPCODE_MARS_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_MARS_REQUEST); |
| 46 | public static ArpOpcode ARP_OPCODE_MARS_MULTI = new ArpOpcode(ARP_OPCODE_VAL_MARS_MULTI); |
| 47 | public static ArpOpcode ARP_OPCODE_MARS_MSERV = new ArpOpcode(ARP_OPCODE_VAL_MARS_MSERV); |
| 48 | public static ArpOpcode ARP_OPCODE_MARS_JOIN = new ArpOpcode(ARP_OPCODE_VAL_MARS_JOIN); |
| 49 | public static ArpOpcode ARP_OPCODE_MARS_LEAVE = new ArpOpcode(ARP_OPCODE_VAL_MARS_LEAVE); |
| 50 | public static ArpOpcode ARP_OPCODE_MARS_NAK = new ArpOpcode(ARP_OPCODE_VAL_MARS_NAK); |
| 51 | public static ArpOpcode ARP_OPCODE_MARS_UNSERV = new ArpOpcode(ARP_OPCODE_VAL_MARS_UNSERV); |
| 52 | public static ArpOpcode ARP_OPCODE_MARS_SJOIN = new ArpOpcode(ARP_OPCODE_VAL_MARS_SJOIN); |
| 53 | public static ArpOpcode ARP_OPCODE_MARS_SLEAVE = new ArpOpcode(ARP_OPCODE_VAL_MARS_SLEAVE); |
| 54 | public static ArpOpcode ARP_OPCODE_MARS_GROUPLIST_REQUEST = new ArpOpcode(ARP_OPCODE_VAL_MARS_GROUPLIST_REQUEST); |
| 55 | public static ArpOpcode ARP_OPCODE_MARS_GROUPLIST_REPLY = new ArpOpcode(ARP_OPCODE_VAL_MARS_GROUPLIST_REPLY); |
| 56 | public static ArpOpcode ARP_OPCODE_MARS_REDIRECT_MAP = new ArpOpcode(ARP_OPCODE_VAL_MARS_REDIRECT_MAP); |
| 57 | public static ArpOpcode ARP_OPCODE_MAPOS_UNARP = new ArpOpcode(ARP_OPCODE_VAL_MAPOS_UNARP); |
| 58 | public static ArpOpcode ARP_OPCODE_OP_EXP1 = new ArpOpcode(ARP_OPCODE_VAL_OP_EXP1); |
| 59 | public static ArpOpcode ARP_OPCODE_OP_EXP2 = new ArpOpcode(ARP_OPCODE_VAL_OP_EXP2); |
| 60 | |
| 61 | private static final int MIN_OPCODE = 0; |
| 62 | private static final int MAX_OPCODE = 0xFFFF; |
| 63 | |
| 64 | private final int opcode; |
| 65 | |
| 66 | private ArpOpcode(int opcode) { |
| 67 | this.opcode = opcode; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public int getLength() { |
| 72 | return LENGTH; |
| 73 | } |
| 74 | |
| 75 | public int getOpcode() { |
| 76 | return this.opcode; |
| 77 | } |
| 78 | |
| 79 | public static ArpOpcode of(int opcode) { |
| 80 | if (opcode < MIN_OPCODE || opcode > MAX_OPCODE) |
| 81 | throw new IllegalArgumentException("Invalid ARP opcode: " + opcode); |
| 82 | switch (opcode) { |
| 83 | case ARP_OPCODE_VAL_REQUEST: |
| 84 | return ARP_OPCODE_REQUEST; |
| 85 | case ARP_OPCODE_VAL_REPLY: |
| 86 | return ARP_OPCODE_REPLY; |
| 87 | case ARP_OPCODE_VAL_REQUEST_REVERSE: |
| 88 | return ARP_OPCODE_REQUEST_REVERSE; |
| 89 | case ARP_OPCODE_VAL_REPLY_REVERSE: |
| 90 | return ARP_OPCODE_REPLY_REVERSE; |
| 91 | case ARP_OPCODE_VAL_DRARP_REQUEST: |
| 92 | return ARP_OPCODE_DRARP_REQUEST; |
| 93 | case ARP_OPCODE_VAL_DRARP_REPLY: |
| 94 | return ARP_OPCODE_DRARP_REPLY; |
| 95 | case ARP_OPCODE_VAL_DRARP_ERROR: |
| 96 | return ARP_OPCODE_DRARP_ERROR; |
| 97 | case ARP_OPCODE_VAL_INARP_REQUEST: |
| 98 | return ARP_OPCODE_INARP_REQUEST; |
| 99 | case ARP_OPCODE_VAL_INARP_REPLY: |
| 100 | return ARP_OPCODE_INARP_REPLY; |
| 101 | case ARP_OPCODE_VAL_ARP_NAK: |
| 102 | return ARP_OPCODE_ARP_NAK; |
| 103 | case ARP_OPCODE_VAL_MARS_REQUEST: |
| 104 | return ARP_OPCODE_MARS_REQUEST; |
| 105 | case ARP_OPCODE_VAL_MARS_MULTI: |
| 106 | return ARP_OPCODE_MARS_MULTI; |
| 107 | case ARP_OPCODE_VAL_MARS_MSERV: |
| 108 | return ARP_OPCODE_MARS_MSERV; |
| 109 | case ARP_OPCODE_VAL_MARS_JOIN: |
| 110 | return ARP_OPCODE_MARS_JOIN; |
| 111 | case ARP_OPCODE_VAL_MARS_LEAVE: |
| 112 | return ARP_OPCODE_MARS_LEAVE; |
| 113 | case ARP_OPCODE_VAL_MARS_NAK: |
| 114 | return ARP_OPCODE_MARS_NAK; |
| 115 | case ARP_OPCODE_VAL_MARS_UNSERV: |
| 116 | return ARP_OPCODE_MARS_UNSERV; |
| 117 | case ARP_OPCODE_VAL_MARS_SJOIN: |
| 118 | return ARP_OPCODE_MARS_SJOIN; |
| 119 | case ARP_OPCODE_VAL_MARS_SLEAVE: |
| 120 | return ARP_OPCODE_MARS_SLEAVE; |
| 121 | case ARP_OPCODE_VAL_MARS_GROUPLIST_REQUEST: |
| 122 | return ARP_OPCODE_MARS_GROUPLIST_REQUEST; |
| 123 | case ARP_OPCODE_VAL_MARS_GROUPLIST_REPLY: |
| 124 | return ARP_OPCODE_MARS_GROUPLIST_REPLY; |
| 125 | case ARP_OPCODE_VAL_MARS_REDIRECT_MAP: |
| 126 | return ARP_OPCODE_MARS_REDIRECT_MAP; |
| 127 | case ARP_OPCODE_VAL_MAPOS_UNARP: |
| 128 | return ARP_OPCODE_MAPOS_UNARP; |
| 129 | case ARP_OPCODE_VAL_OP_EXP1: |
| 130 | return ARP_OPCODE_OP_EXP1; |
| 131 | case ARP_OPCODE_VAL_OP_EXP2: |
| 132 | return ARP_OPCODE_OP_EXP2; |
| 133 | default: |
| 134 | return new ArpOpcode(opcode); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | public void write2Bytes(ChannelBuffer c) { |
| 139 | c.writeShort(this.opcode); |
| 140 | } |
| 141 | |
| 142 | public static ArpOpcode read2Bytes(ChannelBuffer c) { |
| 143 | return ArpOpcode.of(c.readUnsignedShort()); |
| 144 | } |
| 145 | } |