BUG-2794: incorporate ofjava instruction changes
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / OFToMDSalFlowConvertor.java
index 72066e8ffecc8efa468c2f7e453fa0b2e66e2223..0eb3f4b171daf542ee44c0ae6686df18e5e5e77c 100644 (file)
@@ -7,17 +7,20 @@
  */
 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
 
-import org.opendaylight.openflowplugin.openflow.md.util.OpenflowVersion;
+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;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ClearActionsCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.GoToTableCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.MeterCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteActionsCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.WriteMetadataCaseBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.clear.actions._case.ClearActionsBuilder;
@@ -29,145 +32,121 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instru
 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.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.MeterCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteActionsCase;
 import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.List;
 
-public class OFToMDSalFlowConvertor {
-    
+public final class OFToMDSalFlowConvertor {
+
+    private OFToMDSalFlowConvertor() {
+        // hiding implicite constructor
+    }
+
     /**
      * Method convert Openflow 1.3+ specific instructions to MD-SAL format
      * flow instruction
+     *
      * @param instructions
-     * @param ofVersion current ofp version
+     * @param ofVersion    current ofp version
      * @return
      */
     public static Instructions toSALInstruction(
             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>();
-        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);
+        int instructionTreeNodekey = 0;
+        org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.Instruction salInstruction;
+        for (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction switchInst : instructions) {
+            if (switchInst.getInstructionChoice() instanceof ApplyActionsCase) {
+
+                ApplyActionsCase actionsInstruction = ((ApplyActionsCase) switchInst.getInstructionChoice());
                 ApplyActionsCaseBuilder applyActionsCaseBuilder = new ApplyActionsCaseBuilder();
                 ApplyActionsBuilder applyActionsBuilder = new ApplyActionsBuilder();
-                
-                
-                applyActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getAction(), ofVersion)));
-                
+
+
+                applyActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getApplyActions().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.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions.class)){
-                InstructionBuilder instBuilder = new InstructionBuilder();
-                
+                salInstruction = applyActionsCaseBuilder.build();
+            } else if (switchInst.getInstructionChoice() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCase) {
+
+                org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCase clearActionsCase = 
+                        ((org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCase) switchInst.getInstructionChoice());
                 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.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable.class)){
-                
-                TableIdInstruction tableIdInstruction = (TableIdInstruction)switchInst.getAugmentation(TableIdInstruction.class);
-                
+                salInstruction = clearActionsCaseBuilder.build();
+            } else if (switchInst.getInstructionChoice() instanceof GotoTableCase) {
+
+                GotoTableCase gotoTableCase = ((GotoTableCase) switchInst.getInstructionChoice());
+
                 GoToTableCaseBuilder goToTableCaseBuilder = new GoToTableCaseBuilder();
                 GoToTableBuilder goToTableBuilder = new GoToTableBuilder();
-                goToTableBuilder.setTableId(tableIdInstruction.getTableId());
+                goToTableBuilder.setTableId(gotoTableCase.getGotoTable().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.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();
-                
+
+                salInstruction = goToTableCaseBuilder.build();
+            } else if (switchInst.getInstructionChoice() instanceof MeterCase) {
+
+                MeterCase meterIdInstruction = ((MeterCase) switchInst.getInstructionChoice());
+
+
                 MeterCaseBuilder meterCaseBuilder = new MeterCaseBuilder();
                 MeterBuilder meterBuilder = new MeterBuilder();
-                meterBuilder.setMeterId(new MeterId(meterIdInstruction.getMeterId()));
+                meterBuilder.setMeterId(new MeterId(meterIdInstruction.getMeter().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.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions.class)){
-                
-                ActionsInstruction actionsInstruction = (ActionsInstruction)switchInst.getAugmentation(ActionsInstruction.class);
-                
+
+                salInstruction = meterCaseBuilder.build();
+            } else if (switchInst.getInstructionChoice() instanceof WriteActionsCase) {
+
+                WriteActionsCase writeActionsCase = ((WriteActionsCase) switchInst.getInstructionChoice());
+
                 WriteActionsCaseBuilder writeActionsCaseBuilder = new WriteActionsCaseBuilder();
                 WriteActionsBuilder writeActionsBuilder = new WriteActionsBuilder();
-                writeActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(actionsInstruction.getAction(), ofVersion)));
+                writeActionsBuilder.setAction(wrapActionList(ActionConvertor.toMDSalActions(writeActionsCase.getWriteActions().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.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata.class)){
-
-                MetadataInstruction metadataInstruction = (MetadataInstruction)switchInst.getAugmentation(MetadataInstruction.class);
-                
+
+                salInstruction = writeActionsCaseBuilder.build();
+            } else if (switchInst.getInstructionChoice() instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase) {
+
+                org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase writeMetadataCase = 
+                        ((org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.WriteMetadataCase) switchInst.getInstructionChoice());
                 WriteMetadataCaseBuilder writeMetadataCaseBuilder = new WriteMetadataCaseBuilder();
                 WriteMetadataBuilder writeMetadataBuilder = new WriteMetadataBuilder();
-                writeMetadataBuilder.setMetadata(new BigInteger(1, metadataInstruction.getMetadata()));
-                writeMetadataBuilder.setMetadataMask(new BigInteger(1, metadataInstruction.getMetadataMask()));
+                writeMetadataBuilder.setMetadata(new BigInteger(writeMetadataCase.getWriteMetadata().getMetadata()));
+                writeMetadataBuilder.setMetadataMask(new BigInteger(writeMetadataCase.getWriteMetadata().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());
+                salInstruction = writeMetadataCaseBuilder.build();
+            } else {
+                continue;
             }
+
+            InstructionBuilder instBuilder = new InstructionBuilder();
+            instBuilder.setInstruction(salInstruction);
+            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));
@@ -175,36 +154,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<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action> actionsList, OpenflowVersion ofVersion) {
+            List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.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, ofVersion)));
-                
+        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();
     }
-    
+
 }