Bug 5540 - ActionConvertor, ActionResponseConvertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / OFToMDSalFlowConvertor.java
1 /*
2  * Copyright (c) 2013, 2015 IBM Corporation 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.openflow.md.core.sal.convertor;
10
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Optional;
16 import org.opendaylight.openflowplugin.api.OFConstants;
17 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
18 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.data.ActionResponseConvertorData;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
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.instruction.instruction.ApplyActionsCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
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.instruction.go.to.table._case.GoToTableBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.MeterBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActionsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCase;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.MeterCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteActionsCase;
44
45 public final class OFToMDSalFlowConvertor {
46
47     private OFToMDSalFlowConvertor() {
48         // hiding implicite constructor
49     }
50
51     /**
52      * Method convert Openflow 1.3+ specific instructions to MD-SAL format
53      * flow instruction
54      *
55      * @param instructions instructions
56      * @param ofVersion    current ofp version
57      * @return instruction converted to SAL instruction
58      */
59     public static Instructions toSALInstruction(
60             List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction> instructions, OpenflowVersion ofVersion) {
61
62         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
63
64         List<Instruction> salInstructionList = new ArrayList<Instruction>();
65         int instructionTreeNodekey = 0;
66         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction salInstruction;
67         for (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction switchInst : instructions) {
68             if (switchInst.getInstructionChoice() instanceof ApplyActionsCase) {
69
70                 ApplyActionsCase actionsInstruction = ((ApplyActionsCase) switchInst.getInstructionChoice());
71                 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
72                 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
73
74                 final ActionResponseConvertorData actionResponseConvertorData = new ActionResponseConvertorData(ofVersion.getVersion());
75                 actionResponseConvertorData.setActionPath(ActionPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_APPLYACTIONSCASE_APPLYACTIONS_ACTION_ACTION);
76
77                 final Optional<List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action>> actions = ConvertorManager.getInstance().convert(
78                         actionsInstruction.getApplyActions().getAction(), actionResponseConvertorData);
79
80                 applyActionsBuilder.setAction(wrapActionList(actions.orElse(Collections.emptyList())));
81                 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
82                 salInstruction = applyActionsCaseBuilder.build();
83             } else if (switchInst.getInstructionChoice() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCase) {
84
85                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCase clearActionsCase =
86                         ((org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCase) switchInst.getInstructionChoice());
87                 ClearActionsCaseBuilder clearActionsCaseBuilder = new ClearActionsCaseBuilder();
88                 salInstruction = clearActionsCaseBuilder.build();
89             } else if (switchInst.getInstructionChoice() instanceof GotoTableCase) {
90
91                 GotoTableCase gotoTableCase = ((GotoTableCase) switchInst.getInstructionChoice());
92
93                 GoToTableCaseBuilder goToTableCaseBuilder = new GoToTableCaseBuilder();
94                 GoToTableBuilder goToTableBuilder = new GoToTableBuilder();
95                 goToTableBuilder.setTableId(gotoTableCase.getGotoTable().getTableId());
96                 goToTableCaseBuilder.setGoToTable(goToTableBuilder.build());
97
98                 salInstruction = goToTableCaseBuilder.build();
99             } else if (switchInst.getInstructionChoice() instanceof MeterCase) {
100
101                 MeterCase meterIdInstruction = ((MeterCase) switchInst.getInstructionChoice());
102
103
104                 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
105                 MeterBuilder meterBuilder = new MeterBuilder();
106                 meterBuilder.setMeterId(new MeterId(meterIdInstruction.getMeter().getMeterId()));
107                 meterCaseBuilder.setMeter(meterBuilder.build());
108
109                 salInstruction = meterCaseBuilder.build();
110             } else if (switchInst.getInstructionChoice() instanceof WriteActionsCase) {
111
112                 WriteActionsCase writeActionsCase = ((WriteActionsCase) switchInst.getInstructionChoice());
113
114                 WriteActionsCaseBuilder writeActionsCaseBuilder = new WriteActionsCaseBuilder();
115                 WriteActionsBuilder writeActionsBuilder = new WriteActionsBuilder();
116
117                 final ActionResponseConvertorData actionResponseConvertorData = new ActionResponseConvertorData(ofVersion.getVersion());
118                 actionResponseConvertorData.setActionPath(ActionPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION);
119
120                 final Optional<List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action>> actions = ConvertorManager.getInstance().convert(
121                         writeActionsCase.getWriteActions().getAction(), actionResponseConvertorData);
122
123                 writeActionsBuilder.setAction(wrapActionList(actions.orElse(Collections.emptyList())));
124                 writeActionsCaseBuilder.setWriteActions(writeActionsBuilder.build());
125
126                 salInstruction = writeActionsCaseBuilder.build();
127             } else if (switchInst.getInstructionChoice() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase) {
128
129                 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase writeMetadataCase =
130                         ((org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase) switchInst.getInstructionChoice());
131                 WriteMetadataCaseBuilder writeMetadataCaseBuilder = new WriteMetadataCaseBuilder();
132                 WriteMetadataBuilder writeMetadataBuilder = new WriteMetadataBuilder();
133                 writeMetadataBuilder.setMetadata(new BigInteger(OFConstants.SIGNUM_UNSIGNED, writeMetadataCase.getWriteMetadata().getMetadata()));
134                 writeMetadataBuilder.setMetadataMask(new BigInteger(OFConstants.SIGNUM_UNSIGNED, writeMetadataCase.getWriteMetadata().getMetadataMask()));
135                 writeMetadataCaseBuilder.setWriteMetadata(writeMetadataBuilder.build());
136                 salInstruction = writeMetadataCaseBuilder.build();
137             } else {
138                 continue;
139             }
140
141             InstructionBuilder instBuilder = new InstructionBuilder();
142             instBuilder.setInstruction(salInstruction);
143             instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
144             instBuilder.setOrder(instructionTreeNodekey);
145             instructionTreeNodekey++;
146             salInstructionList.add(instBuilder.build());
147
148         }
149         instructionsBuilder.setInstruction(salInstructionList);
150         return instructionsBuilder.build();
151     }
152
153     /*
154      * Method wrapping all the actions org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action
155      * in org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action, to set appropriate keys
156      * for actions.
157      */
158     private static List<Action> wrapActionList(List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> actionList) {
159         List<Action> actions = new ArrayList<>();
160
161         int actionKey = 0;
162         for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action action : actionList) {
163             ActionBuilder wrappedAction = new ActionBuilder();
164             wrappedAction.setAction(action);
165             wrappedAction.setKey(new ActionKey(actionKey));
166             wrappedAction.setOrder(actionKey);
167             actions.add(wrappedAction.build());
168             actionKey++;
169         }
170
171         return actions;
172     }
173
174     /**
175      * Method wraps openflow 1.0 actions list to Apply Action Instructions
176      *
177      * @param ofVersion current ofp version
178      * @param actionsList list of action
179      * @return OF10 actions as an instructions
180      */
181
182     public static Instructions wrapOF10ActionsToInstruction(
183             List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action> actionsList, OpenflowVersion ofVersion) {
184         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
185
186         List<Instruction> salInstructionList = new ArrayList<Instruction>();
187
188         ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
189         ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
190
191         final ActionResponseConvertorData actionResponseConvertorData = new ActionResponseConvertorData(ofVersion.getVersion());
192         actionResponseConvertorData.setActionPath(ActionPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION);
193
194         final Optional<List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action>> actions = ConvertorManager.getInstance().convert(
195                 actionsList, actionResponseConvertorData);
196
197         applyActionsBuilder.setAction(wrapActionList(actions.orElse(Collections.emptyList())));
198         applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
199
200         InstructionBuilder instBuilder = new InstructionBuilder();
201         instBuilder.setInstruction(applyActionsCaseBuilder.build());
202         instBuilder.setKey(new InstructionKey(0));
203         instBuilder.setOrder(0);
204         salInstructionList.add(instBuilder.build());
205
206         instructionsBuilder.setInstruction(salInstructionList);
207         return instructionsBuilder.build();
208     }
209
210 }