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