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.MaxLengthAction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.StripVlan;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInputBuilder;
40 * @author michal.polkorab
43 public class OF10PacketOutInputMessageFactoryTest {
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.OF10_VERSION_ID, PacketOutInput.class));
60 * Testing of {@link OF10PacketOutInputMessageFactory} for correct translation from POJO
64 public void testPacketOutInputMessage() throws Exception {
65 PacketOutInputBuilder builder = new PacketOutInputBuilder();
66 BufferHelper.setupHeader(builder, EncodeConstants.OF10_VERSION_ID);
67 builder.setBufferId(256L);
68 builder.setInPort(new PortNumber(257L));
69 List<Action> actions = new ArrayList<>();
70 ActionBuilder actionBuilder = new ActionBuilder();
71 actionBuilder.setType(Output.class);
72 PortActionBuilder portBuilder = new PortActionBuilder();
73 portBuilder.setPort(new PortNumber((long) 42));
74 actionBuilder.addAugmentation(PortAction.class, portBuilder.build());
75 MaxLengthActionBuilder maxLen = new MaxLengthActionBuilder();
76 maxLen.setMaxLength(50);
77 actionBuilder.addAugmentation(MaxLengthAction.class, maxLen.build());
78 actions.add(actionBuilder.build());
79 actionBuilder = new ActionBuilder();
80 actionBuilder.setType(StripVlan.class);
81 builder.setAction(actions);
82 actions.add(actionBuilder.build());
83 builder.setAction(actions);
84 builder.setData(ByteBufUtils.hexStringToBytes("00 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14"));
85 PacketOutInput message = builder.build();
87 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
88 packetOutFactory.serialize(message, out);
90 BufferHelper.checkHeaderV10(out, (byte) 13, 48);
91 Assert.assertEquals("Wrong BufferId", 256, out.readUnsignedInt());
92 Assert.assertEquals("Wrong PortNumber", 257, out.readUnsignedShort());
93 Assert.assertEquals("Wrong actions length", 16, out.readUnsignedShort());
94 Assert.assertEquals("Wrong action type", 0, out.readUnsignedShort());
95 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
96 Assert.assertEquals("Wrong port", 42, out.readUnsignedShort());
97 Assert.assertEquals("Wrong maxlength", 50, out.readUnsignedShort());
98 Assert.assertEquals("Wrong action type", 3, out.readUnsignedShort());
99 Assert.assertEquals("Wrong action length", 8, out.readUnsignedShort());
101 Assert.assertArrayEquals("Wrong data", message.getData(), out.readBytes(out.readableBytes()).array());
102 Assert.assertTrue("Unread data", out.readableBytes() == 0);