2 * Copyright IBM Corporation, 2013. All rights reserved.
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
8 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
14 import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.clear.actions._case.ClearActionsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.MeterBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActionsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;
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.meter.types.rev130918.MeterId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
41 public class OFToMDSalFlowConvertor {
44 * Method convert Openflow 1.3+ specific instructions to MD-SAL format
48 * @param ofVersion current ofp version
51 public static Instructions toSALInstruction(
52 List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction> instructions, OpenflowVersion ofVersion) {
54 InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
56 List<Instruction> salInstructionList = new ArrayList<Instruction>();
57 int instructionTreeNodekey = 0;
58 for (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction switchInst : instructions) {
59 if (switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions.class)) {
61 ActionsInstruction actionsInstruction = (ActionsInstruction) switchInst.getAugmentation(ActionsInstruction.class);
62 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
63 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
66 applyActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getAction(), ofVersion, ActionPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_APPLYACTIONSCASE_APPLYACTIONS_ACTION_ACTION)));
68 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
70 InstructionBuilder instBuilder = new InstructionBuilder();
71 instBuilder.setInstruction(applyActionsCaseBuilder.build());
72 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
73 instBuilder.setOrder(instructionTreeNodekey);
74 instructionTreeNodekey++;
75 salInstructionList.add(instBuilder.build());
76 } else if (switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions.class)) {
77 InstructionBuilder instBuilder = new InstructionBuilder();
79 ClearActionsCaseBuilder clearActionsCaseBuilder = new ClearActionsCaseBuilder();
80 ClearActionsBuilder clearActionsBuilder = new ClearActionsBuilder();
81 clearActionsCaseBuilder.setClearActions(clearActionsBuilder.build());
83 instBuilder.setInstruction(clearActionsCaseBuilder.build());
84 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
85 instBuilder.setOrder(instructionTreeNodekey);
86 instructionTreeNodekey++;
88 salInstructionList.add(instBuilder.build());
89 } else if (switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable.class)) {
91 TableIdInstruction tableIdInstruction = (TableIdInstruction) switchInst.getAugmentation(TableIdInstruction.class);
93 GoToTableCaseBuilder goToTableCaseBuilder = new GoToTableCaseBuilder();
94 GoToTableBuilder goToTableBuilder = new GoToTableBuilder();
95 goToTableBuilder.setTableId(tableIdInstruction.getTableId());
96 goToTableCaseBuilder.setGoToTable(goToTableBuilder.build());
98 InstructionBuilder instBuilder = new InstructionBuilder();
99 instBuilder.setInstruction(goToTableCaseBuilder.build());
100 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
101 instBuilder.setOrder(instructionTreeNodekey);
102 instructionTreeNodekey++;
104 salInstructionList.add(instBuilder.build());
105 } else if (switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter.class)) {
107 MeterIdInstruction meterIdInstruction = (MeterIdInstruction) switchInst.getAugmentation(MeterIdInstruction.class);
109 InstructionBuilder instBuilder = new InstructionBuilder();
111 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
112 MeterBuilder meterBuilder = new MeterBuilder();
113 meterBuilder.setMeterId(new MeterId(meterIdInstruction.getMeterId()));
114 meterCaseBuilder.setMeter(meterBuilder.build());
116 instBuilder.setInstruction(meterCaseBuilder.build());
117 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
118 instBuilder.setOrder(instructionTreeNodekey);
119 instructionTreeNodekey++;
121 salInstructionList.add(instBuilder.build());
122 } else if (switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions.class)) {
124 ActionsInstruction actionsInstruction = (ActionsInstruction) switchInst.getAugmentation(ActionsInstruction.class);
126 WriteActionsCaseBuilder writeActionsCaseBuilder = new WriteActionsCaseBuilder();
127 WriteActionsBuilder writeActionsBuilder = new WriteActionsBuilder();
128 writeActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getAction(), ofVersion, ActionPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION)));
129 writeActionsCaseBuilder.setWriteActions(writeActionsBuilder.build());
131 InstructionBuilder instBuilder = new InstructionBuilder();
132 instBuilder.setInstruction(writeActionsCaseBuilder.build());
133 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
134 instBuilder.setOrder(instructionTreeNodekey);
135 instructionTreeNodekey++;
137 salInstructionList.add(instBuilder.build());
139 } else if (switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata.class)) {
141 MetadataInstruction metadataInstruction = (MetadataInstruction) switchInst.getAugmentation(MetadataInstruction.class);
143 WriteMetadataCaseBuilder writeMetadataCaseBuilder = new WriteMetadataCaseBuilder();
144 WriteMetadataBuilder writeMetadataBuilder = new WriteMetadataBuilder();
145 writeMetadataBuilder.setMetadata(new BigInteger(1, metadataInstruction.getMetadata()));
146 writeMetadataBuilder.setMetadataMask(new BigInteger(1, metadataInstruction.getMetadataMask()));
147 writeMetadataCaseBuilder.setWriteMetadata(writeMetadataBuilder.build());
149 InstructionBuilder instBuilder = new InstructionBuilder();
150 instBuilder.setInstruction(writeMetadataCaseBuilder.build());
151 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
152 instBuilder.setOrder(instructionTreeNodekey);
153 instructionTreeNodekey++;
155 salInstructionList.add(instBuilder.build());
158 instructionsBuilder.setInstruction(salInstructionList);
159 return instructionsBuilder.build();
163 * Method wrapping all the actions org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action
164 * in org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action, to set appropriate keys
167 private static List<Action> wrapActionList(List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> actionList) {
168 List<Action> actions = new ArrayList<>();
171 for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action action : actionList) {
172 ActionBuilder wrappedAction = new ActionBuilder();
173 wrappedAction.setAction(action);
174 wrappedAction.setKey(new ActionKey(actionKey));
175 wrappedAction.setOrder(actionKey);
176 actions.add(wrappedAction.build());
184 * Method wraps openflow 1.0 actions list to Apply Action Instructions
186 * @param ofVersion current ofp version
189 public static Instructions wrapOF10ActionsToInstruction(
190 List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action> actionsList, OpenflowVersion ofVersion) {
191 InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
193 List<Instruction> salInstructionList = new ArrayList<Instruction>();
195 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
196 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
198 applyActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsList, ofVersion, ActionPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION)));
200 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
202 InstructionBuilder instBuilder = new InstructionBuilder();
203 instBuilder.setInstruction(applyActionsCaseBuilder.build());
204 instBuilder.setKey(new InstructionKey(0));
205 instBuilder.setOrder(0);
206 salInstructionList.add(instBuilder.build());
208 instructionsBuilder.setInstruction(salInstructionList);
209 return instructionsBuilder.build();