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 MatchBuilder match = new MatchBuilder();
66 match.setEthernetMatch(ethernetMatch);
68 Uri outputPort = dstPort.getValue().firstKeyOf(NodeConnector.class).getId();
70 Action outputToControllerAction = new ActionBuilder()
72 .setAction(new OutputActionCaseBuilder()
73 .setOutputAction(new OutputActionBuilder()
74 .setMaxLength(Uint16.MAX_VALUE)
75 .setOutputNodeConnector(outputPort)
80 // Create an Apply Action
81 ApplyActions applyActions = new ApplyActionsBuilder()
82 .setAction(Map.of(outputToControllerAction.key(), 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(Map.of(applyActionsInstruction.key(), applyActionsInstruction))
102 .setPriority(priority)
103 .setBufferId(OFConstants.OFP_NO_BUFFER)
104 .setHardTimeout(Uint16.ZERO)
105 .setIdleTimeout(Uint16.ZERO)
106 .setFlags(new FlowModFlags(false, false, false, false, false));
112 * Returns a{@link FlowBuilder} forwarding all packets to controller port.
114 public static FlowBuilder createFwdAllToControllerFlow(final Uint8 tableId, final Uint16 priority,
115 final FlowId flowId) {
116 // Create output action -> send to controller
117 OutputActionBuilder output = new OutputActionBuilder();
118 output.setMaxLength(Uint16.MAX_VALUE);
119 Uri controllerPort = new Uri(OutputPortValues.CONTROLLER.toString());
120 output.setOutputNodeConnector(controllerPort);
122 Action action = new ActionBuilder()
123 .setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build())
124 .withKey(new ActionKey(0))
127 // Create an Apply Action
128 ApplyActionsBuilder aab = new ApplyActionsBuilder().setAction(Map.of(action.key(), action));
130 // Wrap our Apply Action in an Instruction
131 Instruction instruction = new InstructionBuilder()
132 .setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build())
133 .withKey(new InstructionKey(0))
136 // Put our Instruction in a list of Instructions
137 InstructionsBuilder isb = new InstructionsBuilder()
138 .setInstruction(Map.of(instruction.key(), instruction));
140 MatchBuilder matchBuilder = new MatchBuilder();
141 FlowBuilder allToCtrlFlow = new FlowBuilder().setTableId(tableId).setFlowName("allPacketsToCtrl").setId(flowId)
142 .withKey(new FlowKey(flowId));
144 .setMatch(matchBuilder.build())
145 .setInstructions(isb.build())
146 .setPriority(priority)
147 .setBufferId(OFConstants.OFP_NO_BUFFER)
148 .setHardTimeout(Uint16.ZERO)
149 .setIdleTimeout(Uint16.ZERO)
150 .setFlags(new FlowModFlags(false, false, false, false, false));
152 return allToCtrlFlow;