From 8dbddef7a44dc651fcec2c47e7c1e67b6078acb8 Mon Sep 17 00:00:00 2001 From: Michal Polkorab Date: Thu, 16 Jan 2014 13:16:36 +0100 Subject: [PATCH] Instructions fix - Write/Apply actions instructions now support sending drop action Signed-off-by: Michal Polkorab --- .../impl/util/InstructionsSerializer.java | 36 +++++++++++-------- .../FlowModInputMessageFactoryTest.java | 11 +++++- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/InstructionsSerializer.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/InstructionsSerializer.java index be90d891..80ce60d7 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/InstructionsSerializer.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/util/InstructionsSerializer.java @@ -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 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 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)) { diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactoryTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactoryTest.java index c1aa119d..0785e108 100644 --- a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactoryTest.java +++ b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/FlowModInputMessageFactoryTest.java @@ -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){ -- 2.36.6