Fix checkstyle violations in openflow-protocol-impl - part 12
[openflowplugin.git] / openflowjava / 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 import java.util.ArrayList;
14 import java.util.List;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
20 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
21 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
22 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
23 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
24 import org.opendaylight.openflowjava.util.ByteBufUtils;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.StripVlanCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputActionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
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  * Unit tests for OF10PacketOutInputMessageFactory.
36  *
37  * @author michal.polkorab
38  */
39 public class OF10PacketOutInputMessageFactoryTest {
40
41     private SerializerRegistry registry;
42     private OFSerializer<PacketOutInput> packetOutFactory;
43
44     /**
45      * Initializes serializer registry and stores correct factory in field.
46      */
47     @Before
48     public void startUp() {
49         registry = new SerializerRegistryImpl();
50         registry.init();
51         packetOutFactory = registry.getSerializer(
52                 new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, PacketOutInput.class));
53     }
54
55     /**
56      * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO.
57      */
58     @Test
59     public void testPacketOutInputMessage() throws Exception {
60         PacketOutInputBuilder builder = new PacketOutInputBuilder();
61         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
62         builder.setBufferId(256L);
63         builder.setInPort(new PortNumber(257L));
64         final List<Action> actions = new ArrayList<>();
65         OutputActionCaseBuilder caseBuilder = new OutputActionCaseBuilder();
66         OutputActionBuilder outputBuilder = new OutputActionBuilder();
67         outputBuilder.setPort(new PortNumber(42L));
68         outputBuilder.setMaxLength(50);
69         caseBuilder.setOutputAction(outputBuilder.build());
70         ActionBuilder actionBuilder = new ActionBuilder();
71         actionBuilder.setActionChoice(caseBuilder.build());
72         actions.add(actionBuilder.build());
73         actionBuilder = new ActionBuilder();
74         actionBuilder.setActionChoice(new StripVlanCaseBuilder().build());
75         builder.setAction(actions);
76         actions.add(actionBuilder.build());
77         builder.setAction(actions);
78         builder.setData(ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"));
79         PacketOutInput message = builder.build();
80
81         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
82         packetOutFactory.serialize(message, out);
83
84         BufferHelper.checkHeaderV10(out, (byte) 13, 48);
85         Assert.assertEquals("Wrong BufferId", 256, out.readUnsignedInt());
86         Assert.assertEquals("Wrong PortNumber", 257, out.readUnsignedShort());
87         Assert.assertEquals("Wrong actions length", 16, out.readUnsignedShort());
88         Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
89         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
90         Assert.assertEquals("Wrong port", 42, out.readUnsignedShort());
91         Assert.assertEquals("Wrong maxlength", 50, out.readUnsignedShort());
92         Assert.assertEquals("Wrong action type", 3, out.readUnsignedShort());
93         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
94         out.skipBytes(4);
95         byte[] readData = new byte[out.readableBytes()];
96         out.readBytes(readData);
97         Assert.assertArrayEquals("Wrong data", message.getData(), readData);
98         Assert.assertTrue("Unread data", out.readableBytes() == 0);
99     }
100
101     /**
102      * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO.
103      */
104     @Test
105     public void testPacketOutInputWithNoData() throws Exception {
106         PacketOutInputBuilder builder = new PacketOutInputBuilder();
107         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
108         builder.setBufferId(256L);
109         builder.setInPort(new PortNumber(257L));
110         List<Action> actions = new ArrayList<>();
111         builder.setAction(actions);
112         builder.setData(null);
113         PacketOutInput message = builder.build();
114
115         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
116         packetOutFactory.serialize(message, out);
117
118         BufferHelper.checkHeaderV10(out, (byte) 13, 16);
119         out.skipBytes(8); // skip packet out message to data index
120         Assert.assertTrue("Unread data", out.readableBytes() == 0);
121     }
122 }