437ed396951a932e6cf9b118c13c637022429d49
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / HelloInputMessageFactoryTest.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;\r
3 \r
4 import io.netty.buffer.ByteBuf;\r
5 import io.netty.buffer.UnpooledByteBufAllocator;\r
6 \r
7 import java.util.ArrayList;\r
8 import java.util.List;\r
9 \r
10 import org.junit.Assert;\r
11 import org.junit.Test;\r
12 import org.opendaylight.openflowjava.protocol.impl.deserialization.factories.HelloMessageFactoryTest;\r
13 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloElementType;\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.ElementsBuilder;\r
19 \r
20 /**\r
21  * @author michal.polkorab\r
22  *\r
23  */\r
24 public class HelloInputMessageFactoryTest {\r
25 \r
26     /**\r
27      * Testing of {@link HelloInputMessageFactory} for correct translation from POJO\r
28      * @throws Exception \r
29      */\r
30     @Test\r
31     public void testWithoutElementsSet() throws Exception {\r
32         HelloInputBuilder hib = new HelloInputBuilder();\r
33         BufferHelper.setupHeader(hib);\r
34         HelloInput hi = hib.build();\r
35         \r
36         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
37         HelloInputMessageFactory himf = HelloInputMessageFactory.getInstance();\r
38         himf.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, hi);\r
39         \r
40         BufferHelper.checkHeaderV13(out, himf.getMessageType(), himf.computeLength());\r
41     }\r
42     \r
43     /**\r
44      * Testing of {@link HelloInputMessageFactory} for correct translation from POJO\r
45      * @throws Exception \r
46      */\r
47     @Test\r
48     public void testWithElementsSet() throws Exception {\r
49         HelloInputBuilder builder = new HelloInputBuilder();\r
50         BufferHelper.setupHeader(builder);\r
51         builder.setElements(createElement());\r
52         HelloInput message = builder.build();\r
53         \r
54         ByteBuf out = UnpooledByteBufAllocator.DEFAULT.buffer();\r
55         HelloInputMessageFactory factory = HelloInputMessageFactory.getInstance();\r
56         factory.messageToBuffer(HelloMessageFactoryTest.VERSION_YET_SUPPORTED, out, message);\r
57         \r
58         BufferHelper.checkHeaderV13(out, factory.getMessageType(), factory.computeLength());\r
59         Elements element = readElement(out).get(0);\r
60         Assert.assertEquals("Wrong element type", createElement().get(0).getType(), element.getType());\r
61         Assert.assertArrayEquals("Wrong element bitmap", createElement().get(0).getVersionBitmap().toArray(), element.getVersionBitmap().toArray());\r
62     }\r
63     \r
64     private static List<Elements> createElement() {\r
65         ElementsBuilder elementsBuilder = new ElementsBuilder();\r
66         List<Elements> elementsList = new ArrayList<Elements>();\r
67         List<Boolean> booleanList = new ArrayList<Boolean>();\r
68 \r
69         for (int i = 0; i < 64; i++) {\r
70             booleanList.add(true);\r
71         }\r
72 \r
73         elementsBuilder.setType(HelloElementType.forValue(1));\r
74         elementsBuilder.setVersionBitmap(booleanList);\r
75         elementsList.add(elementsBuilder.build());\r
76         \r
77         return elementsList;\r
78     }\r
79     \r
80     private static List<Elements> readElement(ByteBuf input) {\r
81         ElementsBuilder elementsBuilder = new ElementsBuilder();\r
82         List<Elements> elementsList = new ArrayList<Elements>();\r
83         elementsBuilder.setType(HelloElementType.forValue(input.readUnsignedShort()));\r
84         int arrayLength = input.readableBytes()/4;\r
85         int[] versionBitmap = new int[arrayLength];\r
86         for (int i = 0; i < arrayLength; i++) {\r
87             versionBitmap[i] = (int) input.readUnsignedInt();\r
88         }\r
89         elementsBuilder.setVersionBitmap(readVersionBitmap(versionBitmap));\r
90         elementsList.add(elementsBuilder.build());\r
91         return elementsList;\r
92     }\r
93     \r
94     private static List<Boolean> readVersionBitmap(int[] input){\r
95         List<Boolean> versionBitmapList = new ArrayList<>();\r
96         for (int i = 0; i < input.length; i++) {\r
97             int mask = input[i];\r
98             for (int j = 0; j < Integer.SIZE; j++) {\r
99                     versionBitmapList.add((mask & (1<<j)) != 0);\r
100             }\r
101         }\r
102         return versionBitmapList;\r
103     }\r
104 \r
105 }\r