Minor model refactor
[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.grouping.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
34
35 /**
36  * @author michal.polkorab
37  *
38  */
39 public class OF10PacketOutInputMessageFactoryTest {
40
41     /**
42      * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO
43      * @throws Exception 
44      */
45     @Test
46     public void testPacketOutInputMessage() throws Exception {
47         PacketOutInputBuilder builder = new PacketOutInputBuilder();
48         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
49         builder.setBufferId(256L);
50         builder.setInPort(new PortNumber(257L));
51         List<Action> actions = new ArrayList<>();
52         ActionBuilder actionBuilder = new ActionBuilder();
53         actionBuilder.setType(Output.class);
54         PortActionBuilder portBuilder = new PortActionBuilder();
55         portBuilder.setPort(new PortNumber((long) 42));
56         actionBuilder.addAugmentation(PortAction.class, portBuilder.build());
57         MaxLengthActionBuilder maxLen = new MaxLengthActionBuilder();
58         maxLen.setMaxLength(50);
59         actionBuilder.addAugmentation(MaxLengthAction.class, maxLen.build());
60         actions.add(actionBuilder.build());
61         actionBuilder = new ActionBuilder();
62         actionBuilder.setType(StripVlan.class);
63         builder.setAction(actions);
64         actions.add(actionBuilder.build());
65         builder.setAction(actions);
66         builder.setData(ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"));
67         PacketOutInput message = builder.build();
68         
69         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
70         OF10PacketOutInputMessageFactory factory = OF10PacketOutInputMessageFactory.getInstance();
71         factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message);
72         
73         BufferHelper.checkHeaderV10(out, (byte) 13, 48);
74         Assert.assertEquals("Wrong BufferId", 256, out.readUnsignedInt());
75         Assert.assertEquals("Wrong PortNumber", 257, out.readUnsignedShort());
76         Assert.assertEquals("Wrong actions length", 16, out.readUnsignedShort());
77         Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
78         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
79         Assert.assertEquals("Wrong port", 42, out.readUnsignedShort());
80         Assert.assertEquals("Wrong maxlength", 50, out.readUnsignedShort());
81         Assert.assertEquals("Wrong action type", 3, out.readUnsignedShort());
82         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
83         out.skipBytes(4);
84         Assert.assertArrayEquals("Wrong data", message.getData(), out.readBytes(out.readableBytes()).array());
85         Assert.assertTrue("Unread data", out.readableBytes() == 0);
86     }
87
88 }