Merge "Convert OF samples to use DTCL instead of DCL"
[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.inventory.rev130819.node.NodeConnectorKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
42
43 public class FlowUtils {
44     private FlowUtils() {
45         //prohibite to instantiate util class
46     }
47
48     /**
49      * @param tableId
50      * @param priority
51      * @param srcMac
52      * @param dstMac
53      * @param dstPort
54      * @return {@link FlowBuilder} forwarding all packets to controller port
55      */
56     public static FlowBuilder createDirectMacToMacFlow(final Short tableId, final int priority, final MacAddress srcMac,
57             final MacAddress dstMac, final NodeConnectorRef dstPort) {
58         FlowBuilder macToMacFlow = new FlowBuilder()
59                 .setTableId(tableId)
60                 .setFlowName("mac2mac");
61         macToMacFlow.setId(new FlowId(Long.toString(macToMacFlow.hashCode())));
62
63         EthernetMatch ethernetMatch = new EthernetMatchBuilder()
64                 .setEthernetSource(new EthernetSourceBuilder()
65                         .setAddress(srcMac)
66                         .build())
67                 .setEthernetDestination(new EthernetDestinationBuilder()
68                         .setAddress(dstMac)
69                         .build())
70                 .build();
71
72         MatchBuilder match = new MatchBuilder();
73         match.setEthernetMatch(ethernetMatch);
74
75         Uri outputPort = dstPort.getValue().firstKeyOf(NodeConnector.class, NodeConnectorKey.class).getId();
76
77         Action outputToControllerAction = new ActionBuilder()
78                 .setOrder(0)
79                 .setAction(new OutputActionCaseBuilder()
80                         .setOutputAction(new OutputActionBuilder()
81                                 .setMaxLength(Integer.valueOf(0xffff))
82                                 .setOutputNodeConnector(outputPort)
83                                 .build())
84                         .build())
85                 .build();
86
87         // Create an Apply Action
88         ApplyActions applyActions = new ApplyActionsBuilder().setAction(ImmutableList.of(outputToControllerAction))
89                 .build();
90
91         // Wrap our Apply Action in an Instruction
92         Instruction applyActionsInstruction = new InstructionBuilder()
93                 .setOrder(0)
94                 .setInstruction(new ApplyActionsCaseBuilder()
95                         .setApplyActions(applyActions)
96                         .build())
97                 .build();
98
99         // Put our Instruction in a list of Instructions
100
101         macToMacFlow
102                 .setMatch(new MatchBuilder()
103                         .setEthernetMatch(ethernetMatch)
104                         .build())
105                 .setInstructions(new InstructionsBuilder()
106                         .setInstruction(ImmutableList.of(applyActionsInstruction))
107                         .build())
108                 .setPriority(priority)
109                 .setBufferId(OFConstants.OFP_NO_BUFFER)
110                 .setHardTimeout(0)
111                 .setIdleTimeout(0)
112                 .setFlags(new FlowModFlags(false, false, false, false, false));
113
114         return macToMacFlow;
115     }
116
117     /**
118      * @param tableId
119      * @param priority
120      * @param flowId
121      * @return {@link FlowBuilder} forwarding all packets to controller port
122      */
123     public static FlowBuilder createFwdAllToControllerFlow(final Short tableId, final int priority, final FlowId flowId) {
124         FlowBuilder allToCtrlFlow = new FlowBuilder().setTableId(tableId).setFlowName("allPacketsToCtrl").setId(flowId)
125                 .setKey(new FlowKey(flowId));
126
127         MatchBuilder matchBuilder = new MatchBuilder();
128
129         // Create output action -> send to controller
130         OutputActionBuilder output = new OutputActionBuilder();
131         output.setMaxLength(Integer.valueOf(0xffff));
132         Uri controllerPort = new Uri(OutputPortValues.CONTROLLER.toString());
133         output.setOutputNodeConnector(controllerPort);
134
135         ActionBuilder ab = new ActionBuilder();
136         ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
137         ab.setOrder(0);
138         ab.setKey(new ActionKey(0));
139
140         List<Action> actionList = new ArrayList<Action>();
141         actionList.add(ab.build());
142
143         // Create an Apply Action
144         ApplyActionsBuilder aab = new ApplyActionsBuilder();
145         aab.setAction(actionList);
146
147         // Wrap our Apply Action in an Instruction
148         InstructionBuilder ib = new InstructionBuilder();
149         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
150         ib.setOrder(0);
151         ib.setKey(new InstructionKey(0));
152
153         // Put our Instruction in a list of Instructions
154         InstructionsBuilder isb = new InstructionsBuilder();
155         List<Instruction> instructions = new ArrayList<Instruction>();
156         instructions.add(ib.build());
157         isb.setInstruction(instructions);
158
159         allToCtrlFlow
160             .setMatch(matchBuilder.build())
161             .setInstructions(isb.build())
162             .setPriority(priority)
163             .setBufferId(OFConstants.OFP_NO_BUFFER)
164             .setHardTimeout(0)
165             .setIdleTimeout(0)
166             .setFlags(new FlowModFlags(false, false, false, false, false));
167
168         return allToCtrlFlow;
169     }
170 }