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