2 * Copyright (c) 2014, 2015 Cisco Systems, 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
8 package org.opendaylight.openflowplugin.learningswitch;
11 import org.opendaylight.openflowplugin.api.OFConstants;
12 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActions;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
38 import org.opendaylight.yangtools.yang.common.Uint16;
39 import org.opendaylight.yangtools.yang.common.Uint8;
41 public final class FlowUtils {
43 //prohibite to instantiate util class
47 * Returns a {@link FlowBuilder} forwarding all packets to controller port.
49 public static FlowBuilder createDirectMacToMacFlow(final Uint8 tableId, final Uint16 priority,
50 final MacAddress srcMac, final MacAddress dstMac, final NodeConnectorRef dstPort) {
51 FlowBuilder macToMacFlow = new FlowBuilder()
53 .setFlowName("mac2mac");
54 macToMacFlow.setId(new FlowId(Long.toString(macToMacFlow.hashCode())));
56 EthernetMatch ethernetMatch = new EthernetMatchBuilder()
57 .setEthernetSource(new EthernetSourceBuilder()
60 .setEthernetDestination(new EthernetDestinationBuilder()
65 Uri outputPort = dstPort.getValue().firstKeyOf(NodeConnector.class).getId();
67 Action outputToControllerAction = new ActionBuilder()
69 .setAction(new OutputActionCaseBuilder()
70 .setOutputAction(new OutputActionBuilder()
71 .setMaxLength(Uint16.MAX_VALUE)
72 .setOutputNodeConnector(outputPort)
77 // Create an Apply Action
78 ApplyActions applyActions = new ApplyActionsBuilder()
79 .setAction(Map.of(outputToControllerAction.key(), outputToControllerAction))
82 // Wrap our Apply Action in an Instruction
83 Instruction applyActionsInstruction = new InstructionBuilder()
85 .setInstruction(new ApplyActionsCaseBuilder()
86 .setApplyActions(applyActions)
90 // Put our Instruction in a list of Instructions
93 .setMatch(new MatchBuilder()
94 .setEthernetMatch(ethernetMatch)
96 .setInstructions(new InstructionsBuilder()
97 .setInstruction(Map.of(applyActionsInstruction.key(), applyActionsInstruction))
99 .setPriority(priority)
100 .setBufferId(OFConstants.OFP_NO_BUFFER)
101 .setHardTimeout(Uint16.ZERO)
102 .setIdleTimeout(Uint16.ZERO)
103 .setFlags(new FlowModFlags(false, false, false, false, false));
109 * Returns a{@link FlowBuilder} forwarding all packets to controller port.
111 public static FlowBuilder createFwdAllToControllerFlow(final Uint8 tableId, final Uint16 priority,
112 final FlowId flowId) {
113 // Create output action -> send to controller
114 OutputActionBuilder output = new OutputActionBuilder();
115 output.setMaxLength(Uint16.MAX_VALUE);
116 Uri controllerPort = new Uri(OutputPortValues.CONTROLLER.toString());
117 output.setOutputNodeConnector(controllerPort);
119 Action action = new ActionBuilder()
120 .setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build())
121 .withKey(new ActionKey(0))
124 // Create an Apply Action
125 ApplyActionsBuilder aab = new ApplyActionsBuilder().setAction(Map.of(action.key(), action));
127 // Wrap our Apply Action in an Instruction
128 Instruction instruction = new InstructionBuilder()
129 .setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build())
130 .withKey(new InstructionKey(0))
133 // Put our Instruction in a list of Instructions
134 InstructionsBuilder isb = new InstructionsBuilder()
135 .setInstruction(Map.of(instruction.key(), instruction));
137 MatchBuilder matchBuilder = new MatchBuilder();
138 FlowBuilder allToCtrlFlow = new FlowBuilder().setTableId(tableId).setFlowName("allPacketsToCtrl").setId(flowId)
139 .withKey(new FlowKey(flowId));
141 .setMatch(matchBuilder.build())
142 .setInstructions(isb.build())
143 .setPriority(priority)
144 .setBufferId(OFConstants.OFP_NO_BUFFER)
145 .setHardTimeout(Uint16.ZERO)
146 .setIdleTimeout(Uint16.ZERO)
147 .setFlags(new FlowModFlags(false, false, false, false, false));
149 return allToCtrlFlow;