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.SerializerRegistry;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
21 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupModCommand;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsListBuilder;
32 * Unit tests for GroupModInputMessageFactory.
34 * @author timotej.kubas
36 public class GroupModInputMessageFactoryTest {
37 private static final byte MESSAGE_TYPE = 15;
38 private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1;
39 private SerializerRegistry registry;
40 private GroupModInputMessageFactory groupModFactory;
43 * Initializes serializer registry and stores correct factory in field.
46 public void startUp() {
47 registry = new SerializerRegistryImpl();
49 groupModFactory = new GroupModInputMessageFactory(false);
53 * Testing of {@link GroupModInputMessageFactory} for correct translation from POJO.
56 public void testGroupModInputMessage() throws Exception {
57 GroupModInputBuilder builder = new GroupModInputBuilder();
58 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
59 builder.setCommand(GroupModCommand.forValue(2));
60 builder.setType(GroupType.forValue(3));
61 builder.setGroupId(new GroupId(256L));
62 List<BucketsList> exp = createBucketsList();
63 builder.setBucketsList(exp);
64 final GroupModInput message = builder.build();
66 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
68 // simulate parent message
73 groupModFactory.serialize(message, out);
74 // read parent message
79 BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 32);
80 Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readUnsignedShort());
81 Assert.assertEquals("Wrong type", message.getType().getIntValue(), out.readUnsignedByte());
82 out.skipBytes(PADDING_IN_GROUP_MOD_MESSAGE);
83 Assert.assertEquals("Wrong groupId", message.getGroupId().getValue().intValue(), out.readUnsignedInt());
84 List<BucketsList> rec = createBucketsListFromBufer(out);
85 Assert.assertArrayEquals("Wrong bucketList", exp.toArray(), rec.toArray());
88 private static List<BucketsList> createBucketsList() {
89 final List<BucketsList> bucketsList = new ArrayList<>();
90 BucketsListBuilder bucketsBuilder = new BucketsListBuilder();
91 bucketsBuilder.setWeight(10);
92 bucketsBuilder.setWatchPort(new PortNumber(65L));
93 bucketsBuilder.setWatchGroup(22L);
94 BucketsList bucket = bucketsBuilder.build();
95 bucketsList.add(bucket);
99 private static List<BucketsList> createBucketsListFromBufer(ByteBuf out) {
100 final List<BucketsList> bucketsList = new ArrayList<>();
101 BucketsListBuilder bucketsBuilder = new BucketsListBuilder();
102 out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
103 bucketsBuilder.setWeight(out.readUnsignedShort());
104 bucketsBuilder.setWatchPort(new PortNumber(out.readUnsignedInt()));
105 bucketsBuilder.setWatchGroup(out.readUnsignedInt());
107 BucketsList bucket = bucketsBuilder.build();
108 bucketsList.add(bucket);
113 * Testing of {@link GroupModInputMessageFactory} for correct translation from POJO.
116 public void testGroupModInputWithNoBuckets() throws Exception {
117 GroupModInputBuilder builder = new GroupModInputBuilder();
118 BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
119 builder.setCommand(GroupModCommand.forValue(2));
120 builder.setType(GroupType.forValue(3));
121 builder.setGroupId(new GroupId(256L));
122 GroupModInput message = builder.build();
124 ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
125 groupModFactory.serialize(message, out);
127 BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 16);
128 Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readUnsignedShort());
129 Assert.assertEquals("Wrong type", message.getType().getIntValue(), out.readUnsignedByte());
130 out.skipBytes(PADDING_IN_GROUP_MOD_MESSAGE);
131 Assert.assertEquals("Wrong groupId", message.getGroupId().getValue().intValue(), out.readUnsignedInt());
132 Assert.assertTrue("Unexpected data", out.readableBytes() == 0);