7e78f561c02dd660e43e7c1fcfeb1e894fbd27bf
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / PacketOutInputMessageFactoryTest.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.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeAction;\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeActionBuilder;\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopVlan;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushVlan;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsListBuilder;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.actions.list.ActionBuilder;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;\r
26 \r
27 /**\r
28  * @author timotej.kubas\r
29  *\r
30  */\r
31 public class PacketOutInputMessageFactoryTest {\r
32     private static final byte MESSAGE_TYPE = 13;\r
33     private static final byte PADDING_IN_PACKET_OUT_MESSAGE = 6;\r
34     private static final int PADDING_IN_ACTION_HEADER = 4;\r
35        \r
36     /**\r
37      * Testing of {@link PacketOutInputMessageFactory} for correct translation from POJO\r
38      * @throws Exception \r
39      */\r
40     @Test\r
41     public void testPacketOutInputMessage() throws Exception {\r
42         PacketOutInputBuilder builder = new PacketOutInputBuilder();\r
43         BufferHelper.setupHeader(builder);\r
44         builder.setBufferId(256L);\r
45         builder.setInPort(new PortNumber(256L));\r
46         List<ActionsList> actions = new ArrayList<>();\r
47         ActionsListBuilder actionsListBuilder = new ActionsListBuilder();\r
48         ActionBuilder actionBuilder = new ActionBuilder();\r
49         actionBuilder.setType(PushVlan.class);\r
50         EthertypeActionBuilder ethertypeBuilder = new EthertypeActionBuilder();\r
51         ethertypeBuilder.setEthertype(new EtherType(25));\r
52         actionBuilder.addAugmentation(EthertypeAction.class, ethertypeBuilder.build());\r
53         actionsListBuilder.setAction(actionBuilder.build());\r
54         actions.add(actionsListBuilder.build());\r
55         actionBuilder = new ActionBuilder();\r
56         actionBuilder.setType(PopVlan.class);\r
57         actionsListBuilder.setAction(actionBuilder.build());\r
58         actions.add(actionsListBuilder.build());\r
59         builder.setActionsList(actions);\r
60         builder.setData(ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"));\r
61         PacketOutInput message = builder.build();\r
62         \r
63         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
64         PacketOutInputMessageFactory factory = PacketOutInputMessageFactory.getInstance();\r
65         factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message);\r
66         \r
67         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 56);\r
68         Assert.assertEquals("Wrong BufferId", message.getBufferId().longValue(), out.readUnsignedInt());\r
69         Assert.assertEquals("Wrong PortNumber", message.getInPort().getValue().longValue(), out.readUnsignedInt());\r
70         Assert.assertEquals("Wrong ActionsLength", 16, out.readUnsignedShort());\r
71         out.skipBytes(PADDING_IN_PACKET_OUT_MESSAGE);\r
72         Assert.assertEquals("Wrong action type", 17, out.readUnsignedShort());\r
73         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
74         Assert.assertEquals("Wrong ethertype", 25, out.readUnsignedShort());\r
75         out.skipBytes(Short.SIZE / Byte.SIZE);\r
76         Assert.assertEquals("Wrong action type", 18, out.readUnsignedShort());\r
77         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());\r
78         out.skipBytes(PADDING_IN_ACTION_HEADER);\r
79         Assert.assertArrayEquals("Wrong data", message.getData(), out.readBytes(out.readableBytes()).array());\r
80     }\r
81 }\r