blob: d1fc61fd49d0203effea4fc0ba97d3861f281aa6 [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 Gebertba2d3b72015-10-22 11:14:31 +0200104 return new SetQueueInstruction(queueId, port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200105 }
106
Jian Li1ef82db2016-03-03 14:43:21 -0800107 /**
108 * Creates a meter instruction.
109 *
110 * @param meterId Meter Id
111 * @return meter instruction
112 */
alshabib10c810b2015-08-18 16:59:04 -0700113 public static MeterInstruction meterTraffic(final MeterId meterId) {
114 checkNotNull(meterId, "meter id cannot be null");
115 return new MeterInstruction(meterId);
116 }
117
sangho8995ac52015-02-04 11:29:03 -0800118 /**
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700119 * Creates an L0 modification with the specified OCh signal.
120 *
121 * @param lambda OCh signal
122 * @return an L0 modification
123 */
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700124 public static L0ModificationInstruction modL0Lambda(Lambda lambda) {
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700125 checkNotNull(lambda, "L0 OCh signal cannot be null");
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700126
Sho SHIMIZUa114d892016-03-11 16:48:19 -0800127 if (lambda instanceof OchSignal) {
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700128 return new ModOchSignalInstruction((OchSignal) lambda);
129 } else {
130 throw new UnsupportedOperationException(String.format("Unsupported type: %s", lambda));
131 }
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700132 }
133
134 /**
Yafit Hadar52d81552015-10-07 12:26:52 +0300135 * Creates an L1 modification with the specified ODU signal Id.
136 *
137 * @param oduSignalId ODU Signal Id
138 * @return a L1 modification
139 */
140 public static L1ModificationInstruction modL1OduSignalId(OduSignalId oduSignalId) {
141 checkNotNull(oduSignalId, "L1 ODU signal ID cannot be null");
142 return new ModOduSignalIdInstruction(oduSignalId);
143 }
144 /**
alshabib55a55d92014-09-16 11:59:31 -0700145 * Creates a l2 src modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800146 *
147 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700148 * @return a l2 modification
149 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700150 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700151 checkNotNull(addr, "Src l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700152 return new L2ModificationInstruction.ModEtherInstruction(
153 L2ModificationInstruction.L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -0700154 }
155
156 /**
157 * Creates a L2 dst modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800158 *
159 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700160 * @return a L2 modification
161 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700162 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700163 checkNotNull(addr, "Dst l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700164 return new L2ModificationInstruction.ModEtherInstruction(
165 L2ModificationInstruction.L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -0700166 }
167
168 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800169 * Creates a VLAN ID modification.
170 *
171 * @param vlanId the VLAN ID to modify to
alshabib7410fea2014-09-16 13:48:39 -0700172 * @return a L2 modification
173 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700174 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700175 checkNotNull(vlanId, "VLAN id cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700176 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
alshabib55a55d92014-09-16 11:59:31 -0700177 }
178
alshabib7410fea2014-09-16 13:48:39 -0700179 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800180 * Creates a VLAN PCP modification.
181 *
182 * @param vlanPcp the PCP to modify to
alshabib7410fea2014-09-16 13:48:39 -0700183 * @return a L2 modification
184 */
185 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
186 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700187 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
alshabib7410fea2014-09-16 13:48:39 -0700188 }
189
190 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800191 * Creates a MPLS label modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800192 *
193 * @param mplsLabel MPLS label to set
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800194 * @return a L2 Modification
195 */
Michele Santuari4b6019e2014-12-19 11:31:45 +0100196 public static L2ModificationInstruction modMplsLabel(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800197 checkNotNull(mplsLabel, "MPLS label cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700198 return new L2ModificationInstruction.ModMplsLabelInstruction(mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800199 }
sangho3f97a17d2015-01-29 22:56:29 -0800200
201 /**
Saurav Das73a7dd42015-08-19 22:20:31 -0700202 * Creates a MPLS BOS bit modification.
203 *
204 * @param mplsBos MPLS BOS bit to set (true) or unset (false)
205 * @return a L2 Modification
206 */
207 public static L2ModificationInstruction modMplsBos(boolean mplsBos) {
208 return new L2ModificationInstruction.ModMplsBosInstruction(mplsBos);
209 }
210
211 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800212 * Creates a MPLS decrement TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800213 *
214 * @return a L2 Modification
215 */
216 public static L2ModificationInstruction decMplsTtl() {
alshabibd17abc22015-04-21 18:26:35 -0700217 return new L2ModificationInstruction.ModMplsTtlInstruction();
sangho3f97a17d2015-01-29 22:56:29 -0800218 }
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800219
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800220 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800221 * Creates a L3 IPv4 src modification.
222 *
223 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700224 * @return a L3 modification
225 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700226 public static L3ModificationInstruction modL3Src(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800227 checkNotNull(addr, "Src l3 IPv4 address cannot be null");
228 return new ModIPInstruction(L3SubType.IPV4_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700229 }
230
231 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800232 * Creates a L3 IPv4 dst modification.
233 *
234 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700235 * @return a L3 modification
236 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700237 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800238 checkNotNull(addr, "Dst l3 IPv4 address cannot be null");
239 return new ModIPInstruction(L3SubType.IPV4_DST, addr);
240 }
241
242 /**
243 * Creates a L3 IPv6 src modification.
244 *
245 * @param addr the IPv6 address to modify to
246 * @return a L3 modification
247 */
248 public static L3ModificationInstruction modL3IPv6Src(IpAddress addr) {
249 checkNotNull(addr, "Src l3 IPv6 address cannot be null");
250 return new ModIPInstruction(L3SubType.IPV6_SRC, addr);
251 }
252
253 /**
254 * Creates a L3 IPv6 dst modification.
255 *
256 * @param addr the IPv6 address to modify to
257 * @return a L3 modification
258 */
259 public static L3ModificationInstruction modL3IPv6Dst(IpAddress addr) {
260 checkNotNull(addr, "Dst l3 IPv6 address cannot be null");
261 return new ModIPInstruction(L3SubType.IPV6_DST, addr);
262 }
263
264 /**
265 * Creates a L3 IPv6 Flow Label modification.
266 *
267 * @param flowLabel the IPv6 flow label to modify to (20 bits)
268 * @return a L3 modification
269 */
270 public static L3ModificationInstruction modL3IPv6FlowLabel(int flowLabel) {
271 return new ModIPv6FlowLabelInstruction(flowLabel);
alshabib7410fea2014-09-16 13:48:39 -0700272 }
273
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800274 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800275 * Creates a L3 decrement TTL modification.
276 *
sangho3f97a17d2015-01-29 22:56:29 -0800277 * @return a L3 modification
278 */
279 public static L3ModificationInstruction decNwTtl() {
280 return new ModTtlInstruction(L3SubType.DEC_TTL);
281 }
282
283 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800284 * Creates a L3 copy TTL to outer header modification.
285 *
sangho3f97a17d2015-01-29 22:56:29 -0800286 * @return a L3 modification
287 */
288 public static L3ModificationInstruction copyTtlOut() {
289 return new ModTtlInstruction(L3SubType.TTL_OUT);
290 }
291
292 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800293 * Creates a L3 copy TTL to inner header modification.
294 *
sangho3f97a17d2015-01-29 22:56:29 -0800295 * @return a L3 modification
296 */
297 public static L3ModificationInstruction copyTtlIn() {
298 return new ModTtlInstruction(L3SubType.TTL_IN);
299 }
300
301 /**
lishuai3cce60b2015-12-01 19:35:16 +0800302 * Creates a L3 ARP IP src modification.
303 *
304 * @param addr the ip address to modify to
305 * @return a L3 modification
306 */
307 public static L3ModificationInstruction modArpSpa(IpAddress addr) {
308 checkNotNull(addr, "Src l3 ARP IP address cannot be null");
309 return new ModArpIPInstruction(L3SubType.ARP_SPA, addr);
310 }
311
312 /**
313 * Creates a l3 ARP Ether src modification.
314 *
315 * @param addr the mac address to modify to
316 * @return a l3 modification
317 */
318 public static L3ModificationInstruction modArpSha(MacAddress addr) {
319 checkNotNull(addr, "Src l3 ARP address cannot be null");
320 return new ModArpEthInstruction(L3SubType.ARP_SHA, addr);
321 }
322
323 /**
324 * Creates a l3 ARP operation modification.
325 *
326 * @param op the ARP operation to modify to
327 * @return a l3 modification
328 */
329 public static L3ModificationInstruction modL3ArpOp(short op) {
lishuai3cce60b2015-12-01 19:35:16 +0800330 return new ModArpOpInstruction(L3SubType.ARP_OP, op);
331 }
332
333 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800334 * Creates a push MPLS header instruction.
335 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800336 * @return a L2 modification.
337 */
338 public static Instruction pushMpls() {
Jian Li11260a02016-05-19 13:07:22 -0700339 return new L2ModificationInstruction.ModMplsHeaderInstruction(
alshabibd17abc22015-04-21 18:26:35 -0700340 L2ModificationInstruction.L2SubType.MPLS_PUSH,
alshabib7b808c52015-06-26 14:22:24 -0700341 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800342 }
343
344 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800345 * Creates a pop MPLS header instruction.
346 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800347 * @return a L2 modification.
348 */
349 public static Instruction popMpls() {
Jian Li11260a02016-05-19 13:07:22 -0700350 return new L2ModificationInstruction.ModMplsHeaderInstruction(
alshabibd17abc22015-04-21 18:26:35 -0700351 L2ModificationInstruction.L2SubType.MPLS_POP,
alshabib7b808c52015-06-26 14:22:24 -0700352 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800353 }
alshabib7410fea2014-09-16 13:48:39 -0700354
sangho3f97a17d2015-01-29 22:56:29 -0800355 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800356 * Creates a pop MPLS header instruction with a particular ethertype.
sangho3f97a17d2015-01-29 22:56:29 -0800357 *
358 * @param etherType Ethernet type to set
359 * @return a L2 modification.
alshabib7b808c52015-06-26 14:22:24 -0700360 */
361 public static Instruction popMpls(EthType etherType) {
362 checkNotNull(etherType, "Ethernet type cannot be null");
Jian Li11260a02016-05-19 13:07:22 -0700363 return new L2ModificationInstruction.ModMplsHeaderInstruction(
alshabibd17abc22015-04-21 18:26:35 -0700364 L2ModificationInstruction.L2SubType.MPLS_POP, etherType);
sangho3f97a17d2015-01-29 22:56:29 -0800365 }
366
Saurav Dasfbe25c52015-03-04 11:12:00 -0800367 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800368 * Creates a pop VLAN header instruction.
369 *
370 * @return a L2 modification
Saurav Dasfbe25c52015-03-04 11:12:00 -0800371 */
372 public static Instruction popVlan() {
Jian Li11260a02016-05-19 13:07:22 -0700373 return new L2ModificationInstruction.ModVlanHeaderInstruction(
alshabibd17abc22015-04-21 18:26:35 -0700374 L2ModificationInstruction.L2SubType.VLAN_POP);
Saurav Dasfbe25c52015-03-04 11:12:00 -0800375 }
376
377 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800378 * Creates a push VLAN header instruction.
379 *
380 * @return a L2 modification
381 */
382 public static Instruction pushVlan() {
Jian Li11260a02016-05-19 13:07:22 -0700383 return new L2ModificationInstruction.ModVlanHeaderInstruction(
alshabib7b808c52015-06-26 14:22:24 -0700384 L2ModificationInstruction.L2SubType.VLAN_PUSH,
385 EthType.EtherType.VLAN.ethType());
Jonathan Hart54b406b2015-03-06 16:24:14 -0800386 }
387
388 /**
Konstantinos Kanonakis9215ff22016-11-04 13:28:11 -0500389 * Creates a push VLAN header instruction using the supplied Ethernet type.
390 *
391 * @param ethType the Ethernet type to use
392 * @return a L2 modification
393 */
394 public static Instruction pushVlan(EthType ethType) {
395 return new L2ModificationInstruction.ModVlanHeaderInstruction(
396 L2ModificationInstruction.L2SubType.VLAN_PUSH,
397 ethType);
398 }
399
400 /**
alshabibd17abc22015-04-21 18:26:35 -0700401 * Sends the packet to the table id.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800402 *
alshabibd17abc22015-04-21 18:26:35 -0700403 * @param tableId flow rule table id
Thomas Vachuska3e2b6512015-03-05 09:25:03 -0800404 * @return table type transition instruction
Saurav Dasfbe25c52015-03-04 11:12:00 -0800405 */
alshabibd17abc22015-04-21 18:26:35 -0700406 public static Instruction transition(Integer tableId) {
407 checkNotNull(tableId, "Table id cannot be null");
408 return new TableTypeTransition(tableId);
alshabib9af70072015-02-09 14:34:16 -0800409 }
410
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800411 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700412 * Writes metadata to associate with a packet.
413 *
414 * @param metadata the metadata value to write
415 * @param metadataMask the bits to mask for the metadata value
416 * @return metadata instruction
417 */
418 public static Instruction writeMetadata(long metadata, long metadataMask) {
419 return new MetadataInstruction(metadata, metadataMask);
420 }
421
422 /**
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700423 * Creates a Tunnel ID modification.
424 *
425 * @param tunnelId the Tunnel ID to modify to
426 * @return a L2 modification
427 */
428 public static L2ModificationInstruction modTunnelId(long tunnelId) {
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700429 return new L2ModificationInstruction.ModTunnelIdInstruction(tunnelId);
430 }
431
432 /**
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700433 * Creates a TCP src modification.
434 *
435 * @param port the TCP port number to modify to
436 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700437 */
438 public static L4ModificationInstruction modTcpSrc(TpPort port) {
439 checkNotNull(port, "Src TCP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700440 return new ModTransportPortInstruction(L4SubType.TCP_SRC, port);
441 }
442
443 /**
444 * Creates a TCP dst modification.
445 *
446 * @param port the TCP port number to modify to
447 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700448 */
449 public static L4ModificationInstruction modTcpDst(TpPort port) {
450 checkNotNull(port, "Dst TCP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700451 return new ModTransportPortInstruction(L4SubType.TCP_DST, port);
452 }
453
454 /**
455 * Creates a UDP src modification.
456 *
457 * @param port the UDP port number to modify to
458 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700459 */
460 public static L4ModificationInstruction modUdpSrc(TpPort port) {
461 checkNotNull(port, "Src UDP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700462 return new ModTransportPortInstruction(L4SubType.UDP_SRC, port);
463 }
464
465 /**
466 * Creates a UDP dst modification.
467 *
468 * @param port the UDP port number to modify to
469 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700470 */
471 public static L4ModificationInstruction modUdpDst(TpPort port) {
472 checkNotNull(port, "Dst UDP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700473 return new ModTransportPortInstruction(L4SubType.UDP_DST, port);
474 }
475
476 /**
Frank Wangdf383212017-06-23 09:17:41 +0800477 * Creates a protocol independent instruction.
478 *
479 * @param piTableAction protocol independent instruction
480 * @return extension instruction
481 */
482 public static PiInstruction piTableAction(PiTableAction piTableAction) {
483 checkNotNull(piTableAction, "PiTableAction instruction cannot be null");
484 return new PiInstruction(piTableAction);
485 }
486
487 /**
Thiago Santos4a69ef82018-08-21 21:18:05 -0700488 * Creates an IP DSCP modification.
489 *
Rory Savagedaeb9492020-02-18 14:35:50 -0500490 * @param ipDscp the DSCP value to modify to
Thiago Santos4a69ef82018-08-21 21:18:05 -0700491 * @return a L3 modification
492 */
Rory Savagedaeb9492020-02-18 14:35:50 -0500493 public static Instruction modIpDscp(byte ipDscp) {
494 return new L3ModificationInstruction.ModDscpInstruction(L3SubType.IP_DSCP, ipDscp);
Thiago Santos4a69ef82018-08-21 21:18:05 -0700495 }
496
497 /**
Jonathan Hart3c259162015-10-21 21:31:19 -0700498 * Creates an extension instruction.
499 *
500 * @param extension extension instruction
501 * @param deviceId device ID
502 * @return extension instruction
503 */
alshabib880b6442015-11-23 22:13:04 -0800504 public static ExtensionInstructionWrapper extension(ExtensionTreatment extension,
Jonathan Hart3c259162015-10-21 21:31:19 -0700505 DeviceId deviceId) {
506 checkNotNull(extension, "Extension instruction cannot be null");
507 checkNotNull(deviceId, "Device ID cannot be null");
508 return new ExtensionInstructionWrapper(extension, deviceId);
509 }
510
511 /**
Cem Türker3baff672017-10-12 15:09:01 +0300512 * Creates a stat trigger instruction.
513 *
514 * @param statTriggerMap map keeps stat trigger threshold
515 * @param flag stat trigger flag
516 * @return stat trigger instruction
517 */
518 public static StatTriggerInstruction statTrigger(Map<StatTriggerField, Long> statTriggerMap,
519 StatTriggerFlag flag) {
520 checkNotNull(statTriggerMap, "Stat trigger map cannot be null");
521 checkNotNull(flag, "Stat trigger flag cannot be null");
522 return new StatTriggerInstruction(statTriggerMap, flag);
523 }
524
525 /**
Charles Chan7efabeb2015-09-28 15:12:19 -0700526 * No Action instruction.
527 */
528 public static final class NoActionInstruction implements Instruction {
529
530 private NoActionInstruction() {}
531
532 @Override
533 public Type type() {
534 return Type.NOACTION;
535 }
536
537 @Override
538 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800539 return type().toString();
Charles Chan7efabeb2015-09-28 15:12:19 -0700540 }
541
542 @Override
543 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700544 return type().ordinal();
Charles Chan7efabeb2015-09-28 15:12:19 -0700545 }
546
547 @Override
548 public boolean equals(Object obj) {
549 if (this == obj) {
550 return true;
551 }
552 if (obj instanceof NoActionInstruction) {
553 return true;
554 }
555 return false;
556 }
557 }
558
559 /**
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800560 * Output Instruction.
sangho8995ac52015-02-04 11:29:03 -0800561 */
alshabib55a55d92014-09-16 11:59:31 -0700562 public static final class OutputInstruction implements Instruction {
563 private final PortNumber port;
564
565 private OutputInstruction(PortNumber port) {
566 this.port = port;
567 }
568
569 public PortNumber port() {
570 return port;
571 }
572
573 @Override
574 public Type type() {
575 return Type.OUTPUT;
576 }
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800577
alshabib99b8fdc2014-09-25 14:30:22 -0700578 @Override
579 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800580 return type().toString() + SEPARATOR + port.toString();
alshabib99b8fdc2014-09-25 14:30:22 -0700581 }
alshabib8ca53902014-10-07 13:11:17 -0700582
583 @Override
584 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700585 return Objects.hash(type().ordinal(), port);
alshabib8ca53902014-10-07 13:11:17 -0700586 }
587
588 @Override
589 public boolean equals(Object obj) {
590 if (this == obj) {
591 return true;
592 }
593 if (obj instanceof OutputInstruction) {
594 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700595 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700596
597 }
598 return false;
599 }
alshabib55a55d92014-09-16 11:59:31 -0700600 }
601
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800602 /**
603 * Group Instruction.
sangho8995ac52015-02-04 11:29:03 -0800604 */
sangho8995ac52015-02-04 11:29:03 -0800605 public static final class GroupInstruction implements Instruction {
606 private final GroupId groupId;
607
608 private GroupInstruction(GroupId groupId) {
609 this.groupId = groupId;
610 }
611
612 public GroupId groupId() {
613 return groupId;
614 }
615
616 @Override
617 public Type type() {
618 return Type.GROUP;
619 }
alshabib9af70072015-02-09 14:34:16 -0800620
sangho8995ac52015-02-04 11:29:03 -0800621 @Override
622 public String toString() {
Saurav Das0fd79d92016-03-07 10:58:36 -0800623 return type().toString() + SEPARATOR + "0x" + Integer.toHexString(groupId.id());
sangho8995ac52015-02-04 11:29:03 -0800624 }
625
626 @Override
627 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700628 return Objects.hash(type().ordinal(), groupId);
sangho8995ac52015-02-04 11:29:03 -0800629 }
630
631 @Override
632 public boolean equals(Object obj) {
633 if (this == obj) {
634 return true;
635 }
636 if (obj instanceof GroupInstruction) {
637 GroupInstruction that = (GroupInstruction) obj;
638 return Objects.equals(groupId, that.groupId);
639
640 }
641 return false;
642 }
643 }
644
Saurav Das86af8f12015-05-25 23:55:33 -0700645 /**
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200646 * Set-Queue Instruction.
647 */
648 public static final class SetQueueInstruction implements Instruction {
649 private final long queueId;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200650 private final PortNumber port;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200651
652 private SetQueueInstruction(long queueId) {
653 this.queueId = queueId;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200654 this.port = null;
655 }
656
657 private SetQueueInstruction(long queueId, PortNumber port) {
658 this.queueId = queueId;
659 this.port = port;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200660 }
661
662 public long queueId() {
663 return queueId;
664 }
665
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200666 public PortNumber port() {
667 return port;
668 }
669
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200670 @Override
671 public Type type() {
672 return Type.QUEUE;
673 }
674
675 @Override
676 public String toString() {
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200677 MoreObjects.ToStringHelper toStringHelper = toStringHelper(type().toString());
678 toStringHelper.add("queueId", queueId);
679
680 if (port() != null) {
681 toStringHelper.add("port", port);
682 }
683 return toStringHelper.toString();
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200684 }
685
686 @Override
687 public int hashCode() {
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200688 return Objects.hash(type().ordinal(), queueId, port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200689 }
690
691 @Override
692 public boolean equals(Object obj) {
693 if (this == obj) {
694 return true;
695 }
696 if (obj instanceof SetQueueInstruction) {
697 SetQueueInstruction that = (SetQueueInstruction) obj;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200698 return Objects.equals(queueId, that.queueId) && Objects.equals(port, that.port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200699
700 }
701 return false;
702 }
703 }
704
705 /**
alshabib10c810b2015-08-18 16:59:04 -0700706 * A meter instruction.
707 */
708 public static final class MeterInstruction implements Instruction {
709 private final MeterId meterId;
710
711 private MeterInstruction(MeterId meterId) {
712 this.meterId = meterId;
713 }
714
715 public MeterId meterId() {
716 return meterId;
717 }
718
719 @Override
720 public Type type() {
721 return Type.METER;
722 }
723
724 @Override
725 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800726 return type().toString() + SEPARATOR + meterId.id();
alshabib10c810b2015-08-18 16:59:04 -0700727 }
728
729 @Override
730 public int hashCode() {
731 return Objects.hash(type().ordinal(), meterId);
732 }
733
734 @Override
735 public boolean equals(Object obj) {
736 if (this == obj) {
737 return true;
738 }
739 if (obj instanceof MeterInstruction) {
740 MeterInstruction that = (MeterInstruction) obj;
741 return Objects.equals(meterId, that.meterId);
742
743 }
744 return false;
745 }
746 }
747
748 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700749 * Transition instruction.
750 */
alshabibd17abc22015-04-21 18:26:35 -0700751 public static class TableTypeTransition implements Instruction {
752 private final Integer tableId;
753
754 TableTypeTransition(Integer tableId) {
755 this.tableId = tableId;
alshabib9af70072015-02-09 14:34:16 -0800756 }
757
758 @Override
759 public Type type() {
760 return Type.TABLE;
761 }
762
alshabibd17abc22015-04-21 18:26:35 -0700763 public Integer tableId() {
764 return this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800765 }
766
767 @Override
768 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800769 return type().toString() + SEPARATOR + this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800770 }
771
772 @Override
773 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700774 return Objects.hash(type().ordinal(), tableId);
alshabib9af70072015-02-09 14:34:16 -0800775 }
776
777 @Override
778 public boolean equals(Object obj) {
779 if (this == obj) {
780 return true;
781 }
782 if (obj instanceof TableTypeTransition) {
783 TableTypeTransition that = (TableTypeTransition) obj;
alshabibd17abc22015-04-21 18:26:35 -0700784 return Objects.equals(tableId, that.tableId);
alshabib9af70072015-02-09 14:34:16 -0800785
786 }
787 return false;
788 }
Saurav Das86af8f12015-05-25 23:55:33 -0700789 }
alshabib9af70072015-02-09 14:34:16 -0800790
Saurav Das86af8f12015-05-25 23:55:33 -0700791 /**
792 * Metadata instruction.
793 */
794 public static class MetadataInstruction implements Instruction {
795 private final long metadata;
796 private final long metadataMask;
797
798 MetadataInstruction(long metadata, long metadataMask) {
799 this.metadata = metadata;
800 this.metadataMask = metadataMask;
801 }
802
803 @Override
804 public Type type() {
805 return Type.METADATA;
806 }
807
808 public long metadata() {
809 return this.metadata;
810 }
811
812 public long metadataMask() {
813 return this.metadataMask;
814 }
815
816 @Override
817 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800818 return type().toString() + SEPARATOR +
819 Long.toHexString(this.metadata) + "/" +
820 Long.toHexString(this.metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700821 }
822
823 @Override
824 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700825 return Objects.hash(type().ordinal(), metadata, metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700826 }
827
828 @Override
829 public boolean equals(Object obj) {
830 if (this == obj) {
831 return true;
832 }
833 if (obj instanceof MetadataInstruction) {
834 MetadataInstruction that = (MetadataInstruction) obj;
835 return Objects.equals(metadata, that.metadata) &&
836 Objects.equals(metadataMask, that.metadataMask);
837
838 }
839 return false;
840 }
alshabib9af70072015-02-09 14:34:16 -0800841 }
Saurav Das73a7dd42015-08-19 22:20:31 -0700842
Jonathan Hart3c259162015-10-21 21:31:19 -0700843 /**
844 * Extension instruction.
845 */
846 public static class ExtensionInstructionWrapper implements Instruction {
alshabib880b6442015-11-23 22:13:04 -0800847 private final ExtensionTreatment extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700848 private final DeviceId deviceId;
849
alshabib880b6442015-11-23 22:13:04 -0800850 ExtensionInstructionWrapper(ExtensionTreatment extension, DeviceId deviceId) {
851 extensionTreatment = extension;
Jonathan Hart3c259162015-10-21 21:31:19 -0700852 this.deviceId = deviceId;
853 }
854
alshabib880b6442015-11-23 22:13:04 -0800855 public ExtensionTreatment extensionInstruction() {
856 return extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700857 }
858
859 public DeviceId deviceId() {
860 return deviceId;
861 }
862
863 @Override
864 public Type type() {
865 return Type.EXTENSION;
866 }
867
868 @Override
869 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800870 return type().toString() + SEPARATOR + deviceId + "/" + extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700871 }
872
873 @Override
874 public int hashCode() {
alshabib880b6442015-11-23 22:13:04 -0800875 return Objects.hash(type().ordinal(), extensionTreatment, deviceId);
Jonathan Hart3c259162015-10-21 21:31:19 -0700876 }
877
878 @Override
879 public boolean equals(Object obj) {
880 if (this == obj) {
881 return true;
882 }
883 if (obj instanceof ExtensionInstructionWrapper) {
884 ExtensionInstructionWrapper that = (ExtensionInstructionWrapper) obj;
alshabib880b6442015-11-23 22:13:04 -0800885 return Objects.equals(extensionTreatment, that.extensionTreatment)
Jonathan Hart3c259162015-10-21 21:31:19 -0700886 && Objects.equals(deviceId, that.deviceId);
887
888 }
889 return false;
890 }
891 }
892
Cem Türker3baff672017-10-12 15:09:01 +0300893 public static class StatTriggerInstruction implements Instruction {
894 private Map<StatTriggerField, Long> statTriggerFieldMap;
895 private StatTriggerFlag statTriggerFlag;
896
897
898 StatTriggerInstruction(Map<StatTriggerField, Long> statTriggerMap,
899 StatTriggerFlag flag) {
900 this.statTriggerFieldMap = ImmutableMap.copyOf(statTriggerMap);
901 this.statTriggerFlag = flag;
902 }
903
904 public Map<StatTriggerField, Long> getStatTriggerFieldMap() {
905 return statTriggerFieldMap;
906 }
907
908 public StatTriggerFlag getStatTriggerFlag() {
909 return statTriggerFlag;
910 }
911
912 public Long getStatValue(StatTriggerField field) {
913 return statTriggerFieldMap.get(field);
914 }
915
916 @Override
917 public Type type() {
918 return Type.STAT_TRIGGER;
919 }
920
921 @Override
922 public String toString() {
923 return "StatTriggerInstruction{" +
924 "statTriggerFieldMap=" + statTriggerFieldMap +
925 ", statTriggerFlag=" + statTriggerFlag +
926 '}';
927 }
928
929 @Override
930 public boolean equals(Object o) {
931 if (this == o) {
932 return true;
933 }
934 if (o == null || getClass() != o.getClass()) {
935 return false;
936 }
937
938 StatTriggerInstruction that = (StatTriggerInstruction) o;
939
940 if (!Objects.equals(statTriggerFieldMap, that.statTriggerFieldMap)) {
941 return false;
942 }
943
944 return statTriggerFlag == that.statTriggerFlag;
945 }
946
947 @Override
948 public int hashCode() {
949 int result = statTriggerFieldMap != null ? statTriggerFieldMap.hashCode() : 0;
950 result = 31 * result + (statTriggerFlag != null ? statTriggerFlag.hashCode() : 0);
951 return result;
952 }
953 }
954
alshabib55a55d92014-09-16 11:59:31 -0700955}
alshabib8ca53902014-10-07 13:11:17 -0700956
957