1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
\r
2 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
\r
4 import io.netty.buffer.ByteBuf;
\r
5 import io.netty.buffer.UnpooledByteBufAllocator;
\r
7 import java.util.ArrayList;
\r
8 import java.util.List;
\r
10 import org.junit.Assert;
\r
11 import org.junit.Test;
\r
12 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest;
\r
13 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
\r
14 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
\r
15 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeAction;
\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.EthertypeActionBuilder;
\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PopVlan;
\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.PushVlan;
\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;
\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsListBuilder;
\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.actions.list.ActionBuilder;
\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType;
\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
\r
29 * @author timotej.kubas
\r
32 public class PacketOutInputMessageFactoryTest {
\r
33 private static final byte MESSAGE_TYPE = 13;
\r
34 private static final byte PADDING_IN_PACKET_OUT_MESSAGE = 6;
\r
35 private static final int PADDING_IN_ACTION_HEADER = 4;
\r
38 * Testing of {@link PacketOutInputMessageFactory} for correct translation from POJO
\r
39 * @throws Exception
\r
42 public void testPacketOutInputMessage() throws Exception {
\r
43 PacketOutInputBuilder builder = new PacketOutInputBuilder();
\r
44 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
\r
45 builder.setBufferId(256L);
\r
46 builder.setInPort(new PortNumber(256L));
\r
47 List<ActionsList> actions = new ArrayList<>();
\r
48 ActionsListBuilder actionsListBuilder = new ActionsListBuilder();
\r
49 ActionBuilder actionBuilder = new ActionBuilder();
\r
50 actionBuilder.setType(PushVlan.class);
\r
51 EthertypeActionBuilder ethertypeBuilder = new EthertypeActionBuilder();
\r
52 ethertypeBuilder.setEthertype(new EtherType(25));
\r
53 actionBuilder.addAugmentation(EthertypeAction.class, ethertypeBuilder.build());
\r
54 actionsListBuilder.setAction(actionBuilder.build());
\r
55 actions.add(actionsListBuilder.build());
\r
56 actionBuilder = new ActionBuilder();
\r
57 actionBuilder.setType(PopVlan.class);
\r
58 actionsListBuilder.setAction(actionBuilder.build());
\r
59 actions.add(actionsListBuilder.build());
\r
60 builder.setActionsList(actions);
\r
61 builder.setData(ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"));
\r
62 PacketOutInput message = builder.build();
\r
64 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
\r
65 PacketOutInputMessageFactory factory = PacketOutInputMessageFactory.getInstance();
\r
66 factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message);
\r
68 BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 56);
\r
69 Assert.assertEquals("Wrong BufferId", message.getBufferId().longValue(), out.readUnsignedInt());
\r
70 Assert.assertEquals("Wrong PortNumber", message.getInPort().getValue().longValue(), out.readUnsignedInt());
\r
71 Assert.assertEquals("Wrong ActionsLength", 16, out.readUnsignedShort());
\r
72 out.skipBytes(PADDING_IN_PACKET_OUT_MESSAGE);
\r
73 Assert.assertEquals("Wrong action type", 17, out.readUnsignedShort());
\r
74 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
\r
75 Assert.assertEquals("Wrong ethertype", 25, out.readUnsignedShort());
\r
76 out.skipBytes(Short.SIZE / Byte.SIZE);
\r
77 Assert.assertEquals("Wrong action type", 18, out.readUnsignedShort());
\r
78 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
\r
79 out.skipBytes(PADDING_IN_ACTION_HEADER);
\r
80 Assert.assertArrayEquals("Wrong data", message.getData(), out.readBytes(out.readableBytes()).array());
\r