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;
40 public class OFToMDSalFlowConvertor {
43 * Method convert Openflow 1.3+ specific instructions to MD-SAL format
48 public static Instructions toSALInstruction(
49 List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction> instructions) {
51 InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
53 List<Instruction> salInstructionList = new ArrayList<Instruction>();
54 int instructionTreeNodekey=0;
55 for(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction switchInst : instructions){
56 if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions.class)){
58 ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
59 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
60 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
63 applyActionsBuilder.setAction(
65 ActionConvertor.toMDSalActions(actionsInstruction.getAction()
69 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
71 InstructionBuilder instBuilder = new InstructionBuilder();
72 instBuilder.setInstruction(applyActionsCaseBuilder.build());
73 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
74 instBuilder.setOrder(instructionTreeNodekey);
75 instructionTreeNodekey++;
76 salInstructionList.add(instBuilder.build());
77 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions.class)){
78 InstructionBuilder instBuilder = new InstructionBuilder();
80 ClearActionsCaseBuilder clearActionsCaseBuilder = new ClearActionsCaseBuilder();
81 ClearActionsBuilder clearActionsBuilder = new ClearActionsBuilder();
82 clearActionsCaseBuilder.setClearActions(clearActionsBuilder.build());
84 instBuilder.setInstruction(clearActionsCaseBuilder.build());
85 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
86 instBuilder.setOrder(instructionTreeNodekey);
87 instructionTreeNodekey++;
89 salInstructionList.add(instBuilder.build());
90 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable.class)){
92 TableIdInstruction tableIdInstruction = (TableIdInstruction)switchInst.getAugmentation(TableIdInstruction.class);
94 GoToTableCaseBuilder goToTableCaseBuilder = new GoToTableCaseBuilder();
95 GoToTableBuilder goToTableBuilder = new GoToTableBuilder();
96 goToTableBuilder.setTableId(tableIdInstruction.getTableId());
97 goToTableCaseBuilder.setGoToTable(goToTableBuilder.build());
99 InstructionBuilder instBuilder = new InstructionBuilder();
100 instBuilder.setInstruction(goToTableCaseBuilder.build());
101 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
102 instBuilder.setOrder(instructionTreeNodekey);
103 instructionTreeNodekey++;
105 salInstructionList.add(instBuilder.build());
106 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter.class)){
108 MeterIdInstruction meterIdInstruction = (MeterIdInstruction) switchInst.getAugmentation(MeterIdInstruction.class);
110 InstructionBuilder instBuilder = new InstructionBuilder();
112 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
113 MeterBuilder meterBuilder = new MeterBuilder();
114 meterBuilder.setMeterId(new MeterId(meterIdInstruction.getMeterId()));
115 meterCaseBuilder.setMeter(meterBuilder.build());
117 instBuilder.setInstruction(meterCaseBuilder.build());
118 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
119 instBuilder.setOrder(instructionTreeNodekey);
120 instructionTreeNodekey++;
122 salInstructionList.add(instBuilder.build());
123 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions.class)){
125 ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
127 WriteActionsCaseBuilder writeActionsCaseBuilder = new WriteActionsCaseBuilder();
128 WriteActionsBuilder writeActionsBuilder = new WriteActionsBuilder();
129 writeActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getAction())));
130 writeActionsCaseBuilder.setWriteActions(writeActionsBuilder.build());
132 InstructionBuilder instBuilder = new InstructionBuilder();
133 instBuilder.setInstruction(writeActionsCaseBuilder.build());
134 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
135 instBuilder.setOrder(instructionTreeNodekey);
136 instructionTreeNodekey++;
138 salInstructionList.add(instBuilder.build());
140 }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata.class)){
142 MetadataInstruction metadataInstruction = (MetadataInstruction)switchInst.getAugmentation(MetadataInstruction.class);
144 WriteMetadataCaseBuilder writeMetadataCaseBuilder = new WriteMetadataCaseBuilder();
145 WriteMetadataBuilder writeMetadataBuilder = new WriteMetadataBuilder();
146 writeMetadataBuilder.setMetadata(new BigInteger(1, metadataInstruction.getMetadata()));
147 writeMetadataBuilder.setMetadataMask(new BigInteger(1, metadataInstruction.getMetadataMask()));
148 writeMetadataCaseBuilder.setWriteMetadata(writeMetadataBuilder.build());
150 InstructionBuilder instBuilder = new InstructionBuilder();
151 instBuilder.setInstruction(writeMetadataCaseBuilder.build());
152 instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
153 instBuilder.setOrder(instructionTreeNodekey);
154 instructionTreeNodekey++;
156 salInstructionList.add(instBuilder.build());
159 instructionsBuilder.setInstruction(salInstructionList);
160 return instructionsBuilder.build();
164 * Method wrapping all the actions org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action
165 * in org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action, to set appropriate keys
168 private static List<Action> wrapActionList(List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> actionList){
169 List<Action> actions = new ArrayList<>();
172 for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action action : actionList){
173 ActionBuilder wrappedAction = new ActionBuilder();
174 wrappedAction.setAction(action);
175 wrappedAction.setKey(new ActionKey(actionKey));
176 wrappedAction.setOrder(actionKey);
177 actions.add(wrappedAction.build());
185 * Method wraps openflow 1.0 actions list to Apply Action Instructions
188 public static Instructions wrapOF10ActionsToInstruction(
189 List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action> 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();