1 package org.opendaylight.openflowplugin.learningswitch;
3 import java.util.ArrayList;
6 import org.opendaylight.openflowplugin.api.OFConstants;
7 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
8 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
9 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActions;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
35 import com.google.common.collect.ImmutableList;
37 public class FlowUtils {
39 //prohibite to instantiate util class
48 * @return {@link FlowBuilder} forwarding all packets to controller port
50 public static FlowBuilder createDirectMacToMacFlow(final Short tableId, final int priority, final MacAddress srcMac,
51 final MacAddress dstMac, final NodeConnectorRef dstPort) {
52 FlowBuilder macToMacFlow = new FlowBuilder()
54 .setFlowName("mac2mac");
55 macToMacFlow.setId(new FlowId(Long.toString(macToMacFlow.hashCode())));
57 EthernetMatch ethernetMatch = new EthernetMatchBuilder()
58 .setEthernetSource(new EthernetSourceBuilder()
61 .setEthernetDestination(new EthernetDestinationBuilder()
66 MatchBuilder match = new MatchBuilder();
67 match.setEthernetMatch(ethernetMatch);
69 Uri outputPort = dstPort.getValue().firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId();
71 Action outputToControllerAction = new ActionBuilder()
73 .setAction(new OutputActionCaseBuilder()
74 .setOutputAction(new OutputActionBuilder()
75 .setMaxLength(Integer.valueOf(0xffff))
76 .setOutputNodeConnector(outputPort)
81 // Create an Apply Action
82 ApplyActions applyActions = new ApplyActionsBuilder().setAction(ImmutableList.of(outputToControllerAction))
85 // Wrap our Apply Action in an Instruction
86 Instruction applyActionsInstruction = new InstructionBuilder()
88 .setInstruction(new ApplyActionsCaseBuilder()
89 .setApplyActions(applyActions)
93 // Put our Instruction in a list of Instructions
96 .setMatch(new MatchBuilder()
97 .setEthernetMatch(ethernetMatch)
99 .setInstructions(new InstructionsBuilder()
100 .setInstruction(ImmutableList.of(applyActionsInstruction))
102 .setPriority(priority)
103 .setBufferId(OFConstants.OFP_NO_BUFFER)
106 .setFlags(new FlowModFlags(false, false, false, false, false));
115 * @return {@link FlowBuilder} forwarding all packets to controller port
117 public static FlowBuilder createFwdAllToControllerFlow(final Short tableId, final int priority, final FlowId flowId) {
118 FlowBuilder allToCtrlFlow = new FlowBuilder().setTableId(tableId).setFlowName("allPacketsToCtrl").setId(flowId)
119 .setKey(new FlowKey(flowId));
121 MatchBuilder matchBuilder = new MatchBuilder();
123 // Create output action -> send to controller
124 OutputActionBuilder output = new OutputActionBuilder();
125 output.setMaxLength(Integer.valueOf(0xffff));
126 Uri controllerPort = new Uri(OutputPortValues.CONTROLLER.toString());
127 output.setOutputNodeConnector(controllerPort);
129 ActionBuilder ab = new ActionBuilder();
130 ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
132 ab.setKey(new ActionKey(0));
134 List<Action> actionList = new ArrayList<Action>();
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 InstructionBuilder ib = new InstructionBuilder();
143 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
145 ib.setKey(new InstructionKey(0));
147 // Put our Instruction in a list of Instructions
148 InstructionsBuilder isb = new InstructionsBuilder();
149 List<Instruction> instructions = new ArrayList<Instruction>();
150 instructions.add(ib.build());
151 isb.setInstruction(instructions);
154 .setMatch(matchBuilder.build())
155 .setInstructions(isb.build())
156 .setPriority(priority)
157 .setBufferId(OFConstants.OFP_NO_BUFFER)
160 .setFlags(new FlowModFlags(false, false, false, false, false));
162 return allToCtrlFlow;