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
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
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;
35 * Unit tests for OF10PacketOutInputMessageFactory.
37 * @author michal.polkorab
39 public class OF10PacketOutInputMessageFactoryTest {
41 private SerializerRegistry registry;
42 private OFSerializer<PacketOutInput> packetOutFactory;
45 * Initializes serializer registry and stores correct factory in field.
48 public void startUp() {
49 registry = new SerializerRegistryImpl();
51 packetOutFactory = registry.getSerializer(
52 new MessageTypeKey<>(EncodeConstants.OF10_VERSION_ID, PacketOutInput.class));
56 * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO.
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();
81 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
82 packetOutFactory.serialize(message, out);
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());
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);
102 * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO.
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();
115 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
116 packetOutFactory.serialize(message, out);
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);