604ba23159641ab2a0f265495a00817e3019e213
[openflowplugin.git] / openflowjava / 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 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import io.netty.buffer.UnpooledByteBufAllocator;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
19 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
20 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
21 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
22 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
23 import org.opendaylight.openflowjava.util.ByteBufUtils;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PopVlanCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PushVlanCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.push.vlan._case.PushVlanActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.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 import org.opendaylight.yangtools.yang.common.Uint16;
34 import org.opendaylight.yangtools.yang.common.Uint32;
35
36 /**
37  * Unit tests for PacketOutInputMessageFactory.
38  *
39  * @author timotej.kubas
40  */
41 public class PacketOutInputMessageFactoryTest {
42     private static final byte MESSAGE_TYPE = 13;
43     private static final byte PADDING_IN_PACKET_OUT_MESSAGE = 6;
44     private static final int PADDING_IN_ACTION_HEADER = 4;
45     private SerializerRegistry registry;
46     private OFSerializer<PacketOutInput> packetOutFactory;
47
48     /**
49      * Initializes serializer registry and stores correct factory in field.
50      */
51     @Before
52     public void startUp() {
53         registry = new SerializerRegistryImpl();
54         registry.init();
55         packetOutFactory = registry.getSerializer(
56                 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, PacketOutInput.class));
57     }
58
59     /**
60      * Testing of {@link PacketOutInputMessageFactory} for correct translation from POJO.
61      */
62     @Test
63     public void testPacketOutInputMessage() throws Exception {
64         PacketOutInputBuilder builder = new PacketOutInputBuilder();
65         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
66         builder.setBufferId(Uint32.valueOf(256));
67         builder.setInPort(new PortNumber(Uint32.valueOf(256)));
68         final List<Action> actions = new ArrayList<>();
69         ActionBuilder actionBuilder = new ActionBuilder();
70         PushVlanCaseBuilder pushVlanCaseBuilder = new PushVlanCaseBuilder();
71         PushVlanActionBuilder pushVlanBuilder = new PushVlanActionBuilder();
72         pushVlanBuilder.setEthertype(new EtherType(new EtherType(Uint16.valueOf(25))));
73         pushVlanCaseBuilder.setPushVlanAction(pushVlanBuilder.build());
74         actionBuilder.setActionChoice(pushVlanCaseBuilder.build());
75         actions.add(actionBuilder.build());
76         actionBuilder = new ActionBuilder();
77         actionBuilder.setActionChoice(new PopVlanCaseBuilder().build());
78         actions.add(actionBuilder.build());
79         builder.setAction(actions);
80         builder.setData(ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"));
81         PacketOutInput message = builder.build();
82
83         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
84         packetOutFactory.serialize(message, out);
85
86         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 56);
87         Assert.assertEquals("Wrong BufferId", message.getBufferId().longValue(), out.readUnsignedInt());
88         Assert.assertEquals("Wrong PortNumber", message.getInPort().getValue().longValue(), out.readUnsignedInt());
89         Assert.assertEquals("Wrong ActionsLength", 16, out.readUnsignedShort());
90         out.skipBytes(PADDING_IN_PACKET_OUT_MESSAGE);
91         Assert.assertEquals("Wrong action type", 17, out.readUnsignedShort());
92         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
93         Assert.assertEquals("Wrong ethertype", 25, out.readUnsignedShort());
94         out.skipBytes(Short.BYTES);
95         Assert.assertEquals("Wrong action type", 18, out.readUnsignedShort());
96         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
97         out.skipBytes(PADDING_IN_ACTION_HEADER);
98         byte[] readData = new byte[out.readableBytes()];
99         out.readBytes(readData);
100         Assert.assertArrayEquals("Wrong data", message.getData(), readData);
101     }
102
103     /**
104      * Testing of {@link PacketOutInputMessageFactory} for correct translation from POJO.
105      */
106     @Test
107     public void testPacketOutInputWithNoData() throws Exception {
108         PacketOutInputBuilder builder = new PacketOutInputBuilder();
109         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
110         builder.setBufferId(Uint32.valueOf(256));
111         builder.setInPort(new PortNumber(Uint32.valueOf(256)));
112         List<Action> actions = new ArrayList<>();
113         builder.setAction(actions);
114         builder.setData(null);
115         PacketOutInput message = builder.build();
116
117         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
118         packetOutFactory.serialize(message, out);
119
120         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 24);
121         out.skipBytes(16); // skip packet out message to data index
122         Assert.assertTrue("Unexpected data", out.readableBytes() == 0);
123     }
124 }