7be6047af8eefb182d3b6e79ace98a40b67f4f9b
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / GroupModInputMessageFactoryTest.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
10
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;
30
31 /**
32  * Unit tests for GroupModInputMessageFactory.
33  *
34  * @author timotej.kubas
35  */
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;
41
42     /**
43      * Initializes serializer registry and stores correct factory in field.
44      */
45     @Before
46     public void startUp() {
47         registry = new SerializerRegistryImpl();
48         registry.init();
49         groupModFactory = new GroupModInputMessageFactory(false);
50     }
51
52     /**
53      * Testing of {@link GroupModInputMessageFactory} for correct translation from POJO.
54      */
55     @Test
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();
65
66         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
67
68         // simulate parent message
69         out.writeInt(1);
70         out.writeZero(2);
71         out.writeShort(3);
72
73         groupModFactory.serialize(message, out);
74         // read parent message
75         out.readInt();
76         out.skipBytes(2);
77         out.readShort();
78
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());
86     }
87
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);
96         return bucketsList;
97     }
98
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());
106         out.skipBytes(4);
107         BucketsList bucket = bucketsBuilder.build();
108         bucketsList.add(bucket);
109         return bucketsList;
110     }
111
112     /**
113      * Testing of {@link GroupModInputMessageFactory} for correct translation from POJO.
114      */
115     @Test
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();
123
124         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
125         groupModFactory.serialize(message, out);
126
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);
133     }
134 }