Instructions quickfix + tests
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / InstructionsSerializerTest.java
diff --git a/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/InstructionsSerializerTest.java b/openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/util/InstructionsSerializerTest.java
new file mode 100644 (file)
index 0000000..7a90123
--- /dev/null
@@ -0,0 +1,191 @@
+/*\r
+ * Copyright (c) 2013 Pantheon Technologies s.r.o. and others.  All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
+ */\r
+package org.opendaylight.openflowjava.protocol.impl.util;\r
+\r
+import io.netty.buffer.ByteBuf;\r
+import io.netty.buffer.UnpooledByteBufAllocator;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+\r
+\r
+\r
+import org.junit.Assert;\r
+import org.junit.Test;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstruction;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ActionsInstructionBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeAction;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeActionBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstructionBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstructionBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTtlAction;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTtlActionBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstructionBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopPbb;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushVlan;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwTtl;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsListBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.actions.list.ActionBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.InstructionsBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;\r
+\r
+/**\r
+ * @author michal.polkorab\r
+ *\r
+ */\r
+public class InstructionsSerializerTest {\r
+\r
+    /**\r
+     * Testing instructions translation\r
+     */\r
+    @Test\r
+    public void test() {\r
+        List<Instructions> instructions = new ArrayList<>();\r
+        // Goto_table instruction\r
+        InstructionsBuilder builder = new InstructionsBuilder();\r
+        builder.setType(GotoTable.class);\r
+        TableIdInstructionBuilder tableIdBuilder = new TableIdInstructionBuilder();\r
+        tableIdBuilder.setTableId((short) 5);\r
+        builder.addAugmentation(TableIdInstruction.class, tableIdBuilder.build());\r
+        instructions.add(builder.build());\r
+        builder = new InstructionsBuilder();\r
+        // Write_metadata instruction\r
+        builder.setType(WriteMetadata.class);\r
+        MetadataInstructionBuilder metaBuilder = new MetadataInstructionBuilder();\r
+        metaBuilder.setMetadata(ByteBufUtils.hexStringToBytes("00 01 02 03 04 05 06 07"));\r
+        metaBuilder.setMetadataMask(ByteBufUtils.hexStringToBytes("07 06 05 04 03 02 01 00"));\r
+        builder.addAugmentation(MetadataInstruction.class, metaBuilder.build());\r
+        instructions.add(builder.build());\r
+        // Clear_actions instruction\r
+        builder = new InstructionsBuilder();\r
+        builder.setType(ClearActions.class);\r
+        instructions.add(builder.build());\r
+        // Meter instruction\r
+        builder = new InstructionsBuilder();\r
+        builder.setType(Meter.class);\r
+        MeterIdInstructionBuilder meterBuilder = new MeterIdInstructionBuilder();\r
+        meterBuilder.setMeterId(42L);\r
+        builder.addAugmentation(MeterIdInstruction.class, meterBuilder.build());\r
+        instructions.add(builder.build());\r
+        // Write_actions instruction\r
+        builder = new InstructionsBuilder();\r
+        builder.setType(WriteActions.class);\r
+        ActionsInstructionBuilder actionsBuilder = new ActionsInstructionBuilder();\r
+        List<ActionsList> actions = new ArrayList<>();\r
+        ActionsListBuilder listBuilder = new ActionsListBuilder();\r
+        ActionBuilder actBuilder = new ActionBuilder();\r
+        actBuilder.setType(Output.class);\r
+        PortActionBuilder portBuilder = new PortActionBuilder();\r
+        portBuilder.setPort(new PortNumber(45L));\r
+        actBuilder.addAugmentation(PortAction.class, portBuilder.build());\r
+        MaxLengthActionBuilder maxBuilder = new MaxLengthActionBuilder();\r
+        maxBuilder.setMaxLength(55);\r
+        actBuilder.addAugmentation(MaxLengthAction.class, maxBuilder.build());\r
+        listBuilder.setAction(actBuilder.build());\r
+        actions.add(listBuilder.build());\r
+        listBuilder = new ActionsListBuilder();\r
+        actBuilder = new ActionBuilder();\r
+        actBuilder.setType(SetNwTtl.class);\r
+        NwTtlActionBuilder nwTtl = new NwTtlActionBuilder();\r
+        nwTtl.setNwTtl((short) 64);\r
+        actBuilder.addAugmentation(NwTtlAction.class, nwTtl.build());\r
+        listBuilder.setAction(actBuilder.build());\r
+        actions.add(listBuilder.build());\r
+        actionsBuilder.setActionsList(actions);\r
+        builder.addAugmentation(ActionsInstruction.class, actionsBuilder.build());\r
+        instructions.add(builder.build());\r
+        // Apply_actions instruction\r
+        builder = new InstructionsBuilder();\r
+        builder.setType(ApplyActions.class);\r
+        actionsBuilder = new ActionsInstructionBuilder();\r
+        actions = new ArrayList<>();\r
+        listBuilder = new ActionsListBuilder();\r
+        actBuilder = new ActionBuilder();\r
+        actBuilder.setType(PushVlan.class);\r
+        EthertypeActionBuilder ethertypeBuilder = new EthertypeActionBuilder();\r
+        ethertypeBuilder.setEthertype(new EtherType(14));\r
+        actBuilder.addAugmentation(EthertypeAction.class, ethertypeBuilder.build());\r
+        listBuilder.setAction(actBuilder.build());\r
+        actions.add(listBuilder.build());\r
+        listBuilder = new ActionsListBuilder();\r
+        actBuilder = new ActionBuilder();\r
+        actBuilder.setType(PopPbb.class);\r
+        listBuilder.setAction(actBuilder.build());\r
+        actions.add(listBuilder.build());\r
+        actionsBuilder.setActionsList(actions);\r
+        builder.addAugmentation(ActionsInstruction.class, actionsBuilder.build());\r
+        instructions.add(builder.build());\r
+        \r
+        ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
+        InstructionsSerializer.encodeInstructions(instructions, out);\r
+        \r
+        Assert.assertEquals("Wrong instruction type", 1, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong instruction table-id", 5, out.readUnsignedByte());\r
+        out.skipBytes(3);\r
+        Assert.assertEquals("Wrong instruction type", 2, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong instruction length", 24, out.readUnsignedShort());\r
+        out.skipBytes(4);\r
+        byte[] actual = new byte[8];\r
+        out.readBytes(actual);\r
+        Assert.assertEquals("Wrong instruction metadata", "00 01 02 03 04 05 06 07",\r
+                ByteBufUtils.bytesToHexString(actual));\r
+        actual = new byte[8];\r
+        out.readBytes(actual);\r
+        Assert.assertEquals("Wrong instruction metadata-mask", "07 06 05 04 03 02 01 00",\r
+                ByteBufUtils.bytesToHexString(actual));\r
+        Assert.assertEquals("Wrong instruction type", 5, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort());\r
+        out.skipBytes(4);\r
+        Assert.assertEquals("Wrong instruction type", 6, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong instruction length", 8, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong instruction meter-id", 42, out.readUnsignedInt());\r
+        Assert.assertEquals("Wrong instruction type", 3, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong instruction length", 32, out.readUnsignedShort());\r
+        out.skipBytes(4);\r
+        Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong action length", 16, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong action type", 45, out.readUnsignedInt());\r
+        Assert.assertEquals("Wrong action type", 55, out.readUnsignedShort());\r
+        out.skipBytes(6);\r
+        Assert.assertEquals("Wrong action type", 23, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong action type", 64, out.readUnsignedByte());\r
+        out.skipBytes(3);\r
+        Assert.assertEquals("Wrong instruction type", 4, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong instruction length", 24, out.readUnsignedShort());\r
+        out.skipBytes(4);\r
+        Assert.assertEquals("Wrong action type", 17, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong action ethertype", 14, out.readUnsignedShort());\r
+        out.skipBytes(2);\r
+        Assert.assertEquals("Wrong action type", 27, out.readUnsignedShort());\r
+        Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
+        out.skipBytes(4);\r
+        Assert.assertTrue("Not all data were read", out.readableBytes() == 0);\r
+    }\r
+\r
+}\r