c5d41114c2cd36341af27b9e21d055c7419b170c
[openflowjava.git] / 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
14 import java.util.ArrayList;
15 import java.util.List;
16
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.api.util.EncodeConstants;
24 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializerRegistryImpl;
25 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupModCommand;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.GroupType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsList;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.buckets.grouping.BucketsListBuilder;
34
35 /**
36  * @author timotej.kubas
37  *
38  */
39 public class GroupModInputMessageFactoryTest {
40     private static final byte MESSAGE_TYPE = 15;
41     private static final byte PADDING_IN_GROUP_MOD_MESSAGE = 1;
42     private SerializerRegistry registry;
43     private OFSerializer<GroupModInput> groupModFactory;
44
45     /**
46      * Initializes serializer registry and stores correct factory in field
47      */
48     @Before
49     public void startUp() {
50         registry = new SerializerRegistryImpl();
51         registry.init();
52         groupModFactory = registry.getSerializer(
53                 new MessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, GroupModInput.class));
54     }
55
56     /**
57      * @throws Exception
58      * Testing of {@link GroupModInputMessageFactory} for correct translation from POJO
59      */
60     @Test
61     public void testGroupModInputMessage() throws Exception {
62         GroupModInputBuilder builder = new GroupModInputBuilder();
63         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
64         builder.setCommand(GroupModCommand.forValue(2));
65         builder.setType(GroupType.forValue(3));
66         builder.setGroupId(new GroupId(256L));
67         List<BucketsList> exp = createBucketsList();
68         builder.setBucketsList(exp);
69         GroupModInput message = builder.build();
70         
71         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
72         groupModFactory.serialize(message, out);
73
74         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 32);
75         Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readUnsignedShort());
76         Assert.assertEquals("Wrong type", message.getType().getIntValue(), out.readUnsignedByte());
77         out.skipBytes(PADDING_IN_GROUP_MOD_MESSAGE);
78         Assert.assertEquals("Wrong groupId", message.getGroupId().getValue().intValue(), out.readUnsignedInt());
79         List<BucketsList> rec = createBucketsListFromBufer(out);
80         Assert.assertArrayEquals("Wrong bucketList", exp.toArray(), rec.toArray());
81     }
82     
83     private static List<BucketsList> createBucketsList(){
84         List<BucketsList> bucketsList = new ArrayList<>();
85         BucketsListBuilder bucketsBuilder = new BucketsListBuilder();
86         BucketsList bucket;
87         bucketsBuilder.setWeight(10);
88         bucketsBuilder.setWatchPort(new PortNumber(65L));
89         bucketsBuilder.setWatchGroup(22L);
90         bucket = bucketsBuilder.build();
91         bucketsList.add(bucket);
92         return bucketsList;
93     }
94     
95     private static List<BucketsList> createBucketsListFromBufer(ByteBuf out){
96         List<BucketsList> bucketsList = new ArrayList<>();
97         BucketsListBuilder bucketsBuilder = new BucketsListBuilder();
98         BucketsList bucket;
99         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
100         bucketsBuilder.setWeight(out.readUnsignedShort());
101         bucketsBuilder.setWatchPort(new PortNumber(out.readUnsignedInt()));
102         bucketsBuilder.setWatchGroup(out.readUnsignedInt());
103         out.skipBytes(4);
104         bucket = bucketsBuilder.build();
105         bucketsList.add(bucket);
106         return bucketsList;
107     }
108
109     /**
110      * Testing of {@link GroupModInputMessageFactory} for correct translation from POJO
111      * @throws Exception
112      */
113     @Test
114     public void testGroupModInputWithNoBuckets() throws Exception {
115         GroupModInputBuilder builder = new GroupModInputBuilder();
116         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
117         builder.setCommand(GroupModCommand.forValue(2));
118         builder.setType(GroupType.forValue(3));
119         builder.setGroupId(new GroupId(256L));
120         GroupModInput message = builder.build();
121
122         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
123         groupModFactory.serialize(message, out);
124
125         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 16);
126         Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readUnsignedShort());
127         Assert.assertEquals("Wrong type", message.getType().getIntValue(), out.readUnsignedByte());
128         out.skipBytes(PADDING_IN_GROUP_MOD_MESSAGE);
129         Assert.assertEquals("Wrong groupId", message.getGroupId().getValue().intValue(), out.readUnsignedInt());
130         Assert.assertTrue("Unexpected data", out.readableBytes() == 0);
131     }
132 }