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