blob: 97f57dbe80c7e9e7bf3b781e850eb9a34fea6d71 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow.instructions;
alshabib55a55d92014-09-16 11:59:31 -070017
alshabib7b808c52015-06-26 14:22:24 -070018import org.onlab.packet.EthType;
Jonathan Hart54b406b2015-03-06 16:24:14 -080019import org.onlab.packet.IpAddress;
20import org.onlab.packet.MacAddress;
21import org.onlab.packet.MplsLabel;
22import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080023import org.onosproject.core.GroupId;
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070024import org.onosproject.net.IndexedLambda;
25import org.onosproject.net.Lambda;
Sho SHIMIZUe9e12752015-05-05 14:45:40 -070026import org.onosproject.net.OchSignal;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.PortNumber;
28import org.onosproject.net.flow.instructions.L0ModificationInstruction.L0SubType;
29import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModLambdaInstruction;
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070030import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
32import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080033import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
sangho3f97a17d2015-01-29 22:56:29 -080034import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModTtlInstruction;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070035import org.onosproject.net.flow.instructions.L4ModificationInstruction.L4SubType;
36import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction;
alshabib10c810b2015-08-18 16:59:04 -070037import org.onosproject.net.meter.MeterId;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080038
Jonathan Hart54b406b2015-03-06 16:24:14 -080039import java.util.Objects;
40
41import static com.google.common.base.MoreObjects.toStringHelper;
42import static com.google.common.base.Preconditions.checkNotNull;
alshabib64231f62014-09-16 17:58:36 -070043
alshabib55a55d92014-09-16 11:59:31 -070044/**
45 * Factory class for creating various traffic treatment instructions.
46 */
47public final class Instructions {
48
49
50 // Ban construction
51 private Instructions() {}
52
53 /**
54 * Creates an output instruction using the specified port number. This can
55 * include logical ports such as CONTROLLER, FLOOD, etc.
56 *
57 * @param number port number
58 * @return output instruction
59 */
60 public static OutputInstruction createOutput(final PortNumber number) {
61 checkNotNull(number, "PortNumber cannot be null");
62 return new OutputInstruction(number);
63 }
64
65 /**
66 * Creates a drop instruction.
Jonathan Hart54b406b2015-03-06 16:24:14 -080067 *
alshabib55a55d92014-09-16 11:59:31 -070068 * @return drop instruction
69 */
70 public static DropInstruction createDrop() {
71 return new DropInstruction();
72 }
73
74 /**
sangho8995ac52015-02-04 11:29:03 -080075 * Creates a group instruction.
76 *
77 * @param groupId Group Id
78 * @return group instruction
79 */
80 public static GroupInstruction createGroup(final GroupId groupId) {
81 checkNotNull(groupId, "GroupId cannot be null");
82 return new GroupInstruction(groupId);
83 }
84
alshabib10c810b2015-08-18 16:59:04 -070085 public static MeterInstruction meterTraffic(final MeterId meterId) {
86 checkNotNull(meterId, "meter id cannot be null");
87 return new MeterInstruction(meterId);
88 }
89
sangho8995ac52015-02-04 11:29:03 -080090 /**
Marc De Leenheer49087752014-10-23 13:54:09 -070091 * Creates a l0 modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -080092 *
93 * @param lambda the lambda to modify to
Marc De Leenheer49087752014-10-23 13:54:09 -070094 * @return a l0 modification
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070095 * @deprecated in Cardinal Release. Use {@link #modL0Lambda(Lambda)} instead.
Marc De Leenheer49087752014-10-23 13:54:09 -070096 */
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070097 @Deprecated
Marc De Leenheer49087752014-10-23 13:54:09 -070098 public static L0ModificationInstruction modL0Lambda(short lambda) {
99 checkNotNull(lambda, "L0 lambda cannot be null");
100 return new ModLambdaInstruction(L0SubType.LAMBDA, lambda);
101 }
102
103 /**
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700104 * Creates an L0 modification with the specified OCh signal.
105 *
106 * @param lambda OCh signal
107 * @return an L0 modification
108 */
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700109 public static L0ModificationInstruction modL0Lambda(Lambda lambda) {
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700110 checkNotNull(lambda, "L0 OCh signal cannot be null");
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700111
112 if (lambda instanceof IndexedLambda) {
113 return new ModLambdaInstruction(L0SubType.LAMBDA, (short) ((IndexedLambda) lambda).index());
114 } else if (lambda instanceof OchSignal) {
115 return new ModOchSignalInstruction((OchSignal) lambda);
116 } else {
117 throw new UnsupportedOperationException(String.format("Unsupported type: %s", lambda));
118 }
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700119 }
120
121 /**
alshabib55a55d92014-09-16 11:59:31 -0700122 * Creates a l2 src modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800123 *
124 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700125 * @return a l2 modification
126 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700127 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700128 checkNotNull(addr, "Src l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700129 return new L2ModificationInstruction.ModEtherInstruction(
130 L2ModificationInstruction.L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -0700131 }
132
133 /**
134 * Creates a L2 dst modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800135 *
136 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700137 * @return a L2 modification
138 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700139 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700140 checkNotNull(addr, "Dst l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700141 return new L2ModificationInstruction.ModEtherInstruction(
142 L2ModificationInstruction.L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -0700143 }
144
145 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800146 * Creates a VLAN ID modification.
147 *
148 * @param vlanId the VLAN ID to modify to
alshabib7410fea2014-09-16 13:48:39 -0700149 * @return a L2 modification
150 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700151 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700152 checkNotNull(vlanId, "VLAN id cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700153 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
alshabib55a55d92014-09-16 11:59:31 -0700154 }
155
alshabib7410fea2014-09-16 13:48:39 -0700156 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800157 * Creates a VLAN PCP modification.
158 *
159 * @param vlanPcp the PCP to modify to
alshabib7410fea2014-09-16 13:48:39 -0700160 * @return a L2 modification
161 */
162 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
163 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700164 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
alshabib7410fea2014-09-16 13:48:39 -0700165 }
166
167 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800168 * Creates a MPLS label modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800169 *
170 * @param mplsLabel MPLS label to set
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800171 * @return a L2 Modification
172 */
Michele Santuari4b6019e2014-12-19 11:31:45 +0100173 public static L2ModificationInstruction modMplsLabel(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800174 checkNotNull(mplsLabel, "MPLS label cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700175 return new L2ModificationInstruction.ModMplsLabelInstruction(mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800176 }
sangho3f97a17d2015-01-29 22:56:29 -0800177
178 /**
Saurav Das73a7dd42015-08-19 22:20:31 -0700179 * Creates a MPLS BOS bit modification.
180 *
181 * @param mplsBos MPLS BOS bit to set (true) or unset (false)
182 * @return a L2 Modification
183 */
184 public static L2ModificationInstruction modMplsBos(boolean mplsBos) {
185 return new L2ModificationInstruction.ModMplsBosInstruction(mplsBos);
186 }
187
188 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800189 * Creates a MPLS decrement TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800190 *
191 * @return a L2 Modification
192 */
193 public static L2ModificationInstruction decMplsTtl() {
alshabibd17abc22015-04-21 18:26:35 -0700194 return new L2ModificationInstruction.ModMplsTtlInstruction();
sangho3f97a17d2015-01-29 22:56:29 -0800195 }
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800196
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800197 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800198 * Creates a L3 IPv4 src modification.
199 *
200 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700201 * @return a L3 modification
202 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700203 public static L3ModificationInstruction modL3Src(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800204 checkNotNull(addr, "Src l3 IPv4 address cannot be null");
205 return new ModIPInstruction(L3SubType.IPV4_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700206 }
207
208 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800209 * Creates a L3 IPv4 dst modification.
210 *
211 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700212 * @return a L3 modification
213 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700214 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800215 checkNotNull(addr, "Dst l3 IPv4 address cannot be null");
216 return new ModIPInstruction(L3SubType.IPV4_DST, addr);
217 }
218
219 /**
220 * Creates a L3 IPv6 src modification.
221 *
222 * @param addr the IPv6 address to modify to
223 * @return a L3 modification
224 */
225 public static L3ModificationInstruction modL3IPv6Src(IpAddress addr) {
226 checkNotNull(addr, "Src l3 IPv6 address cannot be null");
227 return new ModIPInstruction(L3SubType.IPV6_SRC, addr);
228 }
229
230 /**
231 * Creates a L3 IPv6 dst modification.
232 *
233 * @param addr the IPv6 address to modify to
234 * @return a L3 modification
235 */
236 public static L3ModificationInstruction modL3IPv6Dst(IpAddress addr) {
237 checkNotNull(addr, "Dst l3 IPv6 address cannot be null");
238 return new ModIPInstruction(L3SubType.IPV6_DST, addr);
239 }
240
241 /**
242 * Creates a L3 IPv6 Flow Label modification.
243 *
244 * @param flowLabel the IPv6 flow label to modify to (20 bits)
245 * @return a L3 modification
246 */
247 public static L3ModificationInstruction modL3IPv6FlowLabel(int flowLabel) {
248 return new ModIPv6FlowLabelInstruction(flowLabel);
alshabib7410fea2014-09-16 13:48:39 -0700249 }
250
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800251 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800252 * Creates a L3 decrement TTL modification.
253 *
sangho3f97a17d2015-01-29 22:56:29 -0800254 * @return a L3 modification
255 */
256 public static L3ModificationInstruction decNwTtl() {
257 return new ModTtlInstruction(L3SubType.DEC_TTL);
258 }
259
260 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800261 * Creates a L3 copy TTL to outer header modification.
262 *
sangho3f97a17d2015-01-29 22:56:29 -0800263 * @return a L3 modification
264 */
265 public static L3ModificationInstruction copyTtlOut() {
266 return new ModTtlInstruction(L3SubType.TTL_OUT);
267 }
268
269 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800270 * Creates a L3 copy TTL to inner header modification.
271 *
sangho3f97a17d2015-01-29 22:56:29 -0800272 * @return a L3 modification
273 */
274 public static L3ModificationInstruction copyTtlIn() {
275 return new ModTtlInstruction(L3SubType.TTL_IN);
276 }
277
278 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800279 * Creates a push MPLS header instruction.
280 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800281 * @return a L2 modification.
282 */
283 public static Instruction pushMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700284 return new L2ModificationInstruction.PushHeaderInstructions(
285 L2ModificationInstruction.L2SubType.MPLS_PUSH,
alshabib7b808c52015-06-26 14:22:24 -0700286 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800287 }
288
289 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800290 * Creates a pop MPLS header instruction.
291 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800292 * @return a L2 modification.
293 */
294 public static Instruction popMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700295 return new L2ModificationInstruction.PushHeaderInstructions(
296 L2ModificationInstruction.L2SubType.MPLS_POP,
alshabib7b808c52015-06-26 14:22:24 -0700297 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800298 }
alshabib7410fea2014-09-16 13:48:39 -0700299
sangho3f97a17d2015-01-29 22:56:29 -0800300 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800301 * Creates a pop MPLS header instruction with a particular ethertype.
sangho3f97a17d2015-01-29 22:56:29 -0800302 *
303 * @param etherType Ethernet type to set
304 * @return a L2 modification.
Sho SHIMIZUbe63b232015-06-30 10:57:58 -0700305 * @deprecated in Cardinal Release
sangho3f97a17d2015-01-29 22:56:29 -0800306 */
alshabib7b808c52015-06-26 14:22:24 -0700307 @Deprecated
alshabib0ad43982015-05-07 13:43:13 -0700308 public static Instruction popMpls(int etherType) {
sangho3f97a17d2015-01-29 22:56:29 -0800309 checkNotNull(etherType, "Ethernet type cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700310 return new L2ModificationInstruction.PushHeaderInstructions(
alshabib7b808c52015-06-26 14:22:24 -0700311 L2ModificationInstruction.L2SubType.MPLS_POP, new EthType(etherType));
312 }
313
314
315 /**
316 * Creates a pop MPLS header instruction with a particular ethertype.
317 *
318 * @param etherType Ethernet type to set
319 * @return a L2 modification.
320 */
321 public static Instruction popMpls(EthType etherType) {
322 checkNotNull(etherType, "Ethernet type cannot be null");
323 return new L2ModificationInstruction.PushHeaderInstructions(
alshabibd17abc22015-04-21 18:26:35 -0700324 L2ModificationInstruction.L2SubType.MPLS_POP, etherType);
sangho3f97a17d2015-01-29 22:56:29 -0800325 }
326
Saurav Dasfbe25c52015-03-04 11:12:00 -0800327 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800328 * Creates a pop VLAN header instruction.
329 *
330 * @return a L2 modification
Saurav Dasfbe25c52015-03-04 11:12:00 -0800331 */
332 public static Instruction popVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700333 return new L2ModificationInstruction.PopVlanInstruction(
334 L2ModificationInstruction.L2SubType.VLAN_POP);
Saurav Dasfbe25c52015-03-04 11:12:00 -0800335 }
336
337 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800338 * Creates a push VLAN header instruction.
339 *
340 * @return a L2 modification
341 */
342 public static Instruction pushVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700343 return new L2ModificationInstruction.PushHeaderInstructions(
alshabib7b808c52015-06-26 14:22:24 -0700344 L2ModificationInstruction.L2SubType.VLAN_PUSH,
345 EthType.EtherType.VLAN.ethType());
Jonathan Hart54b406b2015-03-06 16:24:14 -0800346 }
347
348 /**
alshabibd17abc22015-04-21 18:26:35 -0700349 * Sends the packet to the table id.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800350 *
alshabibd17abc22015-04-21 18:26:35 -0700351 * @param tableId flow rule table id
Thomas Vachuska3e2b6512015-03-05 09:25:03 -0800352 * @return table type transition instruction
Saurav Dasfbe25c52015-03-04 11:12:00 -0800353 */
alshabibd17abc22015-04-21 18:26:35 -0700354 public static Instruction transition(Integer tableId) {
355 checkNotNull(tableId, "Table id cannot be null");
356 return new TableTypeTransition(tableId);
alshabib9af70072015-02-09 14:34:16 -0800357 }
358
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800359 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700360 * Writes metadata to associate with a packet.
361 *
362 * @param metadata the metadata value to write
363 * @param metadataMask the bits to mask for the metadata value
364 * @return metadata instruction
365 */
366 public static Instruction writeMetadata(long metadata, long metadataMask) {
367 return new MetadataInstruction(metadata, metadataMask);
368 }
369
370 /**
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700371 * Creates a Tunnel ID modification.
372 *
373 * @param tunnelId the Tunnel ID to modify to
374 * @return a L2 modification
375 */
376 public static L2ModificationInstruction modTunnelId(long tunnelId) {
377 checkNotNull(tunnelId, "Tunnel id cannot be null");
378 return new L2ModificationInstruction.ModTunnelIdInstruction(tunnelId);
379 }
380
381 /**
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700382 * Creates a TCP src modification.
383 *
384 * @param port the TCP port number to modify to
385 * @return a L4 modification
386 */
387 public static L4ModificationInstruction modTcpSrc(short port) {
388 checkNotNull(port, "Src TCP port cannot be null");
389 return new ModTransportPortInstruction(L4SubType.TCP_SRC, port);
390 }
391
392 /**
393 * Creates a TCP dst modification.
394 *
395 * @param port the TCP port number to modify to
396 * @return a L4 modification
397 */
398 public static L4ModificationInstruction modTcpDst(short port) {
399 checkNotNull(port, "Dst TCP port cannot be null");
400 return new ModTransportPortInstruction(L4SubType.TCP_DST, port);
401 }
402
403 /**
404 * Creates a UDP src modification.
405 *
406 * @param port the UDP port number to modify to
407 * @return a L4 modification
408 */
409 public static L4ModificationInstruction modUdpSrc(short port) {
410 checkNotNull(port, "Src UDP port cannot be null");
411 return new ModTransportPortInstruction(L4SubType.UDP_SRC, port);
412 }
413
414 /**
415 * Creates a UDP dst modification.
416 *
417 * @param port the UDP port number to modify to
418 * @return a L4 modification
419 */
420 public static L4ModificationInstruction modUdpDst(short port) {
421 checkNotNull(port, "Dst UDP port cannot be null");
422 return new ModTransportPortInstruction(L4SubType.UDP_DST, port);
423 }
424
425 /**
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800426 * Drop instruction.
alshabib55a55d92014-09-16 11:59:31 -0700427 */
alshabib55a55d92014-09-16 11:59:31 -0700428 public static final class DropInstruction implements Instruction {
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800429
430 private DropInstruction() {}
431
alshabib55a55d92014-09-16 11:59:31 -0700432 @Override
433 public Type type() {
434 return Type.DROP;
435 }
alshabib99b8fdc2014-09-25 14:30:22 -0700436
437 @Override
438 public String toString() {
alshabib346b5b32015-03-06 00:42:16 -0800439 return toStringHelper(type().toString()).toString();
alshabib99b8fdc2014-09-25 14:30:22 -0700440 }
alshabib8ca53902014-10-07 13:11:17 -0700441
442 @Override
443 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700444 return Objects.hash(type().ordinal());
alshabib8ca53902014-10-07 13:11:17 -0700445 }
446
447 @Override
448 public boolean equals(Object obj) {
449 if (this == obj) {
450 return true;
451 }
452 if (obj instanceof DropInstruction) {
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800453 return true;
alshabib8ca53902014-10-07 13:11:17 -0700454 }
455 return false;
456 }
alshabib55a55d92014-09-16 11:59:31 -0700457 }
458
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800459 /**
460 * Output Instruction.
sangho8995ac52015-02-04 11:29:03 -0800461 */
alshabib55a55d92014-09-16 11:59:31 -0700462 public static final class OutputInstruction implements Instruction {
463 private final PortNumber port;
464
465 private OutputInstruction(PortNumber port) {
466 this.port = port;
467 }
468
469 public PortNumber port() {
470 return port;
471 }
472
473 @Override
474 public Type type() {
475 return Type.OUTPUT;
476 }
alshabib99b8fdc2014-09-25 14:30:22 -0700477 @Override
478 public String toString() {
479 return toStringHelper(type().toString())
480 .add("port", port).toString();
481 }
alshabib8ca53902014-10-07 13:11:17 -0700482
483 @Override
484 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700485 return Objects.hash(type().ordinal(), port);
alshabib8ca53902014-10-07 13:11:17 -0700486 }
487
488 @Override
489 public boolean equals(Object obj) {
490 if (this == obj) {
491 return true;
492 }
493 if (obj instanceof OutputInstruction) {
494 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700495 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700496
497 }
498 return false;
499 }
alshabib55a55d92014-09-16 11:59:31 -0700500 }
501
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800502 /**
503 * Group Instruction.
sangho8995ac52015-02-04 11:29:03 -0800504 */
sangho8995ac52015-02-04 11:29:03 -0800505 public static final class GroupInstruction implements Instruction {
506 private final GroupId groupId;
507
508 private GroupInstruction(GroupId groupId) {
509 this.groupId = groupId;
510 }
511
512 public GroupId groupId() {
513 return groupId;
514 }
515
516 @Override
517 public Type type() {
518 return Type.GROUP;
519 }
alshabib9af70072015-02-09 14:34:16 -0800520
sangho8995ac52015-02-04 11:29:03 -0800521 @Override
522 public String toString() {
523 return toStringHelper(type().toString())
524 .add("group ID", groupId.id()).toString();
525 }
526
527 @Override
528 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700529 return Objects.hash(type().ordinal(), groupId);
sangho8995ac52015-02-04 11:29:03 -0800530 }
531
532 @Override
533 public boolean equals(Object obj) {
534 if (this == obj) {
535 return true;
536 }
537 if (obj instanceof GroupInstruction) {
538 GroupInstruction that = (GroupInstruction) obj;
539 return Objects.equals(groupId, that.groupId);
540
541 }
542 return false;
543 }
544 }
545
Saurav Das86af8f12015-05-25 23:55:33 -0700546 /**
alshabib10c810b2015-08-18 16:59:04 -0700547 * A meter instruction.
548 */
549 public static final class MeterInstruction implements Instruction {
550 private final MeterId meterId;
551
552 private MeterInstruction(MeterId meterId) {
553 this.meterId = meterId;
554 }
555
556 public MeterId meterId() {
557 return meterId;
558 }
559
560 @Override
561 public Type type() {
562 return Type.METER;
563 }
564
565 @Override
566 public String toString() {
567 return toStringHelper(type().toString())
568 .add("meter ID", meterId.id()).toString();
569 }
570
571 @Override
572 public int hashCode() {
573 return Objects.hash(type().ordinal(), meterId);
574 }
575
576 @Override
577 public boolean equals(Object obj) {
578 if (this == obj) {
579 return true;
580 }
581 if (obj instanceof MeterInstruction) {
582 MeterInstruction that = (MeterInstruction) obj;
583 return Objects.equals(meterId, that.meterId);
584
585 }
586 return false;
587 }
588 }
589
590 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700591 * Transition instruction.
592 */
alshabibd17abc22015-04-21 18:26:35 -0700593 public static class TableTypeTransition implements Instruction {
594 private final Integer tableId;
595
596 TableTypeTransition(Integer tableId) {
597 this.tableId = tableId;
alshabib9af70072015-02-09 14:34:16 -0800598 }
599
600 @Override
601 public Type type() {
602 return Type.TABLE;
603 }
604
alshabibd17abc22015-04-21 18:26:35 -0700605 public Integer tableId() {
606 return this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800607 }
608
609 @Override
610 public String toString() {
611 return toStringHelper(type().toString())
alshabibd17abc22015-04-21 18:26:35 -0700612 .add("tableId", this.tableId).toString();
alshabib9af70072015-02-09 14:34:16 -0800613 }
614
615 @Override
616 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700617 return Objects.hash(type().ordinal(), tableId);
alshabib9af70072015-02-09 14:34:16 -0800618 }
619
620 @Override
621 public boolean equals(Object obj) {
622 if (this == obj) {
623 return true;
624 }
625 if (obj instanceof TableTypeTransition) {
626 TableTypeTransition that = (TableTypeTransition) obj;
alshabibd17abc22015-04-21 18:26:35 -0700627 return Objects.equals(tableId, that.tableId);
alshabib9af70072015-02-09 14:34:16 -0800628
629 }
630 return false;
631 }
Saurav Das86af8f12015-05-25 23:55:33 -0700632 }
alshabib9af70072015-02-09 14:34:16 -0800633
Saurav Das86af8f12015-05-25 23:55:33 -0700634 /**
635 * Metadata instruction.
636 */
637 public static class MetadataInstruction implements Instruction {
638 private final long metadata;
639 private final long metadataMask;
640
641 MetadataInstruction(long metadata, long metadataMask) {
642 this.metadata = metadata;
643 this.metadataMask = metadataMask;
644 }
645
646 @Override
647 public Type type() {
648 return Type.METADATA;
649 }
650
651 public long metadata() {
652 return this.metadata;
653 }
654
655 public long metadataMask() {
656 return this.metadataMask;
657 }
658
659 @Override
660 public String toString() {
661 return toStringHelper(type().toString())
662 .add("metadata", Long.toHexString(this.metadata))
663 .add("metadata mask", Long.toHexString(this.metadataMask))
664 .toString();
665 }
666
667 @Override
668 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700669 return Objects.hash(type().ordinal(), metadata, metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700670 }
671
672 @Override
673 public boolean equals(Object obj) {
674 if (this == obj) {
675 return true;
676 }
677 if (obj instanceof MetadataInstruction) {
678 MetadataInstruction that = (MetadataInstruction) obj;
679 return Objects.equals(metadata, that.metadata) &&
680 Objects.equals(metadataMask, that.metadataMask);
681
682 }
683 return false;
684 }
alshabib9af70072015-02-09 14:34:16 -0800685 }
Saurav Das73a7dd42015-08-19 22:20:31 -0700686
alshabib55a55d92014-09-16 11:59:31 -0700687}
alshabib8ca53902014-10-07 13:11:17 -0700688
689