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;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.clear.actions._case.ClearActionsBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.go.to.table._case.GoToTableBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.meter._case.MeterBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.actions._case.WriteActionsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;
41 public class OFToMDSalFlowConvertor {
44 * Method convert Openflow 1.3+ specific instructions to MD-SAL format
49 public static Instructions toSALInstruction(
50 List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions> instructions) {
52 InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
54 List<Instruction> salInstructionList = new ArrayList<Instruction>();
55 int instructionTreeNodekey=0;
56 for(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions switchInst : instructions){
57 if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions.class)){
59 ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
60 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
61 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
64 applyActionsBuilder.setAction(
66 ActionConvertor.toMDSalActions(actionsInstruction.getActionsList()
70 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
72 InstructionBuilder instBuilder = new InstructionBuilder();
73 instBuilder.setInstruction(applyActionsCaseBuilder.build());
74 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
75 instBuilder.setOrder(instructionTreeNodekey);
76 instructionTreeNodekey++;
77 salInstructionList.add(instBuilder.build());
78 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions.class)){
79 InstructionBuilder instBuilder = new InstructionBuilder();
81 ClearActionsCaseBuilder clearActionsCaseBuilder = new ClearActionsCaseBuilder();
82 ClearActionsBuilder clearActionsBuilder = new ClearActionsBuilder();
83 clearActionsCaseBuilder.setClearActions(clearActionsBuilder.build());
85 instBuilder.setInstruction(clearActionsCaseBuilder.build());
86 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
87 instBuilder.setOrder(instructionTreeNodekey);
88 instructionTreeNodekey++;
90 salInstructionList.add(instBuilder.build());
91 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable.class)){
93 TableIdInstruction tableIdInstruction = (TableIdInstruction)switchInst.getAugmentation(TableIdInstruction.class);
95 GoToTableCaseBuilder goToTableCaseBuilder = new GoToTableCaseBuilder();
96 GoToTableBuilder goToTableBuilder = new GoToTableBuilder();
97 goToTableBuilder.setTableId(tableIdInstruction.getTableId());
98 goToTableCaseBuilder.setGoToTable(goToTableBuilder.build());
100 InstructionBuilder instBuilder = new InstructionBuilder();
101 instBuilder.setInstruction(goToTableCaseBuilder.build());
102 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
103 instBuilder.setOrder(instructionTreeNodekey);
104 instructionTreeNodekey++;
106 salInstructionList.add(instBuilder.build());
107 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter.class)){
109 MeterIdInstruction meterIdInstruction = (MeterIdInstruction) switchInst.getAugmentation(MeterIdInstruction.class);
111 InstructionBuilder instBuilder = new InstructionBuilder();
113 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
114 MeterBuilder meterBuilder = new MeterBuilder();
115 meterBuilder.setMeterId(new MeterId(meterIdInstruction.getMeterId()));
116 meterCaseBuilder.setMeter(meterBuilder.build());
118 instBuilder.setInstruction(meterCaseBuilder.build());
119 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
120 instBuilder.setOrder(instructionTreeNodekey);
121 instructionTreeNodekey++;
123 salInstructionList.add(instBuilder.build());
124 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions.class)){
126 ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
128 WriteActionsCaseBuilder writeActionsCaseBuilder = new WriteActionsCaseBuilder();
129 WriteActionsBuilder writeActionsBuilder = new WriteActionsBuilder();
130 writeActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getActionsList())));
131 writeActionsCaseBuilder.setWriteActions(writeActionsBuilder.build());
133 InstructionBuilder instBuilder = new InstructionBuilder();
134 instBuilder.setInstruction(writeActionsCaseBuilder.build());
135 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
136 instBuilder.setOrder(instructionTreeNodekey);
137 instructionTreeNodekey++;
139 salInstructionList.add(instBuilder.build());
141 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata.class)){
143 MetadataInstruction metadataInstruction = (MetadataInstruction)switchInst.getAugmentation(MetadataInstruction.class);
145 WriteMetadataCaseBuilder writeMetadataCaseBuilder = new WriteMetadataCaseBuilder();
146 WriteMetadataBuilder writeMetadataBuilder = new WriteMetadataBuilder();
147 writeMetadataBuilder.setMetadata(new BigInteger(1, metadataInstruction.getMetadata()));
148 writeMetadataBuilder.setMetadataMask(new BigInteger(1, metadataInstruction.getMetadataMask()));
149 writeMetadataCaseBuilder.setWriteMetadata(writeMetadataBuilder.build());
151 InstructionBuilder instBuilder = new InstructionBuilder();
152 instBuilder.setInstruction(writeMetadataCaseBuilder.build());
153 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
154 instBuilder.setOrder(instructionTreeNodekey);
155 instructionTreeNodekey++;
157 salInstructionList.add(instBuilder.build());
160 instructionsBuilder.setInstruction(salInstructionList);
161 return instructionsBuilder.build();
165 * Method wrapping all the actions org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action
166 * in org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action, to set appropriate keys
169 private static List<Action> wrapActionList(List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> actionList){
170 List<Action> actions = new ArrayList<>();
173 for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action action : actionList){
174 ActionBuilder wrappedAction = new ActionBuilder();
175 wrappedAction.setAction(action);
176 wrappedAction.setKey(new ActionKey(actionKey));
177 wrappedAction.setOrder(actionKey);
178 actions.add(wrappedAction.build());
186 * Method wraps openflow 1.0 actions list to Apply Action Instructions
189 public static Instructions wrapOF10ActionsToInstruction(List<ActionsList> actionsList) {
190 InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
192 List<Instruction> salInstructionList = new ArrayList<Instruction>();
194 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
195 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
197 applyActionsBuilder.setAction(
199 ActionConvertor.toMDSalActions(actionsList
203 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
205 InstructionBuilder instBuilder = new InstructionBuilder();
206 instBuilder.setInstruction(applyActionsCaseBuilder.build());
207 instBuilder.setKey(new InstructionKey(0));
208 instBuilder.setOrder(0);
209 salInstructionList.add(instBuilder.build());
211 instructionsBuilder.setInstruction(salInstructionList);
212 return instructionsBuilder.build();