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 NORMAL Reserved Port Instruction (packet_in)
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 Output Port Instruction
150 * @param ib Map InstructionBuilder without any instructions
151 * @param dpidLong Long the datapath ID of a switch/node
152 * @param port Long representing a port on a switch/node
153 * @return ib InstructionBuilder Map with instructions
155 public static InstructionBuilder createOutputPortInstructions(InstructionBuilder ib, Long dpidLong, Long port) {
157 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + port);
158 LOG.debug("createOutputPortInstructions() Node Connector ID is - Type=openflow: DPID={} inPort={} ",
161 List<Action> actionList = new ArrayList<>();
162 ActionBuilder ab = new ActionBuilder();
163 OutputActionBuilder oab = new OutputActionBuilder();
164 oab.setOutputNodeConnector(ncid);
166 ab.setAction(new OutputActionCaseBuilder().setOutputAction(oab.build()).build());
168 ab.setKey(new ActionKey(0));
169 actionList.add(ab.build());
171 // Create an Apply Action
172 ApplyActionsBuilder aab = new ApplyActionsBuilder();
173 aab.setAction(actionList);
174 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
180 * add Output Port action to Instruction action list.
181 * This is use for flow with single output port actions.
182 * Flow with mutiple output port actions should use createOutputPortInstructions() method.
184 * @param ib Map InstructionBuilder without any instructions
185 * @param dpidLong Long the datapath ID of a switch/node
186 * @param port Long representing a port on a switch/node
187 * @return ib InstructionBuilder Map with instructions
189 public static InstructionBuilder addOutputPortInstructions(InstructionBuilder ib,
190 Long dpidLong, Long port,
191 List<Instruction> instructions) {
192 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + port);
194 "addOutputPortInstructions() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}",
195 dpidLong, port, instructions);
197 List<Action> actionList = new ArrayList<>();
198 ActionBuilder ab = new ActionBuilder();
200 List<Action> existingActions;
201 if (instructions != null) {
202 for (Instruction in : instructions) {
203 if (in.getInstruction() instanceof ApplyActionsCase) {
204 existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
205 actionList.addAll(existingActions);
210 /* Create output action for this port*/
211 OutputActionBuilder oab = new OutputActionBuilder();
212 oab.setOutputNodeConnector(ncid);
213 ab.setAction(new OutputActionCaseBuilder().setOutputAction(oab.build()).build());
214 ab.setOrder(actionList.size());
215 ab.setKey(new ActionKey(actionList.size()));
216 actionList.add(ab.build());
218 // Create an Apply Action
219 ApplyActionsBuilder aab = new ApplyActionsBuilder();
220 aab.setAction(actionList);
221 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
227 * Remove Output Port from Instruction
229 * @param ib Map InstructionBuilder without any instructions
230 * @param dpidLong Long the datapath ID of a switch/node
231 * @param port Long representing a port on a switch/node
232 * @return ib InstructionBuilder Map with instructions
234 public static boolean removeOutputPortFromInstructions(InstructionBuilder ib,
235 Long dpidLong, Long port, List<Instruction> instructions) {
237 final NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + port);
238 final Uri ncidUri = new Uri(ncid);
240 "removeOutputPortFromInstructions() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}",
241 dpidLong, port, instructions);
243 List<Action> actionList = new ArrayList<>();
246 // Start of by locating actions that will have port removed, from the existing instructionList
248 List<Action> existingActions;
249 if (instructions != null) {
250 for (Instruction in : instructions) {
251 if (in.getInstruction() instanceof ApplyActionsCase) {
252 existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
253 actionList.addAll(existingActions);
259 int removedActionOrder = 0;
260 boolean isPortDeleted = false;
261 boolean removeFlow = true;
263 // Locate specific action that has the port to be removed. Then, take note on its action order
264 // and remove it from list, in addition to flag that it was found.
266 for (Action action : actionList) {
267 if (action.getAction() instanceof OutputActionCase) {
268 OutputActionCase opAction = (OutputActionCase) action.getAction();
269 if (opAction.getOutputAction().getOutputNodeConnector().equals(ncidUri)) {
270 /* Find the output port in action list and remove */
271 removedActionOrder = action.getOrder();
272 actionList.remove(action);
273 isPortDeleted = true;
280 // Iterate through all actions in the modified list and adjust the order of
281 // the actions left behind. With that, all actions that have order higher than
282 // the action removed gets their value decremented by 1. Note that this iteration
283 // visits all entries to account for cases where the list order is not the same
284 // as the action's order.
286 for (int i = 0; i < actionList.size(); i++) {
287 Action action = actionList.get(i);
288 if (action.getOrder() > removedActionOrder) {
289 /* Shift the action by rebuilding action, using adjusted order */
290 ab = new ActionBuilder();
291 ab.setAction(action.getAction());
292 ab.setOrder(action.getOrder() - 1);
293 ab.setKey(new ActionKey(action.getOrder() - 1));
294 Action actionNewOrder = ab.build();
295 actionList.remove(action);
296 actionList.add(i, actionNewOrder);
297 } else if (action.getOrder() == removedActionOrder) {
298 // Sanity: implementation assumes no two actions have the same order
300 LOG.error("Found action with same order as the action removed for {}, order {} index {}: {}",
301 ncid, removedActionOrder, i, action);
304 // If action refers to a port output, then flow should be preserved.
305 // We do this, so that if we are only left with non-output port actions,
306 // we still remove the flow
308 if (action.getAction() instanceof OutputActionCase) {
313 // If port we are asked to delete is not found, this implementation will leave actions
314 // alone and not remove the flow, as long as a remaining OutputActionCase is found.
316 for (Action action : actionList) {
317 if (action.getAction() instanceof OutputActionCase) {
324 /* Put new action list in Apply Action instruction */
326 ApplyActionsBuilder aab = new ApplyActionsBuilder();
327 aab.setAction(actionList);
328 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
329 LOG.debug("removeOutputPortFromInstructions() : applyAction {}", aab.build());
332 /* if all output ports are removed. Return true to indicate flow remove */
338 * Create Set Vlan ID Instruction - This includes push vlan action, and set field -> vlan vid action
340 * @param ib Map InstructionBuilder without any instructions
341 * @param vlanId Integer representing a VLAN ID Integer representing a VLAN ID
342 * @return ib Map InstructionBuilder with instructions
344 public static InstructionBuilder createSetVlanInstructions(InstructionBuilder ib, VlanId vlanId) {
346 List<Action> actionList = new ArrayList<>();
347 ActionBuilder ab = new ActionBuilder();
349 /* First we push vlan header */
350 PushVlanActionBuilder vlan = new PushVlanActionBuilder();
351 vlan.setEthernetType(IPV4);
352 ab.setAction(new PushVlanActionCaseBuilder().setPushVlanAction(vlan.build()).build());
354 actionList.add(ab.build());
356 /* Then we set vlan id value as vlanId */
357 SetVlanIdActionBuilder vl = new SetVlanIdActionBuilder();
358 vl.setVlanId(vlanId);
359 ab = new ActionBuilder();
360 ab.setAction(new SetVlanIdActionCaseBuilder().setSetVlanIdAction(vl.build()).build());
362 actionList.add(ab.build());
363 // Create an Apply Action
364 ApplyActionsBuilder aab = new ApplyActionsBuilder();
365 aab.setAction(actionList);
367 // Wrap our Apply Action in an Instruction
368 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
374 * Create Pop Vlan Instruction - this remove vlan header
376 * @param ib Map InstructionBuilder without any instructions
377 * @return ib Map InstructionBuilder with instructions
379 public static InstructionBuilder createPopVlanInstructions(InstructionBuilder ib) {
381 List<Action> actionList = new ArrayList<>();
382 ActionBuilder ab = new ActionBuilder();
384 PopVlanActionBuilder popVlanActionBuilder = new PopVlanActionBuilder();
385 ab.setAction(new PopVlanActionCaseBuilder().setPopVlanAction(popVlanActionBuilder.build()).build());
387 actionList.add(ab.build());
389 // Create an Apply Action
390 ApplyActionsBuilder aab = new ApplyActionsBuilder();
391 aab.setAction(actionList);
393 // Wrap our Apply Action in an Instruction
394 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
400 * Create Set IPv4 Source Instruction
402 * @param ib Map InstructionBuilder without any instructions
403 * @param prefixsrc String containing an IPv4 prefix
404 * @return ib Map InstructionBuilder with instructions
406 public static InstructionBuilder createNwSrcInstructions(InstructionBuilder ib, Ipv4Prefix prefixsrc) {
408 List<Action> actionList = new ArrayList<>();
409 ActionBuilder ab = new ActionBuilder();
411 SetNwSrcActionBuilder setNwsrcActionBuilder = new SetNwSrcActionBuilder();
412 Ipv4Builder ipsrc = new Ipv4Builder();
413 ipsrc.setIpv4Address(prefixsrc);
414 setNwsrcActionBuilder.setAddress(ipsrc.build());
415 ab.setAction(new SetNwSrcActionCaseBuilder().setSetNwSrcAction(setNwsrcActionBuilder.build()).build());
417 ab.setKey(new ActionKey(0));
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 Destination Instruction
433 * @param ib Map InstructionBuilder without any instructions
434 * @param prefixdst String containing an IPv4 prefix
435 * @param extraAction (optional) Additional action to be performed in actionList
436 * @return ib Map InstructionBuilder with instructions
438 public static InstructionBuilder createNwDstInstructions(InstructionBuilder ib, Ipv4Prefix prefixdst,
439 ActionBuilder extraAction) {
441 List<Action> actionList = new ArrayList<>();
442 ActionBuilder ab = new ActionBuilder();
444 SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
445 Ipv4Builder ipdst = new Ipv4Builder();
446 ipdst.setIpv4Address(prefixdst);
447 setNwDstActionBuilder.setAddress(ipdst.build());
448 ab.setAction(new SetNwDstActionCaseBuilder().setSetNwDstAction(setNwDstActionBuilder.build()).build());
450 ab.setKey(new ActionKey(0));
451 actionList.add(ab.build());
453 if (extraAction != null) {
454 extraAction.setOrder(1);
455 extraAction.setKey(new ActionKey(1));
456 actionList.add(extraAction.build());
459 // Create an Apply Action
460 ApplyActionsBuilder aab = new ApplyActionsBuilder();
461 aab.setAction(actionList);
463 // Wrap our Apply Action in an Instruction
464 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
470 * Create Drop Instruction
472 * @param ib Map InstructionBuilder without any instructions
473 * @return ib Map InstructionBuilder with instructions
475 public static InstructionBuilder createDropInstructions(InstructionBuilder ib) {
477 DropActionBuilder dab = new DropActionBuilder();
478 DropAction dropAction = dab.build();
479 ActionBuilder ab = new ActionBuilder();
480 ab.setAction(new DropActionCaseBuilder().setDropAction(dropAction).build());
482 ab.setKey(new ActionKey(0));
484 // Add our drop action to a list
485 List<Action> actionList = new ArrayList<>();
486 actionList.add(ab.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 GOTO Table Instruction Builder
501 * @param ib Map InstructionBuilder without any instructions
502 * @param tableId short representing a flow table ID short representing a flow table ID
503 * @return ib Map InstructionBuilder with instructions
505 public static InstructionBuilder createGotoTableInstructions(InstructionBuilder ib, short tableId) {
507 GoToTableBuilder gttb = new GoToTableBuilder();
508 gttb.setTableId(tableId);
510 // Wrap our Apply Action in an InstructionBuilder
511 ib.setInstruction(new GoToTableCaseBuilder().setGoToTable(gttb.build()).build());
517 * Create Set Tunnel ID Instruction Builder
519 * @param ib Map InstructionBuilder without any instructions
520 * @param tunnelId BigInteger representing a tunnel ID
521 * @return ib Map InstructionBuilder with instructions
523 public static InstructionBuilder createSetTunnelIdInstructions(InstructionBuilder ib, BigInteger tunnelId) {
525 List<Action> actionList = new ArrayList<>();
526 ActionBuilder ab = new ActionBuilder();
527 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
529 // Build the Set Tunnel Field Action
530 TunnelBuilder tunnel = new TunnelBuilder();
531 tunnel.setTunnelId(tunnelId);
532 setFieldBuilder.setTunnel(tunnel.build());
533 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
535 ab.setKey(new ActionKey(0));
536 actionList.add(ab.build());
538 ApplyActionsBuilder aab = new ApplyActionsBuilder();
539 aab.setAction(actionList);
541 // Wrap the Apply Action in an InstructionBuilder and return
542 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
548 * Create Set Source TCP Port Instruction
550 * @param ib Map InstructionBuilder without any instructions
551 * @param tcpport Integer representing a source TCP port
552 * @return ib Map InstructionBuilder with instructions
554 public static InstructionBuilder createSetSrcTCPPort(InstructionBuilder ib, PortNumber tcpport) {
556 List<Action> actionList = new ArrayList<>();
557 ActionBuilder ab = new ActionBuilder();
558 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
560 // Build the Destination TCP Port
561 PortNumber tcpsrcport = new PortNumber(tcpport);
562 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
563 tcpmatch.setTcpSourcePort(tcpsrcport);
565 setFieldBuilder.setLayer4Match(tcpmatch.build());
566 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
568 ab.setKey(new ActionKey(0));
569 actionList.add(ab.build());
571 ApplyActionsBuilder aab = new ApplyActionsBuilder();
572 aab.setAction(actionList);
573 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
579 * Create Set Destination 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 createSetDstTCPPort(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 tcpdstport = new PortNumber(tcpport);
593 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
594 tcpmatch.setTcpDestinationPort(tcpdstport);
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 Source UDP Port Instruction
612 * @param ib Map InstructionBuilder without any instructions
613 * @param udpport Integer representing a source UDP port
614 * @return ib Map InstructionBuilder with instructions
616 public static InstructionBuilder createSetSrcUDPPort(InstructionBuilder ib, PortNumber udpport) {
618 List<Action> actionList = new ArrayList<>();
619 ActionBuilder ab = new ActionBuilder();
620 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
622 // Build the Destination TCP Port
623 PortNumber udpsrcport = new PortNumber(udpport);
624 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
625 udpmatch.setUdpSourcePort(udpsrcport);
627 setFieldBuilder.setLayer4Match(udpmatch.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 Destination UDP Port Instruction
643 * @param ib Map InstructionBuilder without any instructions
644 * @param udpport Integer representing a destination UDP port
645 * @return ib Map InstructionBuilder with instructions
647 public static InstructionBuilder createSetDstUDPPort(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 udpdstport = new PortNumber(udpport);
655 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
656 udpmatch.setUdpDestinationPort(udpdstport);
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 ICMP Code Instruction
674 * @param ib Map InstructionBuilder without any instructions
675 * @param code short repesenting an ICMP code
676 * @return ib Map InstructionBuilder with instructions
679 public static InstructionBuilder createSetIcmpCodeInstruction(InstructionBuilder ib, short code) {
681 List<Action> actionList = new ArrayList<>();
682 ActionBuilder ab = new ActionBuilder();
683 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
684 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
686 // Build the ICMPv4 Code Match
687 icmpv4match.setIcmpv4Code(code);
688 setFieldBuilder.setIcmpv4Match(icmpv4match.build());
690 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
692 ab.setKey(new ActionKey(0));
693 actionList.add(ab.build());
694 ApplyActionsBuilder aab = new ApplyActionsBuilder();
695 aab.setAction(actionList);
697 // Wrap our Apply Action in an Instruction
698 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
704 * Create Set ICMP Code Instruction
706 * @param ib Map InstructionBuilder without any instructions
707 * @return ib Map InstructionBuilder with instructions
709 public static InstructionBuilder createSetIcmpTypeInstruction(InstructionBuilder ib, short type) {
711 List<Action> actionList = new ArrayList<>();
712 ActionBuilder ab = new ActionBuilder();
713 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
714 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
716 // Build the ICMPv4 Code Match
717 icmpv4match.setIcmpv4Code(type);
718 setFieldBuilder.setIcmpv4Match(icmpv4match.build());
720 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
722 ab.setKey(new ActionKey(0));
723 actionList.add(ab.build());
724 ApplyActionsBuilder aab = new ApplyActionsBuilder();
725 aab.setAction(actionList);
727 // Wrap our Apply Action in an Instruction
728 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
734 * Create Decrement TTL Instruction
736 * @param ib Map InstructionBuilder without any instructions
737 * @return ib Map InstructionBuilder with instructions
739 public static InstructionBuilder createDecNwTtlInstructions(InstructionBuilder ib) {
740 DecNwTtlBuilder decNwTtlBuilder = new DecNwTtlBuilder();
741 DecNwTtl decNwTtl = decNwTtlBuilder.build();
742 ActionBuilder ab = new ActionBuilder();
743 ab.setAction(new DecNwTtlCaseBuilder().setDecNwTtl(decNwTtl).build());
745 ab.setKey(new ActionKey(0));
747 // Add our drop action to a list
748 List<Action> actionList = new ArrayList<>();
749 actionList.add(ab.build());
751 // Create an Apply Action
752 ApplyActionsBuilder aab = new ApplyActionsBuilder();
753 aab.setAction(actionList);
755 // Wrap our Apply Action in an Instruction
756 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
762 * Set ARP_SHA Instructions
763 * @param ib Map InstructionBuilder
764 * @param macsrc the macsrc
765 * @return instructionbuilder with new instructions
767 public static InstructionBuilder createSrcArpMacInstructions(InstructionBuilder ib, MacAddress macsrc) {
769 List<Action> actionList = new ArrayList<>();
770 ActionBuilder ab = new ActionBuilder();
772 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
773 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
774 ArpSourceHardwareAddressBuilder arpsrc = new ArpSourceHardwareAddressBuilder();
775 arpsrc.setAddress(macsrc);
776 arpmatch.setArpSourceHardwareAddress(arpsrc.build());
777 setFieldBuilder.setLayer3Match(arpmatch.build());
778 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
780 ab.setKey(new ActionKey(0));
781 actionList.add(ab.build());
783 ApplyActionsBuilder aab = new ApplyActionsBuilder();
784 aab.setAction(actionList);
785 // Wrap our Apply Action in an Instruction
786 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
792 * Set ARP_THA Instructions
793 * @param ib Map InstructionBuilder
794 * @param macdst the macdst
795 * @return instructionbuilder with new attributes
797 public static InstructionBuilder createDstArpMacInstructions(InstructionBuilder ib, MacAddress macdst) {
799 List<Action> actionList = new ArrayList<>();
800 ActionBuilder ab = new ActionBuilder();
801 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
803 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
804 ArpTargetHardwareAddressBuilder arpdst = new ArpTargetHardwareAddressBuilder();
805 arpdst.setAddress(macdst);
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_TPA Instructions
822 * @param ib Map InstructionBuilder
823 * @param dstiparp the dstiparp
824 * @return instructionbuilder with new attributes
826 public static InstructionBuilder createDstArpIpInstructions(InstructionBuilder ib, Ipv4Prefix dstiparp) {
828 List<Action> actionList = new ArrayList<>();
829 ActionBuilder ab = new ActionBuilder();
830 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
832 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
833 arpmatch.setArpTargetTransportAddress(dstiparp);
834 setFieldBuilder.setLayer3Match(arpmatch.build());
835 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
837 ab.setKey(new ActionKey(0));
838 actionList.add(ab.build());
840 ApplyActionsBuilder aab = new ApplyActionsBuilder();
841 aab.setAction(actionList);
842 // Wrap our Apply Action in an Instruction
843 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
849 * Set ARP_SPA Instructions
850 * @param ib Map InstructionBuilder
851 * @param srciparp the srciparp
852 * @return instructionbuilder with new attributes
854 public static InstructionBuilder createSrcArpIpInstructions(InstructionBuilder ib, Ipv4Prefix srciparp) {
856 List<Action> actionList = new ArrayList<>();
857 ActionBuilder ab = new ActionBuilder();
858 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
860 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
861 arpmatch.setArpSourceTransportAddress(srciparp);
862 setFieldBuilder.setLayer3Match(arpmatch.build());
863 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
865 ab.setKey(new ActionKey(0));
866 actionList.add(ab.build());
868 ApplyActionsBuilder aab = new ApplyActionsBuilder();
869 aab.setAction(actionList);
870 // Wrap our Apply Action in an Instruction
871 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
877 * Set the Tunnel EndpointIPv4 Source Address
878 * @param ib Map InstructionBuilder
879 * @param srcIp Ipv4Prefix source IP for the tunnel endpoint (TEP)
880 * @return instructionbuilder with new attributes
882 public static InstructionBuilder createTunnelIpv4SrcInstructions(InstructionBuilder ib, Ipv4Prefix srcIp) {
884 List<Action> actionList = new ArrayList<>();
885 ActionBuilder ab = new ActionBuilder();
887 // Build the tunnel endpoint source IPv4 address
888 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
890 // Add the new IPv4 object as the tunnel destination
891 TunnelIpv4MatchBuilder tunnelIpv4MatchBuilder = new TunnelIpv4MatchBuilder();
892 tunnelIpv4MatchBuilder.setTunnelIpv4Source(srcIp);
893 setFieldBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
895 // Add the IPv4 tunnel src to the set_field value
896 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
898 ab.setKey(new ActionKey(0));
899 actionList.add(ab.build());
901 // Resulting action is a per/flow src TEP (set_field:172.16.100.100->tun_src)
902 ApplyActionsBuilder aab = new ApplyActionsBuilder();
903 aab.setAction(actionList);
904 // Wrap our Apply Action in an Instruction
905 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
911 * Set the Tunnel EndpointIPv4 Destination Address
912 * @param ib Map InstructionBuilder
913 * @param dstIp Ipv4Prefix destination IP for the tunnel endpoint (TEP)
914 * @return instructionbuilder with new attributes
916 public static InstructionBuilder createTunnelIpv4DstInstructions(InstructionBuilder ib, Ipv4Prefix dstIp) {
918 List<Action> actionList = new ArrayList<>();
919 ActionBuilder ab = new ActionBuilder();
921 // Build the tunnel endpoint dst IPv4 address
922 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
924 // Add the new IPv4 object as the tunnel destination
925 TunnelIpv4MatchBuilder tunnelIpv4MatchBuilder = new TunnelIpv4MatchBuilder();
926 tunnelIpv4MatchBuilder.setTunnelIpv4Destination(dstIp);
927 setFieldBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
929 // Add the IPv4 tunnel src to the set_field value
930 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
932 ab.setKey(new ActionKey(0));
933 actionList.add(ab.build());
935 // Resulting action is a per/flow src TEP (set_field:172.16.100.100->tun_src)
936 ApplyActionsBuilder aab = new ApplyActionsBuilder();
937 aab.setAction(actionList);
938 // Wrap our Apply Action in an Instruction
939 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
945 * Set IPv4 Source Address Instructions
946 * @param ib Map InstructionBuilder
947 * @param metaData BigInteger representing the OpenFlow metadata
948 * @param metaDataMask BigInteger representing the OpenFlow metadata mask
949 * @return instructionbuilder with new attributes
951 public static InstructionBuilder createMetadataInstructions(InstructionBuilder ib, BigInteger metaData, BigInteger metaDataMask) {
953 WriteMetadataBuilder aab = new WriteMetadataBuilder();
954 aab.setMetadata(metaData);
955 aab.setMetadataMask(metaDataMask);
956 ib.setInstruction(new WriteMetadataCaseBuilder().setWriteMetadata(aab.build()).build());
960 * TODO Determine why the following fails serialization
961 * with a null value. Per the spec it is optional.
962 * The metadata match, the Flowmod works w/o an accompanying mask.
964 * if (metaDataMask != null) {
965 * aab.setMetadataMask(metaDataMask);
973 public static InstructionBuilder createDlSrcInstructions(InstructionBuilder ib, MacAddress macAddress) {
975 List<Action> actionList = new ArrayList<>();
976 ActionBuilder ab = new ActionBuilder();
978 SetDlSrcActionBuilder dlSrcActionBuilder= new SetDlSrcActionBuilder();
980 dlSrcActionBuilder.setAddress(macAddress);
982 ab.setAction(new SetDlSrcActionCaseBuilder().setSetDlSrcAction(dlSrcActionBuilder.build()).build());
984 ab.setKey(new ActionKey(0));
985 actionList.add(ab.build());
988 // Create an Apply Action
989 ApplyActionsBuilder aab = new ApplyActionsBuilder();
990 aab.setAction(actionList);
992 // Wrap our Apply Action in an Instruction
993 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
998 public static InstructionBuilder createDlDstInstructions(InstructionBuilder ib, MacAddress macAddress) {
1000 List<Action> actionList = new ArrayList<>();
1001 ActionBuilder ab = new ActionBuilder();
1003 SetDlDstActionBuilder dlDstActionBuilder= new SetDlDstActionBuilder();
1005 dlDstActionBuilder.setAddress(macAddress);
1007 ab.setAction(new SetDlDstActionCaseBuilder().setSetDlDstAction(dlDstActionBuilder.build()).build());
1009 ab.setKey(new ActionKey(0));
1010 actionList.add(ab.build());
1012 // Create an Apply Action
1013 ApplyActionsBuilder aab = new ApplyActionsBuilder();
1014 aab.setAction(actionList);
1016 // Wrap our Apply Action in an Instruction
1017 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1022 public static List<Action>
1023 actionList(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action... actions) {
1025 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> alist
1026 = new ArrayList<>();
1028 for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action action : actions) {
1029 alist.add(new ActionBuilder()
1031 .setKey(new ActionKey(count))
1039 public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction
1040 applyActionIns(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action... actions) {
1042 return new ApplyActionsCaseBuilder()
1043 .setApplyActions(new ApplyActionsBuilder()
1044 .setAction(actionList(actions))
1049 public static Instructions getInstructions(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction... instructions) {
1050 List<Instruction> ins
1051 = new ArrayList<>();
1053 for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction i : instructions) {
1054 ins.add(new InstructionBuilder()
1056 .setKey(new InstructionKey(order))
1061 return new InstructionsBuilder().setInstruction(ins).build();
1064 public static Instructions dropInstructions() {
1065 return getInstructions(applyActionIns(dropAction()));
1069 * Get a list of Instructions containing Nicira extensions that can have
1070 * additional OF/OXM instructions added to the returned Instruction list
1072 * @param instructions org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instructions
1073 * @return instruction list that additional
1075 public static List<Instruction> getInstructionList(
1076 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction... instructions) {
1077 List<Instruction> ins
1078 = new ArrayList<>();
1080 for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction i : instructions) {
1081 ins.add(new InstructionBuilder()