Merge "Fix for openflow devices not connecting to controller"
[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 package org.opendaylight.openflowplugin.learningswitch;
9
10 import java.util.Map;
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;
40
41 public final class FlowUtils {
42     private FlowUtils() {
43         //prohibite to instantiate util class
44     }
45
46     /**
47      * Returns a {@link FlowBuilder} forwarding all packets to controller port.
48      */
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()
52                 .setTableId(tableId)
53                 .setFlowName("mac2mac");
54         macToMacFlow.setId(new FlowId(Long.toString(macToMacFlow.hashCode())));
55
56         EthernetMatch ethernetMatch = new EthernetMatchBuilder()
57                 .setEthernetSource(new EthernetSourceBuilder()
58                         .setAddress(srcMac)
59                         .build())
60                 .setEthernetDestination(new EthernetDestinationBuilder()
61                         .setAddress(dstMac)
62                         .build())
63                 .build();
64
65         MatchBuilder match = new MatchBuilder();
66         match.setEthernetMatch(ethernetMatch);
67
68         Uri outputPort = dstPort.getValue().firstKeyOf(NodeConnector.class).getId();
69
70         Action outputToControllerAction = new ActionBuilder()
71                 .setOrder(0)
72                 .setAction(new OutputActionCaseBuilder()
73                         .setOutputAction(new OutputActionBuilder()
74                                 .setMaxLength(Uint16.MAX_VALUE)
75                                 .setOutputNodeConnector(outputPort)
76                                 .build())
77                         .build())
78                 .build();
79
80         // Create an Apply Action
81         ApplyActions applyActions = new ApplyActionsBuilder()
82                 .setAction(Map.of(outputToControllerAction.key(), 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(Map.of(applyActionsInstruction.key(), applyActionsInstruction))
101                         .build())
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));
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 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);
121
122         Action action = new ActionBuilder()
123                 .setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build())
124                 .withKey(new ActionKey(0))
125                 .build();
126
127         // Create an Apply Action
128         ApplyActionsBuilder aab = new ApplyActionsBuilder().setAction(Map.of(action.key(), action));
129
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))
134                 .build();
135
136         // Put our Instruction in a list of Instructions
137         InstructionsBuilder isb = new InstructionsBuilder()
138                 .setInstruction(Map.of(instruction.key(), instruction));
139
140         MatchBuilder matchBuilder = new MatchBuilder();
141         FlowBuilder allToCtrlFlow = new FlowBuilder().setTableId(tableId).setFlowName("allPacketsToCtrl").setId(flowId)
142                 .withKey(new FlowKey(flowId));
143         allToCtrlFlow
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));
151
152         return allToCtrlFlow;
153     }
154 }