01fe88711751b3e28bc434d70fd3248095515cb2
[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.augments.rev150225.MaxLengthAction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MaxLengthActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.PortAction;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.PortActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.StripVlan;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
38
39 /**
40  * @author michal.polkorab
41  *
42  */
43 public class OF10PacketOutInputMessageFactoryTest {
44
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.OF10_VERSION_ID, PacketOutInput.class));
57     }
58
59     /**
60      * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO
61      * @throws Exception
62      */
63     @Test
64     public void testPacketOutInputMessage() throws Exception {
65         PacketOutInputBuilder builder = new PacketOutInputBuilder();
66         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
67         builder.setBufferId(256L);
68         builder.setInPort(new PortNumber(257L));
69         List<Action> actions = new ArrayList<>();
70         ActionBuilder actionBuilder = new ActionBuilder();
71         actionBuilder.setType(Output.class);
72         PortActionBuilder portBuilder = new PortActionBuilder();
73         portBuilder.setPort(new PortNumber((long) 42));
74         actionBuilder.addAugmentation(PortAction.class, portBuilder.build());
75         MaxLengthActionBuilder maxLen = new MaxLengthActionBuilder();
76         maxLen.setMaxLength(50);
77         actionBuilder.addAugmentation(MaxLengthAction.class, maxLen.build());
78         actions.add(actionBuilder.build());
79         actionBuilder = new ActionBuilder();
80         actionBuilder.setType(StripVlan.class);
81         builder.setAction(actions);
82         actions.add(actionBuilder.build());
83         builder.setAction(actions);
84         builder.setData(ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"));
85         PacketOutInput message = builder.build();
86
87         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
88         packetOutFactory.serialize(message, out);
89
90         BufferHelper.checkHeaderV10(out, (byte) 13, 48);
91         Assert.assertEquals("Wrong BufferId", 256, out.readUnsignedInt());
92         Assert.assertEquals("Wrong PortNumber", 257, out.readUnsignedShort());
93         Assert.assertEquals("Wrong actions length", 16, out.readUnsignedShort());
94         Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
95         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
96         Assert.assertEquals("Wrong port", 42, out.readUnsignedShort());
97         Assert.assertEquals("Wrong maxlength", 50, out.readUnsignedShort());
98         Assert.assertEquals("Wrong action type", 3, out.readUnsignedShort());
99         Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
100         out.skipBytes(4);
101         Assert.assertArrayEquals("Wrong data", message.getData(), out.readBytes(out.readableBytes()).array());
102         Assert.assertTrue("Unread data", out.readableBytes() == 0);
103     }
104
105     /**
106      * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO
107      * @throws Exception
108      */
109     @Test
110     public void testPacketOutInputWithNoData() throws Exception {
111         PacketOutInputBuilder builder = new PacketOutInputBuilder();
112         BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
113         builder.setBufferId(256L);
114         builder.setInPort(new PortNumber(257L));
115         List<Action> actions = new ArrayList<>();
116         builder.setAction(actions);
117         builder.setData(null);
118         PacketOutInput message = builder.build();
119
120         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
121         packetOutFactory.serialize(message, out);
122
123         BufferHelper.checkHeaderV10(out, (byte) 13, 16);
124         out.skipBytes(8); // skip packet out message to data index
125         Assert.assertTrue("Unread data", out.readableBytes() == 0);
126     }
127 }