Extensibility support (deserialization part)
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / HelloMessageFactoryTest.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.deserialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageCodeKey;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
22 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializerRegistryImpl;
23 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;
24 import org.opendaylight.openflowjava.protocol.impl.util.EncodeConstants;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.HelloElementType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.Elements;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.hello.ElementsBuilder;
29
30 /**
31  * @author michal.polkorab
32  * @author timotej.kubas
33  */
34 public class HelloMessageFactoryTest {
35
36     /** Number of currently supported version / codec */
37     public static final Short VERSION_YET_SUPPORTED = 0x04;
38     private OFDeserializer<HelloMessage> helloFactory;
39
40     /**
41      * Initializes deserializer registry and lookups correct deserializer
42      */
43     @Before
44     public void startUp() {
45         DeserializerRegistry registry = new DeserializerRegistryImpl();
46         registry.init();
47         helloFactory = registry.getDeserializer(
48                 new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, 0, HelloMessage.class));
49     }
50
51     /**
52      * Testing {@link HelloMessageFactory} for correct translation into POJO
53      */
54     @Test
55     public void test() {
56         ByteBuf bb = BufferHelper.buildBuffer("00 01 " // type
57                                             + "00 0c " // length
58                                             + "00 00 00 11 " // bitmap 1
59                                             + "00 00 00 00 " // bitmap 2
60                                             + "00 00 00 00"  // padding
61                 );
62         HelloMessage builtByFactory = BufferHelper.deserialize(helloFactory, bb);
63
64         BufferHelper.checkHeaderV13(builtByFactory);
65         List<Elements> element = createElement();
66         Assert.assertEquals("Wrong type", element.get(0).getType(), builtByFactory.getElements().get(0).getType());
67         Assert.assertEquals("Wrong versionBitmap", element.get(0).getVersionBitmap(), builtByFactory.getElements().get(0).getVersionBitmap());
68     }
69     
70     private static List<Elements> createElement() {
71         ElementsBuilder elementsBuilder = new ElementsBuilder();
72         List<Elements> elementsList = new ArrayList<>();
73         List<Boolean> booleanList = new ArrayList<>();
74         booleanList.add(true);
75         booleanList.add(false);
76         booleanList.add(false);
77         booleanList.add(false);
78         booleanList.add(true);
79         for (int i = 1; i < 60; i++) {
80             booleanList.add(false);
81         }
82         elementsBuilder.setType(HelloElementType.forValue(1));
83         elementsBuilder.setVersionBitmap(booleanList);
84         elementsList.add(elementsBuilder.build());
85         
86         return elementsList;
87     }
88 }