Merge "Bug 8293: Add table writer to bulk-o-matic"
[openflowplugin.git] / extension / openflowplugin-extension-onf / src / test / java / org / opendaylight / openflowplugin / extension / onf / deserializer / BundleControlFactoryTest.java
1 /*
2  * Copyright (c) 2017 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.openflowplugin.extension.onf.deserializer;
10
11 import io.netty.buffer.ByteBuf;
12 import org.junit.Assert;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Matchers;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
20 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
21 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
22 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
23 import org.opendaylight.openflowplugin.extension.onf.ByteBufUtils;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleControlType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleFlags;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundlePropertyType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.common.grouping.BundleProperty;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.property.grouping.bundle.property.entry.BundlePropertyExperimenter;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.property.grouping.bundle.property.entry.bundle.property.experimenter.BundlePropertyExperimenterData;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.experimenter.input.experimenter.data.of.choice.BundleControlOnf;
31
32 /**
33  * Tests for {@link org.opendaylight.openflowplugin.extension.onf.deserializer.BundleControlFactory}.
34  */
35 @RunWith(MockitoJUnitRunner.class)
36 public class BundleControlFactoryTest {
37
38     private final OFDeserializer<BundleControlOnf> factory = new BundleControlFactory();
39     @Mock
40     DeserializerRegistry registry;
41     @Mock
42     OFDeserializer<BundlePropertyExperimenterData> experimenterPropertyDeserializer;
43
44     @Test
45     public void testDeserializeWithoutProperties() {
46         ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 01 " // bundle ID
47                                                        + "00 01 " // type
48                                                        + "00 03"); // flags
49         BundleControlOnf builtByFactory = factory.deserialize(buffer);
50         Assert.assertEquals(1, builtByFactory.getOnfControlGroupingData().getBundleId().getValue().intValue());
51         BundleFlags flags = new BundleFlags(true, true);
52         Assert.assertEquals("Wrong atomic flag",
53                 flags.isAtomic(), builtByFactory.getOnfControlGroupingData().getFlags().isAtomic());
54         Assert.assertEquals("Wrong ordered flag",
55                 flags.isOrdered(), builtByFactory.getOnfControlGroupingData().getFlags().isOrdered());
56         Assert.assertEquals("Wrong type",
57                 BundleControlType.ONFBCTOPENREPLY, builtByFactory.getOnfControlGroupingData().getType());
58         Assert.assertTrue("Properties not empty",
59                 builtByFactory.getOnfControlGroupingData().getBundleProperty().isEmpty());
60     }
61
62     @Test
63     public void testDeserializeWithProperties() {
64         ByteBuf buffer = ByteBufUtils.hexStringToByteBuf("00 00 00 01 " // bundle ID
65                                                        + "00 05 " // type
66                                                        + "00 02 " // flags
67                                                        + "ff ff " // type 1
68                                                        + "00 0c " // length 1
69                                                        + "00 00 00 01 " // experimenter ID 1
70                                                        + "00 00 00 02 " // experimenter type 1
71                                                        + "00 00 00 00 " // experimenter data 1
72                                                        + "00 00 " // type 2
73                                                        + "00 04 " // length 2
74                                                        + "00 00 00 00"); // data 2
75         Mockito.when(registry.getDeserializer(Matchers.any(MessageCodeKey.class)))
76                 .thenReturn(experimenterPropertyDeserializer);
77         ((DeserializerRegistryInjector)factory).injectDeserializerRegistry(registry);
78         BundleControlOnf builtByFactory = factory.deserialize(buffer);
79         Assert.assertEquals(1, builtByFactory.getOnfControlGroupingData().getBundleId().getValue().intValue());
80         BundleFlags flags = new BundleFlags(false, true);
81         Assert.assertEquals("Wrong atomic flag",
82                 flags.isAtomic(), builtByFactory.getOnfControlGroupingData().getFlags().isAtomic());
83         Assert.assertEquals("Wrong ordered flag",
84                 flags.isOrdered(),
85                 builtByFactory.getOnfControlGroupingData().getFlags().isOrdered());
86         Assert.assertEquals("Wrong type",
87                 BundleControlType.ONFBCTCOMMITREPLY, builtByFactory.getOnfControlGroupingData().getType());
88         BundleProperty property = builtByFactory.getOnfControlGroupingData().getBundleProperty().get(0);
89         Assert.assertEquals("Wrong bundle property type",
90                 BundlePropertyType.ONFETBPTEXPERIMENTER, property.getType());
91         BundlePropertyExperimenter experimenterProperty
92                 = (BundlePropertyExperimenter) property.getBundlePropertyEntry();
93         Assert.assertEquals("Wrong experimenter ID",
94                 1, experimenterProperty.getExperimenter().getValue().intValue());
95         Assert.assertEquals("Wrong experimenter type",
96                 2, experimenterProperty.getExpType().longValue());
97         Mockito.verify(experimenterPropertyDeserializer, Mockito.times(1)).deserialize(buffer);
98     }
99
100 }