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