OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / samples / learning-switch / src / main / java / org / opendaylight / openflowplugin / learningswitch / FlowUtils.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.learningswitch;
10
11 import com.google.common.collect.ImmutableList;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.openflowplugin.api.OFConstants;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActions;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
41
42 public final class FlowUtils {
43     private FlowUtils() {
44         //prohibite to instantiate util class
45     }
46
47     /**
48      * Returns a {@link FlowBuilder} forwarding all packets to controller port.
49      */
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()
53                 .setTableId(tableId)
54                 .setFlowName("mac2mac");
55         macToMacFlow.setId(new FlowId(Long.toString(macToMacFlow.hashCode())));
56
57         EthernetMatch ethernetMatch = new EthernetMatchBuilder()
58                 .setEthernetSource(new EthernetSourceBuilder()
59                         .setAddress(srcMac)
60                         .build())
61                 .setEthernetDestination(new EthernetDestinationBuilder()
62                         .setAddress(dstMac)
63                         .build())
64                 .build();
65
66         MatchBuilder match = new MatchBuilder();
67         match.setEthernetMatch(ethernetMatch);
68
69         Uri outputPort = dstPort.getValue().firstKeyOf(NodeConnector.class).getId();
70
71         Action outputToControllerAction = new ActionBuilder()
72                 .setOrder(0)
73                 .setAction(new OutputActionCaseBuilder()
74                         .setOutputAction(new OutputActionBuilder()
75                                 .setMaxLength(Integer.valueOf(0xffff))
76                                 .setOutputNodeConnector(outputPort)
77                                 .build())
78                         .build())
79                 .build();
80
81         // Create an Apply Action
82         ApplyActions applyActions = new ApplyActionsBuilder().setAction(ImmutableList.of(outputToControllerAction))
83                 .build();
84
85         // Wrap our Apply Action in an Instruction
86         Instruction applyActionsInstruction = new InstructionBuilder()
87                 .setOrder(0)
88                 .setInstruction(new ApplyActionsCaseBuilder()
89                         .setApplyActions(applyActions)
90                         .build())
91                 .build();
92
93         // Put our Instruction in a list of Instructions
94
95         macToMacFlow
96                 .setMatch(new MatchBuilder()
97                         .setEthernetMatch(ethernetMatch)
98                         .build())
99                 .setInstructions(new InstructionsBuilder()
100                         .setInstruction(ImmutableList.of(applyActionsInstruction))
101                         .build())
102                 .setPriority(priority)
103                 .setBufferId(OFConstants.OFP_NO_BUFFER)
104                 .setHardTimeout(0)
105                 .setIdleTimeout(0)
106                 .setFlags(new FlowModFlags(false, false, false, false, false));
107
108         return macToMacFlow;
109     }
110
111     /**
112      * Returns a{@link FlowBuilder} forwarding all packets to controller port.
113      */
114     public static FlowBuilder createFwdAllToControllerFlow(final Short tableId, final int priority,
115             final FlowId flowId) {
116         // Create output action -> send to controller
117         OutputActionBuilder output = new OutputActionBuilder();
118         output.setMaxLength(Integer.valueOf(0xffff));
119         Uri controllerPort = new Uri(OutputPortValues.CONTROLLER.toString());
120         output.setOutputNodeConnector(controllerPort);
121
122         ActionBuilder ab = new ActionBuilder();
123         ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
124         ab.setOrder(0);
125         ab.withKey(new ActionKey(0));
126
127         List<Action> actionList = new ArrayList<>();
128         actionList.add(ab.build());
129
130         // Create an Apply Action
131         ApplyActionsBuilder aab = new ApplyActionsBuilder();
132         aab.setAction(actionList);
133
134         // Wrap our Apply Action in an Instruction
135         InstructionBuilder ib = new InstructionBuilder();
136         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
137         ib.setOrder(0);
138         ib.withKey(new InstructionKey(0));
139
140         // Put our Instruction in a list of Instructions
141         InstructionsBuilder isb = new InstructionsBuilder();
142         List<Instruction> instructions = new ArrayList<>();
143         instructions.add(ib.build());
144         isb.setInstruction(instructions);
145
146         MatchBuilder matchBuilder = new MatchBuilder();
147         FlowBuilder allToCtrlFlow = new FlowBuilder().setTableId(tableId).setFlowName("allPacketsToCtrl").setId(flowId)
148                 .withKey(new FlowKey(flowId));
149         allToCtrlFlow
150             .setMatch(matchBuilder.build())
151             .setInstructions(isb.build())
152             .setPriority(priority)
153             .setBufferId(OFConstants.OFP_NO_BUFFER)
154             .setHardTimeout(0)
155             .setIdleTimeout(0)
156             .setFlags(new FlowModFlags(false, false, false, false, false));
157
158         return allToCtrlFlow;
159     }
160 }