blob: b0fbf5b4e725537b35bea8c2306b378b164626d2 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow.instructions;
alshabib55a55d92014-09-16 11:59:31 -070017
Steffen Gebertba2d3b72015-10-22 11:14:31 +020018import com.google.common.base.MoreObjects;
alshabib7b808c52015-06-26 14:22:24 -070019import org.onlab.packet.EthType;
Jonathan Hart54b406b2015-03-06 16:24:14 -080020import org.onlab.packet.IpAddress;
21import org.onlab.packet.MacAddress;
22import org.onlab.packet.MplsLabel;
Hyunsun Mooncf732fb2015-08-22 21:04:23 -070023import org.onlab.packet.TpPort;
Jonathan Hart54b406b2015-03-06 16:24:14 -080024import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080025import org.onosproject.core.GroupId;
Jonathan Hart3c259162015-10-21 21:31:19 -070026import org.onosproject.net.DeviceId;
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070027import org.onosproject.net.Lambda;
Sho SHIMIZUe9e12752015-05-05 14:45:40 -070028import org.onosproject.net.OchSignal;
Yafit Hadar52d81552015-10-07 12:26:52 +030029import org.onosproject.net.OduSignalId;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.PortNumber;
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -070031import org.onosproject.net.flow.instructions.L0ModificationInstruction.ModOchSignalInstruction;
Yafit Hadar52d81552015-10-07 12:26:52 +030032import org.onosproject.net.flow.instructions.L1ModificationInstruction.ModOduSignalIdInstruction;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType;
34import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPInstruction;
lishuai3cce60b2015-12-01 19:35:16 +080035import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpIPInstruction;
36import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpEthInstruction;
37import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModArpOpInstruction;
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -080038import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModIPv6FlowLabelInstruction;
sangho3f97a17d2015-01-29 22:56:29 -080039import org.onosproject.net.flow.instructions.L3ModificationInstruction.ModTtlInstruction;
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -070040import org.onosproject.net.flow.instructions.L4ModificationInstruction.L4SubType;
41import org.onosproject.net.flow.instructions.L4ModificationInstruction.ModTransportPortInstruction;
alshabib10c810b2015-08-18 16:59:04 -070042import org.onosproject.net.meter.MeterId;
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -080043
Jonathan Hart54b406b2015-03-06 16:24:14 -080044import java.util.Objects;
45
46import static com.google.common.base.MoreObjects.toStringHelper;
47import static com.google.common.base.Preconditions.checkNotNull;
alshabib64231f62014-09-16 17:58:36 -070048
alshabib55a55d92014-09-16 11:59:31 -070049/**
50 * Factory class for creating various traffic treatment instructions.
51 */
52public final class Instructions {
53
Jonathan Hartc7840bd2016-01-21 23:26:29 -080054 private static final String SEPARATOR = ":";
55
alshabib55a55d92014-09-16 11:59:31 -070056 // Ban construction
57 private Instructions() {}
58
59 /**
60 * Creates an output instruction using the specified port number. This can
61 * include logical ports such as CONTROLLER, FLOOD, etc.
62 *
63 * @param number port number
64 * @return output instruction
65 */
66 public static OutputInstruction createOutput(final PortNumber number) {
67 checkNotNull(number, "PortNumber cannot be null");
68 return new OutputInstruction(number);
69 }
70
71 /**
Charles Chan7efabeb2015-09-28 15:12:19 -070072 * Creates a no action instruction.
73 *
74 * @return no action instruction
75 */
76 public static NoActionInstruction createNoAction() {
77 return new NoActionInstruction();
78 }
79
80 /**
sangho8995ac52015-02-04 11:29:03 -080081 * Creates a group instruction.
82 *
83 * @param groupId Group Id
84 * @return group instruction
85 */
86 public static GroupInstruction createGroup(final GroupId groupId) {
87 checkNotNull(groupId, "GroupId cannot be null");
88 return new GroupInstruction(groupId);
89 }
90
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +020091 /**
92 * Creates a set-queue instruction.
93 *
94 * @param queueId Queue Id
Steffen Gebertba2d3b72015-10-22 11:14:31 +020095 * @param port Port number
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +020096 * @return set-queue instruction
97 */
Steffen Gebertba2d3b72015-10-22 11:14:31 +020098 public static SetQueueInstruction setQueue(final long queueId, final PortNumber port) {
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +020099 checkNotNull(queueId, "queue ID cannot be null");
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200100 return new SetQueueInstruction(queueId, port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200101 }
102
Jian Li1ef82db2016-03-03 14:43:21 -0800103 /**
104 * Creates a meter instruction.
105 *
106 * @param meterId Meter Id
107 * @return meter instruction
108 */
alshabib10c810b2015-08-18 16:59:04 -0700109 public static MeterInstruction meterTraffic(final MeterId meterId) {
110 checkNotNull(meterId, "meter id cannot be null");
111 return new MeterInstruction(meterId);
112 }
113
sangho8995ac52015-02-04 11:29:03 -0800114 /**
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700115 * Creates an L0 modification with the specified OCh signal.
116 *
117 * @param lambda OCh signal
118 * @return an L0 modification
119 */
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700120 public static L0ModificationInstruction modL0Lambda(Lambda lambda) {
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700121 checkNotNull(lambda, "L0 OCh signal cannot be null");
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700122
Sho SHIMIZUa114d892016-03-11 16:48:19 -0800123 if (lambda instanceof OchSignal) {
Sho SHIMIZU2e7ac842015-05-05 15:45:38 -0700124 return new ModOchSignalInstruction((OchSignal) lambda);
125 } else {
126 throw new UnsupportedOperationException(String.format("Unsupported type: %s", lambda));
127 }
Sho SHIMIZUe9e12752015-05-05 14:45:40 -0700128 }
129
130 /**
Yafit Hadar52d81552015-10-07 12:26:52 +0300131 * Creates an L1 modification with the specified ODU signal Id.
132 *
133 * @param oduSignalId ODU Signal Id
134 * @return a L1 modification
135 */
136 public static L1ModificationInstruction modL1OduSignalId(OduSignalId oduSignalId) {
137 checkNotNull(oduSignalId, "L1 ODU signal ID cannot be null");
138 return new ModOduSignalIdInstruction(oduSignalId);
139 }
140 /**
alshabib55a55d92014-09-16 11:59:31 -0700141 * Creates a l2 src modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800142 *
143 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700144 * @return a l2 modification
145 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700146 public static L2ModificationInstruction modL2Src(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700147 checkNotNull(addr, "Src l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700148 return new L2ModificationInstruction.ModEtherInstruction(
149 L2ModificationInstruction.L2SubType.ETH_SRC, addr);
alshabib55a55d92014-09-16 11:59:31 -0700150 }
151
152 /**
153 * Creates a L2 dst modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800154 *
155 * @param addr the mac address to modify to
alshabib55a55d92014-09-16 11:59:31 -0700156 * @return a L2 modification
157 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700158 public static L2ModificationInstruction modL2Dst(MacAddress addr) {
alshabib55a55d92014-09-16 11:59:31 -0700159 checkNotNull(addr, "Dst l2 address cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700160 return new L2ModificationInstruction.ModEtherInstruction(
161 L2ModificationInstruction.L2SubType.ETH_DST, addr);
alshabib55a55d92014-09-16 11:59:31 -0700162 }
163
164 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800165 * Creates a VLAN ID modification.
166 *
167 * @param vlanId the VLAN ID to modify to
alshabib7410fea2014-09-16 13:48:39 -0700168 * @return a L2 modification
169 */
Ayaka Koshibea9c199f2014-09-16 16:21:40 -0700170 public static L2ModificationInstruction modVlanId(VlanId vlanId) {
alshabib55a55d92014-09-16 11:59:31 -0700171 checkNotNull(vlanId, "VLAN id cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700172 return new L2ModificationInstruction.ModVlanIdInstruction(vlanId);
alshabib55a55d92014-09-16 11:59:31 -0700173 }
174
alshabib7410fea2014-09-16 13:48:39 -0700175 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800176 * Creates a VLAN PCP modification.
177 *
178 * @param vlanPcp the PCP to modify to
alshabib7410fea2014-09-16 13:48:39 -0700179 * @return a L2 modification
180 */
181 public static L2ModificationInstruction modVlanPcp(Byte vlanPcp) {
182 checkNotNull(vlanPcp, "VLAN Pcp cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700183 return new L2ModificationInstruction.ModVlanPcpInstruction(vlanPcp);
alshabib7410fea2014-09-16 13:48:39 -0700184 }
185
186 /**
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800187 * Creates a MPLS label modification.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800188 *
189 * @param mplsLabel MPLS label to set
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800190 * @return a L2 Modification
191 */
Michele Santuari4b6019e2014-12-19 11:31:45 +0100192 public static L2ModificationInstruction modMplsLabel(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800193 checkNotNull(mplsLabel, "MPLS label cannot be null");
alshabibd17abc22015-04-21 18:26:35 -0700194 return new L2ModificationInstruction.ModMplsLabelInstruction(mplsLabel);
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800195 }
sangho3f97a17d2015-01-29 22:56:29 -0800196
197 /**
Saurav Das73a7dd42015-08-19 22:20:31 -0700198 * Creates a MPLS BOS bit modification.
199 *
200 * @param mplsBos MPLS BOS bit to set (true) or unset (false)
201 * @return a L2 Modification
202 */
203 public static L2ModificationInstruction modMplsBos(boolean mplsBos) {
204 return new L2ModificationInstruction.ModMplsBosInstruction(mplsBos);
205 }
206
207 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800208 * Creates a MPLS decrement TTL modification.
sangho3f97a17d2015-01-29 22:56:29 -0800209 *
210 * @return a L2 Modification
211 */
212 public static L2ModificationInstruction decMplsTtl() {
alshabibd17abc22015-04-21 18:26:35 -0700213 return new L2ModificationInstruction.ModMplsTtlInstruction();
sangho3f97a17d2015-01-29 22:56:29 -0800214 }
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800215
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800216 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800217 * Creates a L3 IPv4 src modification.
218 *
219 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700220 * @return a L3 modification
221 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700222 public static L3ModificationInstruction modL3Src(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800223 checkNotNull(addr, "Src l3 IPv4 address cannot be null");
224 return new ModIPInstruction(L3SubType.IPV4_SRC, addr);
alshabib7410fea2014-09-16 13:48:39 -0700225 }
226
227 /**
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800228 * Creates a L3 IPv4 dst modification.
229 *
230 * @param addr the IPv4 address to modify to
alshabib7410fea2014-09-16 13:48:39 -0700231 * @return a L3 modification
232 */
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700233 public static L3ModificationInstruction modL3Dst(IpAddress addr) {
Pavlin Radoslavovfebe82c2015-02-11 19:08:15 -0800234 checkNotNull(addr, "Dst l3 IPv4 address cannot be null");
235 return new ModIPInstruction(L3SubType.IPV4_DST, addr);
236 }
237
238 /**
239 * Creates a L3 IPv6 src modification.
240 *
241 * @param addr the IPv6 address to modify to
242 * @return a L3 modification
243 */
244 public static L3ModificationInstruction modL3IPv6Src(IpAddress addr) {
245 checkNotNull(addr, "Src l3 IPv6 address cannot be null");
246 return new ModIPInstruction(L3SubType.IPV6_SRC, addr);
247 }
248
249 /**
250 * Creates a L3 IPv6 dst modification.
251 *
252 * @param addr the IPv6 address to modify to
253 * @return a L3 modification
254 */
255 public static L3ModificationInstruction modL3IPv6Dst(IpAddress addr) {
256 checkNotNull(addr, "Dst l3 IPv6 address cannot be null");
257 return new ModIPInstruction(L3SubType.IPV6_DST, addr);
258 }
259
260 /**
261 * Creates a L3 IPv6 Flow Label modification.
262 *
263 * @param flowLabel the IPv6 flow label to modify to (20 bits)
264 * @return a L3 modification
265 */
266 public static L3ModificationInstruction modL3IPv6FlowLabel(int flowLabel) {
267 return new ModIPv6FlowLabelInstruction(flowLabel);
alshabib7410fea2014-09-16 13:48:39 -0700268 }
269
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800270 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800271 * Creates a L3 decrement TTL modification.
272 *
sangho3f97a17d2015-01-29 22:56:29 -0800273 * @return a L3 modification
274 */
275 public static L3ModificationInstruction decNwTtl() {
276 return new ModTtlInstruction(L3SubType.DEC_TTL);
277 }
278
279 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800280 * Creates a L3 copy TTL to outer header modification.
281 *
sangho3f97a17d2015-01-29 22:56:29 -0800282 * @return a L3 modification
283 */
284 public static L3ModificationInstruction copyTtlOut() {
285 return new ModTtlInstruction(L3SubType.TTL_OUT);
286 }
287
288 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800289 * Creates a L3 copy TTL to inner header modification.
290 *
sangho3f97a17d2015-01-29 22:56:29 -0800291 * @return a L3 modification
292 */
293 public static L3ModificationInstruction copyTtlIn() {
294 return new ModTtlInstruction(L3SubType.TTL_IN);
295 }
296
297 /**
lishuai3cce60b2015-12-01 19:35:16 +0800298 * Creates a L3 ARP IP src modification.
299 *
300 * @param addr the ip address to modify to
301 * @return a L3 modification
302 */
303 public static L3ModificationInstruction modArpSpa(IpAddress addr) {
304 checkNotNull(addr, "Src l3 ARP IP address cannot be null");
305 return new ModArpIPInstruction(L3SubType.ARP_SPA, addr);
306 }
307
308 /**
309 * Creates a l3 ARP Ether src modification.
310 *
311 * @param addr the mac address to modify to
312 * @return a l3 modification
313 */
314 public static L3ModificationInstruction modArpSha(MacAddress addr) {
315 checkNotNull(addr, "Src l3 ARP address cannot be null");
316 return new ModArpEthInstruction(L3SubType.ARP_SHA, addr);
317 }
318
319 /**
320 * Creates a l3 ARP operation modification.
321 *
322 * @param op the ARP operation to modify to
323 * @return a l3 modification
324 */
325 public static L3ModificationInstruction modL3ArpOp(short op) {
326 checkNotNull(op, "Arp operation cannot be null");
327 return new ModArpOpInstruction(L3SubType.ARP_OP, op);
328 }
329
330 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800331 * Creates a push MPLS header instruction.
332 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800333 * @return a L2 modification.
334 */
335 public static Instruction pushMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700336 return new L2ModificationInstruction.PushHeaderInstructions(
337 L2ModificationInstruction.L2SubType.MPLS_PUSH,
alshabib7b808c52015-06-26 14:22:24 -0700338 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800339 }
340
341 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800342 * Creates a pop MPLS header instruction.
343 *
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800344 * @return a L2 modification.
345 */
346 public static Instruction popMpls() {
alshabibd17abc22015-04-21 18:26:35 -0700347 return new L2ModificationInstruction.PushHeaderInstructions(
348 L2ModificationInstruction.L2SubType.MPLS_POP,
alshabib7b808c52015-06-26 14:22:24 -0700349 EthType.EtherType.MPLS_UNICAST.ethType());
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800350 }
alshabib7410fea2014-09-16 13:48:39 -0700351
sangho3f97a17d2015-01-29 22:56:29 -0800352 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800353 * Creates a pop MPLS header instruction with a particular ethertype.
sangho3f97a17d2015-01-29 22:56:29 -0800354 *
355 * @param etherType Ethernet type to set
356 * @return a L2 modification.
alshabib7b808c52015-06-26 14:22:24 -0700357 */
358 public static Instruction popMpls(EthType etherType) {
359 checkNotNull(etherType, "Ethernet type cannot be null");
360 return new L2ModificationInstruction.PushHeaderInstructions(
alshabibd17abc22015-04-21 18:26:35 -0700361 L2ModificationInstruction.L2SubType.MPLS_POP, etherType);
sangho3f97a17d2015-01-29 22:56:29 -0800362 }
363
Saurav Dasfbe25c52015-03-04 11:12:00 -0800364 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800365 * Creates a pop VLAN header instruction.
366 *
367 * @return a L2 modification
Saurav Dasfbe25c52015-03-04 11:12:00 -0800368 */
369 public static Instruction popVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700370 return new L2ModificationInstruction.PopVlanInstruction(
371 L2ModificationInstruction.L2SubType.VLAN_POP);
Saurav Dasfbe25c52015-03-04 11:12:00 -0800372 }
373
374 /**
Jonathan Hart54b406b2015-03-06 16:24:14 -0800375 * Creates a push VLAN header instruction.
376 *
377 * @return a L2 modification
378 */
379 public static Instruction pushVlan() {
alshabibd17abc22015-04-21 18:26:35 -0700380 return new L2ModificationInstruction.PushHeaderInstructions(
alshabib7b808c52015-06-26 14:22:24 -0700381 L2ModificationInstruction.L2SubType.VLAN_PUSH,
382 EthType.EtherType.VLAN.ethType());
Jonathan Hart54b406b2015-03-06 16:24:14 -0800383 }
384
385 /**
alshabibd17abc22015-04-21 18:26:35 -0700386 * Sends the packet to the table id.
Jonathan Hart54b406b2015-03-06 16:24:14 -0800387 *
alshabibd17abc22015-04-21 18:26:35 -0700388 * @param tableId flow rule table id
Thomas Vachuska3e2b6512015-03-05 09:25:03 -0800389 * @return table type transition instruction
Saurav Dasfbe25c52015-03-04 11:12:00 -0800390 */
alshabibd17abc22015-04-21 18:26:35 -0700391 public static Instruction transition(Integer tableId) {
392 checkNotNull(tableId, "Table id cannot be null");
393 return new TableTypeTransition(tableId);
alshabib9af70072015-02-09 14:34:16 -0800394 }
395
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800396 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700397 * Writes metadata to associate with a packet.
398 *
399 * @param metadata the metadata value to write
400 * @param metadataMask the bits to mask for the metadata value
401 * @return metadata instruction
402 */
403 public static Instruction writeMetadata(long metadata, long metadataMask) {
404 return new MetadataInstruction(metadata, metadataMask);
405 }
406
407 /**
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700408 * Creates a Tunnel ID modification.
409 *
410 * @param tunnelId the Tunnel ID to modify to
411 * @return a L2 modification
412 */
413 public static L2ModificationInstruction modTunnelId(long tunnelId) {
414 checkNotNull(tunnelId, "Tunnel id cannot be null");
415 return new L2ModificationInstruction.ModTunnelIdInstruction(tunnelId);
416 }
417
418 /**
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700419 * Creates a TCP src modification.
420 *
421 * @param port the TCP port number to modify to
422 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700423 */
424 public static L4ModificationInstruction modTcpSrc(TpPort port) {
425 checkNotNull(port, "Src TCP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700426 return new ModTransportPortInstruction(L4SubType.TCP_SRC, port);
427 }
428
429 /**
430 * Creates a TCP dst modification.
431 *
432 * @param port the TCP port number to modify to
433 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700434 */
435 public static L4ModificationInstruction modTcpDst(TpPort port) {
436 checkNotNull(port, "Dst TCP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700437 return new ModTransportPortInstruction(L4SubType.TCP_DST, port);
438 }
439
440 /**
441 * Creates a UDP src modification.
442 *
443 * @param port the UDP port number to modify to
444 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700445 */
446 public static L4ModificationInstruction modUdpSrc(TpPort port) {
447 checkNotNull(port, "Src UDP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700448 return new ModTransportPortInstruction(L4SubType.UDP_SRC, port);
449 }
450
451 /**
452 * Creates a UDP dst modification.
453 *
454 * @param port the UDP port number to modify to
455 * @return a L4 modification
Hyunsun Mooncf732fb2015-08-22 21:04:23 -0700456 */
457 public static L4ModificationInstruction modUdpDst(TpPort port) {
458 checkNotNull(port, "Dst UDP port cannot be null");
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700459 return new ModTransportPortInstruction(L4SubType.UDP_DST, port);
460 }
461
462 /**
Jonathan Hart3c259162015-10-21 21:31:19 -0700463 * Creates an extension instruction.
464 *
465 * @param extension extension instruction
466 * @param deviceId device ID
467 * @return extension instruction
468 */
alshabib880b6442015-11-23 22:13:04 -0800469 public static ExtensionInstructionWrapper extension(ExtensionTreatment extension,
Jonathan Hart3c259162015-10-21 21:31:19 -0700470 DeviceId deviceId) {
471 checkNotNull(extension, "Extension instruction cannot be null");
472 checkNotNull(deviceId, "Device ID cannot be null");
473 return new ExtensionInstructionWrapper(extension, deviceId);
474 }
475
476 /**
Charles Chan7efabeb2015-09-28 15:12:19 -0700477 * No Action instruction.
478 */
479 public static final class NoActionInstruction implements Instruction {
480
481 private NoActionInstruction() {}
482
483 @Override
484 public Type type() {
485 return Type.NOACTION;
486 }
487
488 @Override
489 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800490 return type().toString();
Charles Chan7efabeb2015-09-28 15:12:19 -0700491 }
492
493 @Override
494 public int hashCode() {
HIGUCHI Yutaca9cc8e2015-10-29 23:26:51 -0700495 return type().ordinal();
Charles Chan7efabeb2015-09-28 15:12:19 -0700496 }
497
498 @Override
499 public boolean equals(Object obj) {
500 if (this == obj) {
501 return true;
502 }
503 if (obj instanceof NoActionInstruction) {
504 return true;
505 }
506 return false;
507 }
508 }
509
510 /**
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800511 * Output Instruction.
sangho8995ac52015-02-04 11:29:03 -0800512 */
alshabib55a55d92014-09-16 11:59:31 -0700513 public static final class OutputInstruction implements Instruction {
514 private final PortNumber port;
515
516 private OutputInstruction(PortNumber port) {
517 this.port = port;
518 }
519
520 public PortNumber port() {
521 return port;
522 }
523
524 @Override
525 public Type type() {
526 return Type.OUTPUT;
527 }
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800528
alshabib99b8fdc2014-09-25 14:30:22 -0700529 @Override
530 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800531 return type().toString() + SEPARATOR + port.toString();
alshabib99b8fdc2014-09-25 14:30:22 -0700532 }
alshabib8ca53902014-10-07 13:11:17 -0700533
534 @Override
535 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700536 return Objects.hash(type().ordinal(), port);
alshabib8ca53902014-10-07 13:11:17 -0700537 }
538
539 @Override
540 public boolean equals(Object obj) {
541 if (this == obj) {
542 return true;
543 }
544 if (obj instanceof OutputInstruction) {
545 OutputInstruction that = (OutputInstruction) obj;
Yuta HIGUCHI4ce65292014-11-01 12:09:55 -0700546 return Objects.equals(port, that.port);
alshabib8ca53902014-10-07 13:11:17 -0700547
548 }
549 return false;
550 }
alshabib55a55d92014-09-16 11:59:31 -0700551 }
552
Yuta HIGUCHI6a479642015-02-08 01:28:50 -0800553 /**
554 * Group Instruction.
sangho8995ac52015-02-04 11:29:03 -0800555 */
sangho8995ac52015-02-04 11:29:03 -0800556 public static final class GroupInstruction implements Instruction {
557 private final GroupId groupId;
558
559 private GroupInstruction(GroupId groupId) {
560 this.groupId = groupId;
561 }
562
563 public GroupId groupId() {
564 return groupId;
565 }
566
567 @Override
568 public Type type() {
569 return Type.GROUP;
570 }
alshabib9af70072015-02-09 14:34:16 -0800571
sangho8995ac52015-02-04 11:29:03 -0800572 @Override
573 public String toString() {
Saurav Das0fd79d92016-03-07 10:58:36 -0800574 return type().toString() + SEPARATOR + "0x" + Integer.toHexString(groupId.id());
sangho8995ac52015-02-04 11:29:03 -0800575 }
576
577 @Override
578 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700579 return Objects.hash(type().ordinal(), groupId);
sangho8995ac52015-02-04 11:29:03 -0800580 }
581
582 @Override
583 public boolean equals(Object obj) {
584 if (this == obj) {
585 return true;
586 }
587 if (obj instanceof GroupInstruction) {
588 GroupInstruction that = (GroupInstruction) obj;
589 return Objects.equals(groupId, that.groupId);
590
591 }
592 return false;
593 }
594 }
595
Saurav Das86af8f12015-05-25 23:55:33 -0700596 /**
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200597 * Set-Queue Instruction.
598 */
599 public static final class SetQueueInstruction implements Instruction {
600 private final long queueId;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200601 private final PortNumber port;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200602
603 private SetQueueInstruction(long queueId) {
604 this.queueId = queueId;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200605 this.port = null;
606 }
607
608 private SetQueueInstruction(long queueId, PortNumber port) {
609 this.queueId = queueId;
610 this.port = port;
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200611 }
612
613 public long queueId() {
614 return queueId;
615 }
616
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200617 public PortNumber port() {
618 return port;
619 }
620
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200621 @Override
622 public Type type() {
623 return Type.QUEUE;
624 }
625
626 @Override
627 public String toString() {
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200628 MoreObjects.ToStringHelper toStringHelper = toStringHelper(type().toString());
629 toStringHelper.add("queueId", queueId);
630
631 if (port() != null) {
632 toStringHelper.add("port", port);
633 }
634 return toStringHelper.toString();
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200635 }
636
637 @Override
638 public int hashCode() {
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200639 return Objects.hash(type().ordinal(), queueId, port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200640 }
641
642 @Override
643 public boolean equals(Object obj) {
644 if (this == obj) {
645 return true;
646 }
647 if (obj instanceof SetQueueInstruction) {
648 SetQueueInstruction that = (SetQueueInstruction) obj;
Steffen Gebertba2d3b72015-10-22 11:14:31 +0200649 return Objects.equals(queueId, that.queueId) && Objects.equals(port, that.port);
Steffen Gebertbbfdaaa2015-09-29 11:01:46 +0200650
651 }
652 return false;
653 }
654 }
655
656 /**
alshabib10c810b2015-08-18 16:59:04 -0700657 * A meter instruction.
658 */
659 public static final class MeterInstruction implements Instruction {
660 private final MeterId meterId;
661
662 private MeterInstruction(MeterId meterId) {
663 this.meterId = meterId;
664 }
665
666 public MeterId meterId() {
667 return meterId;
668 }
669
670 @Override
671 public Type type() {
672 return Type.METER;
673 }
674
675 @Override
676 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800677 return type().toString() + SEPARATOR + meterId.id();
alshabib10c810b2015-08-18 16:59:04 -0700678 }
679
680 @Override
681 public int hashCode() {
682 return Objects.hash(type().ordinal(), meterId);
683 }
684
685 @Override
686 public boolean equals(Object obj) {
687 if (this == obj) {
688 return true;
689 }
690 if (obj instanceof MeterInstruction) {
691 MeterInstruction that = (MeterInstruction) obj;
692 return Objects.equals(meterId, that.meterId);
693
694 }
695 return false;
696 }
697 }
698
699 /**
Saurav Das86af8f12015-05-25 23:55:33 -0700700 * Transition instruction.
701 */
alshabibd17abc22015-04-21 18:26:35 -0700702 public static class TableTypeTransition implements Instruction {
703 private final Integer tableId;
704
705 TableTypeTransition(Integer tableId) {
706 this.tableId = tableId;
alshabib9af70072015-02-09 14:34:16 -0800707 }
708
709 @Override
710 public Type type() {
711 return Type.TABLE;
712 }
713
alshabibd17abc22015-04-21 18:26:35 -0700714 public Integer tableId() {
715 return this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800716 }
717
718 @Override
719 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800720 return type().toString() + SEPARATOR + this.tableId;
alshabib9af70072015-02-09 14:34:16 -0800721 }
722
723 @Override
724 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700725 return Objects.hash(type().ordinal(), tableId);
alshabib9af70072015-02-09 14:34:16 -0800726 }
727
728 @Override
729 public boolean equals(Object obj) {
730 if (this == obj) {
731 return true;
732 }
733 if (obj instanceof TableTypeTransition) {
734 TableTypeTransition that = (TableTypeTransition) obj;
alshabibd17abc22015-04-21 18:26:35 -0700735 return Objects.equals(tableId, that.tableId);
alshabib9af70072015-02-09 14:34:16 -0800736
737 }
738 return false;
739 }
Saurav Das86af8f12015-05-25 23:55:33 -0700740 }
alshabib9af70072015-02-09 14:34:16 -0800741
Saurav Das86af8f12015-05-25 23:55:33 -0700742 /**
743 * Metadata instruction.
744 */
745 public static class MetadataInstruction implements Instruction {
746 private final long metadata;
747 private final long metadataMask;
748
749 MetadataInstruction(long metadata, long metadataMask) {
750 this.metadata = metadata;
751 this.metadataMask = metadataMask;
752 }
753
754 @Override
755 public Type type() {
756 return Type.METADATA;
757 }
758
759 public long metadata() {
760 return this.metadata;
761 }
762
763 public long metadataMask() {
764 return this.metadataMask;
765 }
766
767 @Override
768 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800769 return type().toString() + SEPARATOR +
770 Long.toHexString(this.metadata) + "/" +
771 Long.toHexString(this.metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700772 }
773
774 @Override
775 public int hashCode() {
Thomas Vachuska6c8eff32015-05-27 16:11:44 -0700776 return Objects.hash(type().ordinal(), metadata, metadataMask);
Saurav Das86af8f12015-05-25 23:55:33 -0700777 }
778
779 @Override
780 public boolean equals(Object obj) {
781 if (this == obj) {
782 return true;
783 }
784 if (obj instanceof MetadataInstruction) {
785 MetadataInstruction that = (MetadataInstruction) obj;
786 return Objects.equals(metadata, that.metadata) &&
787 Objects.equals(metadataMask, that.metadataMask);
788
789 }
790 return false;
791 }
alshabib9af70072015-02-09 14:34:16 -0800792 }
Saurav Das73a7dd42015-08-19 22:20:31 -0700793
Jonathan Hart3c259162015-10-21 21:31:19 -0700794 /**
795 * Extension instruction.
796 */
797 public static class ExtensionInstructionWrapper implements Instruction {
alshabib880b6442015-11-23 22:13:04 -0800798 private final ExtensionTreatment extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700799 private final DeviceId deviceId;
800
alshabib880b6442015-11-23 22:13:04 -0800801 ExtensionInstructionWrapper(ExtensionTreatment extension, DeviceId deviceId) {
802 extensionTreatment = extension;
Jonathan Hart3c259162015-10-21 21:31:19 -0700803 this.deviceId = deviceId;
804 }
805
alshabib880b6442015-11-23 22:13:04 -0800806 public ExtensionTreatment extensionInstruction() {
807 return extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700808 }
809
810 public DeviceId deviceId() {
811 return deviceId;
812 }
813
814 @Override
815 public Type type() {
816 return Type.EXTENSION;
817 }
818
819 @Override
820 public String toString() {
Jonathan Hartc7840bd2016-01-21 23:26:29 -0800821 return type().toString() + SEPARATOR + deviceId + "/" + extensionTreatment;
Jonathan Hart3c259162015-10-21 21:31:19 -0700822 }
823
824 @Override
825 public int hashCode() {
alshabib880b6442015-11-23 22:13:04 -0800826 return Objects.hash(type().ordinal(), extensionTreatment, deviceId);
Jonathan Hart3c259162015-10-21 21:31:19 -0700827 }
828
829 @Override
830 public boolean equals(Object obj) {
831 if (this == obj) {
832 return true;
833 }
834 if (obj instanceof ExtensionInstructionWrapper) {
835 ExtensionInstructionWrapper that = (ExtensionInstructionWrapper) obj;
alshabib880b6442015-11-23 22:13:04 -0800836 return Objects.equals(extensionTreatment, that.extensionTreatment)
Jonathan Hart3c259162015-10-21 21:31:19 -0700837 && Objects.equals(deviceId, that.deviceId);
838
839 }
840 return false;
841 }
842 }
843
alshabib55a55d92014-09-16 11:59:31 -0700844}
alshabib8ca53902014-10-07 13:11:17 -0700845
846