2 * Copyright (c) 2013, 2015 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 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DecNwTtlCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlDstActionCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlSrcActionCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.nw.ttl._case.DecNwTtl;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.nw.ttl._case.DecNwTtlBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropAction;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.vlan.action._case.PopVlanActionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanActionBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.dl.dst.action._case.SetDlDstActionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.dl.src.action._case.SetDlSrcActionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.dst.action._case.SetNwDstActionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.src.action._case.SetNwSrcActionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.vlan.id.action._case.SetVlanIdActionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.TunnelIpv4MatchBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
74 import java.math.BigInteger;
75 import java.util.ArrayList;
76 import java.util.List;
78 public class InstructionUtils {
79 private static final Logger LOG = LoggerFactory.getLogger(InstructionUtils.class);
80 private static final int IPV4 = 0x8100;
81 private static final int MAX_LENGTH = 0xffff;
84 * Create Send to Controller Reserved Port Instruction (packet_in)
86 * @param nodeName Uri Prefix, containing nodeConnectorType and dpId (aka NodeId)
87 * @param ib Map InstructionBuilder without any instructions
88 * @return ib Map InstructionBuilder with instructions
90 public static InstructionBuilder createSendToControllerInstructions(String nodeName, InstructionBuilder ib) {
92 List<Action> actionList = new ArrayList<>();
93 ActionBuilder ab = new ActionBuilder();
95 OutputActionBuilder output = new OutputActionBuilder();
96 output.setMaxLength(MAX_LENGTH);
97 NodeId nodeId = new NodeId(nodeName);
98 output.setOutputNodeConnector(new NodeConnectorId(nodeId.getValue() + ":"
99 + OutputPortValues.CONTROLLER.toString()));
100 ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
102 ab.setKey(new ActionKey(0));
103 actionList.add(ab.build());
105 // Create an Apply Action
106 ApplyActionsBuilder aab = new ApplyActionsBuilder();
107 aab.setAction(actionList);
109 // Wrap our Apply Action in an Instruction
110 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
116 * Create LOCAL Reserved Port Instruction
118 * @param nodeName Uri Prefix, containing nodeConnectorType and dpId (aka NodeId)
119 * @param ib Map InstructionBuilder without any instructions
120 * @return ib Map InstructionBuilder with instructions
123 public static InstructionBuilder createNormalInstructions(String nodeName, InstructionBuilder ib) {
125 List<Action> actionList = new ArrayList<>();
126 ActionBuilder ab = new ActionBuilder();
128 OutputActionBuilder output = new OutputActionBuilder();
129 NodeId nodeId = new NodeId(nodeName);
130 output.setOutputNodeConnector(new NodeConnectorId(nodeId.getValue() + ":"
131 + OutputPortValues.NORMAL.toString()));
132 ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
134 ab.setKey(new ActionKey(0));
135 actionList.add(ab.build());
137 // Create an Apply Action
138 ApplyActionsBuilder aab = new ApplyActionsBuilder();
139 aab.setAction(actionList);
141 // Wrap our Apply Action in an Instruction
142 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
148 * Create NORMAL Reserved Port Instruction (packet_in)
150 * @param ib Map InstructionBuilder without any instructions
151 * @param dpidLong Long the datapath ID of a switch/node
152 * @return ib Map InstructionBuilder with instructions
154 public static InstructionBuilder createLocalInstructions(InstructionBuilder ib, long dpidLong) {
155 List<Action> actionList = new ArrayList<>();
156 ActionBuilder ab = new ActionBuilder();
158 OutputActionBuilder output = new OutputActionBuilder();
159 output.setOutputNodeConnector(new NodeConnectorId("openflow:" + dpidLong + ":"
160 + OutputPortValues.LOCAL.toString()));
161 ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
163 ab.setKey(new ActionKey(0));
164 actionList.add(ab.build());
166 // Create an Apply Action
167 ApplyActionsBuilder aab = new ApplyActionsBuilder();
168 aab.setAction(actionList);
170 // Wrap our Apply Action in an Instruction
171 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
177 * Create Output Port Instruction
179 * @param ib Map InstructionBuilder without any instructions
180 * @param dpidLong Long the datapath ID of a switch/node
181 * @param port Long representing a port on a switch/node
182 * @return ib InstructionBuilder Map with instructions
184 public static InstructionBuilder createOutputPortInstructions(InstructionBuilder ib, Long dpidLong, Long port) {
186 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + port);
187 LOG.debug("createOutputPortInstructions() Node Connector ID is - Type=openflow: DPID={} inPort={} ",
190 List<Action> actionList = new ArrayList<>();
191 ActionBuilder ab = new ActionBuilder();
192 OutputActionBuilder oab = new OutputActionBuilder();
193 oab.setOutputNodeConnector(ncid);
195 ab.setAction(new OutputActionCaseBuilder().setOutputAction(oab.build()).build());
197 ab.setKey(new ActionKey(0));
198 actionList.add(ab.build());
200 // Create an Apply Action
201 ApplyActionsBuilder aab = new ApplyActionsBuilder();
202 aab.setAction(actionList);
203 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
209 * add Output Port action to Instruction action list.
210 * This is use for flow with single output port actions.
211 * Flow with mutiple output port actions should use createOutputPortInstructions() method.
213 * @param ib Map InstructionBuilder without any instructions
214 * @param dpidLong Long the datapath ID of a switch/node
215 * @param port Long representing a port on a switch/node
216 * @return ib InstructionBuilder Map with instructions
218 public static InstructionBuilder addOutputPortInstructions(InstructionBuilder ib,
219 Long dpidLong, Long port,
220 List<Instruction> instructions) {
221 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + port);
223 "addOutputPortInstructions() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}",
224 dpidLong, port, instructions);
226 List<Action> actionList = new ArrayList<>();
227 ActionBuilder ab = new ActionBuilder();
229 List<Action> existingActions;
230 if (instructions != null) {
231 for (Instruction in : instructions) {
232 if (in.getInstruction() instanceof ApplyActionsCase) {
233 existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
234 actionList.addAll(existingActions);
239 /* Create output action for this port*/
240 OutputActionBuilder oab = new OutputActionBuilder();
241 oab.setOutputNodeConnector(ncid);
242 ab.setAction(new OutputActionCaseBuilder().setOutputAction(oab.build()).build());
243 ab.setOrder(actionList.size());
244 ab.setKey(new ActionKey(actionList.size()));
245 actionList.add(ab.build());
247 // Create an Apply Action
248 ApplyActionsBuilder aab = new ApplyActionsBuilder();
249 aab.setAction(actionList);
250 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
256 * Remove Output Port from Instruction
258 * @param ib Map InstructionBuilder without any instructions
259 * @param dpidLong Long the datapath ID of a switch/node
260 * @param port Long representing a port on a switch/node
261 * @return ib InstructionBuilder Map with instructions
263 public static boolean removeOutputPortFromInstructions(InstructionBuilder ib,
264 Long dpidLong, Long port, List<Instruction> instructions) {
266 final NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + port);
267 final Uri ncidUri = new Uri(ncid);
269 "removeOutputPortFromInstructions() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}",
270 dpidLong, port, instructions);
272 List<Action> actionList = new ArrayList<>();
275 // Start of by locating actions that will have port removed, from the existing instructionList
277 List<Action> existingActions;
278 if (instructions != null) {
279 for (Instruction in : instructions) {
280 if (in.getInstruction() instanceof ApplyActionsCase) {
281 existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
282 actionList.addAll(existingActions);
288 int removedActionOrder = 0;
289 boolean isPortDeleted = false;
290 boolean removeFlow = true;
292 // Locate specific action that has the port to be removed. Then, take note on its action order
293 // and remove it from list, in addition to flag that it was found.
295 for (Action action : actionList) {
296 if (action.getAction() instanceof OutputActionCase) {
297 OutputActionCase opAction = (OutputActionCase) action.getAction();
298 if (opAction.getOutputAction().getOutputNodeConnector().equals(ncidUri)) {
299 /* Find the output port in action list and remove */
300 removedActionOrder = action.getOrder();
301 actionList.remove(action);
302 isPortDeleted = true;
309 // Iterate through all actions in the modified list and adjust the order of
310 // the actions left behind. With that, all actions that have order higher than
311 // the action removed gets their value decremented by 1. Note that this iteration
312 // visits all entries to account for cases where the list order is not the same
313 // as the action's order.
315 for (int i = 0; i < actionList.size(); i++) {
316 Action action = actionList.get(i);
317 if (action.getOrder() > removedActionOrder) {
318 /* Shift the action by rebuilding action, using adjusted order */
319 ab = new ActionBuilder();
320 ab.setAction(action.getAction());
321 ab.setOrder(action.getOrder() - 1);
322 ab.setKey(new ActionKey(action.getOrder() - 1));
323 Action actionNewOrder = ab.build();
324 actionList.remove(action);
325 actionList.add(i, actionNewOrder);
326 } else if (action.getOrder() == removedActionOrder) {
327 // Sanity: implementation assumes no two actions have the same order
329 LOG.error("Found action with same order as the action removed for {}, order {} index {}: {}",
330 ncid, removedActionOrder, i, action);
333 // If action refers to a port output, then flow should be preserved.
334 // We do this, so that if we are only left with non-output port actions,
335 // we still remove the flow
337 if (action.getAction() instanceof OutputActionCase) {
342 // If port we are asked to delete is not found, this implementation will leave actions
343 // alone and not remove the flow, as long as a remaining OutputActionCase is found.
345 for (Action action : actionList) {
346 if (action.getAction() instanceof OutputActionCase) {
353 /* Put new action list in Apply Action instruction */
355 ApplyActionsBuilder aab = new ApplyActionsBuilder();
356 aab.setAction(actionList);
357 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
358 LOG.debug("removeOutputPortFromInstructions() : applyAction {}", aab.build());
361 /* if all output ports are removed. Return true to indicate flow remove */
367 * Create Set Vlan ID Instruction - This includes push vlan action, and set field -> vlan vid action
369 * @param ib Map InstructionBuilder without any instructions
370 * @param vlanId Integer representing a VLAN ID Integer representing a VLAN ID
371 * @return ib Map InstructionBuilder with instructions
373 public static InstructionBuilder createSetVlanInstructions(InstructionBuilder ib, VlanId vlanId) {
375 List<Action> actionList = new ArrayList<>();
376 ActionBuilder ab = new ActionBuilder();
378 /* First we push vlan header */
379 PushVlanActionBuilder vlan = new PushVlanActionBuilder();
380 vlan.setEthernetType(IPV4);
381 ab.setAction(new PushVlanActionCaseBuilder().setPushVlanAction(vlan.build()).build());
383 actionList.add(ab.build());
385 /* Then we set vlan id value as vlanId */
386 SetVlanIdActionBuilder vl = new SetVlanIdActionBuilder();
387 vl.setVlanId(vlanId);
388 ab = new ActionBuilder();
389 ab.setAction(new SetVlanIdActionCaseBuilder().setSetVlanIdAction(vl.build()).build());
391 actionList.add(ab.build());
392 // Create an Apply Action
393 ApplyActionsBuilder aab = new ApplyActionsBuilder();
394 aab.setAction(actionList);
396 // Wrap our Apply Action in an Instruction
397 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
403 * Create Pop Vlan Instruction - this remove vlan header
405 * @param ib Map InstructionBuilder without any instructions
406 * @return ib Map InstructionBuilder with instructions
408 public static InstructionBuilder createPopVlanInstructions(InstructionBuilder ib) {
410 List<Action> actionList = new ArrayList<>();
411 ActionBuilder ab = new ActionBuilder();
413 PopVlanActionBuilder popVlanActionBuilder = new PopVlanActionBuilder();
414 ab.setAction(new PopVlanActionCaseBuilder().setPopVlanAction(popVlanActionBuilder.build()).build());
416 actionList.add(ab.build());
418 // Create an Apply Action
419 ApplyActionsBuilder aab = new ApplyActionsBuilder();
420 aab.setAction(actionList);
422 // Wrap our Apply Action in an Instruction
423 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
429 * Create Set IPv4 Source Instruction
431 * @param ib Map InstructionBuilder without any instructions
432 * @param prefixsrc String containing an IPv4 prefix
433 * @return ib Map InstructionBuilder with instructions
435 public static InstructionBuilder createNwSrcInstructions(InstructionBuilder ib, Ipv4Prefix prefixsrc) {
437 List<Action> actionList = new ArrayList<>();
438 ActionBuilder ab = new ActionBuilder();
440 SetNwSrcActionBuilder setNwsrcActionBuilder = new SetNwSrcActionBuilder();
441 Ipv4Builder ipsrc = new Ipv4Builder();
442 ipsrc.setIpv4Address(prefixsrc);
443 setNwsrcActionBuilder.setAddress(ipsrc.build());
444 ab.setAction(new SetNwSrcActionCaseBuilder().setSetNwSrcAction(setNwsrcActionBuilder.build()).build());
446 ab.setKey(new ActionKey(0));
447 actionList.add(ab.build());
449 // Create an Apply Action
450 ApplyActionsBuilder aab = new ApplyActionsBuilder();
451 aab.setAction(actionList);
453 // Wrap our Apply Action in an Instruction
454 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
460 * Create Set IPv4 Destination Instruction
462 * @param ib Map InstructionBuilder without any instructions
463 * @param prefixdst String containing an IPv4 prefix
464 * @param extraAction (optional) Additional action to be performed in actionList
465 * @return ib Map InstructionBuilder with instructions
467 public static InstructionBuilder createNwDstInstructions(InstructionBuilder ib, Ipv4Prefix prefixdst,
468 ActionBuilder extraAction) {
470 List<Action> actionList = new ArrayList<>();
471 ActionBuilder ab = new ActionBuilder();
473 SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
474 Ipv4Builder ipdst = new Ipv4Builder();
475 ipdst.setIpv4Address(prefixdst);
476 setNwDstActionBuilder.setAddress(ipdst.build());
477 ab.setAction(new SetNwDstActionCaseBuilder().setSetNwDstAction(setNwDstActionBuilder.build()).build());
479 ab.setKey(new ActionKey(0));
480 actionList.add(ab.build());
482 if (extraAction != null) {
483 extraAction.setOrder(1);
484 extraAction.setKey(new ActionKey(1));
485 actionList.add(extraAction.build());
488 // Create an Apply Action
489 ApplyActionsBuilder aab = new ApplyActionsBuilder();
490 aab.setAction(actionList);
492 // Wrap our Apply Action in an Instruction
493 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
499 * Create Drop Instruction
501 * @param ib Map InstructionBuilder without any instructions
502 * @return ib Map InstructionBuilder with instructions
504 public static InstructionBuilder createDropInstructions(InstructionBuilder ib) {
506 DropActionBuilder dab = new DropActionBuilder();
507 DropAction dropAction = dab.build();
508 ActionBuilder ab = new ActionBuilder();
509 ab.setAction(new DropActionCaseBuilder().setDropAction(dropAction).build());
511 ab.setKey(new ActionKey(0));
513 // Add our drop action to a list
514 List<Action> actionList = new ArrayList<>();
515 actionList.add(ab.build());
517 // Create an Apply Action
518 ApplyActionsBuilder aab = new ApplyActionsBuilder();
519 aab.setAction(actionList);
521 // Wrap our Apply Action in an Instruction
522 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
528 * Create GOTO Table Instruction Builder
530 * @param ib Map InstructionBuilder without any instructions
531 * @param tableId short representing a flow table ID short representing a flow table ID
532 * @return ib Map InstructionBuilder with instructions
534 public static InstructionBuilder createGotoTableInstructions(InstructionBuilder ib, short tableId) {
536 GoToTableBuilder gttb = new GoToTableBuilder();
537 gttb.setTableId(tableId);
539 // Wrap our Apply Action in an InstructionBuilder
540 ib.setInstruction(new GoToTableCaseBuilder().setGoToTable(gttb.build()).build());
546 * Create Set Tunnel ID Instruction Builder
548 * @param ib Map InstructionBuilder without any instructions
549 * @param tunnelId BigInteger representing a tunnel ID
550 * @return ib Map InstructionBuilder with instructions
552 public static InstructionBuilder createSetTunnelIdInstructions(InstructionBuilder ib, BigInteger tunnelId) {
554 List<Action> actionList = new ArrayList<>();
555 ActionBuilder ab = new ActionBuilder();
556 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
558 // Build the Set Tunnel Field Action
559 TunnelBuilder tunnel = new TunnelBuilder();
560 tunnel.setTunnelId(tunnelId);
561 setFieldBuilder.setTunnel(tunnel.build());
562 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
564 ab.setKey(new ActionKey(0));
565 actionList.add(ab.build());
567 ApplyActionsBuilder aab = new ApplyActionsBuilder();
568 aab.setAction(actionList);
570 // Wrap the Apply Action in an InstructionBuilder and return
571 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
577 * Create Set Source TCP Port Instruction
579 * @param ib Map InstructionBuilder without any instructions
580 * @param tcpport Integer representing a source TCP port
581 * @return ib Map InstructionBuilder with instructions
583 public static InstructionBuilder createSetSrcTCPPort(InstructionBuilder ib, PortNumber tcpport) {
585 List<Action> actionList = new ArrayList<>();
586 ActionBuilder ab = new ActionBuilder();
587 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
589 // Build the Destination TCP Port
590 PortNumber tcpsrcport = new PortNumber(tcpport);
591 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
592 tcpmatch.setTcpSourcePort(tcpsrcport);
594 setFieldBuilder.setLayer4Match(tcpmatch.build());
595 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
597 ab.setKey(new ActionKey(0));
598 actionList.add(ab.build());
600 ApplyActionsBuilder aab = new ApplyActionsBuilder();
601 aab.setAction(actionList);
602 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
608 * Create Set Destination TCP Port Instruction
610 * @param ib Map InstructionBuilder without any instructions
611 * @param tcpport Integer representing a source TCP port
612 * @return ib Map InstructionBuilder with instructions
614 public static InstructionBuilder createSetDstTCPPort(InstructionBuilder ib, PortNumber tcpport) {
616 List<Action> actionList = new ArrayList<>();
617 ActionBuilder ab = new ActionBuilder();
618 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
620 // Build the Destination TCP Port
621 PortNumber tcpdstport = new PortNumber(tcpport);
622 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
623 tcpmatch.setTcpDestinationPort(tcpdstport);
625 setFieldBuilder.setLayer4Match(tcpmatch.build());
626 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
628 ab.setKey(new ActionKey(0));
629 actionList.add(ab.build());
631 ApplyActionsBuilder aab = new ApplyActionsBuilder();
632 aab.setAction(actionList);
633 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
639 * Create Set Source UDP Port Instruction
641 * @param ib Map InstructionBuilder without any instructions
642 * @param udpport Integer representing a source UDP port
643 * @return ib Map InstructionBuilder with instructions
645 public static InstructionBuilder createSetSrcUDPPort(InstructionBuilder ib, PortNumber udpport) {
647 List<Action> actionList = new ArrayList<>();
648 ActionBuilder ab = new ActionBuilder();
649 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
651 // Build the Destination TCP Port
652 PortNumber udpsrcport = new PortNumber(udpport);
653 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
654 udpmatch.setUdpSourcePort(udpsrcport);
656 setFieldBuilder.setLayer4Match(udpmatch.build());
657 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
659 ab.setKey(new ActionKey(0));
660 actionList.add(ab.build());
662 ApplyActionsBuilder aab = new ApplyActionsBuilder();
663 aab.setAction(actionList);
664 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
670 * Create Set Destination UDP Port Instruction
672 * @param ib Map InstructionBuilder without any instructions
673 * @param udpport Integer representing a destination UDP port
674 * @return ib Map InstructionBuilder with instructions
676 public static InstructionBuilder createSetDstUDPPort(InstructionBuilder ib, PortNumber udpport) {
678 List<Action> actionList = new ArrayList<>();
679 ActionBuilder ab = new ActionBuilder();
680 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
682 // Build the Destination TCP Port
683 PortNumber udpdstport = new PortNumber(udpport);
684 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
685 udpmatch.setUdpDestinationPort(udpdstport);
687 setFieldBuilder.setLayer4Match(udpmatch.build());
688 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
690 ab.setKey(new ActionKey(0));
691 actionList.add(ab.build());
693 ApplyActionsBuilder aab = new ApplyActionsBuilder();
694 aab.setAction(actionList);
695 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
701 * Create Set ICMP Code Instruction
703 * @param ib Map InstructionBuilder without any instructions
704 * @param code short repesenting an ICMP code
705 * @return ib Map InstructionBuilder with instructions
708 public static InstructionBuilder createSetIcmpCodeInstruction(InstructionBuilder ib, short code) {
710 List<Action> actionList = new ArrayList<>();
711 ActionBuilder ab = new ActionBuilder();
712 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
713 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
715 // Build the ICMPv4 Code Match
716 icmpv4match.setIcmpv4Code(code);
717 setFieldBuilder.setIcmpv4Match(icmpv4match.build());
719 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
721 ab.setKey(new ActionKey(0));
722 actionList.add(ab.build());
723 ApplyActionsBuilder aab = new ApplyActionsBuilder();
724 aab.setAction(actionList);
726 // Wrap our Apply Action in an Instruction
727 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
733 * Create Set ICMP Code Instruction
735 * @param ib Map InstructionBuilder without any instructions
736 * @return ib Map InstructionBuilder with instructions
738 public static InstructionBuilder createSetIcmpTypeInstruction(InstructionBuilder ib, short type) {
740 List<Action> actionList = new ArrayList<>();
741 ActionBuilder ab = new ActionBuilder();
742 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
743 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
745 // Build the ICMPv4 Code Match
746 icmpv4match.setIcmpv4Code(type);
747 setFieldBuilder.setIcmpv4Match(icmpv4match.build());
749 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
751 ab.setKey(new ActionKey(0));
752 actionList.add(ab.build());
753 ApplyActionsBuilder aab = new ApplyActionsBuilder();
754 aab.setAction(actionList);
756 // Wrap our Apply Action in an Instruction
757 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
763 * Create Decrement TTL Instruction
765 * @param ib Map InstructionBuilder without any instructions
766 * @return ib Map InstructionBuilder with instructions
768 public static InstructionBuilder createDecNwTtlInstructions(InstructionBuilder ib) {
769 DecNwTtlBuilder decNwTtlBuilder = new DecNwTtlBuilder();
770 DecNwTtl decNwTtl = decNwTtlBuilder.build();
771 ActionBuilder ab = new ActionBuilder();
772 ab.setAction(new DecNwTtlCaseBuilder().setDecNwTtl(decNwTtl).build());
774 ab.setKey(new ActionKey(0));
776 // Add our drop action to a list
777 List<Action> actionList = new ArrayList<>();
778 actionList.add(ab.build());
780 // Create an Apply Action
781 ApplyActionsBuilder aab = new ApplyActionsBuilder();
782 aab.setAction(actionList);
784 // Wrap our Apply Action in an Instruction
785 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
791 * Set ARP_SHA Instructions
792 * @param ib Map InstructionBuilder
793 * @param macsrc the macsrc
794 * @return instructionbuilder with new instructions
796 public static InstructionBuilder createSrcArpMacInstructions(InstructionBuilder ib, MacAddress macsrc) {
798 List<Action> actionList = new ArrayList<>();
799 ActionBuilder ab = new ActionBuilder();
801 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
802 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
803 ArpSourceHardwareAddressBuilder arpsrc = new ArpSourceHardwareAddressBuilder();
804 arpsrc.setAddress(macsrc);
805 arpmatch.setArpSourceHardwareAddress(arpsrc.build());
806 setFieldBuilder.setLayer3Match(arpmatch.build());
807 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
809 ab.setKey(new ActionKey(0));
810 actionList.add(ab.build());
812 ApplyActionsBuilder aab = new ApplyActionsBuilder();
813 aab.setAction(actionList);
814 // Wrap our Apply Action in an Instruction
815 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
821 * Set ARP_THA Instructions
822 * @param ib Map InstructionBuilder
823 * @param macdst the macdst
824 * @return instructionbuilder with new attributes
826 public static InstructionBuilder createDstArpMacInstructions(InstructionBuilder ib, MacAddress macdst) {
828 List<Action> actionList = new ArrayList<>();
829 ActionBuilder ab = new ActionBuilder();
830 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
832 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
833 ArpTargetHardwareAddressBuilder arpdst = new ArpTargetHardwareAddressBuilder();
834 arpdst.setAddress(macdst);
835 setFieldBuilder.setLayer3Match(arpmatch.build());
836 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
838 ab.setKey(new ActionKey(0));
839 actionList.add(ab.build());
841 ApplyActionsBuilder aab = new ApplyActionsBuilder();
842 aab.setAction(actionList);
843 // Wrap our Apply Action in an Instruction
844 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
850 * Set ARP_TPA Instructions
851 * @param ib Map InstructionBuilder
852 * @param dstiparp the dstiparp
853 * @return instructionbuilder with new attributes
855 public static InstructionBuilder createDstArpIpInstructions(InstructionBuilder ib, Ipv4Prefix dstiparp) {
857 List<Action> actionList = new ArrayList<>();
858 ActionBuilder ab = new ActionBuilder();
859 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
861 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
862 arpmatch.setArpTargetTransportAddress(dstiparp);
863 setFieldBuilder.setLayer3Match(arpmatch.build());
864 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
866 ab.setKey(new ActionKey(0));
867 actionList.add(ab.build());
869 ApplyActionsBuilder aab = new ApplyActionsBuilder();
870 aab.setAction(actionList);
871 // Wrap our Apply Action in an Instruction
872 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
878 * Set ARP_SPA Instructions
879 * @param ib Map InstructionBuilder
880 * @param srciparp the srciparp
881 * @return instructionbuilder with new attributes
883 public static InstructionBuilder createSrcArpIpInstructions(InstructionBuilder ib, Ipv4Prefix srciparp) {
885 List<Action> actionList = new ArrayList<>();
886 ActionBuilder ab = new ActionBuilder();
887 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
889 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
890 arpmatch.setArpSourceTransportAddress(srciparp);
891 setFieldBuilder.setLayer3Match(arpmatch.build());
892 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
894 ab.setKey(new ActionKey(0));
895 actionList.add(ab.build());
897 ApplyActionsBuilder aab = new ApplyActionsBuilder();
898 aab.setAction(actionList);
899 // Wrap our Apply Action in an Instruction
900 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
906 * Set the Tunnel EndpointIPv4 Source Address
907 * @param ib Map InstructionBuilder
908 * @param srcIp Ipv4Prefix source IP for the tunnel endpoint (TEP)
909 * @return instructionbuilder with new attributes
911 public static InstructionBuilder createTunnelIpv4SrcInstructions(InstructionBuilder ib, Ipv4Prefix srcIp) {
913 List<Action> actionList = new ArrayList<>();
914 ActionBuilder ab = new ActionBuilder();
916 // Build the tunnel endpoint source IPv4 address
917 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
919 // Add the new IPv4 object as the tunnel destination
920 TunnelIpv4MatchBuilder tunnelIpv4MatchBuilder = new TunnelIpv4MatchBuilder();
921 tunnelIpv4MatchBuilder.setTunnelIpv4Source(srcIp);
922 setFieldBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
924 // Add the IPv4 tunnel src to the set_field value
925 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
927 ab.setKey(new ActionKey(0));
928 actionList.add(ab.build());
930 // Resulting action is a per/flow src TEP (set_field:172.16.100.100->tun_src)
931 ApplyActionsBuilder aab = new ApplyActionsBuilder();
932 aab.setAction(actionList);
933 // Wrap our Apply Action in an Instruction
934 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
940 * Set the Tunnel EndpointIPv4 Destination Address
941 * @param ib Map InstructionBuilder
942 * @param dstIp Ipv4Prefix destination IP for the tunnel endpoint (TEP)
943 * @return instructionbuilder with new attributes
945 public static InstructionBuilder createTunnelIpv4DstInstructions(InstructionBuilder ib, Ipv4Prefix dstIp) {
947 List<Action> actionList = new ArrayList<>();
948 ActionBuilder ab = new ActionBuilder();
950 // Build the tunnel endpoint dst IPv4 address
951 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
953 // Add the new IPv4 object as the tunnel destination
954 TunnelIpv4MatchBuilder tunnelIpv4MatchBuilder = new TunnelIpv4MatchBuilder();
955 tunnelIpv4MatchBuilder.setTunnelIpv4Destination(dstIp);
956 setFieldBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
958 // Add the IPv4 tunnel src to the set_field value
959 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
961 ab.setKey(new ActionKey(0));
962 actionList.add(ab.build());
964 // Resulting action is a per/flow src TEP (set_field:172.16.100.100->tun_src)
965 ApplyActionsBuilder aab = new ApplyActionsBuilder();
966 aab.setAction(actionList);
967 // Wrap our Apply Action in an Instruction
968 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
974 * Set IPv4 Source Address Instructions
975 * @param ib Map InstructionBuilder
976 * @param metaData BigInteger representing the OpenFlow metadata
977 * @param metaDataMask BigInteger representing the OpenFlow metadata mask
978 * @return instructionbuilder with new attributes
980 public static InstructionBuilder createMetadataInstructions(InstructionBuilder ib, BigInteger metaData, BigInteger metaDataMask) {
982 WriteMetadataBuilder aab = new WriteMetadataBuilder();
983 aab.setMetadata(metaData);
984 aab.setMetadataMask(metaDataMask);
985 ib.setInstruction(new WriteMetadataCaseBuilder().setWriteMetadata(aab.build()).build());
989 * TODO Determine why the following fails serialization
990 * with a null value. Per the spec it is optional.
991 * The metadata match, the Flowmod works w/o an accompanying mask.
993 * if (metaDataMask != null) {
994 * aab.setMetadataMask(metaDataMask);
1002 public static InstructionBuilder createDlSrcInstructions(InstructionBuilder ib, MacAddress macAddress) {
1004 List<Action> actionList = new ArrayList<>();
1005 ActionBuilder ab = new ActionBuilder();
1007 SetDlSrcActionBuilder dlSrcActionBuilder= new SetDlSrcActionBuilder();
1009 dlSrcActionBuilder.setAddress(macAddress);
1011 ab.setAction(new SetDlSrcActionCaseBuilder().setSetDlSrcAction(dlSrcActionBuilder.build()).build());
1013 ab.setKey(new ActionKey(0));
1014 actionList.add(ab.build());
1017 // Create an Apply Action
1018 ApplyActionsBuilder aab = new ApplyActionsBuilder();
1019 aab.setAction(actionList);
1021 // Wrap our Apply Action in an Instruction
1022 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1027 public static InstructionBuilder createDlDstInstructions(InstructionBuilder ib, MacAddress macAddress) {
1029 List<Action> actionList = new ArrayList<>();
1030 ActionBuilder ab = new ActionBuilder();
1032 SetDlDstActionBuilder dlDstActionBuilder= new SetDlDstActionBuilder();
1034 dlDstActionBuilder.setAddress(macAddress);
1036 ab.setAction(new SetDlDstActionCaseBuilder().setSetDlDstAction(dlDstActionBuilder.build()).build());
1038 ab.setKey(new ActionKey(0));
1039 actionList.add(ab.build());
1041 // Create an Apply Action
1042 ApplyActionsBuilder aab = new ApplyActionsBuilder();
1043 aab.setAction(actionList);
1045 // Wrap our Apply Action in an Instruction
1046 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1051 public static List<Action>
1052 actionList(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action... actions) {
1054 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> alist
1055 = new ArrayList<>();
1057 for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action action : actions) {
1058 alist.add(new ActionBuilder()
1060 .setKey(new ActionKey(count))
1068 public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction
1069 applyActionIns(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action... actions) {
1071 return new ApplyActionsCaseBuilder()
1072 .setApplyActions(new ApplyActionsBuilder()
1073 .setAction(actionList(actions))
1078 public static Instructions getInstructions(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction... instructions) {
1079 List<Instruction> ins
1080 = new ArrayList<>();
1082 for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction i : instructions) {
1083 ins.add(new InstructionBuilder()
1085 .setKey(new InstructionKey(order))
1090 return new InstructionsBuilder().setInstruction(ins).build();
1093 public static Instructions dropInstructions() {
1094 return getInstructions(applyActionIns(dropAction()));
1098 * Get a list of Instructions containing Nicira extensions that can have
1099 * additional OF/OXM instructions added to the returned Instruction list
1101 * @param instructions org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instructions
1102 * @return instruction list that additional
1104 public static List<Instruction> getInstructionList(
1105 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction... instructions) {
1106 List<Instruction> ins
1107 = new ArrayList<>();
1109 for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction i : instructions) {
1110 ins.add(new InstructionBuilder()