blob: cd9ec96b70e21f5bfd9fcb5541516db73202541e [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 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800179 * Creates a MPLS decrement TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800180 *
181 * @return a L2 Modification
182 */
183 public static L2ModificationInstruction decMplsTtl() {
alshabibd17abc22015-04-21 18:26:35 -0700184 return new L2ModificationInstruction.ModMplsTtlInstruction();
sangho3f97a17d2015-01-29 22:56:29 -0800185 }
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800186
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800187 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800188 * Creates a L3 IPv4 src modification.
189 *
190 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700191 * @return a L3 modification
192 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700193 public static L3ModificationInstruction modL3Src(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800194 checkNotNull(addr, "Src l3 IPv4 address cannot be null");
195 return new ModIPInstruction(L3SubType.IPV4_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700196 }
197
198 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800199 * Creates a L3 IPv4 dst modification.
200 *
201 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700202 * @return a L3 modification
203 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700204 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800205 checkNotNull(addr, "Dst l3 IPv4 address cannot be null");
206 return new ModIPInstruction(L3SubType.IPV4_DST, addr);
207 }
208
209 /**
210 * Creates a L3 IPv6 src modification.
211 *
212 * @param addr the IPv6 address to modify to
213 * @return a L3 modification
214 */
215 public static L3ModificationInstruction modL3IPv6Src(IpAddress addr) {
216 checkNotNull(addr, "Src l3 IPv6 address cannot be null");
217 return new ModIPInstruction(L3SubType.IPV6_SRC, addr);
218 }
219
220 /**
221 * Creates a L3 IPv6 dst modification.
222 *
223 * @param addr the IPv6 address to modify to
224 * @return a L3 modification
225 */
226 public static L3ModificationInstruction modL3IPv6Dst(IpAddress addr) {
227 checkNotNull(addr, "Dst l3 IPv6 address cannot be null");
228 return new ModIPInstruction(L3SubType.IPV6_DST, addr);
229 }
230
231 /**
232 * Creates a L3 IPv6 Flow Label modification.
233 *
234 * @param flowLabel the IPv6 flow label to modify to (20 bits)
235 * @return a L3 modification
236 */
237 public static L3ModificationInstruction modL3IPv6FlowLabel(int flowLabel) {
238 return new ModIPv6FlowLabelInstruction(flowLabel);
alshabib7410fea2014-09-16 13:48:39 -0700239 }
240
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800241 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800242 * Creates a L3 decrement TTL modification.
243 *
sangho3f97a17d2015-01-29 22:56:29 -0800244 * @return a L3 modification
245 */
246 public static L3ModificationInstruction decNwTtl() {
247 return new ModTtlInstruction(L3SubType.DEC_TTL);
248 }
249
250 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800251 * Creates a L3 copy TTL to outer header modification.
252 *
sangho3f97a17d2015-01-29 22:56:29 -0800253 * @return a L3 modification
254 */
255 public static L3ModificationInstruction copyTtlOut() {
256 return new ModTtlInstruction(L3SubType.TTL_OUT);
257 }
258
259 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800260 * Creates a L3 copy TTL to inner header modification.
261 *
sangho3f97a17d2015-01-29 22:56:29 -0800262 * @return a L3 modification
263 */
264 public static L3ModificationInstruction copyTtlIn() {
265 return new ModTtlInstruction(L3SubType.TTL_IN);
266 }
267
268 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800269 * Creates a push MPLS header instruction.
270 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800271 * @return a L2 modification.
272 */
273 public static Instruction pushMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700274 return new L2ModificationInstruction.PushHeaderInstructions(
275 L2ModificationInstruction.L2SubType.MPLS_PUSH,
alshabib7b808c52015-06-26 14:22:24 -0700276 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800277 }
278
279 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800280 * Creates a pop MPLS header instruction.
281 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800282 * @return a L2 modification.
283 */
284 public static Instruction popMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700285 return new L2ModificationInstruction.PushHeaderInstructions(
286 L2ModificationInstruction.L2SubType.MPLS_POP,
alshabib7b808c52015-06-26 14:22:24 -0700287 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800288 }
alshabib7410fea2014-09-16 13:48:39 -0700289
sangho3f97a17d2015-01-29 22:56:29 -0800290 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800291 * Creates a pop MPLS header instruction with a particular ethertype.
sangho3f97a17d2015-01-29 22:56:29 -0800292 *
293 * @param etherType Ethernet type to set
294 * @return a L2 modification.
Sho SHIMIZUbe63b232015-06-30 10:57:58 -0700295 * @deprecated in Cardinal Release
sangho3f97a17d2015-01-29 22:56:29 -0800296 */
alshabib7b808c52015-06-26 14:22:24 -0700297 @Deprecated
alshabib0ad43982015-05-07 13:43:13 -0700298 public static Instruction popMpls(int etherType) {
sangho3f97a17d2015-01-29 22:56:29 -0800299 checkNotNull(etherType, "Ethernet type cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700300 return new L2ModificationInstruction.PushHeaderInstructions(
alshabib7b808c52015-06-26 14:22:24 -0700301 L2ModificationInstruction.L2SubType.MPLS_POP, new EthType(etherType));
302 }
303
304
305 /**
306 * Creates a pop MPLS header instruction with a particular ethertype.
307 *
308 * @param etherType Ethernet type to set
309 * @return a L2 modification.
310 */
311 public static Instruction popMpls(EthType etherType) {
312 checkNotNull(etherType, "Ethernet type cannot be null");
313 return new L2ModificationInstruction.PushHeaderInstructions(
alshabibd17abc22015-04-21 18:26:35 -0700314 L2ModificationInstruction.L2SubType.MPLS_POP, etherType);
sangho3f97a17d2015-01-29 22:56:29 -0800315 }
316
Saurav Dasfbe25c52015-03-04 11:12:00 -0800317 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800318 * Creates a pop VLAN header instruction.
319 *
320 * @return a L2 modification
Saurav Dasfbe25c52015-03-04 11:12:00 -0800321 */
322 public static Instruction popVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700323 return new L2ModificationInstruction.PopVlanInstruction(
324 L2ModificationInstruction.L2SubType.VLAN_POP);
Saurav Dasfbe25c52015-03-04 11:12:00 -0800325 }
326
327 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800328 * Creates a push VLAN header instruction.
329 *
330 * @return a L2 modification
331 */
332 public static Instruction pushVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700333 return new L2ModificationInstruction.PushHeaderInstructions(
alshabib7b808c52015-06-26 14:22:24 -0700334 L2ModificationInstruction.L2SubType.VLAN_PUSH,
335 EthType.EtherType.VLAN.ethType());
Jonathan Hart54b406b2015-03-06 16:24:14 -0800336 }
337
338 /**
alshabibd17abc22015-04-21 18:26:35 -0700339 * Sends the packet to the table id.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800340 *
alshabibd17abc22015-04-21 18:26:35 -0700341 * @param tableId flow rule table id
Thomas Vachuska3e2b6512015-03-05 09:25:03 -0800342 * @return table type transition instruction
Saurav Dasfbe25c52015-03-04 11:12:00 -0800343 */
alshabibd17abc22015-04-21 18:26:35 -0700344 public static Instruction transition(Integer tableId) {
345 checkNotNull(tableId, "Table id cannot be null");
346 return new TableTypeTransition(tableId);
alshabib9af70072015-02-09 14:34:16 -0800347 }
348
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800349 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700350 * Writes metadata to associate with a packet.
351 *
352 * @param metadata the metadata value to write
353 * @param metadataMask the bits to mask for the metadata value
354 * @return metadata instruction
355 */
356 public static Instruction writeMetadata(long metadata, long metadataMask) {
357 return new MetadataInstruction(metadata, metadataMask);
358 }
359
360 /**
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700361 * Creates a Tunnel ID modification.
362 *
363 * @param tunnelId the Tunnel ID to modify to
364 * @return a L2 modification
365 */
366 public static L2ModificationInstruction modTunnelId(long tunnelId) {
367 checkNotNull(tunnelId, "Tunnel id cannot be null");
368 return new L2ModificationInstruction.ModTunnelIdInstruction(tunnelId);
369 }
370
371 /**
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700372 * Creates a TCP src modification.
373 *
374 * @param port the TCP port number to modify to
375 * @return a L4 modification
376 */
377 public static L4ModificationInstruction modTcpSrc(short port) {
378 checkNotNull(port, "Src TCP port cannot be null");
379 return new ModTransportPortInstruction(L4SubType.TCP_SRC, port);
380 }
381
382 /**
383 * Creates a TCP dst modification.
384 *
385 * @param port the TCP port number to modify to
386 * @return a L4 modification
387 */
388 public static L4ModificationInstruction modTcpDst(short port) {
389 checkNotNull(port, "Dst TCP port cannot be null");
390 return new ModTransportPortInstruction(L4SubType.TCP_DST, port);
391 }
392
393 /**
394 * Creates a UDP src modification.
395 *
396 * @param port the UDP port number to modify to
397 * @return a L4 modification
398 */
399 public static L4ModificationInstruction modUdpSrc(short port) {
400 checkNotNull(port, "Src UDP port cannot be null");
401 return new ModTransportPortInstruction(L4SubType.UDP_SRC, port);
402 }
403
404 /**
405 * Creates a UDP dst modification.
406 *
407 * @param port the UDP port number to modify to
408 * @return a L4 modification
409 */
410 public static L4ModificationInstruction modUdpDst(short port) {
411 checkNotNull(port, "Dst UDP port cannot be null");
412 return new ModTransportPortInstruction(L4SubType.UDP_DST, port);
413 }
414
415 /**
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800416 * Drop instruction.
alshabib55a55d92014-09-16 11:59:31 -0700417 */
alshabib55a55d92014-09-16 11:59:31 -0700418 public static final class DropInstruction implements Instruction {
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800419
420 private DropInstruction() {}
421
alshabib55a55d92014-09-16 11:59:31 -0700422 @Override
423 public Type type() {
424 return Type.DROP;
425 }
alshabib99b8fdc2014-09-25 14:30:22 -0700426
427 @Override
428 public String toString() {
alshabib346b5b32015-03-06 00:42:16 -0800429 return toStringHelper(type().toString()).toString();
alshabib99b8fdc2014-09-25 14:30:22 -0700430 }
alshabib8ca53902014-10-07 13:11:17 -0700431
432 @Override
433 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700434 return Objects.hash(type().ordinal());
alshabib8ca53902014-10-07 13:11:17 -0700435 }
436
437 @Override
438 public boolean equals(Object obj) {
439 if (this == obj) {
440 return true;
441 }
442 if (obj instanceof DropInstruction) {
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800443 return true;
alshabib8ca53902014-10-07 13:11:17 -0700444 }
445 return false;
446 }
alshabib55a55d92014-09-16 11:59:31 -0700447 }
448
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800449 /**
450 * Output Instruction.
sangho8995ac52015-02-04 11:29:03 -0800451 */
alshabib55a55d92014-09-16 11:59:31 -0700452 public static final class OutputInstruction implements Instruction {
453 private final PortNumber port;
454
455 private OutputInstruction(PortNumber port) {
456 this.port = port;
457 }
458
459 public PortNumber port() {
460 return port;
461 }
462
463 @Override
464 public Type type() {
465 return Type.OUTPUT;
466 }
alshabib99b8fdc2014-09-25 14:30:22 -0700467 @Override
468 public String toString() {
469 return toStringHelper(type().toString())
470 .add("port", port).toString();
471 }
alshabib8ca53902014-10-07 13:11:17 -0700472
473 @Override
474 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700475 return Objects.hash(type().ordinal(), port);
alshabib8ca53902014-10-07 13:11:17 -0700476 }
477
478 @Override
479 public boolean equals(Object obj) {
480 if (this == obj) {
481 return true;
482 }
483 if (obj instanceof OutputInstruction) {
484 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700485 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700486
487 }
488 return false;
489 }
alshabib55a55d92014-09-16 11:59:31 -0700490 }
491
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800492 /**
493 * Group Instruction.
sangho8995ac52015-02-04 11:29:03 -0800494 */
sangho8995ac52015-02-04 11:29:03 -0800495 public static final class GroupInstruction implements Instruction {
496 private final GroupId groupId;
497
498 private GroupInstruction(GroupId groupId) {
499 this.groupId = groupId;
500 }
501
502 public GroupId groupId() {
503 return groupId;
504 }
505
506 @Override
507 public Type type() {
508 return Type.GROUP;
509 }
alshabib9af70072015-02-09 14:34:16 -0800510
sangho8995ac52015-02-04 11:29:03 -0800511 @Override
512 public String toString() {
513 return toStringHelper(type().toString())
514 .add("group ID", groupId.id()).toString();
515 }
516
517 @Override
518 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700519 return Objects.hash(type().ordinal(), groupId);
sangho8995ac52015-02-04 11:29:03 -0800520 }
521
522 @Override
523 public boolean equals(Object obj) {
524 if (this == obj) {
525 return true;
526 }
527 if (obj instanceof GroupInstruction) {
528 GroupInstruction that = (GroupInstruction) obj;
529 return Objects.equals(groupId, that.groupId);
530
531 }
532 return false;
533 }
534 }
535
Saurav Das86af8f12015-05-25 23:55:33 -0700536 /**
alshabib10c810b2015-08-18 16:59:04 -0700537 * A meter instruction.
538 */
539 public static final class MeterInstruction implements Instruction {
540 private final MeterId meterId;
541
542 private MeterInstruction(MeterId meterId) {
543 this.meterId = meterId;
544 }
545
546 public MeterId meterId() {
547 return meterId;
548 }
549
550 @Override
551 public Type type() {
552 return Type.METER;
553 }
554
555 @Override
556 public String toString() {
557 return toStringHelper(type().toString())
558 .add("meter ID", meterId.id()).toString();
559 }
560
561 @Override
562 public int hashCode() {
563 return Objects.hash(type().ordinal(), meterId);
564 }
565
566 @Override
567 public boolean equals(Object obj) {
568 if (this == obj) {
569 return true;
570 }
571 if (obj instanceof MeterInstruction) {
572 MeterInstruction that = (MeterInstruction) obj;
573 return Objects.equals(meterId, that.meterId);
574
575 }
576 return false;
577 }
578 }
579
580 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700581 * Transition instruction.
582 */
alshabibd17abc22015-04-21 18:26:35 -0700583 public static class TableTypeTransition implements Instruction {
584 private final Integer tableId;
585
586 TableTypeTransition(Integer tableId) {
587 this.tableId = tableId;
alshabib9af70072015-02-09 14:34:16 -0800588 }
589
590 @Override
591 public Type type() {
592 return Type.TABLE;
593 }
594
alshabibd17abc22015-04-21 18:26:35 -0700595 public Integer tableId() {
596 return this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800597 }
598
599 @Override
600 public String toString() {
601 return toStringHelper(type().toString())
alshabibd17abc22015-04-21 18:26:35 -0700602 .add("tableId", this.tableId).toString();
alshabib9af70072015-02-09 14:34:16 -0800603 }
604
605 @Override
606 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700607 return Objects.hash(type().ordinal(), tableId);
alshabib9af70072015-02-09 14:34:16 -0800608 }
609
610 @Override
611 public boolean equals(Object obj) {
612 if (this == obj) {
613 return true;
614 }
615 if (obj instanceof TableTypeTransition) {
616 TableTypeTransition that = (TableTypeTransition) obj;
alshabibd17abc22015-04-21 18:26:35 -0700617 return Objects.equals(tableId, that.tableId);
alshabib9af70072015-02-09 14:34:16 -0800618
619 }
620 return false;
621 }
Saurav Das86af8f12015-05-25 23:55:33 -0700622 }
alshabib9af70072015-02-09 14:34:16 -0800623
Saurav Das86af8f12015-05-25 23:55:33 -0700624 /**
625 * Metadata instruction.
626 */
627 public static class MetadataInstruction implements Instruction {
628 private final long metadata;
629 private final long metadataMask;
630
631 MetadataInstruction(long metadata, long metadataMask) {
632 this.metadata = metadata;
633 this.metadataMask = metadataMask;
634 }
635
636 @Override
637 public Type type() {
638 return Type.METADATA;
639 }
640
641 public long metadata() {
642 return this.metadata;
643 }
644
645 public long metadataMask() {
646 return this.metadataMask;
647 }
648
649 @Override
650 public String toString() {
651 return toStringHelper(type().toString())
652 .add("metadata", Long.toHexString(this.metadata))
653 .add("metadata mask", Long.toHexString(this.metadataMask))
654 .toString();
655 }
656
657 @Override
658 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700659 return Objects.hash(type().ordinal(), metadata, metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700660 }
661
662 @Override
663 public boolean equals(Object obj) {
664 if (this == obj) {
665 return true;
666 }
667 if (obj instanceof MetadataInstruction) {
668 MetadataInstruction that = (MetadataInstruction) obj;
669 return Objects.equals(metadata, that.metadata) &&
670 Objects.equals(metadataMask, that.metadataMask);
671
672 }
673 return false;
674 }
alshabib9af70072015-02-09 14:34:16 -0800675 }
alshabib55a55d92014-09-16 11:59:31 -0700676}
alshabib8ca53902014-10-07 13:11:17 -0700677
678