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