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 org.opendaylight.openflowplugin.openflow.md.util.OpenflowVersion;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.clear.actions._case.ClearActionsBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.MeterBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActionsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
37 import java.math.BigInteger;
38 import java.util.ArrayList;
39 import java.util.List;
41 public class OFToMDSalFlowConvertor {
44 * Method convert Openflow 1.3+ specific instructions to MD-SAL format
47 * @param ofVersion current ofp version
50 public static Instructions toSALInstruction(
51 List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction> instructions, OpenflowVersion ofVersion) {
53 InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
55 List<Instruction> salInstructionList = new ArrayList<Instruction>();
56 int instructionTreeNodekey=0;
57 for(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction switchInst : instructions){
58 if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions.class)){
60 ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
61 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
62 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
65 applyActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getAction(), ofVersion)));
67 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
69 InstructionBuilder instBuilder = new InstructionBuilder();
70 instBuilder.setInstruction(applyActionsCaseBuilder.build());
71 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
72 instBuilder.setOrder(instructionTreeNodekey);
73 instructionTreeNodekey++;
74 salInstructionList.add(instBuilder.build());
75 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions.class)){
76 InstructionBuilder instBuilder = new InstructionBuilder();
78 ClearActionsCaseBuilder clearActionsCaseBuilder = new ClearActionsCaseBuilder();
79 ClearActionsBuilder clearActionsBuilder = new ClearActionsBuilder();
80 clearActionsCaseBuilder.setClearActions(clearActionsBuilder.build());
82 instBuilder.setInstruction(clearActionsCaseBuilder.build());
83 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
84 instBuilder.setOrder(instructionTreeNodekey);
85 instructionTreeNodekey++;
87 salInstructionList.add(instBuilder.build());
88 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable.class)){
90 TableIdInstruction tableIdInstruction = (TableIdInstruction)switchInst.getAugmentation(TableIdInstruction.class);
92 GoToTableCaseBuilder goToTableCaseBuilder = new GoToTableCaseBuilder();
93 GoToTableBuilder goToTableBuilder = new GoToTableBuilder();
94 goToTableBuilder.setTableId(tableIdInstruction.getTableId());
95 goToTableCaseBuilder.setGoToTable(goToTableBuilder.build());
97 InstructionBuilder instBuilder = new InstructionBuilder();
98 instBuilder.setInstruction(goToTableCaseBuilder.build());
99 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
100 instBuilder.setOrder(instructionTreeNodekey);
101 instructionTreeNodekey++;
103 salInstructionList.add(instBuilder.build());
104 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter.class)){
106 MeterIdInstruction meterIdInstruction = (MeterIdInstruction) switchInst.getAugmentation(MeterIdInstruction.class);
108 InstructionBuilder instBuilder = new InstructionBuilder();
110 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
111 MeterBuilder meterBuilder = new MeterBuilder();
112 meterBuilder.setMeterId(new MeterId(meterIdInstruction.getMeterId()));
113 meterCaseBuilder.setMeter(meterBuilder.build());
115 instBuilder.setInstruction(meterCaseBuilder.build());
116 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
117 instBuilder.setOrder(instructionTreeNodekey);
118 instructionTreeNodekey++;
120 salInstructionList.add(instBuilder.build());
121 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions.class)){
123 ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
125 WriteActionsCaseBuilder writeActionsCaseBuilder = new WriteActionsCaseBuilder();
126 WriteActionsBuilder writeActionsBuilder = new WriteActionsBuilder();
127 writeActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getAction(), ofVersion)));
128 writeActionsCaseBuilder.setWriteActions(writeActionsBuilder.build());
130 InstructionBuilder instBuilder = new InstructionBuilder();
131 instBuilder.setInstruction(writeActionsCaseBuilder.build());
132 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
133 instBuilder.setOrder(instructionTreeNodekey);
134 instructionTreeNodekey++;
136 salInstructionList.add(instBuilder.build());
138 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata.class)){
140 MetadataInstruction metadataInstruction = (MetadataInstruction)switchInst.getAugmentation(MetadataInstruction.class);
142 WriteMetadataCaseBuilder writeMetadataCaseBuilder = new WriteMetadataCaseBuilder();
143 WriteMetadataBuilder writeMetadataBuilder = new WriteMetadataBuilder();
144 writeMetadataBuilder.setMetadata(new BigInteger(1, metadataInstruction.getMetadata()));
145 writeMetadataBuilder.setMetadataMask(new BigInteger(1, metadataInstruction.getMetadataMask()));
146 writeMetadataCaseBuilder.setWriteMetadata(writeMetadataBuilder.build());
148 InstructionBuilder instBuilder = new InstructionBuilder();
149 instBuilder.setInstruction(writeMetadataCaseBuilder.build());
150 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
151 instBuilder.setOrder(instructionTreeNodekey);
152 instructionTreeNodekey++;
154 salInstructionList.add(instBuilder.build());
157 instructionsBuilder.setInstruction(salInstructionList);
158 return instructionsBuilder.build();
162 * Method wrapping all the actions org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action
163 * in org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action, to set appropriate keys
166 private static List<Action> wrapActionList(List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> actionList){
167 List<Action> actions = new ArrayList<>();
170 for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action action : actionList){
171 ActionBuilder wrappedAction = new ActionBuilder();
172 wrappedAction.setAction(action);
173 wrappedAction.setKey(new ActionKey(actionKey));
174 wrappedAction.setOrder(actionKey);
175 actions.add(wrappedAction.build());
183 * Method wraps openflow 1.0 actions list to Apply Action Instructions
184 * @param ofVersion current ofp version
187 public static Instructions wrapOF10ActionsToInstruction(
188 List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action> actionsList, OpenflowVersion ofVersion) {
189 InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
191 List<Instruction> salInstructionList = new ArrayList<Instruction>();
193 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
194 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
196 applyActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsList, ofVersion)));
198 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
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());
206 instructionsBuilder.setInstruction(salInstructionList);
207 return instructionsBuilder.build();