189fac23f5065ba3fb02d93a5359007d28f254ba
[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.Before;
19 import org.junit.Test;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
22 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
23 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
24 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
25 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
26 import org.opendaylight.openflowjava.util.ByteBufUtils;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.EthertypeAction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.EthertypeActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopVlan;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushVlan;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
37
38 /**
39  * @author timotej.kubas
40  *
41  */
42 public class PacketOutInputMessageFactoryTest {
43     private static final byte MESSAGE_TYPE = 13;
44     private static final byte PADDING_IN_PACKET_OUT_MESSAGE = 6;
45     private static final int PADDING_IN_ACTION_HEADER = 4;
46     private SerializerRegistry registry;
47     private OFSerializer<PacketOutInput> packetOutFactory;
48
49     /**
50      * Initializes serializer registry and stores correct factory in field
51      */
52     @Before
53     public void startUp() {
54         registry = new SerializerRegistryImpl();
55         registry.init();
56         packetOutFactory = registry.getSerializer(
57                 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, PacketOutInput.class));
58     }
59
60     /**
61      * Testing of {@link PacketOutInputMessageFactory} for correct translation from POJO
62      * @throws Exception
63      */
64     @Test
65     public void testPacketOutInputMessage() throws Exception {
66         PacketOutInputBuilder builder = new PacketOutInputBuilder();
67         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
68         builder.setBufferId(256L);
69         builder.setInPort(new PortNumber(256L));
70         List<Action> actions = new ArrayList<>();
71         ActionBuilder actionBuilder = new ActionBuilder();
72         actionBuilder.setType(PushVlan.class);
73         EthertypeActionBuilder ethertypeBuilder = new EthertypeActionBuilder();
74         ethertypeBuilder.setEthertype(new EtherType(25));
75         actionBuilder.addAugmentation(EthertypeAction.class, ethertypeBuilder.build());
76         actions.add(actionBuilder.build());
77         actionBuilder = new ActionBuilder();
78         actionBuilder.setType(PopVlan.class);
79         actions.add(actionBuilder.build());
80         builder.setAction(actions);
81         builder.setData(ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"));
82         PacketOutInput message = builder.build();
83
84         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
85         packetOutFactory.serialize(message, out);
86
87         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 56);
88         Assert.assertEquals("Wrong BufferId", message.getBufferId().longValue(), out.readUnsignedInt());
89         Assert.assertEquals("Wrong PortNumber", message.getInPort().getValue().longValue(), out.readUnsignedInt());
90         Assert.assertEquals("Wrong ActionsLength", 16, out.readUnsignedShort());
91         out.skipBytes(PADDING_IN_PACKET_OUT_MESSAGE);
92         Assert.assertEquals("Wrong action type", 17, out.readUnsignedShort());
93         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
94         Assert.assertEquals("Wrong ethertype", 25, out.readUnsignedShort());
95         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
96         Assert.assertEquals("Wrong action type", 18, out.readUnsignedShort());
97         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
98         out.skipBytes(PADDING_IN_ACTION_HEADER);
99         Assert.assertArrayEquals("Wrong data", message.getData(), out.readBytes(out.readableBytes()).array());
100     }
101
102     /**
103      * Testing of {@link PacketOutInputMessageFactory} for correct translation from POJO
104      * @throws Exception
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(256L);
111         builder.setInPort(new PortNumber(256L));
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 }