Bug 1254 - added basic conversion test for OFToMDSalFlowConvertor 67/11367/3
authorMartin Bobak <mbobak@cisco.com>
Thu, 18 Sep 2014 20:14:30 +0000 (22:14 +0200)
committermichal rehak <mirehak@cisco.com>
Fri, 19 Sep 2014 18:42:25 +0000 (18:42 +0000)
Change-Id: If82e5d0c2d01d8fff89882dad5df9c87ce1baa80
Signed-off-by: Martin Bobak <mbobak@cisco.com>
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/OFToMDSalFlowConvertor.java
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/OFToMDSalFlowConvertorTest.java [new file with mode: 0644]

index 1723399e9bd2b30360decfd0dbcd0edd12c1363a..14cffba4f6449dc608ee2324334fa4d4c562a26a 100644 (file)
@@ -10,9 +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.extension.api.path.ActionPath;
 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;
@@ -40,61 +39,62 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
 
 public class OFToMDSalFlowConvertor {
-    
+
     /**
      * 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;
+        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.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)){
+            } 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.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable.class)){
-                
-                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));
@@ -102,32 +102,32 @@ public class OFToMDSalFlowConvertor {
                 instructionTreeNodekey++;
 
                 salInstructionList.add(instBuilder.build());
-            }else if(switchInst.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter.class)){
-                
+            } 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.getType().equals(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions.class)){
-                
-                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.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));
@@ -135,17 +135,17 @@ public class OFToMDSalFlowConvertor {
                 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);
-                
+            } 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(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));
@@ -158,17 +158,17 @@ public class OFToMDSalFlowConvertor {
         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));
@@ -176,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<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, 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();
     }
-    
+
 }
diff --git a/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/OFToMDSalFlowConvertorTest.java b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/OFToMDSalFlowConvertorTest.java
new file mode 100644 (file)
index 0000000..5a52267
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.Test;
+import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstructionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstructionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstructionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstructionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ActionBase;
+
+/**
+ * Created by Martin Bobak mbobak@cisco.com on 9/18/14.
+ */
+public class OFToMDSalFlowConvertorTest {
+
+    private static final int PRESET_COUNT = 7;
+
+    @Test
+    /**
+     * Test method for {@link OFToMDSalFlowConvertor#wrapOF10ActionsToInstruction(java.util.List, org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion)}
+     */
+    public void testWrapOF10ActionsToInstruction() {
+        ActionBuilder actionBuilder = new ActionBuilder();
+        List<Action> actions = new ArrayList<>();
+        for (int j = 0; j < PRESET_COUNT; j++) {
+            actionBuilder.setType(MockActionBase.class);
+            actions.add(actionBuilder.build());
+        }
+        Instructions instructions = OFToMDSalFlowConvertor.wrapOF10ActionsToInstruction(actions, OpenflowVersion.OF13);
+        assertNotNull(instructions);
+    }
+        @Test
+    /**
+     * Test method for {@link OFToMDSalFlowConvertor#toSALInstruction(java.util.List, org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion)}
+     */
+    public void testToSALInstruction() {
+        List<Instruction> instructionsList = new ArrayList<>();
+        InstructionBuilder instructionBuilder = new InstructionBuilder();
+        for (int i = 0; i < PRESET_COUNT; i++) {
+            instructionBuilder.setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions.class);
+            ActionsInstructionBuilder actionsInstructionBuilder = new ActionsInstructionBuilder();
+            ActionBuilder actionBuilder = new ActionBuilder();
+            List<Action> actions = new ArrayList<>();
+            for (int j = 0; j < PRESET_COUNT; j++) {
+                actionBuilder.setType(MockActionBase.class);
+                actions.add(actionBuilder.build());
+            }
+            actionsInstructionBuilder.setAction(actions);
+            instructionBuilder.addAugmentation(ActionsInstruction.class, actionsInstructionBuilder.build());
+            instructionsList.add(instructionBuilder.build());
+        }
+
+        Instructions instructions = OFToMDSalFlowConvertor.toSALInstruction(instructionsList, OpenflowVersion.OF13);
+        assertNotNull(instructions);
+        assertEquals(PRESET_COUNT, instructions.getInstruction().size());
+
+        instructionsList = new ArrayList<>();
+        for (int i = 0; i < PRESET_COUNT; i++) {
+            instructionBuilder.setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions.class);
+            TableIdInstructionBuilder tableIdInstructionBuilder = new TableIdInstructionBuilder();
+            tableIdInstructionBuilder.setTableId((short) i);
+            instructionBuilder.addAugmentation(TableIdInstruction.class, tableIdInstructionBuilder.build());
+            instructionsList.add(instructionBuilder.build());
+        }
+
+        instructions = OFToMDSalFlowConvertor.toSALInstruction(instructionsList, OpenflowVersion.OF13);
+        assertNotNull(instructions);
+        assertEquals(PRESET_COUNT, instructions.getInstruction().size());
+
+        instructionsList = new ArrayList<>();
+        for (int i = 0; i < PRESET_COUNT; i++) {
+            instructionBuilder.setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions.class);
+            MeterIdInstructionBuilder meterIdInstructionBuilder = new MeterIdInstructionBuilder();
+            meterIdInstructionBuilder.setMeterId(Long.valueOf(i));
+            instructionBuilder.addAugmentation(MeterIdInstruction.class, meterIdInstructionBuilder.build());
+            instructionsList.add(instructionBuilder.build());
+        }
+
+        instructions = OFToMDSalFlowConvertor.toSALInstruction(instructionsList, OpenflowVersion.OF13);
+        assertNotNull(instructions);
+        assertEquals(PRESET_COUNT, instructions.getInstruction().size());
+
+
+        instructionsList = new ArrayList<>();
+        for (int i = 0; i < PRESET_COUNT; i++) {
+            instructionBuilder.setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions.class);
+            ActionsInstructionBuilder actionsInstructionBuilder = new ActionsInstructionBuilder();
+            ActionBuilder actionBuilder = new ActionBuilder();
+            List<Action> actions = new ArrayList<>();
+            for (int j = 0; j < PRESET_COUNT; j++) {
+                actionBuilder.setType(MockActionBase.class);
+                actions.add(actionBuilder.build());
+            }
+            actionsInstructionBuilder.setAction(actions);
+            instructionBuilder.addAugmentation(ActionsInstruction.class, actionsInstructionBuilder.build());
+            instructionsList.add(instructionBuilder.build());
+        }
+
+        instructions = OFToMDSalFlowConvertor.toSALInstruction(instructionsList, OpenflowVersion.OF13);
+        assertNotNull(instructions);
+        assertEquals(PRESET_COUNT, instructions.getInstruction().size());
+
+
+        instructionsList = new ArrayList<>();
+        for (int i = 0; i < PRESET_COUNT; i++) {
+            instructionBuilder.setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions.class);
+            MetadataInstructionBuilder metadataInstructionBuilder = new MetadataInstructionBuilder();
+            metadataInstructionBuilder.setMetadata(new byte[i]);
+            metadataInstructionBuilder.setMetadataMask(new byte[i]);
+            instructionBuilder.addAugmentation(MetadataInstruction.class, metadataInstructionBuilder.build());
+            instructionsList.add(instructionBuilder.build());
+        }
+
+        instructions = OFToMDSalFlowConvertor.toSALInstruction(instructionsList, OpenflowVersion.OF13);
+        assertNotNull(instructions);
+        assertEquals(PRESET_COUNT, instructions.getInstruction().size());
+
+
+    }
+
+    private static final class MockActionBase extends ActionBase {
+
+    }
+
+}