Instructions fix - Write/Apply actions instructions now support sending drop action 03/4303/1
authorMichal Polkorab <michal.polkorab@pantheon.sk>
Thu, 16 Jan 2014 12:16:36 +0000 (13:16 +0100)
committerMichal Polkorab <michal.polkorab@pantheon.sk>
Thu, 16 Jan 2014 12:16:59 +0000 (13:16 +0100)
Signed-off-by: Michal Polkorab <michal.polkorab@pantheon.sk>
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/InstructionsSerializer.java
openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactoryTest.java

index be90d891142335745fe427afd8bf469033d73fe1..80ce60d74662c286c89b83381376927275f94960 100644 (file)
@@ -44,15 +44,14 @@ public abstract class InstructionsSerializer {
     private static final byte EXPERIMENTER_TYPE = 7;
     private static final byte GOTO_TABLE_LENGTH = 8;
     private static final byte WRITE_METADATA_LENGTH = 24;
-    private static final byte WRITE_ACTIONS_LENGTH = 8;
-    private static final byte APPLY_ACTIONS_LENGTH = 8;
-    private static final byte CLEAR_ACTIONS_LENGTH = 8;
     private static final byte METER_LENGTH = 8;
     private static final byte EXPERIMENTER_LENGTH = 8;
+    private static final byte ACTIONS_INSTRUCTION_LENGTH = 8;
     private static final byte PADDING_IN_GOTO_TABLE = 3;
     private static final byte PADDING_IN_WRITE_METADATA = 4;
     private static final byte PADDING_IN_CLEAR_ACTIONS = 4;
     private static final byte INSTRUCTION_IDS_LENGTH = 4;
+    private static final byte PADDING_IN_ACTIONS_INSTRUCTION = 4;
     
     /**
      * Encodes instructions
@@ -78,7 +77,7 @@ public abstract class InstructionsSerializer {
                 } else if (type.isAssignableFrom(ApplyActions.class)) {
                     writeActionsInstruction(out, instruction, APPLY_ACTIONS_TYPE);
                 } else if (type.isAssignableFrom(ClearActions.class)) {
-                    writeTypeAndLength(out, CLEAR_ACTIONS_TYPE, CLEAR_ACTIONS_LENGTH);
+                    writeTypeAndLength(out, CLEAR_ACTIONS_TYPE, ACTIONS_INSTRUCTION_LENGTH);
                     ByteBufUtils.padBuffer(PADDING_IN_CLEAR_ACTIONS, out);
                 } else if (type.isAssignableFrom(Meter.class)) {
                     writeTypeAndLength(out, METER_TYPE, METER_LENGTH);
@@ -132,13 +131,16 @@ public abstract class InstructionsSerializer {
 
     private static void writeActionsInstruction(ByteBuf out,
             Instructions instruction, int type) {
-        final byte ACTIONS_INSTRUCTION_LENGTH = 8;
-        final byte PADDING_IN_ACTIONS_INSTRUCTION = 4;
         out.writeShort(type);
-        List<ActionsList> actions = instruction.getAugmentation(ActionsInstruction.class).getActionsList();
-        out.writeShort(ACTIONS_INSTRUCTION_LENGTH + ActionsSerializer.computeLengthOfActions(actions));
-        ByteBufUtils.padBuffer(PADDING_IN_ACTIONS_INSTRUCTION, out);
-        ActionsSerializer.encodeActions(actions, out);
+        if (instruction.getAugmentation(ActionsInstruction.class) != null) {
+            List<ActionsList> actions = instruction.getAugmentation(ActionsInstruction.class).getActionsList();
+            out.writeShort(ACTIONS_INSTRUCTION_LENGTH + ActionsSerializer.computeLengthOfActions(actions));
+            ByteBufUtils.padBuffer(PADDING_IN_ACTIONS_INSTRUCTION, out);
+            ActionsSerializer.encodeActions(actions, out);
+        } else {
+            out.writeShort(ACTIONS_INSTRUCTION_LENGTH);
+            ByteBufUtils.padBuffer(PADDING_IN_ACTIONS_INSTRUCTION, out);
+        }
     }
     
     /**
@@ -156,13 +158,19 @@ public abstract class InstructionsSerializer {
                 } else if (type.isAssignableFrom(WriteMetadata.class)) {
                     length += WRITE_METADATA_LENGTH;
                 } else if (type.isAssignableFrom(WriteActions.class)) {
-                    length += WRITE_ACTIONS_LENGTH + ActionsSerializer.computeLengthOfActions(
+                    length += ACTIONS_INSTRUCTION_LENGTH;
+                    if (instruction.getAugmentation(ActionsInstruction.class) != null) {
+                        length += ActionsSerializer.computeLengthOfActions(
                             instruction.getAugmentation(ActionsInstruction.class).getActionsList());
+                    }
                 } else if (type.isAssignableFrom(ApplyActions.class)) {
-                    length += APPLY_ACTIONS_LENGTH + ActionsSerializer.computeLengthOfActions(
-                            instruction.getAugmentation(ActionsInstruction.class).getActionsList());
+                    length += ACTIONS_INSTRUCTION_LENGTH;
+                    if (instruction.getAugmentation(ActionsInstruction.class) != null) {
+                        length += ActionsSerializer.computeLengthOfActions(
+                                instruction.getAugmentation(ActionsInstruction.class).getActionsList());
+                    }
                 } else if (type.isAssignableFrom(ClearActions.class)) {
-                    length += CLEAR_ACTIONS_LENGTH;
+                    length += ACTIONS_INSTRUCTION_LENGTH;
                 } else if (type.isAssignableFrom(Meter.class)) {
                     length += METER_LENGTH;
                 } else if (type.isAssignableFrom(Experimenter.class)) {
index c1aa119d9144be5b40c0382b9eefb3f07ab750e1..0785e1089977bdcb90a3a8a0f1cfe1aba4aebf37 100644 (file)
@@ -28,6 +28,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortNumberMatchEntryBuilder;
 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.instruction.rev130731.ApplyActions;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions;
@@ -108,6 +109,10 @@ public class FlowModInputMessageFactoryTest {
         metaBuilder.setMetadataMask(cookieMask);
         insBuilder.addAugmentation(MetadataInstruction.class, metaBuilder.build());
         instructions.add(insBuilder.build());
+        insBuilder = new InstructionsBuilder();
+        insBuilder.setType(ApplyActions.class);
+        insBuilder.addAugmentation(MetadataInstruction.class, metaBuilder.build());
+        instructions.add(insBuilder.build());
         builder.setInstructions(instructions);
         FlowModInput message = builder.build();
         
@@ -159,7 +164,11 @@ public class FlowModInputMessageFactoryTest {
         byte[] cookieMaskRead = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
         out.readBytes(cookieMaskRead);
         Assert.assertArrayEquals("Wrong metadata", cookie, cookieRead);
-        Assert.assertArrayEquals("Wrong metadata mask", cookieMask, cookieMaskRead); 
+        Assert.assertArrayEquals("Wrong metadata mask", cookieMask, cookieMaskRead);
+        Assert.assertEquals("Wrong instruction type", 4, out.readUnsignedShort());
+        Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort());
+        out.skipBytes(4);
+        Assert.assertTrue("Unread data", out.readableBytes() == 0);
     }
     
     private static FlowModFlags createFlowModFlagsFromBitmap(int input){