Merge "OF1.0 fixes"
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / OF10PacketOutInputMessageFactoryTest.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;\r
3 \r
4 import io.netty.buffer.ByteBuf;\r
5 import io.netty.buffer.UnpooledByteBufAllocator;\r
6 \r
7 import java.util.ArrayList;\r
8 import java.util.List;\r
9 \r
10 import org.junit.Assert;\r
11 import org.junit.Test;\r
12 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest;\r
13 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;\r
14 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
15 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.StripVlan;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsListBuilder;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.actions.list.ActionBuilder;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;\r
28 \r
29 /**\r
30  * @author michal.polkorab\r
31  *\r
32  */\r
33 public class OF10PacketOutInputMessageFactoryTest {\r
34 \r
35     /**\r
36      * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO\r
37      * @throws Exception \r
38      */\r
39     @Test\r
40     public void testPacketOutInputMessage() throws Exception {\r
41         PacketOutInputBuilder builder = new PacketOutInputBuilder();\r
42         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);\r
43         builder.setBufferId(256L);\r
44         builder.setInPort(new PortNumber(257L));\r
45         List<ActionsList> actions = new ArrayList<>();\r
46         ActionsListBuilder actionsListBuilder = new ActionsListBuilder();\r
47         ActionBuilder actionBuilder = new ActionBuilder();\r
48         actionBuilder.setType(Output.class);\r
49         PortActionBuilder portBuilder = new PortActionBuilder();\r
50         portBuilder.setPort(new PortNumber((long) 42));\r
51         actionBuilder.addAugmentation(PortAction.class, portBuilder.build());\r
52         MaxLengthActionBuilder maxLen = new MaxLengthActionBuilder();\r
53         maxLen.setMaxLength(50);\r
54         actionBuilder.addAugmentation(MaxLengthAction.class, maxLen.build());\r
55         actionsListBuilder.setAction(actionBuilder.build());\r
56         actions.add(actionsListBuilder.build());\r
57         actionsListBuilder = new ActionsListBuilder();\r
58         actionBuilder = new ActionBuilder();\r
59         actionBuilder.setType(StripVlan.class);\r
60         actionsListBuilder.setAction(actionBuilder.build());\r
61         builder.setActionsList(actions);\r
62         actions.add(actionsListBuilder.build());\r
63         builder.setActionsList(actions);\r
64         builder.setData(ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"));\r
65         PacketOutInput message = builder.build();\r
66         \r
67         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
68         OF10PacketOutInputMessageFactory factory = OF10PacketOutInputMessageFactory.getInstance();\r
69         factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message);\r
70         \r
71         BufferHelper.checkHeaderV10(out, (byte) 13, 48);\r
72         Assert.assertEquals("Wrong BufferId", 256, out.readUnsignedInt());\r
73         Assert.assertEquals("Wrong PortNumber", 257, out.readUnsignedShort());\r
74         Assert.assertEquals("Wrong actions length", 16, out.readUnsignedShort());\r
75         Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());\r
76         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
77         Assert.assertEquals("Wrong port", 42, out.readUnsignedShort());\r
78         Assert.assertEquals("Wrong maxlength", 50, out.readUnsignedShort());\r
79         Assert.assertEquals("Wrong action type", 3, out.readUnsignedShort());\r
80         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
81         out.skipBytes(4);\r
82         Assert.assertArrayEquals("Wrong data", message.getData(), out.readBytes(out.readableBytes()).array());\r
83         Assert.assertTrue("Unread data", out.readableBytes() == 0);\r
84     }\r
85 \r
86 }\r