2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
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.OutputActionCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.StripVlanCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputActionBuilder;
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.PortNumber;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
32 import org.opendaylight.yangtools.yang.common.Uint16;
33 import org.opendaylight.yangtools.yang.common.Uint32;
36 * Unit tests for OF10PacketOutInputMessageFactory.
38 * @author michal.polkorab
40 public class OF10PacketOutInputMessageFactoryTest {
42 private SerializerRegistry registry;
43 private OFSerializer<PacketOutInput> packetOutFactory;
46 * Initializes serializer registry and stores correct factory in field.
49 public void startUp() {
50 registry = new SerializerRegistryImpl();
52 packetOutFactory = registry.getSerializer(
53 new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, PacketOutInput.class));
57 * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO.
60 public void testPacketOutInputMessage() throws Exception {
61 PacketOutInputBuilder builder = new PacketOutInputBuilder();
62 BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
63 builder.setBufferId(Uint32.valueOf(256));
64 builder.setInPort(new PortNumber(Uint32.valueOf(257)));
65 final List<Action> actions = new ArrayList<>();
66 OutputActionCaseBuilder caseBuilder = new OutputActionCaseBuilder();
67 OutputActionBuilder outputBuilder = new OutputActionBuilder();
68 outputBuilder.setPort(new PortNumber(Uint32.valueOf(42)));
69 outputBuilder.setMaxLength(Uint16.valueOf(50));
70 caseBuilder.setOutputAction(outputBuilder.build());
71 ActionBuilder actionBuilder = new ActionBuilder();
72 actionBuilder.setActionChoice(caseBuilder.build());
73 actions.add(actionBuilder.build());
74 actionBuilder = new ActionBuilder();
75 actionBuilder.setActionChoice(new StripVlanCaseBuilder().build());
76 builder.setAction(actions);
77 actions.add(actionBuilder.build());
78 builder.setAction(actions);
79 builder.setData(ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"));
80 PacketOutInput message = builder.build();
82 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
83 packetOutFactory.serialize(message, out);
85 BufferHelper.checkHeaderV10(out, (byte) 13, 48);
86 Assert.assertEquals("Wrong BufferId", 256, out.readUnsignedInt());
87 Assert.assertEquals("Wrong PortNumber", 257, out.readUnsignedShort());
88 Assert.assertEquals("Wrong actions length", 16, out.readUnsignedShort());
89 Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
90 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
91 Assert.assertEquals("Wrong port", 42, out.readUnsignedShort());
92 Assert.assertEquals("Wrong maxlength", 50, out.readUnsignedShort());
93 Assert.assertEquals("Wrong action type", 3, out.readUnsignedShort());
94 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
96 byte[] readData = new byte[out.readableBytes()];
97 out.readBytes(readData);
98 Assert.assertArrayEquals("Wrong data", message.getData(), readData);
99 Assert.assertTrue("Unread data", out.readableBytes() == 0);
103 * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO.
106 public void testPacketOutInputWithNoData() throws Exception {
107 PacketOutInputBuilder builder = new PacketOutInputBuilder();
108 BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
109 builder.setBufferId(Uint32.valueOf(256));
110 builder.setInPort(new PortNumber(Uint32.valueOf(257)));
111 List<Action> actions = new ArrayList<>();
112 builder.setAction(actions);
113 builder.setData(null);
114 PacketOutInput message = builder.build();
116 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
117 packetOutFactory.serialize(message, out);
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);