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