2 * Copyright (c) 2013 - 2016 Red Hat, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.ovsdb.utils.mdsal.openflow;
11 import static org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils.dropAction;
13 import java.math.BigInteger;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DecNwTtlCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlDstActionCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlSrcActionCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.nw.ttl._case.DecNwTtl;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.nw.ttl._case.DecNwTtlBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropAction;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.vlan.action._case.PopVlanActionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanActionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.dl.dst.action._case.SetDlDstActionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.dl.src.action._case.SetDlSrcActionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.dst.action._case.SetNwDstActionBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.src.action._case.SetNwSrcActionBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.vlan.id.action._case.SetVlanIdActionBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.TunnelIpv4MatchBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
77 import org.slf4j.Logger;
78 import org.slf4j.LoggerFactory;
80 public class InstructionUtils {
81 private static final Logger LOG = LoggerFactory.getLogger(InstructionUtils.class);
82 private static final int IPV4 = 0x8100;
83 private static final int MAX_LENGTH = 0xffff;
86 * Create Send to Controller Reserved Port Instruction (packet_in)
88 * @param nodeName Uri Prefix, containing nodeConnectorType and dpId (aka NodeId)
89 * @param ib Map InstructionBuilder without any instructions
90 * @return ib Map InstructionBuilder with instructions
92 public static InstructionBuilder createSendToControllerInstructions(String nodeName, InstructionBuilder ib) {
94 List<Action> actionList = new ArrayList<>();
95 ActionBuilder ab = new ActionBuilder();
97 OutputActionBuilder output = new OutputActionBuilder();
98 output.setMaxLength(MAX_LENGTH);
99 NodeId nodeId = new NodeId(nodeName);
100 output.setOutputNodeConnector(new NodeConnectorId(nodeId.getValue() + ":"
101 + OutputPortValues.CONTROLLER.toString()));
102 ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
104 ab.setKey(new ActionKey(0));
105 actionList.add(ab.build());
107 // Create an Apply Action
108 ApplyActionsBuilder aab = new ApplyActionsBuilder();
109 aab.setAction(actionList);
111 // Wrap our Apply Action in an Instruction
112 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
118 * Create NORMAL Reserved Port Instruction (packet_in)
120 * @param nodeName Uri Prefix, containing nodeConnectorType and dpId (aka NodeId)
121 * @param ib Map InstructionBuilder without any instructions
122 * @return ib Map InstructionBuilder with instructions
125 public static InstructionBuilder createNormalInstructions(String nodeName, InstructionBuilder ib) {
127 List<Action> actionList = new ArrayList<>();
128 ActionBuilder ab = new ActionBuilder();
130 OutputActionBuilder output = new OutputActionBuilder();
131 NodeId nodeId = new NodeId(nodeName);
132 output.setOutputNodeConnector(new NodeConnectorId(nodeId.getValue() + ":"
133 + OutputPortValues.NORMAL.toString()));
134 ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
136 ab.setKey(new ActionKey(0));
137 actionList.add(ab.build());
139 // Create an Apply Action
140 ApplyActionsBuilder aab = new ApplyActionsBuilder();
141 aab.setAction(actionList);
143 // Wrap our Apply Action in an Instruction
144 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
150 * Create LOCAL Reserved Port Instruction
152 * @param ib Map InstructionBuilder without any instructions
153 * @param dpidLong Long the datapath ID of a switch/node
154 * @return ib Map InstructionBuilder with instructions
156 public static InstructionBuilder createLocalInstructions(InstructionBuilder ib, long dpidLong) {
157 List<Action> actionList = new ArrayList<>();
158 ActionBuilder ab = new ActionBuilder();
160 OutputActionBuilder output = new OutputActionBuilder();
161 output.setOutputNodeConnector(new NodeConnectorId("openflow:" + dpidLong + ":"
162 + OutputPortValues.LOCAL.toString()));
163 ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
165 ab.setKey(new ActionKey(0));
166 actionList.add(ab.build());
168 // Create an Apply Action
169 ApplyActionsBuilder aab = new ApplyActionsBuilder();
170 aab.setAction(actionList);
172 // Wrap our Apply Action in an Instruction
173 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
179 * Create Output Port Instruction
181 * @param ib Map InstructionBuilder without any instructions
182 * @param dpidLong Long the datapath ID of a switch/node
183 * @param port Long representing a port on a switch/node
184 * @return ib InstructionBuilder Map with instructions
186 public static InstructionBuilder createOutputPortInstructions(InstructionBuilder ib, Long dpidLong, Long port) {
188 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + port);
189 LOG.debug("createOutputPortInstructions() Node Connector ID is - Type=openflow: DPID={} inPort={} ",
192 List<Action> actionList = new ArrayList<>();
193 ActionBuilder ab = new ActionBuilder();
194 OutputActionBuilder oab = new OutputActionBuilder();
195 oab.setOutputNodeConnector(ncid);
197 ab.setAction(new OutputActionCaseBuilder().setOutputAction(oab.build()).build());
199 ab.setKey(new ActionKey(0));
200 actionList.add(ab.build());
202 // Create an Apply Action
203 ApplyActionsBuilder aab = new ApplyActionsBuilder();
204 aab.setAction(actionList);
205 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
211 * add Output Port action to Instruction action list.
212 * This is use for flow with single output port actions.
213 * Flow with mutiple output port actions should use createOutputPortInstructions() method.
215 * @param ib Map InstructionBuilder without any instructions
216 * @param dpidLong Long the datapath ID of a switch/node
217 * @param port Long representing a port on a switch/node
218 * @return ib InstructionBuilder Map with instructions
220 public static InstructionBuilder addOutputPortInstructions(InstructionBuilder ib,
221 Long dpidLong, Long port,
222 List<Instruction> instructions) {
223 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + port);
225 "addOutputPortInstructions() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}",
226 dpidLong, port, instructions);
228 List<Action> actionList = new ArrayList<>();
229 ActionBuilder ab = new ActionBuilder();
231 List<Action> existingActions;
232 if (instructions != null) {
233 for (Instruction in : instructions) {
234 if (in.getInstruction() instanceof ApplyActionsCase) {
235 existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
236 actionList.addAll(existingActions);
241 /* Create output action for this port*/
242 OutputActionBuilder oab = new OutputActionBuilder();
243 oab.setOutputNodeConnector(ncid);
244 ab.setAction(new OutputActionCaseBuilder().setOutputAction(oab.build()).build());
245 ab.setOrder(actionList.size());
246 ab.setKey(new ActionKey(actionList.size()));
247 actionList.add(ab.build());
249 // Create an Apply Action
250 ApplyActionsBuilder aab = new ApplyActionsBuilder();
251 aab.setAction(actionList);
252 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
258 * Remove Output Port from Instruction
260 * @param ib Map InstructionBuilder without any instructions
261 * @param dpidLong Long the datapath ID of a switch/node
262 * @param port Long representing a port on a switch/node
263 * @return ib InstructionBuilder Map with instructions
265 public static boolean removeOutputPortFromInstructions(InstructionBuilder ib,
266 Long dpidLong, Long port, List<Instruction> instructions) {
268 final NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + port);
269 final Uri ncidUri = new Uri(ncid);
271 "removeOutputPortFromInstructions() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}",
272 dpidLong, port, instructions);
274 List<Action> actionList = new ArrayList<>();
277 // Start of by locating actions that will have port removed, from the existing instructionList
279 List<Action> existingActions;
280 if (instructions != null) {
281 for (Instruction in : instructions) {
282 if (in.getInstruction() instanceof ApplyActionsCase) {
283 existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
284 actionList.addAll(existingActions);
290 int removedActionOrder = 0;
291 boolean isPortDeleted = false;
292 boolean removeFlow = true;
294 // Locate specific action that has the port to be removed. Then, take note on its action order
295 // and remove it from list, in addition to flag that it was found.
297 for (Action action : actionList) {
298 if (action.getAction() instanceof OutputActionCase) {
299 OutputActionCase opAction = (OutputActionCase) action.getAction();
300 if (opAction.getOutputAction().getOutputNodeConnector().equals(ncidUri)) {
301 /* Find the output port in action list and remove */
302 removedActionOrder = action.getOrder();
303 actionList.remove(action);
304 isPortDeleted = true;
311 // Iterate through all actions in the modified list and adjust the order of
312 // the actions left behind. With that, all actions that have order higher than
313 // the action removed gets their value decremented by 1. Note that this iteration
314 // visits all entries to account for cases where the list order is not the same
315 // as the action's order.
317 for (int i = 0; i < actionList.size(); i++) {
318 Action action = actionList.get(i);
319 if (action.getOrder() > removedActionOrder) {
320 /* Shift the action by rebuilding action, using adjusted order */
321 ab = new ActionBuilder();
322 ab.setAction(action.getAction());
323 ab.setOrder(action.getOrder() - 1);
324 ab.setKey(new ActionKey(action.getOrder() - 1));
325 Action actionNewOrder = ab.build();
326 actionList.remove(action);
327 actionList.add(i, actionNewOrder);
328 } else if (action.getOrder() == removedActionOrder) {
329 // Sanity: implementation assumes no two actions have the same order
331 LOG.error("Found action with same order as the action removed for {}, order {} index {}: {}",
332 ncid, removedActionOrder, i, action);
335 // If action refers to a port output, then flow should be preserved.
336 // We do this, so that if we are only left with non-output port actions,
337 // we still remove the flow
339 if (action.getAction() instanceof OutputActionCase) {
344 // If port we are asked to delete is not found, this implementation will leave actions
345 // alone and not remove the flow, as long as a remaining OutputActionCase is found.
347 for (Action action : actionList) {
348 if (action.getAction() instanceof OutputActionCase) {
355 /* Put new action list in Apply Action instruction */
357 ApplyActionsBuilder aab = new ApplyActionsBuilder();
358 aab.setAction(actionList);
359 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
360 LOG.debug("removeOutputPortFromInstructions() : applyAction {}", aab.build());
363 /* if all output ports are removed. Return true to indicate flow remove */
369 * Create Set Vlan ID Instruction - This includes push vlan action, and set field -> vlan vid action
371 * @param ib Map InstructionBuilder without any instructions
372 * @param vlanId Integer representing a VLAN ID Integer representing a VLAN ID
373 * @return ib Map InstructionBuilder with instructions
375 public static InstructionBuilder createSetVlanInstructions(InstructionBuilder ib, VlanId vlanId) {
377 List<Action> actionList = new ArrayList<>();
378 ActionBuilder ab = new ActionBuilder();
380 /* First we push vlan header */
381 PushVlanActionBuilder vlan = new PushVlanActionBuilder();
382 vlan.setEthernetType(IPV4);
383 ab.setAction(new PushVlanActionCaseBuilder().setPushVlanAction(vlan.build()).build());
385 actionList.add(ab.build());
387 /* Then we set vlan id value as vlanId */
388 SetVlanIdActionBuilder vl = new SetVlanIdActionBuilder();
389 vl.setVlanId(vlanId);
390 ab = new ActionBuilder();
391 ab.setAction(new SetVlanIdActionCaseBuilder().setSetVlanIdAction(vl.build()).build());
393 actionList.add(ab.build());
394 // Create an Apply Action
395 ApplyActionsBuilder aab = new ApplyActionsBuilder();
396 aab.setAction(actionList);
398 // Wrap our Apply Action in an Instruction
399 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
405 * Create Pop Vlan Instruction - this remove vlan header
407 * @param ib Map InstructionBuilder without any instructions
408 * @return ib Map InstructionBuilder with instructions
410 public static InstructionBuilder createPopVlanInstructions(InstructionBuilder ib) {
412 List<Action> actionList = new ArrayList<>();
413 ActionBuilder ab = new ActionBuilder();
415 PopVlanActionBuilder popVlanActionBuilder = new PopVlanActionBuilder();
416 ab.setAction(new PopVlanActionCaseBuilder().setPopVlanAction(popVlanActionBuilder.build()).build());
418 actionList.add(ab.build());
420 // Create an Apply Action
421 ApplyActionsBuilder aab = new ApplyActionsBuilder();
422 aab.setAction(actionList);
424 // Wrap our Apply Action in an Instruction
425 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
431 * Create Set IPv4 Source Instruction
433 * @param ib Map InstructionBuilder without any instructions
434 * @param prefixsrc String containing an IPv4 prefix
435 * @return ib Map InstructionBuilder with instructions
437 public static InstructionBuilder createNwSrcInstructions(InstructionBuilder ib, Ipv4Prefix prefixsrc) {
439 List<Action> actionList = new ArrayList<>();
440 ActionBuilder ab = new ActionBuilder();
442 SetNwSrcActionBuilder setNwsrcActionBuilder = new SetNwSrcActionBuilder();
443 Ipv4Builder ipsrc = new Ipv4Builder();
444 ipsrc.setIpv4Address(prefixsrc);
445 setNwsrcActionBuilder.setAddress(ipsrc.build());
446 ab.setAction(new SetNwSrcActionCaseBuilder().setSetNwSrcAction(setNwsrcActionBuilder.build()).build());
448 ab.setKey(new ActionKey(0));
449 actionList.add(ab.build());
451 // Create an Apply Action
452 ApplyActionsBuilder aab = new ApplyActionsBuilder();
453 aab.setAction(actionList);
455 // Wrap our Apply Action in an Instruction
456 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
462 * Create Set IPv4 Destination Instruction
464 * @param ib Map InstructionBuilder without any instructions
465 * @param prefixdst String containing an IPv4 prefix
466 * @param extraAction (optional) Additional action to be performed in actionList
467 * @return ib Map InstructionBuilder with instructions
469 public static InstructionBuilder createNwDstInstructions(InstructionBuilder ib, Ipv4Prefix prefixdst,
470 ActionBuilder extraAction) {
472 List<Action> actionList = new ArrayList<>();
473 ActionBuilder ab = new ActionBuilder();
475 SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
476 Ipv4Builder ipdst = new Ipv4Builder();
477 ipdst.setIpv4Address(prefixdst);
478 setNwDstActionBuilder.setAddress(ipdst.build());
479 ab.setAction(new SetNwDstActionCaseBuilder().setSetNwDstAction(setNwDstActionBuilder.build()).build());
481 ab.setKey(new ActionKey(0));
482 actionList.add(ab.build());
484 if (extraAction != null) {
485 extraAction.setOrder(1);
486 extraAction.setKey(new ActionKey(1));
487 actionList.add(extraAction.build());
490 // Create an Apply Action
491 ApplyActionsBuilder aab = new ApplyActionsBuilder();
492 aab.setAction(actionList);
494 // Wrap our Apply Action in an Instruction
495 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
501 * Create Drop Instruction
503 * @param ib Map InstructionBuilder without any instructions
504 * @return ib Map InstructionBuilder with instructions
506 public static InstructionBuilder createDropInstructions(InstructionBuilder ib) {
508 DropActionBuilder dab = new DropActionBuilder();
509 DropAction dropAction = dab.build();
510 ActionBuilder ab = new ActionBuilder();
511 ab.setAction(new DropActionCaseBuilder().setDropAction(dropAction).build());
513 ab.setKey(new ActionKey(0));
515 // Add our drop action to a list
516 List<Action> actionList = new ArrayList<>();
517 actionList.add(ab.build());
519 // Create an Apply Action
520 ApplyActionsBuilder aab = new ApplyActionsBuilder();
521 aab.setAction(actionList);
523 // Wrap our Apply Action in an Instruction
524 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
530 * Create GOTO Table Instruction Builder
532 * @param ib Map InstructionBuilder without any instructions
533 * @param tableId short representing a flow table ID short representing a flow table ID
534 * @return ib Map InstructionBuilder with instructions
536 public static InstructionBuilder createGotoTableInstructions(InstructionBuilder ib, short tableId) {
538 GoToTableBuilder gttb = new GoToTableBuilder();
539 gttb.setTableId(tableId);
541 // Wrap our Apply Action in an InstructionBuilder
542 ib.setInstruction(new GoToTableCaseBuilder().setGoToTable(gttb.build()).build());
548 * Create Set Tunnel ID Instruction Builder
550 * @param ib Map InstructionBuilder without any instructions
551 * @param tunnelId BigInteger representing a tunnel ID
552 * @return ib Map InstructionBuilder with instructions
554 public static InstructionBuilder createSetTunnelIdInstructions(InstructionBuilder ib, BigInteger tunnelId) {
556 List<Action> actionList = new ArrayList<>();
557 ActionBuilder ab = new ActionBuilder();
558 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
560 // Build the Set Tunnel Field Action
561 TunnelBuilder tunnel = new TunnelBuilder();
562 tunnel.setTunnelId(tunnelId);
563 setFieldBuilder.setTunnel(tunnel.build());
564 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
566 ab.setKey(new ActionKey(0));
567 actionList.add(ab.build());
569 ApplyActionsBuilder aab = new ApplyActionsBuilder();
570 aab.setAction(actionList);
572 // Wrap the Apply Action in an InstructionBuilder and return
573 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
579 * Create Set Source TCP Port Instruction
581 * @param ib Map InstructionBuilder without any instructions
582 * @param tcpport Integer representing a source TCP port
583 * @return ib Map InstructionBuilder with instructions
585 public static InstructionBuilder createSetSrcTCPPort(InstructionBuilder ib, PortNumber tcpport) {
587 List<Action> actionList = new ArrayList<>();
588 ActionBuilder ab = new ActionBuilder();
589 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
591 // Build the Destination TCP Port
592 PortNumber tcpsrcport = new PortNumber(tcpport);
593 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
594 tcpmatch.setTcpSourcePort(tcpsrcport);
596 setFieldBuilder.setLayer4Match(tcpmatch.build());
597 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
599 ab.setKey(new ActionKey(0));
600 actionList.add(ab.build());
602 ApplyActionsBuilder aab = new ApplyActionsBuilder();
603 aab.setAction(actionList);
604 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
610 * Create Set Destination TCP Port Instruction
612 * @param ib Map InstructionBuilder without any instructions
613 * @param tcpport Integer representing a source TCP port
614 * @return ib Map InstructionBuilder with instructions
616 public static InstructionBuilder createSetDstTCPPort(InstructionBuilder ib, PortNumber tcpport) {
618 List<Action> actionList = new ArrayList<>();
619 ActionBuilder ab = new ActionBuilder();
620 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
622 // Build the Destination TCP Port
623 PortNumber tcpdstport = new PortNumber(tcpport);
624 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
625 tcpmatch.setTcpDestinationPort(tcpdstport);
627 setFieldBuilder.setLayer4Match(tcpmatch.build());
628 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
630 ab.setKey(new ActionKey(0));
631 actionList.add(ab.build());
633 ApplyActionsBuilder aab = new ApplyActionsBuilder();
634 aab.setAction(actionList);
635 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
641 * Create Set Source UDP Port Instruction
643 * @param ib Map InstructionBuilder without any instructions
644 * @param udpport Integer representing a source UDP port
645 * @return ib Map InstructionBuilder with instructions
647 public static InstructionBuilder createSetSrcUDPPort(InstructionBuilder ib, PortNumber udpport) {
649 List<Action> actionList = new ArrayList<>();
650 ActionBuilder ab = new ActionBuilder();
651 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
653 // Build the Destination TCP Port
654 PortNumber udpsrcport = new PortNumber(udpport);
655 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
656 udpmatch.setUdpSourcePort(udpsrcport);
658 setFieldBuilder.setLayer4Match(udpmatch.build());
659 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
661 ab.setKey(new ActionKey(0));
662 actionList.add(ab.build());
664 ApplyActionsBuilder aab = new ApplyActionsBuilder();
665 aab.setAction(actionList);
666 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
672 * Create Set Destination UDP Port Instruction
674 * @param ib Map InstructionBuilder without any instructions
675 * @param udpport Integer representing a destination UDP port
676 * @return ib Map InstructionBuilder with instructions
678 public static InstructionBuilder createSetDstUDPPort(InstructionBuilder ib, PortNumber udpport) {
680 List<Action> actionList = new ArrayList<>();
681 ActionBuilder ab = new ActionBuilder();
682 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
684 // Build the Destination TCP Port
685 PortNumber udpdstport = new PortNumber(udpport);
686 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
687 udpmatch.setUdpDestinationPort(udpdstport);
689 setFieldBuilder.setLayer4Match(udpmatch.build());
690 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
692 ab.setKey(new ActionKey(0));
693 actionList.add(ab.build());
695 ApplyActionsBuilder aab = new ApplyActionsBuilder();
696 aab.setAction(actionList);
697 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
703 * Create Set ICMP Code Instruction
705 * @param ib Map InstructionBuilder without any instructions
706 * @param code short repesenting an ICMP code
707 * @return ib Map InstructionBuilder with instructions
710 public static InstructionBuilder createSetIcmpCodeInstruction(InstructionBuilder ib, short code) {
712 List<Action> actionList = new ArrayList<>();
713 ActionBuilder ab = new ActionBuilder();
714 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
715 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
717 // Build the ICMPv4 Code Match
718 icmpv4match.setIcmpv4Code(code);
719 setFieldBuilder.setIcmpv4Match(icmpv4match.build());
721 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
723 ab.setKey(new ActionKey(0));
724 actionList.add(ab.build());
725 ApplyActionsBuilder aab = new ApplyActionsBuilder();
726 aab.setAction(actionList);
728 // Wrap our Apply Action in an Instruction
729 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
735 * Create Set ICMP Code Instruction
737 * @param ib Map InstructionBuilder without any instructions
738 * @return ib Map InstructionBuilder with instructions
740 public static InstructionBuilder createSetIcmpTypeInstruction(InstructionBuilder ib, short type) {
742 List<Action> actionList = new ArrayList<>();
743 ActionBuilder ab = new ActionBuilder();
744 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
745 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
747 // Build the ICMPv4 Code Match
748 icmpv4match.setIcmpv4Code(type);
749 setFieldBuilder.setIcmpv4Match(icmpv4match.build());
751 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
753 ab.setKey(new ActionKey(0));
754 actionList.add(ab.build());
755 ApplyActionsBuilder aab = new ApplyActionsBuilder();
756 aab.setAction(actionList);
758 // Wrap our Apply Action in an Instruction
759 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
765 * Create Decrement TTL Instruction
767 * @param ib Map InstructionBuilder without any instructions
768 * @return ib Map InstructionBuilder with instructions
770 public static InstructionBuilder createDecNwTtlInstructions(InstructionBuilder ib) {
771 DecNwTtlBuilder decNwTtlBuilder = new DecNwTtlBuilder();
772 DecNwTtl decNwTtl = decNwTtlBuilder.build();
773 ActionBuilder ab = new ActionBuilder();
774 ab.setAction(new DecNwTtlCaseBuilder().setDecNwTtl(decNwTtl).build());
776 ab.setKey(new ActionKey(0));
778 // Add our drop action to a list
779 List<Action> actionList = new ArrayList<>();
780 actionList.add(ab.build());
782 // Create an Apply Action
783 ApplyActionsBuilder aab = new ApplyActionsBuilder();
784 aab.setAction(actionList);
786 // Wrap our Apply Action in an Instruction
787 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
793 * Set ARP_SHA Instructions
794 * @param ib Map InstructionBuilder
795 * @param macsrc the macsrc
796 * @return instructionbuilder with new instructions
798 public static InstructionBuilder createSrcArpMacInstructions(InstructionBuilder ib, MacAddress macsrc) {
800 List<Action> actionList = new ArrayList<>();
801 ActionBuilder ab = new ActionBuilder();
803 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
804 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
805 ArpSourceHardwareAddressBuilder arpsrc = new ArpSourceHardwareAddressBuilder();
806 arpsrc.setAddress(macsrc);
807 arpmatch.setArpSourceHardwareAddress(arpsrc.build());
808 setFieldBuilder.setLayer3Match(arpmatch.build());
809 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
811 ab.setKey(new ActionKey(0));
812 actionList.add(ab.build());
814 ApplyActionsBuilder aab = new ApplyActionsBuilder();
815 aab.setAction(actionList);
816 // Wrap our Apply Action in an Instruction
817 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
823 * Set ARP_THA Instructions
824 * @param ib Map InstructionBuilder
825 * @param macdst the macdst
826 * @return instructionbuilder with new attributes
828 public static InstructionBuilder createDstArpMacInstructions(InstructionBuilder ib, MacAddress macdst) {
830 List<Action> actionList = new ArrayList<>();
831 ActionBuilder ab = new ActionBuilder();
832 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
834 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
835 ArpTargetHardwareAddressBuilder arpdst = new ArpTargetHardwareAddressBuilder();
836 arpdst.setAddress(macdst);
837 setFieldBuilder.setLayer3Match(arpmatch.build());
838 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
840 ab.setKey(new ActionKey(0));
841 actionList.add(ab.build());
843 ApplyActionsBuilder aab = new ApplyActionsBuilder();
844 aab.setAction(actionList);
845 // Wrap our Apply Action in an Instruction
846 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
852 * Set ARP_TPA Instructions
853 * @param ib Map InstructionBuilder
854 * @param dstiparp the dstiparp
855 * @return instructionbuilder with new attributes
857 public static InstructionBuilder createDstArpIpInstructions(InstructionBuilder ib, Ipv4Prefix dstiparp) {
859 List<Action> actionList = new ArrayList<>();
860 ActionBuilder ab = new ActionBuilder();
861 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
863 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
864 arpmatch.setArpTargetTransportAddress(dstiparp);
865 setFieldBuilder.setLayer3Match(arpmatch.build());
866 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
868 ab.setKey(new ActionKey(0));
869 actionList.add(ab.build());
871 ApplyActionsBuilder aab = new ApplyActionsBuilder();
872 aab.setAction(actionList);
873 // Wrap our Apply Action in an Instruction
874 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
880 * Set ARP_SPA Instructions
881 * @param ib Map InstructionBuilder
882 * @param srciparp the srciparp
883 * @return instructionbuilder with new attributes
885 public static InstructionBuilder createSrcArpIpInstructions(InstructionBuilder ib, Ipv4Prefix srciparp) {
887 List<Action> actionList = new ArrayList<>();
888 ActionBuilder ab = new ActionBuilder();
889 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
891 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
892 arpmatch.setArpSourceTransportAddress(srciparp);
893 setFieldBuilder.setLayer3Match(arpmatch.build());
894 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
896 ab.setKey(new ActionKey(0));
897 actionList.add(ab.build());
899 ApplyActionsBuilder aab = new ApplyActionsBuilder();
900 aab.setAction(actionList);
901 // Wrap our Apply Action in an Instruction
902 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
908 * Set the Tunnel EndpointIPv4 Source Address
909 * @param ib Map InstructionBuilder
910 * @param srcIp Ipv4Prefix source IP for the tunnel endpoint (TEP)
911 * @return instructionbuilder with new attributes
913 public static InstructionBuilder createTunnelIpv4SrcInstructions(InstructionBuilder ib, Ipv4Prefix srcIp) {
915 List<Action> actionList = new ArrayList<>();
916 ActionBuilder ab = new ActionBuilder();
918 // Build the tunnel endpoint source IPv4 address
919 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
921 // Add the new IPv4 object as the tunnel destination
922 TunnelIpv4MatchBuilder tunnelIpv4MatchBuilder = new TunnelIpv4MatchBuilder();
923 tunnelIpv4MatchBuilder.setTunnelIpv4Source(srcIp);
924 setFieldBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
926 // Add the IPv4 tunnel src to the set_field value
927 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
929 ab.setKey(new ActionKey(0));
930 actionList.add(ab.build());
932 // Resulting action is a per/flow src TEP (set_field:172.16.100.100->tun_src)
933 ApplyActionsBuilder aab = new ApplyActionsBuilder();
934 aab.setAction(actionList);
935 // Wrap our Apply Action in an Instruction
936 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
942 * Set the Tunnel EndpointIPv4 Destination Address
943 * @param ib Map InstructionBuilder
944 * @param dstIp Ipv4Prefix destination IP for the tunnel endpoint (TEP)
945 * @return instructionbuilder with new attributes
947 public static InstructionBuilder createTunnelIpv4DstInstructions(InstructionBuilder ib, Ipv4Prefix dstIp) {
949 List<Action> actionList = new ArrayList<>();
950 ActionBuilder ab = new ActionBuilder();
952 // Build the tunnel endpoint dst IPv4 address
953 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
955 // Add the new IPv4 object as the tunnel destination
956 TunnelIpv4MatchBuilder tunnelIpv4MatchBuilder = new TunnelIpv4MatchBuilder();
957 tunnelIpv4MatchBuilder.setTunnelIpv4Destination(dstIp);
958 setFieldBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
960 // Add the IPv4 tunnel src to the set_field value
961 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
963 ab.setKey(new ActionKey(0));
964 actionList.add(ab.build());
966 // Resulting action is a per/flow src TEP (set_field:172.16.100.100->tun_src)
967 ApplyActionsBuilder aab = new ApplyActionsBuilder();
968 aab.setAction(actionList);
969 // Wrap our Apply Action in an Instruction
970 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
976 * Set IPv4 Source Address Instructions
977 * @param ib Map InstructionBuilder
978 * @param metaData BigInteger representing the OpenFlow metadata
979 * @param metaDataMask BigInteger representing the OpenFlow metadata mask
980 * @return instructionbuilder with new attributes
982 public static InstructionBuilder createMetadataInstructions(InstructionBuilder ib, BigInteger metaData, BigInteger metaDataMask) {
984 WriteMetadataBuilder aab = new WriteMetadataBuilder();
985 aab.setMetadata(metaData);
986 aab.setMetadataMask(metaDataMask);
987 ib.setInstruction(new WriteMetadataCaseBuilder().setWriteMetadata(aab.build()).build());
991 * TODO Determine why the following fails serialization
992 * with a null value. Per the spec it is optional.
993 * The metadata match, the Flowmod works w/o an accompanying mask.
995 * if (metaDataMask != null) {
996 * aab.setMetadataMask(metaDataMask);
1004 public static InstructionBuilder createDlSrcInstructions(InstructionBuilder ib, MacAddress macAddress) {
1006 List<Action> actionList = new ArrayList<>();
1007 ActionBuilder ab = new ActionBuilder();
1009 SetDlSrcActionBuilder dlSrcActionBuilder= new SetDlSrcActionBuilder();
1011 dlSrcActionBuilder.setAddress(macAddress);
1013 ab.setAction(new SetDlSrcActionCaseBuilder().setSetDlSrcAction(dlSrcActionBuilder.build()).build());
1015 ab.setKey(new ActionKey(0));
1016 actionList.add(ab.build());
1019 // Create an Apply Action
1020 ApplyActionsBuilder aab = new ApplyActionsBuilder();
1021 aab.setAction(actionList);
1023 // Wrap our Apply Action in an Instruction
1024 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1029 public static InstructionBuilder createDlDstInstructions(InstructionBuilder ib, MacAddress macAddress) {
1031 List<Action> actionList = new ArrayList<>();
1032 ActionBuilder ab = new ActionBuilder();
1034 SetDlDstActionBuilder dlDstActionBuilder= new SetDlDstActionBuilder();
1036 dlDstActionBuilder.setAddress(macAddress);
1038 ab.setAction(new SetDlDstActionCaseBuilder().setSetDlDstAction(dlDstActionBuilder.build()).build());
1040 ab.setKey(new ActionKey(0));
1041 actionList.add(ab.build());
1043 // Create an Apply Action
1044 ApplyActionsBuilder aab = new ApplyActionsBuilder();
1045 aab.setAction(actionList);
1047 // Wrap our Apply Action in an Instruction
1048 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1053 public static List<Action>
1054 actionList(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action... actions) {
1056 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> alist
1057 = new ArrayList<>();
1059 for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action action : actions) {
1060 alist.add(new ActionBuilder()
1062 .setKey(new ActionKey(count))
1070 public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction
1071 applyActionIns(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action... actions) {
1073 return new ApplyActionsCaseBuilder()
1074 .setApplyActions(new ApplyActionsBuilder()
1075 .setAction(actionList(actions))
1080 public static Instructions getInstructions(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction... instructions) {
1081 List<Instruction> ins
1082 = new ArrayList<>();
1084 for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction i : instructions) {
1085 ins.add(new InstructionBuilder()
1087 .setKey(new InstructionKey(order))
1092 return new InstructionsBuilder().setInstruction(ins).build();
1095 public static Instructions dropInstructions() {
1096 return getInstructions(applyActionIns(dropAction()));
1100 * Extracts the existing instructions (if any) from the flow.
1102 * @param flow The flow.
1103 * @return The instructions in the flow (empty if none).
1105 public static List<Instruction> extractExistingInstructions(Flow flow) {
1107 Instructions ins = flow.getInstructions();
1109 return ins.getInstruction();
1112 return Collections.emptyList();
1116 * Configures the flow builder to have the single given instruction.
1118 * @param flowBuilder The flow builder.
1119 * @param instruction The instruction.
1120 * @return The flow builder.
1122 public static FlowBuilder setFlowBuilderInstruction(FlowBuilder flowBuilder, Instruction instruction) {
1123 flowBuilder.setInstructions(
1124 new InstructionsBuilder()
1125 .setInstruction(Collections.singletonList(instruction))
1131 * Get a list of Instructions containing Nicira extensions that can have
1132 * additional OF/OXM instructions added to the returned Instruction list
1133 * @param instructions org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instructions
1134 * @return instruction list that additional
1136 public static List<Instruction> getInstructionList(
1137 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction... instructions) {
1138 List<Instruction> ins
1139 = new ArrayList<>();
1141 for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction i : instructions) {
1142 ins.add(new InstructionBuilder()
1151 * Create InstructionBuilder with apply action.
1153 * @param aab the apply action Builder.
1154 * @param order the position of the instruction in the instruction list.
1155 * @param drop indicates whether it is a drop instruction.
1156 * @return the instruction builder.
1158 public static InstructionBuilder createInstructionBuilder(ApplyActionsBuilder aab, int order, boolean drop) {
1159 InstructionBuilder ib = new InstructionBuilder();
1161 ib.setKey(new InstructionKey(order));
1162 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1164 InstructionUtils.createDropInstructions(ib);