Bug 2756 - Action model update
[openflowjava.git] / 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
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.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.StripVlanCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputActionBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
35
36 /**
37  * @author michal.polkorab
38  *
39  */
40 public class OF10PacketOutInputMessageFactoryTest {
41
42     private SerializerRegistry registry;
43     private OFSerializer<PacketOutInput> packetOutFactory;
44
45     /**
46      * Initializes serializer registry and stores correct factory in field
47      */
48     @Before
49     public void startUp() {
50         registry = new SerializerRegistryImpl();
51         registry.init();
52         packetOutFactory = registry.getSerializer(
53                 new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, PacketOutInput.class));
54     }
55
56     /**
57      * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO
58      * @throws Exception
59      */
60     @Test
61     public void testPacketOutInputMessage() throws Exception {
62         PacketOutInputBuilder builder = new PacketOutInputBuilder();
63         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
64         builder.setBufferId(256L);
65         builder.setInPort(new PortNumber(257L));
66         List<Action> actions = new ArrayList<>();
67         ActionBuilder actionBuilder = new ActionBuilder();
68         OutputActionCaseBuilder caseBuilder = new OutputActionCaseBuilder();
69         OutputActionBuilder outputBuilder = new OutputActionBuilder();
70         outputBuilder.setPort(new PortNumber(42L));
71         outputBuilder.setMaxLength(50);
72         caseBuilder.setOutputAction(outputBuilder.build());
73         actionBuilder.setActionChoice(caseBuilder.build());
74         actions.add(actionBuilder.build());
75         actionBuilder = new ActionBuilder();
76         actionBuilder.setActionChoice(new StripVlanCaseBuilder().build());
77         builder.setAction(actions);
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.checkHeaderV10(out, (byte) 13, 48);
87         Assert.assertEquals("Wrong BufferId", 256, out.readUnsignedInt());
88         Assert.assertEquals("Wrong PortNumber", 257, out.readUnsignedShort());
89         Assert.assertEquals("Wrong actions length", 16, out.readUnsignedShort());
90         Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
91         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
92         Assert.assertEquals("Wrong port", 42, out.readUnsignedShort());
93         Assert.assertEquals("Wrong maxlength", 50, out.readUnsignedShort());
94         Assert.assertEquals("Wrong action type", 3, out.readUnsignedShort());
95         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
96         out.skipBytes(4);
97         Assert.assertArrayEquals("Wrong data", message.getData(), out.readBytes(out.readableBytes()).array());
98         Assert.assertTrue("Unread data", out.readableBytes() == 0);
99     }
100
101     /**
102      * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO
103      * @throws Exception
104      */
105     @Test
106     public void testPacketOutInputWithNoData() throws Exception {
107         PacketOutInputBuilder builder = new PacketOutInputBuilder();
108         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
109         builder.setBufferId(256L);
110         builder.setInPort(new PortNumber(257L));
111         List<Action> actions = new ArrayList<>();
112         builder.setAction(actions);
113         builder.setData(null);
114         PacketOutInput message = builder.build();
115
116         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
117         packetOutFactory.serialize(message, out);
118
119         BufferHelper.checkHeaderV10(out, (byte) 13, 16);
120         out.skipBytes(8); // skip packet out message to data index
121         Assert.assertTrue("Unread data", out.readableBytes() == 0);
122     }
123 }