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;
14 import java.util.ArrayList;
15 import java.util.List;
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.PopVlanCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.PushVlanCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.push.vlan._case.PushVlanActionBuilder;
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.EtherType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
38 * @author timotej.kubas
41 public class PacketOutInputMessageFactoryTest {
42 private static final byte MESSAGE_TYPE = 13;
43 private static final byte PADDING_IN_PACKET_OUT_MESSAGE = 6;
44 private static final int PADDING_IN_ACTION_HEADER = 4;
45 private SerializerRegistry registry;
46 private OFSerializer<PacketOutInput> packetOutFactory;
49 * Initializes serializer registry and stores correct factory in field
52 public void startUp() {
53 registry = new SerializerRegistryImpl();
55 packetOutFactory = registry.getSerializer(
56 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, PacketOutInput.class));
60 * Testing of {@link PacketOutInputMessageFactory} for correct translation from POJO
64 public void testPacketOutInputMessage() throws Exception {
65 PacketOutInputBuilder builder = new PacketOutInputBuilder();
66 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
67 builder.setBufferId(256L);
68 builder.setInPort(new PortNumber(256L));
69 List<Action> actions = new ArrayList<>();
70 ActionBuilder actionBuilder = new ActionBuilder();
71 PushVlanCaseBuilder pushVlanCaseBuilder = new PushVlanCaseBuilder();
72 PushVlanActionBuilder pushVlanBuilder = new PushVlanActionBuilder();
73 pushVlanBuilder.setEthertype(new EtherType(new EtherType(25)));
74 pushVlanCaseBuilder.setPushVlanAction(pushVlanBuilder.build());
75 actionBuilder.setActionChoice(pushVlanCaseBuilder.build());
76 actions.add(actionBuilder.build());
77 actionBuilder = new ActionBuilder();
78 actionBuilder.setActionChoice(new PopVlanCaseBuilder().build());
79 actions.add(actionBuilder.build());
80 builder.setAction(actions);
81 builder.setData(ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"));
82 PacketOutInput message = builder.build();
84 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
85 packetOutFactory.serialize(message, out);
87 BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 56);
88 Assert.assertEquals("Wrong BufferId", message.getBufferId().longValue(), out.readUnsignedInt());
89 Assert.assertEquals("Wrong PortNumber", message.getInPort().getValue().longValue(), out.readUnsignedInt());
90 Assert.assertEquals("Wrong ActionsLength", 16, out.readUnsignedShort());
91 out.skipBytes(PADDING_IN_PACKET_OUT_MESSAGE);
92 Assert.assertEquals("Wrong action type", 17, out.readUnsignedShort());
93 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
94 Assert.assertEquals("Wrong ethertype", 25, out.readUnsignedShort());
95 out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
96 Assert.assertEquals("Wrong action type", 18, out.readUnsignedShort());
97 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
98 out.skipBytes(PADDING_IN_ACTION_HEADER);
99 byte[] readData = new byte[out.readableBytes()];
100 out.readBytes(readData);
101 Assert.assertArrayEquals("Wrong data", message.getData(), readData);
105 * Testing of {@link PacketOutInputMessageFactory} for correct translation from POJO
109 public void testPacketOutInputWithNoData() throws Exception {
110 PacketOutInputBuilder builder = new PacketOutInputBuilder();
111 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
112 builder.setBufferId(256L);
113 builder.setInPort(new PortNumber(256L));
114 List<Action> actions = new ArrayList<>();
115 builder.setAction(actions);
116 builder.setData(null);
117 PacketOutInput message = builder.build();
119 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
120 packetOutFactory.serialize(message, out);
122 BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 24);
123 out.skipBytes(16); // skip packet out message to data index
124 Assert.assertTrue("Unexpected data", out.readableBytes() == 0);