Bug 1254 - added basic conversion test for OFToMDSalFlowConvertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / OFToMDSalFlowConvertor.java
index 23c02f49739c6fc2a45c42a76800a4f575c5275c..14cffba4f6449dc608ee2324334fa4d4c562a26a 100644 (file)
@@ -10,7 +10,8 @@ package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.List;
-
+import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
+import org.opendaylight.openflowplugin.extension.api.path.ActionPath;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
@@ -30,126 +31,144 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instru
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.write.metadata._case.WriteMetadataBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;
 
 public class OFToMDSalFlowConvertor {
-    
+
     /**
      * Method convert Openflow 1.3+ specific instructions to MD-SAL format
      * flow instruction
-     * Note: MD-SAL won't augment this data directly to the data store, 
-     * so key setting is not required. If user wants to augment this data
-     * directly to the data store, key setting is required for each instructions. 
+     *
      * @param instructions
+     * @param ofVersion    current ofp version
      * @return
      */
     public static Instructions toSALInstruction(
-            List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions> instructions) {
-        
+            List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction> instructions, OpenflowVersion ofVersion) {
+
         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
-        
+
         List<Instruction> salInstructionList = new ArrayList<Instruction>();
-        
-        for(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions switchInst : instructions){
-            if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions){
-                
-                ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
+        int instructionTreeNodekey = 0;
+        for (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction switchInst : instructions) {
+            if (switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions.class)) {
+
+                ActionsInstruction actionsInstruction = (ActionsInstruction) switchInst.getAugmentation(ActionsInstruction.class);
                 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
                 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
-                
-                
-                applyActionsBuilder.setAction(
-                            wrapActionList(
-                                    ActionConvertor.toMDSalActions(actionsInstruction.getActionsList()
-                                            )
-                                            ));
-                
+
+
+                applyActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getAction(), ofVersion, ActionPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_APPLYACTIONSCASE_APPLYACTIONS_ACTION_ACTION)));
+
                 applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
-                
+
                 InstructionBuilder instBuilder = new InstructionBuilder();
                 instBuilder.setInstruction(applyActionsCaseBuilder.build());
+                instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
+                instBuilder.setOrder(instructionTreeNodekey);
+                instructionTreeNodekey++;
                 salInstructionList.add(instBuilder.build());
-            }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions){
+            } else if (switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions.class)) {
                 InstructionBuilder instBuilder = new InstructionBuilder();
-                
+
                 ClearActionsCaseBuilder clearActionsCaseBuilder = new ClearActionsCaseBuilder();
                 ClearActionsBuilder clearActionsBuilder = new ClearActionsBuilder();
                 clearActionsCaseBuilder.setClearActions(clearActionsBuilder.build());
-                
+
                 instBuilder.setInstruction(clearActionsCaseBuilder.build());
+                instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
+                instBuilder.setOrder(instructionTreeNodekey);
+                instructionTreeNodekey++;
+
                 salInstructionList.add(instBuilder.build());
-            }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable){
-                
-                TableIdInstruction tableIdInstruction = (TableIdInstruction)switchInst.getAugmentation(TableIdInstruction.class);
-                
+            } else if (switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable.class)) {
+
+                TableIdInstruction tableIdInstruction = (TableIdInstruction) switchInst.getAugmentation(TableIdInstruction.class);
+
                 GoToTableCaseBuilder goToTableCaseBuilder = new GoToTableCaseBuilder();
                 GoToTableBuilder goToTableBuilder = new GoToTableBuilder();
                 goToTableBuilder.setTableId(tableIdInstruction.getTableId());
                 goToTableCaseBuilder.setGoToTable(goToTableBuilder.build());
-                
+
                 InstructionBuilder instBuilder = new InstructionBuilder();
                 instBuilder.setInstruction(goToTableCaseBuilder.build());
+                instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
+                instBuilder.setOrder(instructionTreeNodekey);
+                instructionTreeNodekey++;
+
                 salInstructionList.add(instBuilder.build());
-            }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter){
-                
+            } else if (switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter.class)) {
+
                 MeterIdInstruction meterIdInstruction = (MeterIdInstruction) switchInst.getAugmentation(MeterIdInstruction.class);
-                
+
                 InstructionBuilder instBuilder = new InstructionBuilder();
-                
+
                 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
                 MeterBuilder meterBuilder = new MeterBuilder();
                 meterBuilder.setMeterId(new MeterId(meterIdInstruction.getMeterId()));
                 meterCaseBuilder.setMeter(meterBuilder.build());
-                
+
                 instBuilder.setInstruction(meterCaseBuilder.build());
+                instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
+                instBuilder.setOrder(instructionTreeNodekey);
+                instructionTreeNodekey++;
+
                 salInstructionList.add(instBuilder.build());
-            }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions){
-                
-                ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
-                
+            } else if (switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions.class)) {
+
+                ActionsInstruction actionsInstruction = (ActionsInstruction) switchInst.getAugmentation(ActionsInstruction.class);
+
                 WriteActionsCaseBuilder writeActionsCaseBuilder = new WriteActionsCaseBuilder();
                 WriteActionsBuilder writeActionsBuilder = new WriteActionsBuilder();
-                writeActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getActionsList())));
+                writeActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getAction(), ofVersion, ActionPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION)));
                 writeActionsCaseBuilder.setWriteActions(writeActionsBuilder.build());
-                
+
                 InstructionBuilder instBuilder = new InstructionBuilder();
                 instBuilder.setInstruction(writeActionsCaseBuilder.build());
+                instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
+                instBuilder.setOrder(instructionTreeNodekey);
+                instructionTreeNodekey++;
+
                 salInstructionList.add(instBuilder.build());
-            
-            }else if(switchInst instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata){
 
-                MetadataInstruction metadataInstruction = (MetadataInstruction)switchInst.getAugmentation(MetadataInstruction.class);
-                
+            } else if (switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata.class)) {
+
+                MetadataInstruction metadataInstruction = (MetadataInstruction) switchInst.getAugmentation(MetadataInstruction.class);
+
                 WriteMetadataCaseBuilder writeMetadataCaseBuilder = new WriteMetadataCaseBuilder();
                 WriteMetadataBuilder writeMetadataBuilder = new WriteMetadataBuilder();
-                writeMetadataBuilder.setMetadata(new BigInteger(metadataInstruction.getMetadata()));
-                writeMetadataBuilder.setMetadataMask(new BigInteger(metadataInstruction.getMetadataMask()));
+                writeMetadataBuilder.setMetadata(new BigInteger(1, metadataInstruction.getMetadata()));
+                writeMetadataBuilder.setMetadataMask(new BigInteger(1, metadataInstruction.getMetadataMask()));
                 writeMetadataCaseBuilder.setWriteMetadata(writeMetadataBuilder.build());
-                
+
                 InstructionBuilder instBuilder = new InstructionBuilder();
                 instBuilder.setInstruction(writeMetadataCaseBuilder.build());
+                instBuilder.setKey(new InstructionKey(instructionTreeNodekey));
+                instBuilder.setOrder(instructionTreeNodekey);
+                instructionTreeNodekey++;
+
                 salInstructionList.add(instBuilder.build());
             }
         }
         instructionsBuilder.setInstruction(salInstructionList);
         return instructionsBuilder.build();
     }
-    
+
     /*
      * Method wrapping all the actions org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action
      * in org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action, to set appropriate keys
      * for actions. 
      */
-    private static List<Action> wrapActionList(List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> actionList){
-        List<Action> actions = new ArrayList<>(); 
-        
+    private static List<Action> wrapActionList(List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> actionList) {
+        List<Action> actions = new ArrayList<>();
+
         int actionKey = 0;
-        for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action action : actionList){
+        for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action action : actionList) {
             ActionBuilder wrappedAction = new ActionBuilder();
             wrappedAction.setAction(action);
             wrappedAction.setKey(new ActionKey(actionKey));
@@ -157,36 +176,37 @@ public class OFToMDSalFlowConvertor {
             actions.add(wrappedAction.build());
             actionKey++;
         }
-        
+
         return actions;
     }
 
     /**
      * Method wraps openflow 1.0 actions list to Apply Action Instructions
+     *
+     * @param ofVersion current ofp version
      */
-    
-    public static Instructions wrapOF10ActionsToInstruction(List<ActionsList> actionsList) {
+
+    public static Instructions wrapOF10ActionsToInstruction(
+            List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action> actionsList, OpenflowVersion ofVersion) {
         InstructionsBuilder instructionsBuilder = new InstructionsBuilder();
-        
+
         List<Instruction> salInstructionList = new ArrayList<Instruction>();
-        
+
         ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
         ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
 
-        applyActionsBuilder.setAction(
-                    wrapActionList(
-                                ActionConvertor.toMDSalActions(actionsList
-                                        )
-                            ));
-                
+        applyActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsList, ofVersion, ActionPath.FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_INSTRUCTIONS_INSTRUCTION_INSTRUCTION_WRITEACTIONSCASE_WRITEACTIONS_ACTION_ACTION)));
+
         applyActionsCaseBuilder.setApplyActions(applyActionsBuilder.build());
-                
+
         InstructionBuilder instBuilder = new InstructionBuilder();
         instBuilder.setInstruction(applyActionsCaseBuilder.build());
+        instBuilder.setKey(new InstructionKey(0));
+        instBuilder.setOrder(0);
         salInstructionList.add(instBuilder.build());
-        
+
         instructionsBuilder.setInstruction(salInstructionList);
         return instructionsBuilder.build();
     }
-    
+
 }