6e75130f9ebe358eaf3b6f56aa7f86940add4e54
[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.OFSerializer;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
22 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
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
73         // simulate parent message
74         out.writeInt(1);
75         out.writeZero(2);
76         out.writeShort(3);
77
78         groupModFactory.serialize(message, out);
79
80         // read parent message
81         out.readInt();
82         out.skipBytes(2);
83         out.readShort();
84
85         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 32);
86         Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readUnsignedShort());
87         Assert.assertEquals("Wrong type", message.getType().getIntValue(), out.readUnsignedByte());
88         out.skipBytes(PADDING_IN_GROUP_MOD_MESSAGE);
89         Assert.assertEquals("Wrong groupId", message.getGroupId().getValue().intValue(), out.readUnsignedInt());
90         List<BucketsList> rec = createBucketsListFromBufer(out);
91         Assert.assertArrayEquals("Wrong bucketList", exp.toArray(), rec.toArray());
92     }
93
94     private static List<BucketsList> createBucketsList(){
95         List<BucketsList> bucketsList = new ArrayList<>();
96         BucketsListBuilder bucketsBuilder = new BucketsListBuilder();
97         BucketsList bucket;
98         bucketsBuilder.setWeight(10);
99         bucketsBuilder.setWatchPort(new PortNumber(65L));
100         bucketsBuilder.setWatchGroup(22L);
101         bucket = bucketsBuilder.build();
102         bucketsList.add(bucket);
103         return bucketsList;
104     }
105
106     private static List<BucketsList> createBucketsListFromBufer(ByteBuf out){
107         List<BucketsList> bucketsList = new ArrayList<>();
108         BucketsListBuilder bucketsBuilder = new BucketsListBuilder();
109         BucketsList bucket;
110         out.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
111         bucketsBuilder.setWeight(out.readUnsignedShort());
112         bucketsBuilder.setWatchPort(new PortNumber(out.readUnsignedInt()));
113         bucketsBuilder.setWatchGroup(out.readUnsignedInt());
114         out.skipBytes(4);
115         bucket = bucketsBuilder.build();
116         bucketsList.add(bucket);
117         return bucketsList;
118     }
119
120     /**
121      * Testing of {@link GroupModInputMessageFactory} for correct translation from POJO
122      * @throws Exception
123      */
124     @Test
125     public void testGroupModInputWithNoBuckets() throws Exception {
126         GroupModInputBuilder builder = new GroupModInputBuilder();
127         BufferHelper.setupHeader(builder, EncodeConstants.OF13_VERSION_ID);
128         builder.setCommand(GroupModCommand.forValue(2));
129         builder.setType(GroupType.forValue(3));
130         builder.setGroupId(new GroupId(256L));
131         GroupModInput message = builder.build();
132
133         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();
134         groupModFactory.serialize(message, out);
135
136         BufferHelper.checkHeaderV13(out, MESSAGE_TYPE, 16);
137         Assert.assertEquals("Wrong command", message.getCommand().getIntValue(), out.readUnsignedShort());
138         Assert.assertEquals("Wrong type", message.getType().getIntValue(), out.readUnsignedByte());
139         out.skipBytes(PADDING_IN_GROUP_MOD_MESSAGE);
140         Assert.assertEquals("Wrong groupId", message.getGroupId().getValue().intValue(), out.readUnsignedInt());
141         Assert.assertTrue("Unexpected data", out.readableBytes() == 0);
142     }
143 }