Bug 2756 - Match model update
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10PacketOutInputMessageFactoryTest.java
index bc3fa472bd40f2f36784ff32279c59d8c46c500c..01fe88711751b3e28bc434d70fd3248095515cb2 100644 (file)
-/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
-package org.opendaylight.openflowjava.protocol.impl.serialization.factories;\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
-import org.junit.Assert;\r
-import org.junit.Test;\r
-import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest;\r
-import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;\r
-import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
-import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;\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.PortAction;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;\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.StripVlan;\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.types.rev130731.PortNumber;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;\r
-\r
-/**\r
- * @author michal.polkorab\r
- *\r
- */\r
-public class OF10PacketOutInputMessageFactoryTest {\r
-\r
-    /**\r
-     * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO\r
-     * @throws Exception \r
-     */\r
-    @Test\r
-    public void testPacketOutInputMessage() throws Exception {\r
-        PacketOutInputBuilder builder = new PacketOutInputBuilder();\r
-        BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);\r
-        builder.setBufferId(256L);\r
-        builder.setInPort(new PortNumber(257L));\r
-        List<ActionsList> actions = new ArrayList<>();\r
-        ActionsListBuilder actionsListBuilder = new ActionsListBuilder();\r
-        ActionBuilder actionBuilder = new ActionBuilder();\r
-        actionBuilder.setType(Output.class);\r
-        PortActionBuilder portBuilder = new PortActionBuilder();\r
-        portBuilder.setPort(new PortNumber((long) 42));\r
-        actionBuilder.addAugmentation(PortAction.class, portBuilder.build());\r
-        MaxLengthActionBuilder maxLen = new MaxLengthActionBuilder();\r
-        maxLen.setMaxLength(50);\r
-        actionBuilder.addAugmentation(MaxLengthAction.class, maxLen.build());\r
-        actionsListBuilder.setAction(actionBuilder.build());\r
-        actions.add(actionsListBuilder.build());\r
-        actionsListBuilder = new ActionsListBuilder();\r
-        actionBuilder = new ActionBuilder();\r
-        actionBuilder.setType(StripVlan.class);\r
-        actionsListBuilder.setAction(actionBuilder.build());\r
-        builder.setActionsList(actions);\r
-        actions.add(actionsListBuilder.build());\r
-        builder.setActionsList(actions);\r
-        builder.setData(ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"));\r
-        PacketOutInput message = builder.build();\r
-        \r
-        ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
-        OF10PacketOutInputMessageFactory factory = OF10PacketOutInputMessageFactory.getInstance();\r
-        factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message);\r
-        \r
-        BufferHelper.checkHeaderV10(out, (byte) 13, 48);\r
-        Assert.assertEquals("Wrong BufferId", 256, out.readUnsignedInt());\r
-        Assert.assertEquals("Wrong PortNumber", 257, out.readUnsignedShort());\r
-        Assert.assertEquals("Wrong actions length", 16, out.readUnsignedShort());\r
-        Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());\r
-        Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
-        Assert.assertEquals("Wrong port", 42, out.readUnsignedShort());\r
-        Assert.assertEquals("Wrong maxlength", 50, out.readUnsignedShort());\r
-        Assert.assertEquals("Wrong action type", 3, out.readUnsignedShort());\r
-        Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
-        out.skipBytes(4);\r
-        Assert.assertArrayEquals("Wrong data", message.getData(), out.readBytes(out.readableBytes()).array());\r
-        Assert.assertTrue("Unread data", out.readableBytes() == 0);\r
-    }\r
-\r
-}\r
+/*
+ * Copyright (c) 2013 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.serialization.factories;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.UnpooledByteBufAllocator;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
+import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
+import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
+import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
+import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
+import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
+import org.opendaylight.openflowjava.util.ByteBufUtils;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MaxLengthAction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MaxLengthActionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.PortAction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.PortActionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.StripVlan;
+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.types.rev130731.PortNumber;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
+
+/**
+ * @author michal.polkorab
+ *
+ */
+public class OF10PacketOutInputMessageFactoryTest {
+
+    private SerializerRegistry registry;
+    private OFSerializer<PacketOutInput> packetOutFactory;
+
+    /**
+     * Initializes serializer registry and stores correct factory in field
+     */
+    @Before
+    public void startUp() {
+        registry = new SerializerRegistryImpl();
+        registry.init();
+        packetOutFactory = registry.getSerializer(
+                new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, PacketOutInput.class));
+    }
+
+    /**
+     * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO
+     * @throws Exception
+     */
+    @Test
+    public void testPacketOutInputMessage() throws Exception {
+        PacketOutInputBuilder builder = new PacketOutInputBuilder();
+        BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
+        builder.setBufferId(256L);
+        builder.setInPort(new PortNumber(257L));
+        List<Action> actions = new ArrayList<>();
+        ActionBuilder actionBuilder = new ActionBuilder();
+        actionBuilder.setType(Output.class);
+        PortActionBuilder portBuilder = new PortActionBuilder();
+        portBuilder.setPort(new PortNumber((long) 42));
+        actionBuilder.addAugmentation(PortAction.class, portBuilder.build());
+        MaxLengthActionBuilder maxLen = new MaxLengthActionBuilder();
+        maxLen.setMaxLength(50);
+        actionBuilder.addAugmentation(MaxLengthAction.class, maxLen.build());
+        actions.add(actionBuilder.build());
+        actionBuilder = new ActionBuilder();
+        actionBuilder.setType(StripVlan.class);
+        builder.setAction(actions);
+        actions.add(actionBuilder.build());
+        builder.setAction(actions);
+        builder.setData(ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"));
+        PacketOutInput message = builder.build();
+
+        ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
+        packetOutFactory.serialize(message, out);
+
+        BufferHelper.checkHeaderV10(out, (byte) 13, 48);
+        Assert.assertEquals("Wrong BufferId", 256, out.readUnsignedInt());
+        Assert.assertEquals("Wrong PortNumber", 257, out.readUnsignedShort());
+        Assert.assertEquals("Wrong actions length", 16, out.readUnsignedShort());
+        Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
+        Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
+        Assert.assertEquals("Wrong port", 42, out.readUnsignedShort());
+        Assert.assertEquals("Wrong maxlength", 50, out.readUnsignedShort());
+        Assert.assertEquals("Wrong action type", 3, out.readUnsignedShort());
+        Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
+        out.skipBytes(4);
+        Assert.assertArrayEquals("Wrong data", message.getData(), out.readBytes(out.readableBytes()).array());
+        Assert.assertTrue("Unread data", out.readableBytes() == 0);
+    }
+
+    /**
+     * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO
+     * @throws Exception
+     */
+    @Test
+    public void testPacketOutInputWithNoData() throws Exception {
+        PacketOutInputBuilder builder = new PacketOutInputBuilder();
+        BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
+        builder.setBufferId(256L);
+        builder.setInPort(new PortNumber(257L));
+        List<Action> actions = new ArrayList<>();
+        builder.setAction(actions);
+        builder.setData(null);
+        PacketOutInput message = builder.build();
+
+        ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
+        packetOutFactory.serialize(message, out);
+
+        BufferHelper.checkHeaderV10(out, (byte) 13, 16);
+        out.skipBytes(8); // skip packet out message to data index
+        Assert.assertTrue("Unread data", out.readableBytes() == 0);
+    }
+}
\ No newline at end of file