blob: 9bd7df5f9a37bb3b82e1e7eeb991875e9949560b [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
Steffen Gebertba2d3b72015-10-22 11:14:31 +020018import com.google.common.base.MoreObjects;
Cem Türker3baff672017-10-12 15:09:01 +030019import com.google.common.collect.ImmutableMap;
alshabib7b808c52015-06-26 14:22:24 -070020import org.onlab.packet.EthType;
Jonathan Hart54b406b2015-03-06 16:24:14 -080021import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.MplsLabel;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070024import org.onlab.packet.TpPort;
Jonathan Hart54b406b2015-03-06 16:24:14 -080025import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080026import org.onosproject.core.GroupId;
Jonathan Hart3c259162015-10-21 21:31:19 -070027import org.onosproject.net.DeviceId;
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070028import org.onosproject.net.Lambda;
Sho SHIMIZUe9e12752015-05-05 14:45:40 -070029import org.onosproject.net.OchSignal;
Yafit Hadar52d81552015-10-07 12:26:52 +030030import org.onosproject.net.OduSignalId;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.PortNumber;
Cem Türker3baff672017-10-12 15:09:01 +030032import org.onosproject.net.flow.StatTriggerField;
33import org.onosproject.net.flow.StatTriggerFlag;
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070034import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction;
Yafit Hadar52d81552015-10-07 12:26:52 +030035import org.onosproject.net.flow.instructions.L1ModificationInstruction.ModOduSignalIdInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
37import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
lishuai3cce60b2015-12-01 19:35:16 +080038import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpIPInstruction;
39import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpEthInstruction;
40import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpOpInstruction;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080041import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
sangho3f97a17d2015-01-29 22:56:29 -080042import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModTtlInstruction;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070043import org.onosproject.net.flow.instructions.L4ModificationInstruction.L4SubType;
44import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction;
alshabib10c810b2015-08-18 16:59:04 -070045import org.onosproject.net.meter.MeterId;
Frank Wangdf383212017-06-23 09:17:41 +080046import org.onosproject.net.pi.runtime.PiTableAction;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080047
Cem Türker3baff672017-10-12 15:09:01 +030048import java.util.Map;
Jonathan Hart54b406b2015-03-06 16:24:14 -080049import java.util.Objects;
50
51import static com.google.common.base.MoreObjects.toStringHelper;
52import static com.google.common.base.Preconditions.checkNotNull;
alshabib64231f62014-09-16 17:58:36 -070053
alshabib55a55d92014-09-16 11:59:31 -070054/**
55 * Factory class for creating various traffic treatment instructions.
56 */
57public final class Instructions {
58
Jonathan Hartc7840bd2016-01-21 23:26:29 -080059 private static final String SEPARATOR = ":";
60
alshabib55a55d92014-09-16 11:59:31 -070061 // Ban construction
62 private Instructions() {}
63
64 /**
65 * Creates an output instruction using the specified port number. This can
66 * include logical ports such as CONTROLLER, FLOOD, etc.
67 *
68 * @param number port number
69 * @return output instruction
70 */
71 public static OutputInstruction createOutput(final PortNumber number) {
72 checkNotNull(number, "PortNumber cannot be null");
73 return new OutputInstruction(number);
74 }
75
76 /**
Charles Chan7efabeb2015-09-28 15:12:19 -070077 * Creates a no action instruction.
78 *
79 * @return no action instruction
80 */
81 public static NoActionInstruction createNoAction() {
82 return new NoActionInstruction();
83 }
84
85 /**
sangho8995ac52015-02-04 11:29:03 -080086 * Creates a group instruction.
87 *
88 * @param groupId Group Id
89 * @return group instruction
90 */
91 public static GroupInstruction createGroup(final GroupId groupId) {
92 checkNotNull(groupId, "GroupId cannot be null");
93 return new GroupInstruction(groupId);
94 }
95
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +020096 /**
97 * Creates a set-queue instruction.
98 *
99 * @param queueId Queue Id
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200100 * @param port Port number
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200101 * @return set-queue instruction
102 */
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200103 public static SetQueueInstruction setQueue(final long queueId, final PortNumber port) {
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200104 checkNotNull(queueId, "queue ID cannot be null");
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200105 return new SetQueueInstruction(queueId, port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200106 }
107
Jian Li1ef82db2016-03-03 14:43:21 -0800108 /**
109 * Creates a meter instruction.
110 *
111 * @param meterId Meter Id
112 * @return meter instruction
113 */
alshabib10c810b2015-08-18 16:59:04 -0700114 public static MeterInstruction meterTraffic(final MeterId meterId) {
115 checkNotNull(meterId, "meter id cannot be null");
116 return new MeterInstruction(meterId);
117 }
118
sangho8995ac52015-02-04 11:29:03 -0800119 /**
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700120 * Creates an L0 modification with the specified OCh signal.
121 *
122 * @param lambda OCh signal
123 * @return an L0 modification
124 */
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700125 public static L0ModificationInstruction modL0Lambda(Lambda lambda) {
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700126 checkNotNull(lambda, "L0 OCh signal cannot be null");
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700127
Sho SHIMIZUa114d892016-03-11 16:48:19 -0800128 if (lambda instanceof OchSignal) {
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700129 return new ModOchSignalInstruction((OchSignal) lambda);
130 } else {
131 throw new UnsupportedOperationException(String.format("Unsupported type: %s", lambda));
132 }
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700133 }
134
135 /**
Yafit Hadar52d81552015-10-07 12:26:52 +0300136 * Creates an L1 modification with the specified ODU signal Id.
137 *
138 * @param oduSignalId ODU Signal Id
139 * @return a L1 modification
140 */
141 public static L1ModificationInstruction modL1OduSignalId(OduSignalId oduSignalId) {
142 checkNotNull(oduSignalId, "L1 ODU signal ID cannot be null");
143 return new ModOduSignalIdInstruction(oduSignalId);
144 }
145 /**
alshabib55a55d92014-09-16 11:59:31 -0700146 * Creates a l2 src modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800147 *
148 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700149 * @return a l2 modification
150 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700151 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700152 checkNotNull(addr, "Src l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700153 return new L2ModificationInstruction.ModEtherInstruction(
154 L2ModificationInstruction.L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -0700155 }
156
157 /**
158 * Creates a L2 dst modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800159 *
160 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700161 * @return a L2 modification
162 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700163 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700164 checkNotNull(addr, "Dst l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700165 return new L2ModificationInstruction.ModEtherInstruction(
166 L2ModificationInstruction.L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -0700167 }
168
169 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800170 * Creates a VLAN ID modification.
171 *
172 * @param vlanId the VLAN ID to modify to
alshabib7410fea2014-09-16 13:48:39 -0700173 * @return a L2 modification
174 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700175 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700176 checkNotNull(vlanId, "VLAN id cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700177 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
alshabib55a55d92014-09-16 11:59:31 -0700178 }
179
alshabib7410fea2014-09-16 13:48:39 -0700180 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800181 * Creates a VLAN PCP modification.
182 *
183 * @param vlanPcp the PCP to modify to
alshabib7410fea2014-09-16 13:48:39 -0700184 * @return a L2 modification
185 */
186 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
187 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700188 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
alshabib7410fea2014-09-16 13:48:39 -0700189 }
190
191 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800192 * Creates a MPLS label modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800193 *
194 * @param mplsLabel MPLS label to set
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800195 * @return a L2 Modification
196 */
Michele Santuari4b6019e2014-12-19 11:31:45 +0100197 public static L2ModificationInstruction modMplsLabel(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800198 checkNotNull(mplsLabel, "MPLS label cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700199 return new L2ModificationInstruction.ModMplsLabelInstruction(mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800200 }
sangho3f97a17d2015-01-29 22:56:29 -0800201
202 /**
Saurav Das73a7dd42015-08-19 22:20:31 -0700203 * Creates a MPLS BOS bit modification.
204 *
205 * @param mplsBos MPLS BOS bit to set (true) or unset (false)
206 * @return a L2 Modification
207 */
208 public static L2ModificationInstruction modMplsBos(boolean mplsBos) {
209 return new L2ModificationInstruction.ModMplsBosInstruction(mplsBos);
210 }
211
212 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800213 * Creates a MPLS decrement TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800214 *
215 * @return a L2 Modification
216 */
217 public static L2ModificationInstruction decMplsTtl() {
alshabibd17abc22015-04-21 18:26:35 -0700218 return new L2ModificationInstruction.ModMplsTtlInstruction();
sangho3f97a17d2015-01-29 22:56:29 -0800219 }
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800220
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800221 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800222 * Creates a L3 IPv4 src modification.
223 *
224 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700225 * @return a L3 modification
226 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700227 public static L3ModificationInstruction modL3Src(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800228 checkNotNull(addr, "Src l3 IPv4 address cannot be null");
229 return new ModIPInstruction(L3SubType.IPV4_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700230 }
231
232 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800233 * Creates a L3 IPv4 dst modification.
234 *
235 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700236 * @return a L3 modification
237 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700238 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800239 checkNotNull(addr, "Dst l3 IPv4 address cannot be null");
240 return new ModIPInstruction(L3SubType.IPV4_DST, addr);
241 }
242
243 /**
244 * Creates a L3 IPv6 src modification.
245 *
246 * @param addr the IPv6 address to modify to
247 * @return a L3 modification
248 */
249 public static L3ModificationInstruction modL3IPv6Src(IpAddress addr) {
250 checkNotNull(addr, "Src l3 IPv6 address cannot be null");
251 return new ModIPInstruction(L3SubType.IPV6_SRC, addr);
252 }
253
254 /**
255 * Creates a L3 IPv6 dst modification.
256 *
257 * @param addr the IPv6 address to modify to
258 * @return a L3 modification
259 */
260 public static L3ModificationInstruction modL3IPv6Dst(IpAddress addr) {
261 checkNotNull(addr, "Dst l3 IPv6 address cannot be null");
262 return new ModIPInstruction(L3SubType.IPV6_DST, addr);
263 }
264
265 /**
266 * Creates a L3 IPv6 Flow Label modification.
267 *
268 * @param flowLabel the IPv6 flow label to modify to (20 bits)
269 * @return a L3 modification
270 */
271 public static L3ModificationInstruction modL3IPv6FlowLabel(int flowLabel) {
272 return new ModIPv6FlowLabelInstruction(flowLabel);
alshabib7410fea2014-09-16 13:48:39 -0700273 }
274
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800275 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800276 * Creates a L3 decrement TTL modification.
277 *
sangho3f97a17d2015-01-29 22:56:29 -0800278 * @return a L3 modification
279 */
280 public static L3ModificationInstruction decNwTtl() {
281 return new ModTtlInstruction(L3SubType.DEC_TTL);
282 }
283
284 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800285 * Creates a L3 copy TTL to outer header modification.
286 *
sangho3f97a17d2015-01-29 22:56:29 -0800287 * @return a L3 modification
288 */
289 public static L3ModificationInstruction copyTtlOut() {
290 return new ModTtlInstruction(L3SubType.TTL_OUT);
291 }
292
293 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800294 * Creates a L3 copy TTL to inner header modification.
295 *
sangho3f97a17d2015-01-29 22:56:29 -0800296 * @return a L3 modification
297 */
298 public static L3ModificationInstruction copyTtlIn() {
299 return new ModTtlInstruction(L3SubType.TTL_IN);
300 }
301
302 /**
lishuai3cce60b2015-12-01 19:35:16 +0800303 * Creates a L3 ARP IP src modification.
304 *
305 * @param addr the ip address to modify to
306 * @return a L3 modification
307 */
308 public static L3ModificationInstruction modArpSpa(IpAddress addr) {
309 checkNotNull(addr, "Src l3 ARP IP address cannot be null");
310 return new ModArpIPInstruction(L3SubType.ARP_SPA, addr);
311 }
312
313 /**
314 * Creates a l3 ARP Ether src modification.
315 *
316 * @param addr the mac address to modify to
317 * @return a l3 modification
318 */
319 public static L3ModificationInstruction modArpSha(MacAddress addr) {
320 checkNotNull(addr, "Src l3 ARP address cannot be null");
321 return new ModArpEthInstruction(L3SubType.ARP_SHA, addr);
322 }
323
324 /**
325 * Creates a l3 ARP operation modification.
326 *
327 * @param op the ARP operation to modify to
328 * @return a l3 modification
329 */
330 public static L3ModificationInstruction modL3ArpOp(short op) {
331 checkNotNull(op, "Arp operation cannot be null");
332 return new ModArpOpInstruction(L3SubType.ARP_OP, op);
333 }
334
335 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800336 * Creates a push MPLS header instruction.
337 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800338 * @return a L2 modification.
339 */
340 public static Instruction pushMpls() {
Jian Li11260a02016-05-19 13:07:22 -0700341 return new L2ModificationInstruction.ModMplsHeaderInstruction(
alshabibd17abc22015-04-21 18:26:35 -0700342 L2ModificationInstruction.L2SubType.MPLS_PUSH,
alshabib7b808c52015-06-26 14:22:24 -0700343 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800344 }
345
346 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800347 * Creates a pop MPLS header instruction.
348 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800349 * @return a L2 modification.
350 */
351 public static Instruction popMpls() {
Jian Li11260a02016-05-19 13:07:22 -0700352 return new L2ModificationInstruction.ModMplsHeaderInstruction(
alshabibd17abc22015-04-21 18:26:35 -0700353 L2ModificationInstruction.L2SubType.MPLS_POP,
alshabib7b808c52015-06-26 14:22:24 -0700354 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800355 }
alshabib7410fea2014-09-16 13:48:39 -0700356
sangho3f97a17d2015-01-29 22:56:29 -0800357 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800358 * Creates a pop MPLS header instruction with a particular ethertype.
sangho3f97a17d2015-01-29 22:56:29 -0800359 *
360 * @param etherType Ethernet type to set
361 * @return a L2 modification.
alshabib7b808c52015-06-26 14:22:24 -0700362 */
363 public static Instruction popMpls(EthType etherType) {
364 checkNotNull(etherType, "Ethernet type cannot be null");
Jian Li11260a02016-05-19 13:07:22 -0700365 return new L2ModificationInstruction.ModMplsHeaderInstruction(
alshabibd17abc22015-04-21 18:26:35 -0700366 L2ModificationInstruction.L2SubType.MPLS_POP, etherType);
sangho3f97a17d2015-01-29 22:56:29 -0800367 }
368
Saurav Dasfbe25c52015-03-04 11:12:00 -0800369 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800370 * Creates a pop VLAN header instruction.
371 *
372 * @return a L2 modification
Saurav Dasfbe25c52015-03-04 11:12:00 -0800373 */
374 public static Instruction popVlan() {
Jian Li11260a02016-05-19 13:07:22 -0700375 return new L2ModificationInstruction.ModVlanHeaderInstruction(
alshabibd17abc22015-04-21 18:26:35 -0700376 L2ModificationInstruction.L2SubType.VLAN_POP);
Saurav Dasfbe25c52015-03-04 11:12:00 -0800377 }
378
379 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800380 * Creates a push VLAN header instruction.
381 *
382 * @return a L2 modification
383 */
384 public static Instruction pushVlan() {
Jian Li11260a02016-05-19 13:07:22 -0700385 return new L2ModificationInstruction.ModVlanHeaderInstruction(
alshabib7b808c52015-06-26 14:22:24 -0700386 L2ModificationInstruction.L2SubType.VLAN_PUSH,
387 EthType.EtherType.VLAN.ethType());
Jonathan Hart54b406b2015-03-06 16:24:14 -0800388 }
389
390 /**
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -0500391 * Creates a push VLAN header instruction using the supplied Ethernet type.
392 *
393 * @param ethType the Ethernet type to use
394 * @return a L2 modification
395 */
396 public static Instruction pushVlan(EthType ethType) {
397 return new L2ModificationInstruction.ModVlanHeaderInstruction(
398 L2ModificationInstruction.L2SubType.VLAN_PUSH,
399 ethType);
400 }
401
402 /**
alshabibd17abc22015-04-21 18:26:35 -0700403 * Sends the packet to the table id.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800404 *
alshabibd17abc22015-04-21 18:26:35 -0700405 * @param tableId flow rule table id
Thomas Vachuska3e2b6512015-03-05 09:25:03 -0800406 * @return table type transition instruction
Saurav Dasfbe25c52015-03-04 11:12:00 -0800407 */
alshabibd17abc22015-04-21 18:26:35 -0700408 public static Instruction transition(Integer tableId) {
409 checkNotNull(tableId, "Table id cannot be null");
410 return new TableTypeTransition(tableId);
alshabib9af70072015-02-09 14:34:16 -0800411 }
412
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800413 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700414 * Writes metadata to associate with a packet.
415 *
416 * @param metadata the metadata value to write
417 * @param metadataMask the bits to mask for the metadata value
418 * @return metadata instruction
419 */
420 public static Instruction writeMetadata(long metadata, long metadataMask) {
421 return new MetadataInstruction(metadata, metadataMask);
422 }
423
424 /**
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700425 * Creates a Tunnel ID modification.
426 *
427 * @param tunnelId the Tunnel ID to modify to
428 * @return a L2 modification
429 */
430 public static L2ModificationInstruction modTunnelId(long tunnelId) {
431 checkNotNull(tunnelId, "Tunnel id cannot be null");
432 return new L2ModificationInstruction.ModTunnelIdInstruction(tunnelId);
433 }
434
435 /**
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700436 * Creates a TCP src modification.
437 *
438 * @param port the TCP port number to modify to
439 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700440 */
441 public static L4ModificationInstruction modTcpSrc(TpPort port) {
442 checkNotNull(port, "Src TCP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700443 return new ModTransportPortInstruction(L4SubType.TCP_SRC, port);
444 }
445
446 /**
447 * Creates a TCP dst modification.
448 *
449 * @param port the TCP port number to modify to
450 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700451 */
452 public static L4ModificationInstruction modTcpDst(TpPort port) {
453 checkNotNull(port, "Dst TCP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700454 return new ModTransportPortInstruction(L4SubType.TCP_DST, port);
455 }
456
457 /**
458 * Creates a UDP src modification.
459 *
460 * @param port the UDP port number to modify to
461 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700462 */
463 public static L4ModificationInstruction modUdpSrc(TpPort port) {
464 checkNotNull(port, "Src UDP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700465 return new ModTransportPortInstruction(L4SubType.UDP_SRC, port);
466 }
467
468 /**
469 * Creates a UDP dst modification.
470 *
471 * @param port the UDP port number to modify to
472 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700473 */
474 public static L4ModificationInstruction modUdpDst(TpPort port) {
475 checkNotNull(port, "Dst UDP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700476 return new ModTransportPortInstruction(L4SubType.UDP_DST, port);
477 }
478
479 /**
Frank Wangdf383212017-06-23 09:17:41 +0800480 * Creates a protocol independent instruction.
481 *
482 * @param piTableAction protocol independent instruction
483 * @return extension instruction
484 */
485 public static PiInstruction piTableAction(PiTableAction piTableAction) {
486 checkNotNull(piTableAction, "PiTableAction instruction cannot be null");
487 return new PiInstruction(piTableAction);
488 }
489
490 /**
Jonathan Hart3c259162015-10-21 21:31:19 -0700491 * Creates an extension instruction.
492 *
493 * @param extension extension instruction
494 * @param deviceId device ID
495 * @return extension instruction
496 */
alshabib880b6442015-11-23 22:13:04 -0800497 public static ExtensionInstructionWrapper extension(ExtensionTreatment extension,
Jonathan Hart3c259162015-10-21 21:31:19 -0700498 DeviceId deviceId) {
499 checkNotNull(extension, "Extension instruction cannot be null");
500 checkNotNull(deviceId, "Device ID cannot be null");
501 return new ExtensionInstructionWrapper(extension, deviceId);
502 }
503
504 /**
Cem Türker3baff672017-10-12 15:09:01 +0300505 * Creates a stat trigger instruction.
506 *
507 * @param statTriggerMap map keeps stat trigger threshold
508 * @param flag stat trigger flag
509 * @return stat trigger instruction
510 */
511 public static StatTriggerInstruction statTrigger(Map<StatTriggerField, Long> statTriggerMap,
512 StatTriggerFlag flag) {
513 checkNotNull(statTriggerMap, "Stat trigger map cannot be null");
514 checkNotNull(flag, "Stat trigger flag cannot be null");
515 return new StatTriggerInstruction(statTriggerMap, flag);
516 }
517
518 /**
Charles Chan7efabeb2015-09-28 15:12:19 -0700519 * No Action instruction.
520 */
521 public static final class NoActionInstruction implements Instruction {
522
523 private NoActionInstruction() {}
524
525 @Override
526 public Type type() {
527 return Type.NOACTION;
528 }
529
530 @Override
531 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800532 return type().toString();
Charles Chan7efabeb2015-09-28 15:12:19 -0700533 }
534
535 @Override
536 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700537 return type().ordinal();
Charles Chan7efabeb2015-09-28 15:12:19 -0700538 }
539
540 @Override
541 public boolean equals(Object obj) {
542 if (this == obj) {
543 return true;
544 }
545 if (obj instanceof NoActionInstruction) {
546 return true;
547 }
548 return false;
549 }
550 }
551
552 /**
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800553 * Output Instruction.
sangho8995ac52015-02-04 11:29:03 -0800554 */
alshabib55a55d92014-09-16 11:59:31 -0700555 public static final class OutputInstruction implements Instruction {
556 private final PortNumber port;
557
558 private OutputInstruction(PortNumber port) {
559 this.port = port;
560 }
561
562 public PortNumber port() {
563 return port;
564 }
565
566 @Override
567 public Type type() {
568 return Type.OUTPUT;
569 }
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800570
alshabib99b8fdc2014-09-25 14:30:22 -0700571 @Override
572 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800573 return type().toString() + SEPARATOR + port.toString();
alshabib99b8fdc2014-09-25 14:30:22 -0700574 }
alshabib8ca53902014-10-07 13:11:17 -0700575
576 @Override
577 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700578 return Objects.hash(type().ordinal(), port);
alshabib8ca53902014-10-07 13:11:17 -0700579 }
580
581 @Override
582 public boolean equals(Object obj) {
583 if (this == obj) {
584 return true;
585 }
586 if (obj instanceof OutputInstruction) {
587 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700588 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700589
590 }
591 return false;
592 }
alshabib55a55d92014-09-16 11:59:31 -0700593 }
594
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800595 /**
596 * Group Instruction.
sangho8995ac52015-02-04 11:29:03 -0800597 */
sangho8995ac52015-02-04 11:29:03 -0800598 public static final class GroupInstruction implements Instruction {
599 private final GroupId groupId;
600
601 private GroupInstruction(GroupId groupId) {
602 this.groupId = groupId;
603 }
604
605 public GroupId groupId() {
606 return groupId;
607 }
608
609 @Override
610 public Type type() {
611 return Type.GROUP;
612 }
alshabib9af70072015-02-09 14:34:16 -0800613
sangho8995ac52015-02-04 11:29:03 -0800614 @Override
615 public String toString() {
Saurav Das0fd79d92016-03-07 10:58:36 -0800616 return type().toString() + SEPARATOR + "0x" + Integer.toHexString(groupId.id());
sangho8995ac52015-02-04 11:29:03 -0800617 }
618
619 @Override
620 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700621 return Objects.hash(type().ordinal(), groupId);
sangho8995ac52015-02-04 11:29:03 -0800622 }
623
624 @Override
625 public boolean equals(Object obj) {
626 if (this == obj) {
627 return true;
628 }
629 if (obj instanceof GroupInstruction) {
630 GroupInstruction that = (GroupInstruction) obj;
631 return Objects.equals(groupId, that.groupId);
632
633 }
634 return false;
635 }
636 }
637
Saurav Das86af8f12015-05-25 23:55:33 -0700638 /**
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200639 * Set-Queue Instruction.
640 */
641 public static final class SetQueueInstruction implements Instruction {
642 private final long queueId;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200643 private final PortNumber port;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200644
645 private SetQueueInstruction(long queueId) {
646 this.queueId = queueId;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200647 this.port = null;
648 }
649
650 private SetQueueInstruction(long queueId, PortNumber port) {
651 this.queueId = queueId;
652 this.port = port;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200653 }
654
655 public long queueId() {
656 return queueId;
657 }
658
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200659 public PortNumber port() {
660 return port;
661 }
662
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200663 @Override
664 public Type type() {
665 return Type.QUEUE;
666 }
667
668 @Override
669 public String toString() {
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200670 MoreObjects.ToStringHelper toStringHelper = toStringHelper(type().toString());
671 toStringHelper.add("queueId", queueId);
672
673 if (port() != null) {
674 toStringHelper.add("port", port);
675 }
676 return toStringHelper.toString();
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200677 }
678
679 @Override
680 public int hashCode() {
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200681 return Objects.hash(type().ordinal(), queueId, port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200682 }
683
684 @Override
685 public boolean equals(Object obj) {
686 if (this == obj) {
687 return true;
688 }
689 if (obj instanceof SetQueueInstruction) {
690 SetQueueInstruction that = (SetQueueInstruction) obj;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200691 return Objects.equals(queueId, that.queueId) && Objects.equals(port, that.port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200692
693 }
694 return false;
695 }
696 }
697
698 /**
alshabib10c810b2015-08-18 16:59:04 -0700699 * A meter instruction.
700 */
701 public static final class MeterInstruction implements Instruction {
702 private final MeterId meterId;
703
704 private MeterInstruction(MeterId meterId) {
705 this.meterId = meterId;
706 }
707
708 public MeterId meterId() {
709 return meterId;
710 }
711
712 @Override
713 public Type type() {
714 return Type.METER;
715 }
716
717 @Override
718 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800719 return type().toString() + SEPARATOR + meterId.id();
alshabib10c810b2015-08-18 16:59:04 -0700720 }
721
722 @Override
723 public int hashCode() {
724 return Objects.hash(type().ordinal(), meterId);
725 }
726
727 @Override
728 public boolean equals(Object obj) {
729 if (this == obj) {
730 return true;
731 }
732 if (obj instanceof MeterInstruction) {
733 MeterInstruction that = (MeterInstruction) obj;
734 return Objects.equals(meterId, that.meterId);
735
736 }
737 return false;
738 }
739 }
740
741 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700742 * Transition instruction.
743 */
alshabibd17abc22015-04-21 18:26:35 -0700744 public static class TableTypeTransition implements Instruction {
745 private final Integer tableId;
746
747 TableTypeTransition(Integer tableId) {
748 this.tableId = tableId;
alshabib9af70072015-02-09 14:34:16 -0800749 }
750
751 @Override
752 public Type type() {
753 return Type.TABLE;
754 }
755
alshabibd17abc22015-04-21 18:26:35 -0700756 public Integer tableId() {
757 return this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800758 }
759
760 @Override
761 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800762 return type().toString() + SEPARATOR + this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800763 }
764
765 @Override
766 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700767 return Objects.hash(type().ordinal(), tableId);
alshabib9af70072015-02-09 14:34:16 -0800768 }
769
770 @Override
771 public boolean equals(Object obj) {
772 if (this == obj) {
773 return true;
774 }
775 if (obj instanceof TableTypeTransition) {
776 TableTypeTransition that = (TableTypeTransition) obj;
alshabibd17abc22015-04-21 18:26:35 -0700777 return Objects.equals(tableId, that.tableId);
alshabib9af70072015-02-09 14:34:16 -0800778
779 }
780 return false;
781 }
Saurav Das86af8f12015-05-25 23:55:33 -0700782 }
alshabib9af70072015-02-09 14:34:16 -0800783
Saurav Das86af8f12015-05-25 23:55:33 -0700784 /**
785 * Metadata instruction.
786 */
787 public static class MetadataInstruction implements Instruction {
788 private final long metadata;
789 private final long metadataMask;
790
791 MetadataInstruction(long metadata, long metadataMask) {
792 this.metadata = metadata;
793 this.metadataMask = metadataMask;
794 }
795
796 @Override
797 public Type type() {
798 return Type.METADATA;
799 }
800
801 public long metadata() {
802 return this.metadata;
803 }
804
805 public long metadataMask() {
806 return this.metadataMask;
807 }
808
809 @Override
810 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800811 return type().toString() + SEPARATOR +
812 Long.toHexString(this.metadata) + "/" +
813 Long.toHexString(this.metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700814 }
815
816 @Override
817 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700818 return Objects.hash(type().ordinal(), metadata, metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700819 }
820
821 @Override
822 public boolean equals(Object obj) {
823 if (this == obj) {
824 return true;
825 }
826 if (obj instanceof MetadataInstruction) {
827 MetadataInstruction that = (MetadataInstruction) obj;
828 return Objects.equals(metadata, that.metadata) &&
829 Objects.equals(metadataMask, that.metadataMask);
830
831 }
832 return false;
833 }
alshabib9af70072015-02-09 14:34:16 -0800834 }
Saurav Das73a7dd42015-08-19 22:20:31 -0700835
Jonathan Hart3c259162015-10-21 21:31:19 -0700836 /**
837 * Extension instruction.
838 */
839 public static class ExtensionInstructionWrapper implements Instruction {
alshabib880b6442015-11-23 22:13:04 -0800840 private final ExtensionTreatment extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700841 private final DeviceId deviceId;
842
alshabib880b6442015-11-23 22:13:04 -0800843 ExtensionInstructionWrapper(ExtensionTreatment extension, DeviceId deviceId) {
844 extensionTreatment = extension;
Jonathan Hart3c259162015-10-21 21:31:19 -0700845 this.deviceId = deviceId;
846 }
847
alshabib880b6442015-11-23 22:13:04 -0800848 public ExtensionTreatment extensionInstruction() {
849 return extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700850 }
851
852 public DeviceId deviceId() {
853 return deviceId;
854 }
855
856 @Override
857 public Type type() {
858 return Type.EXTENSION;
859 }
860
861 @Override
862 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800863 return type().toString() + SEPARATOR + deviceId + "/" + extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700864 }
865
866 @Override
867 public int hashCode() {
alshabib880b6442015-11-23 22:13:04 -0800868 return Objects.hash(type().ordinal(), extensionTreatment, deviceId);
Jonathan Hart3c259162015-10-21 21:31:19 -0700869 }
870
871 @Override
872 public boolean equals(Object obj) {
873 if (this == obj) {
874 return true;
875 }
876 if (obj instanceof ExtensionInstructionWrapper) {
877 ExtensionInstructionWrapper that = (ExtensionInstructionWrapper) obj;
alshabib880b6442015-11-23 22:13:04 -0800878 return Objects.equals(extensionTreatment, that.extensionTreatment)
Jonathan Hart3c259162015-10-21 21:31:19 -0700879 && Objects.equals(deviceId, that.deviceId);
880
881 }
882 return false;
883 }
884 }
885
Cem Türker3baff672017-10-12 15:09:01 +0300886 public static class StatTriggerInstruction implements Instruction {
887 private Map<StatTriggerField, Long> statTriggerFieldMap;
888 private StatTriggerFlag statTriggerFlag;
889
890
891 StatTriggerInstruction(Map<StatTriggerField, Long> statTriggerMap,
892 StatTriggerFlag flag) {
893 this.statTriggerFieldMap = ImmutableMap.copyOf(statTriggerMap);
894 this.statTriggerFlag = flag;
895 }
896
897 public Map<StatTriggerField, Long> getStatTriggerFieldMap() {
898 return statTriggerFieldMap;
899 }
900
901 public StatTriggerFlag getStatTriggerFlag() {
902 return statTriggerFlag;
903 }
904
905 public Long getStatValue(StatTriggerField field) {
906 return statTriggerFieldMap.get(field);
907 }
908
909 @Override
910 public Type type() {
911 return Type.STAT_TRIGGER;
912 }
913
914 @Override
915 public String toString() {
916 return "StatTriggerInstruction{" +
917 "statTriggerFieldMap=" + statTriggerFieldMap +
918 ", statTriggerFlag=" + statTriggerFlag +
919 '}';
920 }
921
922 @Override
923 public boolean equals(Object o) {
924 if (this == o) {
925 return true;
926 }
927 if (o == null || getClass() != o.getClass()) {
928 return false;
929 }
930
931 StatTriggerInstruction that = (StatTriggerInstruction) o;
932
933 if (!Objects.equals(statTriggerFieldMap, that.statTriggerFieldMap)) {
934 return false;
935 }
936
937 return statTriggerFlag == that.statTriggerFlag;
938 }
939
940 @Override
941 public int hashCode() {
942 int result = statTriggerFieldMap != null ? statTriggerFieldMap.hashCode() : 0;
943 result = 31 * result + (statTriggerFlag != null ? statTriggerFlag.hashCode() : 0);
944 return result;
945 }
946 }
947
alshabib55a55d92014-09-16 11:59:31 -0700948}
alshabib8ca53902014-10-07 13:11:17 -0700949
950