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.MessageTypeKey;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
22 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
23 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
24 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
25 import org.opendaylight.openflowjava.util.ByteBufUtils;
26 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeAction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopVlan;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushVlan;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
39 * @author timotej.kubas
42 public class PacketOutInputMessageFactoryTest {
43 private static final byte MESSAGE_TYPE = 13;
44 private static final byte PADDING_IN_PACKET_OUT_MESSAGE = 6;
45 private static final int PADDING_IN_ACTION_HEADER = 4;
46 private SerializerRegistry registry;
47 private OFSerializer<PacketOutInput> packetOutFactory;
50 * Initializes serializer registry and stores correct factory in field
53 public void startUp() {
54 registry = new SerializerRegistryImpl();
56 packetOutFactory = registry.getSerializer(
57 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, PacketOutInput.class));
61 * Testing of {@link PacketOutInputMessageFactory} for correct translation from POJO
65 public void testPacketOutInputMessage() throws Exception {
66 PacketOutInputBuilder builder = new PacketOutInputBuilder();
67 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
68 builder.setBufferId(256L);
69 builder.setInPort(new PortNumber(256L));
70 List<Action> actions = new ArrayList<>();
71 ActionBuilder actionBuilder = new ActionBuilder();
72 actionBuilder.setType(PushVlan.class);
73 EthertypeActionBuilder ethertypeBuilder = new EthertypeActionBuilder();
74 ethertypeBuilder.setEthertype(new EtherType(25));
75 actionBuilder.addAugmentation(EthertypeAction.class, ethertypeBuilder.build());
76 actions.add(actionBuilder.build());
77 actionBuilder = new ActionBuilder();
78 actionBuilder.setType(PopVlan.class);
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 Assert.assertArrayEquals("Wrong data", message.getData(), out.readBytes(out.readableBytes()).array());