2 * Copyright (C) 2013 Red Hat, Inc.
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
10 package org.opendaylight.ovsdb.utils.mdsal.openflow;
12 import static org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils.dropAction;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DecNwTtlCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlDstActionCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetDlSrcActionCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwDstActionCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetNwSrcActionCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetVlanIdActionCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.nw.ttl._case.DecNwTtl;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.dec.nw.ttl._case.DecNwTtlBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropAction;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.vlan.action._case.PopVlanActionBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanActionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.dl.dst.action._case.SetDlDstActionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.dl.src.action._case.SetDlSrcActionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.dst.action._case.SetNwDstActionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.nw.src.action._case.SetNwSrcActionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.vlan.id.action._case.SetVlanIdActionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Icmpv4MatchBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.TunnelIpv4MatchBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
72 import com.google.common.collect.Lists;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
77 import java.math.BigInteger;
78 import java.util.ArrayList;
79 import java.util.List;
81 public class InstructionUtils {
82 private static final Logger LOG = LoggerFactory.getLogger(InstructionUtils.class);
83 private static final int IPV4 = 0x8100;
84 private static final int MAX_LENGTH = 0xffff;
87 * Create Send to Controller Reserved Port Instruction (packet_in)
89 * @param nodeName Uri Prefix, containing nodeConnectorType and dpId (aka NodeId)
90 * @param ib Map InstructionBuilder without any instructions
91 * @return ib Map InstructionBuilder with instructions
93 public static InstructionBuilder createSendToControllerInstructions(String nodeName, InstructionBuilder ib) {
95 List<Action> actionList = Lists.newArrayList();
96 ActionBuilder ab = new ActionBuilder();
98 OutputActionBuilder output = new OutputActionBuilder();
99 output.setMaxLength(MAX_LENGTH);
100 NodeId nodeId = new NodeId(nodeName);
101 output.setOutputNodeConnector(new NodeConnectorId(nodeId.getValue() + ":"
102 + OutputPortValues.CONTROLLER.toString()));
103 ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
105 ab.setKey(new ActionKey(0));
106 actionList.add(ab.build());
108 // Create an Apply Action
109 ApplyActionsBuilder aab = new ApplyActionsBuilder();
110 aab.setAction(actionList);
112 // Wrap our Apply Action in an Instruction
113 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
119 * Create NORMAL Reserved Port Instruction (packet_in)
121 * @param nodeName Uri Prefix, containing nodeConnectorType and dpId (aka NodeId)
122 * @param ib Map InstructionBuilder without any instructions
123 * @return ib Map InstructionBuilder with instructions
126 public static InstructionBuilder createNormalInstructions(String nodeName, InstructionBuilder ib) {
128 List<Action> actionList = Lists.newArrayList();
129 ActionBuilder ab = new ActionBuilder();
131 OutputActionBuilder output = new OutputActionBuilder();
132 NodeId nodeId = new NodeId(nodeName);
133 output.setOutputNodeConnector(new NodeConnectorId(nodeId.getValue() + ":"
134 + OutputPortValues.NORMAL.toString()));
135 ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
137 ab.setKey(new ActionKey(0));
138 actionList.add(ab.build());
140 // Create an Apply Action
141 ApplyActionsBuilder aab = new ApplyActionsBuilder();
142 aab.setAction(actionList);
144 // Wrap our Apply Action in an Instruction
145 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
151 * Create Output Port Instruction
153 * @param ib Map InstructionBuilder without any instructions
154 * @param dpidLong Long the datapath ID of a switch/node
155 * @param port Long representing a port on a switch/node
156 * @return ib InstructionBuilder Map with instructions
158 public static InstructionBuilder createOutputPortInstructions(InstructionBuilder ib, Long dpidLong, Long port) {
160 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + port);
161 LOG.debug("createOutputPortInstructions() Node Connector ID is - Type=openflow: DPID={} inPort={} ",
164 List<Action> actionList = Lists.newArrayList();
165 ActionBuilder ab = new ActionBuilder();
166 OutputActionBuilder oab = new OutputActionBuilder();
167 oab.setOutputNodeConnector(ncid);
169 ab.setAction(new OutputActionCaseBuilder().setOutputAction(oab.build()).build());
171 ab.setKey(new ActionKey(0));
172 actionList.add(ab.build());
174 // Create an Apply Action
175 ApplyActionsBuilder aab = new ApplyActionsBuilder();
176 aab.setAction(actionList);
177 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
183 * add Output Port action to Instruction action list.
184 * This is use for flow with single output port actions.
185 * Flow with mutiple output port actions should use createOutputPortInstructions() method.
187 * @param ib Map InstructionBuilder without any instructions
188 * @param dpidLong Long the datapath ID of a switch/node
189 * @param port Long representing a port on a switch/node
190 * @return ib InstructionBuilder Map with instructions
192 public static InstructionBuilder addOutputPortInstructions(InstructionBuilder ib,
193 Long dpidLong, Long port,
194 List<Instruction> instructions) {
195 NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + port);
197 "addOutputPortInstructions() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}",
198 dpidLong, port, instructions);
200 List<Action> actionList = Lists.newArrayList();
201 ActionBuilder ab = new ActionBuilder();
203 List<Action> existingActions;
204 if (instructions != null) {
205 for (Instruction in : instructions) {
206 if (in.getInstruction() instanceof ApplyActionsCase) {
207 existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
208 actionList.addAll(existingActions);
213 /* Create output action for this port*/
214 OutputActionBuilder oab = new OutputActionBuilder();
215 oab.setOutputNodeConnector(ncid);
216 ab.setAction(new OutputActionCaseBuilder().setOutputAction(oab.build()).build());
217 ab.setOrder(actionList.size());
218 ab.setKey(new ActionKey(actionList.size()));
219 actionList.add(ab.build());
221 // Create an Apply Action
222 ApplyActionsBuilder aab = new ApplyActionsBuilder();
223 aab.setAction(actionList);
224 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
230 * Remove Output Port from Instruction
232 * @param ib Map InstructionBuilder without any instructions
233 * @param dpidLong Long the datapath ID of a switch/node
234 * @param port Long representing a port on a switch/node
235 * @return ib InstructionBuilder Map with instructions
237 public static boolean removeOutputPortFromInstructions(InstructionBuilder ib,
238 Long dpidLong, Long port, List<Instruction> instructions) {
240 final NodeConnectorId ncid = new NodeConnectorId("openflow:" + dpidLong + ":" + port);
241 final Uri ncidUri = new Uri(ncid);
243 "removeOutputPortFromInstructions() Node Connector ID is - Type=openflow: DPID={} port={} existingInstructions={}",
244 dpidLong, port, instructions);
246 List<Action> actionList = Lists.newArrayList();
249 // Start of by locating actions that will have port removed, from the existing instructionList
251 List<Action> existingActions;
252 if (instructions != null) {
253 for (Instruction in : instructions) {
254 if (in.getInstruction() instanceof ApplyActionsCase) {
255 existingActions = (((ApplyActionsCase) in.getInstruction()).getApplyActions().getAction());
256 actionList.addAll(existingActions);
262 int removedActionOrder = 0;
263 boolean isPortDeleted = false;
264 boolean removeFlow = true;
266 // Locate specific action that has the port to be removed. Then, take note on its action order
267 // and remove it from list, in addition to flag that it was found.
269 for (Action action : actionList) {
270 if (action.getAction() instanceof OutputActionCase) {
271 OutputActionCase opAction = (OutputActionCase) action.getAction();
272 if (opAction.getOutputAction().getOutputNodeConnector().equals(ncidUri)) {
273 /* Find the output port in action list and remove */
274 removedActionOrder = action.getOrder();
275 actionList.remove(action);
276 isPortDeleted = true;
283 // Iterate through all actions in the modified list and adjust the order of
284 // the actions left behind. With that, all actions that have order higher than
285 // the action removed gets their value decremented by 1. Note that this iteration
286 // visits all entries to account for cases where the list order is not the same
287 // as the action's order.
289 for (int i = 0; i < actionList.size(); i++) {
290 Action action = actionList.get(i);
291 if (action.getOrder() > removedActionOrder) {
292 /* Shift the action by rebuilding action, using adjusted order */
293 ab = new ActionBuilder();
294 ab.setAction(action.getAction());
295 ab.setOrder(action.getOrder() - 1);
296 ab.setKey(new ActionKey(action.getOrder() - 1));
297 Action actionNewOrder = ab.build();
298 actionList.remove(action);
299 actionList.add(i, actionNewOrder);
300 } else if (action.getOrder() == removedActionOrder) {
301 // Sanity: implementation assumes no two actions have the same order
303 LOG.error("Found action with same order as the action removed for {}, order {} index {}: {}",
304 ncid, removedActionOrder, i, action);
307 // If action refers to a port output, then flow should be preserved.
308 // We do this, so that if we are only left with non-output port actions,
309 // we still remove the flow
311 if (action.getAction() instanceof OutputActionCase) {
316 // If port we are asked to delete is not found, this implementation will leave actions
317 // alone and not remove the flow, as long as a remaining OutputActionCase is found.
319 for (int i = 0; i < actionList.size(); i++) {
320 if (actionList.get(i).getAction() instanceof OutputActionCase) {
327 /* Put new action list in Apply Action instruction */
329 ApplyActionsBuilder aab = new ApplyActionsBuilder();
330 aab.setAction(actionList);
331 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
332 LOG.debug("removeOutputPortFromInstructions() : applyAction {}", aab.build());
335 /* if all output ports are removed. Return true to indicate flow remove */
341 * Create Set Vlan ID Instruction - This includes push vlan action, and set field -> vlan vid action
343 * @param ib Map InstructionBuilder without any instructions
344 * @param vlanId Integer representing a VLAN ID Integer representing a VLAN ID
345 * @return ib Map InstructionBuilder with instructions
347 public static InstructionBuilder createSetVlanInstructions(InstructionBuilder ib, VlanId vlanId) {
349 List<Action> actionList = Lists.newArrayList();
350 ActionBuilder ab = new ActionBuilder();
352 /* First we push vlan header */
353 PushVlanActionBuilder vlan = new PushVlanActionBuilder();
354 vlan.setEthernetType(IPV4);
355 ab.setAction(new PushVlanActionCaseBuilder().setPushVlanAction(vlan.build()).build());
357 actionList.add(ab.build());
359 /* Then we set vlan id value as vlanId */
360 SetVlanIdActionBuilder vl = new SetVlanIdActionBuilder();
361 vl.setVlanId(vlanId);
362 ab = new ActionBuilder();
363 ab.setAction(new SetVlanIdActionCaseBuilder().setSetVlanIdAction(vl.build()).build());
365 actionList.add(ab.build());
366 // Create an Apply Action
367 ApplyActionsBuilder aab = new ApplyActionsBuilder();
368 aab.setAction(actionList);
370 // Wrap our Apply Action in an Instruction
371 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
377 * Create Pop Vlan Instruction - this remove vlan header
379 * @param ib Map InstructionBuilder without any instructions
380 * @return ib Map InstructionBuilder with instructions
382 public static InstructionBuilder createPopVlanInstructions(InstructionBuilder ib) {
384 List<Action> actionList = Lists.newArrayList();
385 ActionBuilder ab = new ActionBuilder();
387 PopVlanActionBuilder popVlanActionBuilder = new PopVlanActionBuilder();
388 ab.setAction(new PopVlanActionCaseBuilder().setPopVlanAction(popVlanActionBuilder.build()).build());
390 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 Set IPv4 Source Instruction
405 * @param ib Map InstructionBuilder without any instructions
406 * @param prefixsrc String containing an IPv4 prefix
407 * @return ib Map InstructionBuilder with instructions
409 public static InstructionBuilder createNwSrcInstructions(InstructionBuilder ib, Ipv4Prefix prefixsrc) {
411 List<Action> actionList = Lists.newArrayList();
412 ActionBuilder ab = new ActionBuilder();
414 SetNwSrcActionBuilder setNwsrcActionBuilder = new SetNwSrcActionBuilder();
415 Ipv4Builder ipsrc = new Ipv4Builder();
416 ipsrc.setIpv4Address(prefixsrc);
417 setNwsrcActionBuilder.setAddress(ipsrc.build());
418 ab.setAction(new SetNwSrcActionCaseBuilder().setSetNwSrcAction(setNwsrcActionBuilder.build()).build());
420 ab.setKey(new ActionKey(0));
421 actionList.add(ab.build());
423 // Create an Apply Action
424 ApplyActionsBuilder aab = new ApplyActionsBuilder();
425 aab.setAction(actionList);
427 // Wrap our Apply Action in an Instruction
428 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
434 * Create Set IPv4 Destination Instruction
436 * @param ib Map InstructionBuilder without any instructions
437 * @param prefixdst String containing an IPv4 prefix
438 * @param extraAction (optional) Additional action to be performed in actionList
439 * @return ib Map InstructionBuilder with instructions
441 public static InstructionBuilder createNwDstInstructions(InstructionBuilder ib, Ipv4Prefix prefixdst,
442 ActionBuilder extraAction) {
444 List<Action> actionList = Lists.newArrayList();
445 ActionBuilder ab = new ActionBuilder();
447 SetNwDstActionBuilder setNwDstActionBuilder = new SetNwDstActionBuilder();
448 Ipv4Builder ipdst = new Ipv4Builder();
449 ipdst.setIpv4Address(prefixdst);
450 setNwDstActionBuilder.setAddress(ipdst.build());
451 ab.setAction(new SetNwDstActionCaseBuilder().setSetNwDstAction(setNwDstActionBuilder.build()).build());
453 ab.setKey(new ActionKey(0));
454 actionList.add(ab.build());
456 if (extraAction != null) {
457 extraAction.setOrder(1);
458 extraAction.setKey(new ActionKey(1));
459 actionList.add(extraAction.build());
462 // Create an Apply Action
463 ApplyActionsBuilder aab = new ApplyActionsBuilder();
464 aab.setAction(actionList);
466 // Wrap our Apply Action in an Instruction
467 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
473 * Create Drop Instruction
475 * @param ib Map InstructionBuilder without any instructions
476 * @return ib Map InstructionBuilder with instructions
478 public static InstructionBuilder createDropInstructions(InstructionBuilder ib) {
480 DropActionBuilder dab = new DropActionBuilder();
481 DropAction dropAction = dab.build();
482 ActionBuilder ab = new ActionBuilder();
483 ab.setAction(new DropActionCaseBuilder().setDropAction(dropAction).build());
485 ab.setKey(new ActionKey(0));
487 // Add our drop action to a list
488 List<Action> actionList = Lists.newArrayList();
489 actionList.add(ab.build());
491 // Create an Apply Action
492 ApplyActionsBuilder aab = new ApplyActionsBuilder();
493 aab.setAction(actionList);
495 // Wrap our Apply Action in an Instruction
496 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
502 * Create GOTO Table Instruction Builder
504 * @param ib Map InstructionBuilder without any instructions
505 * @param tableId short representing a flow table ID short representing a flow table ID
506 * @return ib Map InstructionBuilder with instructions
508 public static InstructionBuilder createGotoTableInstructions(InstructionBuilder ib, short tableId) {
510 GoToTableBuilder gttb = new GoToTableBuilder();
511 gttb.setTableId(tableId);
513 // Wrap our Apply Action in an InstructionBuilder
514 ib.setInstruction(new GoToTableCaseBuilder().setGoToTable(gttb.build()).build());
520 * Create Set Tunnel ID Instruction Builder
522 * @param ib Map InstructionBuilder without any instructions
523 * @param tunnelId BigInteger representing a tunnel ID
524 * @return ib Map InstructionBuilder with instructions
526 public static InstructionBuilder createSetTunnelIdInstructions(InstructionBuilder ib, BigInteger tunnelId) {
528 List<Action> actionList = Lists.newArrayList();
529 ActionBuilder ab = new ActionBuilder();
530 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
532 // Build the Set Tunnel Field Action
533 TunnelBuilder tunnel = new TunnelBuilder();
534 tunnel.setTunnelId(tunnelId);
535 setFieldBuilder.setTunnel(tunnel.build());
536 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
538 ab.setKey(new ActionKey(0));
539 actionList.add(ab.build());
541 ApplyActionsBuilder aab = new ApplyActionsBuilder();
542 aab.setAction(actionList);
544 // Wrap the Apply Action in an InstructionBuilder and return
545 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
551 * Create Set Source TCP Port Instruction
553 * @param ib Map InstructionBuilder without any instructions
554 * @param tcpport Integer representing a source TCP port
555 * @return ib Map InstructionBuilder with instructions
557 public static InstructionBuilder createSetSrcTCPPort(InstructionBuilder ib, PortNumber tcpport) {
559 List<Action> actionList = Lists.newArrayList();
560 ActionBuilder ab = new ActionBuilder();
561 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
563 // Build the Destination TCP Port
564 PortNumber tcpsrcport = new PortNumber(tcpport);
565 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
566 tcpmatch.setTcpSourcePort(tcpsrcport);
568 setFieldBuilder.setLayer4Match(tcpmatch.build());
569 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
571 ab.setKey(new ActionKey(0));
572 actionList.add(ab.build());
574 ApplyActionsBuilder aab = new ApplyActionsBuilder();
575 aab.setAction(actionList);
576 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
582 * Create Set Destination TCP Port Instruction
584 * @param ib Map InstructionBuilder without any instructions
585 * @param tcpport Integer representing a source TCP port
586 * @return ib Map InstructionBuilder with instructions
588 public static InstructionBuilder createSetDstTCPPort(InstructionBuilder ib, PortNumber tcpport) {
590 List<Action> actionList = Lists.newArrayList();
591 ActionBuilder ab = new ActionBuilder();
592 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
594 // Build the Destination TCP Port
595 PortNumber tcpdstport = new PortNumber(tcpport);
596 TcpMatchBuilder tcpmatch = new TcpMatchBuilder();
597 tcpmatch.setTcpDestinationPort(tcpdstport);
599 setFieldBuilder.setLayer4Match(tcpmatch.build());
600 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
602 ab.setKey(new ActionKey(0));
603 actionList.add(ab.build());
605 ApplyActionsBuilder aab = new ApplyActionsBuilder();
606 aab.setAction(actionList);
607 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
613 * Create Set Source UDP Port Instruction
615 * @param ib Map InstructionBuilder without any instructions
616 * @param udpport Integer representing a source UDP port
617 * @return ib Map InstructionBuilder with instructions
619 public static InstructionBuilder createSetSrcUDPPort(InstructionBuilder ib, PortNumber udpport) {
621 List<Action> actionList = Lists.newArrayList();
622 ActionBuilder ab = new ActionBuilder();
623 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
625 // Build the Destination TCP Port
626 PortNumber udpsrcport = new PortNumber(udpport);
627 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
628 udpmatch.setUdpSourcePort(udpsrcport);
630 setFieldBuilder.setLayer4Match(udpmatch.build());
631 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
633 ab.setKey(new ActionKey(0));
634 actionList.add(ab.build());
636 ApplyActionsBuilder aab = new ApplyActionsBuilder();
637 aab.setAction(actionList);
638 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
644 * Create Set Destination UDP Port Instruction
646 * @param ib Map InstructionBuilder without any instructions
647 * @param udpport Integer representing a destination UDP port
648 * @return ib Map InstructionBuilder with instructions
650 public static InstructionBuilder createSetDstUDPPort(InstructionBuilder ib, PortNumber udpport) {
652 List<Action> actionList = Lists.newArrayList();
653 ActionBuilder ab = new ActionBuilder();
654 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
656 // Build the Destination TCP Port
657 PortNumber udpdstport = new PortNumber(udpport);
658 UdpMatchBuilder udpmatch = new UdpMatchBuilder();
659 udpmatch.setUdpDestinationPort(udpdstport);
661 setFieldBuilder.setLayer4Match(udpmatch.build());
662 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
664 ab.setKey(new ActionKey(0));
665 actionList.add(ab.build());
667 ApplyActionsBuilder aab = new ApplyActionsBuilder();
668 aab.setAction(actionList);
669 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
675 * Create Set ICMP Code Instruction
677 * @param ib Map InstructionBuilder without any instructions
678 * @param code short repesenting an ICMP code
679 * @return ib Map InstructionBuilder with instructions
682 public static InstructionBuilder createSetIcmpCodeInstruction(InstructionBuilder ib, short code) {
684 List<Action> actionList = Lists.newArrayList();
685 ActionBuilder ab = new ActionBuilder();
686 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
687 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
689 // Build the ICMPv4 Code Match
690 icmpv4match.setIcmpv4Code(code);
691 setFieldBuilder.setIcmpv4Match(icmpv4match.build());
693 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
695 ab.setKey(new ActionKey(0));
696 actionList.add(ab.build());
697 ApplyActionsBuilder aab = new ApplyActionsBuilder();
698 aab.setAction(actionList);
700 // Wrap our Apply Action in an Instruction
701 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
707 * Create Set ICMP Code Instruction
709 * @param ib Map InstructionBuilder without any instructions
710 * @return ib Map InstructionBuilder with instructions
712 public static InstructionBuilder createSetIcmpTypeInstruction(InstructionBuilder ib, short type) {
714 List<Action> actionList = Lists.newArrayList();
715 ActionBuilder ab = new ActionBuilder();
716 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
717 Icmpv4MatchBuilder icmpv4match = new Icmpv4MatchBuilder();
719 // Build the ICMPv4 Code Match
720 icmpv4match.setIcmpv4Code(type);
721 setFieldBuilder.setIcmpv4Match(icmpv4match.build());
723 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
725 ab.setKey(new ActionKey(0));
726 actionList.add(ab.build());
727 ApplyActionsBuilder aab = new ApplyActionsBuilder();
728 aab.setAction(actionList);
730 // Wrap our Apply Action in an Instruction
731 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
737 * Create Decrement TTL Instruction
739 * @param ib Map InstructionBuilder without any instructions
740 * @return ib Map InstructionBuilder with instructions
742 public static InstructionBuilder createDecNwTtlInstructions(InstructionBuilder ib) {
743 DecNwTtlBuilder decNwTtlBuilder = new DecNwTtlBuilder();
744 DecNwTtl decNwTtl = decNwTtlBuilder.build();
745 ActionBuilder ab = new ActionBuilder();
746 ab.setAction(new DecNwTtlCaseBuilder().setDecNwTtl(decNwTtl).build());
748 ab.setKey(new ActionKey(0));
750 // Add our drop action to a list
751 List<Action> actionList = Lists.newArrayList();
752 actionList.add(ab.build());
754 // Create an Apply Action
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 * Set ARP_SHA Instructions
766 * @param ib Map InstructionBuilder
767 * @param macsrc the macsrc
768 * @return instructionbuilder with new instructions
770 public static InstructionBuilder createSrcArpMacInstructions(InstructionBuilder ib, MacAddress macsrc) {
772 List<Action> actionList = Lists.newArrayList();
773 ActionBuilder ab = new ActionBuilder();
775 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
776 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
777 ArpSourceHardwareAddressBuilder arpsrc = new ArpSourceHardwareAddressBuilder();
778 arpsrc.setAddress(macsrc);
779 arpmatch.setArpSourceHardwareAddress(arpsrc.build());
780 setFieldBuilder.setLayer3Match(arpmatch.build());
781 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
783 ab.setKey(new ActionKey(0));
784 actionList.add(ab.build());
786 ApplyActionsBuilder aab = new ApplyActionsBuilder();
787 aab.setAction(actionList);
788 // Wrap our Apply Action in an Instruction
789 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
795 * Set ARP_THA Instructions
796 * @param ib Map InstructionBuilder
797 * @param macdst the macdst
798 * @return instructionbuilder with new attributes
800 public static InstructionBuilder createDstArpMacInstructions(InstructionBuilder ib, MacAddress macdst) {
802 List<Action> actionList = Lists.newArrayList();
803 ActionBuilder ab = new ActionBuilder();
804 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
806 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
807 ArpTargetHardwareAddressBuilder arpdst = new ArpTargetHardwareAddressBuilder();
808 arpdst.setAddress(macdst);
809 setFieldBuilder.setLayer3Match(arpmatch.build());
810 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
812 ab.setKey(new ActionKey(0));
813 actionList.add(ab.build());
815 ApplyActionsBuilder aab = new ApplyActionsBuilder();
816 aab.setAction(actionList);
817 // Wrap our Apply Action in an Instruction
818 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
824 * Set ARP_TPA Instructions
825 * @param ib Map InstructionBuilder
826 * @param dstiparp the dstiparp
827 * @return instructionbuilder with new attributes
829 public static InstructionBuilder createDstArpIpInstructions(InstructionBuilder ib, Ipv4Prefix dstiparp) {
831 List<Action> actionList = Lists.newArrayList();
832 ActionBuilder ab = new ActionBuilder();
833 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
835 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
836 arpmatch.setArpTargetTransportAddress(dstiparp);
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_SPA Instructions
853 * @param ib Map InstructionBuilder
854 * @param srciparp the srciparp
855 * @return instructionbuilder with new attributes
857 public static InstructionBuilder createSrcArpIpInstructions(InstructionBuilder ib, Ipv4Prefix srciparp) {
859 List<Action> actionList = Lists.newArrayList();
860 ActionBuilder ab = new ActionBuilder();
861 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
863 ArpMatchBuilder arpmatch = new ArpMatchBuilder();
864 arpmatch.setArpSourceTransportAddress(srciparp);
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 the Tunnel EndpointIPv4 Source Address
881 * @param ib Map InstructionBuilder
882 * @param srcIp Ipv4Prefix source IP for the tunnel endpoint (TEP)
883 * @return instructionbuilder with new attributes
885 public static InstructionBuilder createTunnelIpv4SrcInstructions(InstructionBuilder ib, Ipv4Prefix srcIp) {
887 List<Action> actionList = new ArrayList<Action>();
888 ActionBuilder ab = new ActionBuilder();
890 // Build the tunnel endpoint source IPv4 address
891 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
893 // Add the new IPv4 object as the tunnel destination
894 TunnelIpv4MatchBuilder tunnelIpv4MatchBuilder = new TunnelIpv4MatchBuilder();
895 tunnelIpv4MatchBuilder.setTunnelIpv4Source(srcIp);
896 setFieldBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
898 // Add the IPv4 tunnel src to the set_field value
899 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
901 ab.setKey(new ActionKey(0));
902 actionList.add(ab.build());
904 // Resulting action is a per/flow src TEP (set_field:172.16.100.100->tun_src)
905 ApplyActionsBuilder aab = new ApplyActionsBuilder();
906 aab.setAction(actionList);
907 // Wrap our Apply Action in an Instruction
908 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
914 * Set the Tunnel EndpointIPv4 Destination Address
915 * @param ib Map InstructionBuilder
916 * @param dstIp Ipv4Prefix destination IP for the tunnel endpoint (TEP)
917 * @return instructionbuilder with new attributes
919 public static InstructionBuilder createTunnelIpv4DstInstructions(InstructionBuilder ib, Ipv4Prefix dstIp) {
921 List<Action> actionList = new ArrayList<Action>();
922 ActionBuilder ab = new ActionBuilder();
924 // Build the tunnel endpoint dst IPv4 address
925 SetFieldBuilder setFieldBuilder = new SetFieldBuilder();
927 // Add the new IPv4 object as the tunnel destination
928 TunnelIpv4MatchBuilder tunnelIpv4MatchBuilder = new TunnelIpv4MatchBuilder();
929 tunnelIpv4MatchBuilder.setTunnelIpv4Destination(dstIp);
930 setFieldBuilder.setLayer3Match(tunnelIpv4MatchBuilder.build());
932 // Add the IPv4 tunnel src to the set_field value
933 ab.setAction(new SetFieldCaseBuilder().setSetField(setFieldBuilder.build()).build());
935 ab.setKey(new ActionKey(0));
936 actionList.add(ab.build());
938 // Resulting action is a per/flow src TEP (set_field:172.16.100.100->tun_src)
939 ApplyActionsBuilder aab = new ApplyActionsBuilder();
940 aab.setAction(actionList);
941 // Wrap our Apply Action in an Instruction
942 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
948 * Set IPv4 Source Address Instructions
949 * @param ib Map InstructionBuilder
950 * @param metaData BigInteger representing the OpenFlow metadata
951 * @param metaDataMask BigInteger representing the OpenFlow metadata mask
952 * @return instructionbuilder with new attributes
954 public static InstructionBuilder createMetadataInstructions(InstructionBuilder ib, BigInteger metaData, BigInteger metaDataMask) {
956 WriteMetadataBuilder aab = new WriteMetadataBuilder();
957 aab.setMetadata(metaData);
958 aab.setMetadataMask(metaDataMask);
959 ib.setInstruction(new WriteMetadataCaseBuilder().setWriteMetadata(aab.build()).build());
963 * TODO Determine why the following fails serialization
964 * with a null value. Per the spec it is optional.
965 * The metadata match, the Flowmod works w/o an accompanying mask.
967 * if (metaDataMask != null) {
968 * aab.setMetadataMask(metaDataMask);
976 public static InstructionBuilder createDlSrcInstructions(InstructionBuilder ib, MacAddress macAddress) {
978 List<Action> actionList = Lists.newArrayList();
979 ActionBuilder ab = new ActionBuilder();
981 SetDlSrcActionBuilder dlSrcActionBuilder= new SetDlSrcActionBuilder();
983 dlSrcActionBuilder.setAddress(macAddress);
985 ab.setAction(new SetDlSrcActionCaseBuilder().setSetDlSrcAction(dlSrcActionBuilder.build()).build());
987 ab.setKey(new ActionKey(0));
988 actionList.add(ab.build());
991 // Create an Apply Action
992 ApplyActionsBuilder aab = new ApplyActionsBuilder();
993 aab.setAction(actionList);
995 // Wrap our Apply Action in an Instruction
996 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1001 public static InstructionBuilder createDlDstInstructions(InstructionBuilder ib, MacAddress macAddress) {
1003 List<Action> actionList = Lists.newArrayList();
1004 ActionBuilder ab = new ActionBuilder();
1006 SetDlDstActionBuilder dlDstActionBuilder= new SetDlDstActionBuilder();
1008 dlDstActionBuilder.setAddress(macAddress);
1010 ab.setAction(new SetDlDstActionCaseBuilder().setSetDlDstAction(dlDstActionBuilder.build()).build());
1012 ab.setKey(new ActionKey(0));
1013 actionList.add(ab.build());
1015 // Create an Apply Action
1016 ApplyActionsBuilder aab = new ApplyActionsBuilder();
1017 aab.setAction(actionList);
1019 // Wrap our Apply Action in an Instruction
1020 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
1025 public static ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action>
1026 actionList (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action... actions) {
1028 ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> alist
1029 = new ArrayList<>();
1031 for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action action : actions) {
1032 alist.add(new ActionBuilder()
1034 .setKey(new ActionKey(count))
1042 public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction
1043 applyActionIns(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action... actions) {
1045 return new ApplyActionsCaseBuilder()
1046 .setApplyActions(new ApplyActionsBuilder()
1047 .setAction(actionList(actions))
1052 public static Instructions getInstructions(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction... instructions) {
1053 ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> ins
1054 = new ArrayList<>();
1056 for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction i : instructions) {
1057 ins.add(new InstructionBuilder()
1059 .setKey(new InstructionKey(order))
1064 return new InstructionsBuilder().setInstruction(ins).build();
1067 public static Instructions dropInstructions() {
1068 return getInstructions(applyActionIns(dropAction()));
1072 * Get a list of Instructions containing Nicira extensions that can have
1073 * additional OF/OXM instructions added to the returned Instruction list
1075 * @param instructions org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instructions
1076 * @return instruction list that additional
1078 public static List<Instruction> getInstructionList(
1079 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction... instructions) {
1080 ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction> ins
1081 = new ArrayList<>();
1083 for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction i : instructions) {
1084 ins.add(new InstructionBuilder()