blob: 2a33a0e996ab403607302e6287f565c5359b4128 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import org.jboss.netty.buffer.ChannelBuffer;
4
Andreas Wundsam85c961f2013-09-29 21:22:12 -07005import com.google.common.primitives.UnsignedInts;
6
Yotam Harcholf3f11152013-09-05 16:47:16 -07007public 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 Wundsamb75c4ad2013-09-23 14:45:35 -070065
66 private static final int NONE_VAL = 0;
67 public static final ArpOpcode NONE = new ArpOpcode(NONE_VAL);
68
Yotam Harcholf3f11152013-09-05 16:47:16 -070069 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 Wundsamb75c4ad2013-09-23 14:45:35 -070091 case NONE_VAL:
92 return NONE;
Yotam Harcholf3f11152013-09-05 16:47:16 -070093 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 Wundsamb75c4ad2013-09-23 14:45:35 -0700147
Yotam Harcholf3f11152013-09-05 16:47:16 -0700148 public void write2Bytes(ChannelBuffer c) {
149 c.writeShort(this.opcode);
150 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -0700151
Yotam Harcholf3f11152013-09-05 16:47:16 -0700152 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 Wundsamb75c4ad2013-09-23 14:45:35 -0700160
Yotam Harcholf3f11152013-09-05 16:47:16 -0700161 public int getOpCode() {
162 return opcode;
163 }
Andreas Wundsamb75c4ad2013-09-23 14:45:35 -0700164
Andreas Wundsam85c961f2013-09-29 21:22:12 -0700165 @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 Harcholf3f11152013-09-05 16:47:16 -0700192}