blob: 33b74829dadf0bec086a99d805cbcab6a9cf736b [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 /**
Jonathan Hart3c259162015-10-21 21:31:19 -0700488 * Creates an extension instruction.
489 *
490 * @param extension extension instruction
491 * @param deviceId device ID
492 * @return extension instruction
493 */
alshabib880b6442015-11-23 22:13:04 -0800494 public static ExtensionInstructionWrapper extension(ExtensionTreatment extension,
Jonathan Hart3c259162015-10-21 21:31:19 -0700495 DeviceId deviceId) {
496 checkNotNull(extension, "Extension instruction cannot be null");
497 checkNotNull(deviceId, "Device ID cannot be null");
498 return new ExtensionInstructionWrapper(extension, deviceId);
499 }
500
501 /**
Cem Türker3baff672017-10-12 15:09:01 +0300502 * Creates a stat trigger instruction.
503 *
504 * @param statTriggerMap map keeps stat trigger threshold
505 * @param flag stat trigger flag
506 * @return stat trigger instruction
507 */
508 public static StatTriggerInstruction statTrigger(Map<StatTriggerField, Long> statTriggerMap,
509 StatTriggerFlag flag) {
510 checkNotNull(statTriggerMap, "Stat trigger map cannot be null");
511 checkNotNull(flag, "Stat trigger flag cannot be null");
512 return new StatTriggerInstruction(statTriggerMap, flag);
513 }
514
515 /**
Charles Chan7efabeb2015-09-28 15:12:19 -0700516 * No Action instruction.
517 */
518 public static final class NoActionInstruction implements Instruction {
519
520 private NoActionInstruction() {}
521
522 @Override
523 public Type type() {
524 return Type.NOACTION;
525 }
526
527 @Override
528 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800529 return type().toString();
Charles Chan7efabeb2015-09-28 15:12:19 -0700530 }
531
532 @Override
533 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700534 return type().ordinal();
Charles Chan7efabeb2015-09-28 15:12:19 -0700535 }
536
537 @Override
538 public boolean equals(Object obj) {
539 if (this == obj) {
540 return true;
541 }
542 if (obj instanceof NoActionInstruction) {
543 return true;
544 }
545 return false;
546 }
547 }
548
549 /**
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800550 * Output Instruction.
sangho8995ac52015-02-04 11:29:03 -0800551 */
alshabib55a55d92014-09-16 11:59:31 -0700552 public static final class OutputInstruction implements Instruction {
553 private final PortNumber port;
554
555 private OutputInstruction(PortNumber port) {
556 this.port = port;
557 }
558
559 public PortNumber port() {
560 return port;
561 }
562
563 @Override
564 public Type type() {
565 return Type.OUTPUT;
566 }
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800567
alshabib99b8fdc2014-09-25 14:30:22 -0700568 @Override
569 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800570 return type().toString() + SEPARATOR + port.toString();
alshabib99b8fdc2014-09-25 14:30:22 -0700571 }
alshabib8ca53902014-10-07 13:11:17 -0700572
573 @Override
574 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700575 return Objects.hash(type().ordinal(), port);
alshabib8ca53902014-10-07 13:11:17 -0700576 }
577
578 @Override
579 public boolean equals(Object obj) {
580 if (this == obj) {
581 return true;
582 }
583 if (obj instanceof OutputInstruction) {
584 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700585 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700586
587 }
588 return false;
589 }
alshabib55a55d92014-09-16 11:59:31 -0700590 }
591
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800592 /**
593 * Group Instruction.
sangho8995ac52015-02-04 11:29:03 -0800594 */
sangho8995ac52015-02-04 11:29:03 -0800595 public static final class GroupInstruction implements Instruction {
596 private final GroupId groupId;
597
598 private GroupInstruction(GroupId groupId) {
599 this.groupId = groupId;
600 }
601
602 public GroupId groupId() {
603 return groupId;
604 }
605
606 @Override
607 public Type type() {
608 return Type.GROUP;
609 }
alshabib9af70072015-02-09 14:34:16 -0800610
sangho8995ac52015-02-04 11:29:03 -0800611 @Override
612 public String toString() {
Saurav Das0fd79d92016-03-07 10:58:36 -0800613 return type().toString() + SEPARATOR + "0x" + Integer.toHexString(groupId.id());
sangho8995ac52015-02-04 11:29:03 -0800614 }
615
616 @Override
617 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700618 return Objects.hash(type().ordinal(), groupId);
sangho8995ac52015-02-04 11:29:03 -0800619 }
620
621 @Override
622 public boolean equals(Object obj) {
623 if (this == obj) {
624 return true;
625 }
626 if (obj instanceof GroupInstruction) {
627 GroupInstruction that = (GroupInstruction) obj;
628 return Objects.equals(groupId, that.groupId);
629
630 }
631 return false;
632 }
633 }
634
Saurav Das86af8f12015-05-25 23:55:33 -0700635 /**
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200636 * Set-Queue Instruction.
637 */
638 public static final class SetQueueInstruction implements Instruction {
639 private final long queueId;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200640 private final PortNumber port;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200641
642 private SetQueueInstruction(long queueId) {
643 this.queueId = queueId;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200644 this.port = null;
645 }
646
647 private SetQueueInstruction(long queueId, PortNumber port) {
648 this.queueId = queueId;
649 this.port = port;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200650 }
651
652 public long queueId() {
653 return queueId;
654 }
655
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200656 public PortNumber port() {
657 return port;
658 }
659
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200660 @Override
661 public Type type() {
662 return Type.QUEUE;
663 }
664
665 @Override
666 public String toString() {
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200667 MoreObjects.ToStringHelper toStringHelper = toStringHelper(type().toString());
668 toStringHelper.add("queueId", queueId);
669
670 if (port() != null) {
671 toStringHelper.add("port", port);
672 }
673 return toStringHelper.toString();
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200674 }
675
676 @Override
677 public int hashCode() {
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200678 return Objects.hash(type().ordinal(), queueId, port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200679 }
680
681 @Override
682 public boolean equals(Object obj) {
683 if (this == obj) {
684 return true;
685 }
686 if (obj instanceof SetQueueInstruction) {
687 SetQueueInstruction that = (SetQueueInstruction) obj;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200688 return Objects.equals(queueId, that.queueId) && Objects.equals(port, that.port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200689
690 }
691 return false;
692 }
693 }
694
695 /**
alshabib10c810b2015-08-18 16:59:04 -0700696 * A meter instruction.
697 */
698 public static final class MeterInstruction implements Instruction {
699 private final MeterId meterId;
700
701 private MeterInstruction(MeterId meterId) {
702 this.meterId = meterId;
703 }
704
705 public MeterId meterId() {
706 return meterId;
707 }
708
709 @Override
710 public Type type() {
711 return Type.METER;
712 }
713
714 @Override
715 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800716 return type().toString() + SEPARATOR + meterId.id();
alshabib10c810b2015-08-18 16:59:04 -0700717 }
718
719 @Override
720 public int hashCode() {
721 return Objects.hash(type().ordinal(), meterId);
722 }
723
724 @Override
725 public boolean equals(Object obj) {
726 if (this == obj) {
727 return true;
728 }
729 if (obj instanceof MeterInstruction) {
730 MeterInstruction that = (MeterInstruction) obj;
731 return Objects.equals(meterId, that.meterId);
732
733 }
734 return false;
735 }
736 }
737
738 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700739 * Transition instruction.
740 */
alshabibd17abc22015-04-21 18:26:35 -0700741 public static class TableTypeTransition implements Instruction {
742 private final Integer tableId;
743
744 TableTypeTransition(Integer tableId) {
745 this.tableId = tableId;
alshabib9af70072015-02-09 14:34:16 -0800746 }
747
748 @Override
749 public Type type() {
750 return Type.TABLE;
751 }
752
alshabibd17abc22015-04-21 18:26:35 -0700753 public Integer tableId() {
754 return this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800755 }
756
757 @Override
758 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800759 return type().toString() + SEPARATOR + this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800760 }
761
762 @Override
763 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700764 return Objects.hash(type().ordinal(), tableId);
alshabib9af70072015-02-09 14:34:16 -0800765 }
766
767 @Override
768 public boolean equals(Object obj) {
769 if (this == obj) {
770 return true;
771 }
772 if (obj instanceof TableTypeTransition) {
773 TableTypeTransition that = (TableTypeTransition) obj;
alshabibd17abc22015-04-21 18:26:35 -0700774 return Objects.equals(tableId, that.tableId);
alshabib9af70072015-02-09 14:34:16 -0800775
776 }
777 return false;
778 }
Saurav Das86af8f12015-05-25 23:55:33 -0700779 }
alshabib9af70072015-02-09 14:34:16 -0800780
Saurav Das86af8f12015-05-25 23:55:33 -0700781 /**
782 * Metadata instruction.
783 */
784 public static class MetadataInstruction implements Instruction {
785 private final long metadata;
786 private final long metadataMask;
787
788 MetadataInstruction(long metadata, long metadataMask) {
789 this.metadata = metadata;
790 this.metadataMask = metadataMask;
791 }
792
793 @Override
794 public Type type() {
795 return Type.METADATA;
796 }
797
798 public long metadata() {
799 return this.metadata;
800 }
801
802 public long metadataMask() {
803 return this.metadataMask;
804 }
805
806 @Override
807 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800808 return type().toString() + SEPARATOR +
809 Long.toHexString(this.metadata) + "/" +
810 Long.toHexString(this.metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700811 }
812
813 @Override
814 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700815 return Objects.hash(type().ordinal(), metadata, metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700816 }
817
818 @Override
819 public boolean equals(Object obj) {
820 if (this == obj) {
821 return true;
822 }
823 if (obj instanceof MetadataInstruction) {
824 MetadataInstruction that = (MetadataInstruction) obj;
825 return Objects.equals(metadata, that.metadata) &&
826 Objects.equals(metadataMask, that.metadataMask);
827
828 }
829 return false;
830 }
alshabib9af70072015-02-09 14:34:16 -0800831 }
Saurav Das73a7dd42015-08-19 22:20:31 -0700832
Jonathan Hart3c259162015-10-21 21:31:19 -0700833 /**
834 * Extension instruction.
835 */
836 public static class ExtensionInstructionWrapper implements Instruction {
alshabib880b6442015-11-23 22:13:04 -0800837 private final ExtensionTreatment extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700838 private final DeviceId deviceId;
839
alshabib880b6442015-11-23 22:13:04 -0800840 ExtensionInstructionWrapper(ExtensionTreatment extension, DeviceId deviceId) {
841 extensionTreatment = extension;
Jonathan Hart3c259162015-10-21 21:31:19 -0700842 this.deviceId = deviceId;
843 }
844
alshabib880b6442015-11-23 22:13:04 -0800845 public ExtensionTreatment extensionInstruction() {
846 return extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700847 }
848
849 public DeviceId deviceId() {
850 return deviceId;
851 }
852
853 @Override
854 public Type type() {
855 return Type.EXTENSION;
856 }
857
858 @Override
859 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800860 return type().toString() + SEPARATOR + deviceId + "/" + extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700861 }
862
863 @Override
864 public int hashCode() {
alshabib880b6442015-11-23 22:13:04 -0800865 return Objects.hash(type().ordinal(), extensionTreatment, deviceId);
Jonathan Hart3c259162015-10-21 21:31:19 -0700866 }
867
868 @Override
869 public boolean equals(Object obj) {
870 if (this == obj) {
871 return true;
872 }
873 if (obj instanceof ExtensionInstructionWrapper) {
874 ExtensionInstructionWrapper that = (ExtensionInstructionWrapper) obj;
alshabib880b6442015-11-23 22:13:04 -0800875 return Objects.equals(extensionTreatment, that.extensionTreatment)
Jonathan Hart3c259162015-10-21 21:31:19 -0700876 && Objects.equals(deviceId, that.deviceId);
877
878 }
879 return false;
880 }
881 }
882
Cem Türker3baff672017-10-12 15:09:01 +0300883 public static class StatTriggerInstruction implements Instruction {
884 private Map<StatTriggerField, Long> statTriggerFieldMap;
885 private StatTriggerFlag statTriggerFlag;
886
887
888 StatTriggerInstruction(Map<StatTriggerField, Long> statTriggerMap,
889 StatTriggerFlag flag) {
890 this.statTriggerFieldMap = ImmutableMap.copyOf(statTriggerMap);
891 this.statTriggerFlag = flag;
892 }
893
894 public Map<StatTriggerField, Long> getStatTriggerFieldMap() {
895 return statTriggerFieldMap;
896 }
897
898 public StatTriggerFlag getStatTriggerFlag() {
899 return statTriggerFlag;
900 }
901
902 public Long getStatValue(StatTriggerField field) {
903 return statTriggerFieldMap.get(field);
904 }
905
906 @Override
907 public Type type() {
908 return Type.STAT_TRIGGER;
909 }
910
911 @Override
912 public String toString() {
913 return "StatTriggerInstruction{" +
914 "statTriggerFieldMap=" + statTriggerFieldMap +
915 ", statTriggerFlag=" + statTriggerFlag +
916 '}';
917 }
918
919 @Override
920 public boolean equals(Object o) {
921 if (this == o) {
922 return true;
923 }
924 if (o == null || getClass() != o.getClass()) {
925 return false;
926 }
927
928 StatTriggerInstruction that = (StatTriggerInstruction) o;
929
930 if (!Objects.equals(statTriggerFieldMap, that.statTriggerFieldMap)) {
931 return false;
932 }
933
934 return statTriggerFlag == that.statTriggerFlag;
935 }
936
937 @Override
938 public int hashCode() {
939 int result = statTriggerFieldMap != null ? statTriggerFieldMap.hashCode() : 0;
940 result = 31 * result + (statTriggerFlag != null ? statTriggerFlag.hashCode() : 0);
941 return result;
942 }
943 }
944
alshabib55a55d92014-09-16 11:59:31 -0700945}
alshabib8ca53902014-10-07 13:11:17 -0700946
947