Merge "Fix for openflow devices not connecting to controller"
[openflowplugin.git] / extension / openflowplugin-extension-onf / src / test / java / org / opendaylight / openflowplugin / extension / onf / converter / BundleControlConverterTest.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 package org.opendaylight.openflowplugin.extension.onf.converter;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import org.junit.Assert;
14 import org.junit.Test;
15 import org.mockito.Mockito;
16 import org.opendaylight.openflowplugin.extension.api.ExtensionConvertorData;
17 import org.opendaylight.openflowplugin.extension.api.path.MessagePath;
18 import org.opendaylight.openflowplugin.extension.onf.BundleTestUtils;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.send.experimenter.input.experimenter.message.of.choice.BundleControlSal;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.send.experimenter.input.experimenter.message.of.choice.BundleControlSalBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.send.experimenter.input.experimenter.message.of.choice.bundle.control.sal.SalControlDataBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleControlType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleFlags;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.common.grouping.BundleProperty;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.property.grouping.bundle.property.entry.BundlePropertyExperimenter;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.bundle.property.grouping.bundle.property.entry.bundle.property.experimenter.BundlePropertyExperimenterData;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.experimenter.input.experimenter.data.of.choice.BundleControlOnf;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.experimenter.input.experimenter.data.of.choice.BundleControlOnfBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.experimenter.input.experimenter.data.of.choice.bundle.control.onf.OnfControlGroupingDataBuilder;
32 import org.opendaylight.yangtools.yang.common.Uint32;
33 import org.opendaylight.yangtools.yang.common.Uint64;
34
35 /**
36  * Test for {@link org.opendaylight.openflowplugin.extension.onf.converter.BundleControlConverter}.
37  */
38 public class BundleControlConverterTest {
39
40     private final BundleControlConverter converter = new BundleControlConverter();
41
42     @Test
43     public void testGetExperimenterId() {
44         Assert.assertEquals("Wrong ExperimenterId.", new ExperimenterId(Uint32.valueOf(0x4F4E4600)),
45             converter.getExperimenterId());
46     }
47
48     @Test
49     public void testGetType() {
50         Assert.assertEquals("Wrong type.", 2300, converter.getType());
51     }
52
53     @Test
54     public void testConvertDownWithProperty() {
55         testConvertDown(true);
56     }
57
58     @Test
59     public void testConvertDownWithoutProperty() {
60         testConvertDown(false);
61     }
62
63     @Test
64     public void testConvertUpWithProperty() {
65         testConvertUp(true);
66     }
67
68     @Test
69     public void testConvertUpWithoutProperty() {
70         testConvertUp(true);
71     }
72
73     private void testConvertDown(final boolean withProperty) {
74         final BundleControlSal original = createOFPMessage(withProperty);
75         final ExtensionConvertorData data = new ExtensionConvertorData((short)1);
76         data.setXid(Uint32.valueOf(0L));
77         data.setDatapathId(Uint64.valueOf(BigInteger.ONE));
78         final BundleControlOnf converted = converter.convert(original, data);
79         testConvert(original, converted, withProperty);
80     }
81
82     private void testConvertUp(final boolean withProperty) {
83         final BundleControlOnf original = createOFJMessage(withProperty);
84         final BundleControlSal converted = converter.convert(original, MessagePath.MESSAGE_NOTIFICATION);
85         testConvert(converted, original, withProperty);
86     }
87
88     private static void testConvert(final BundleControlSal ofpMessage,
89                                     final BundleControlOnf ofjMessage,
90                                     final boolean withProperty) {
91         Assert.assertEquals("Wrong BundleId",
92                 new BundleId(
93                         ofpMessage.getSalControlData().getBundleId().getValue()),
94                         ofjMessage.getOnfControlGroupingData().getBundleId()
95         );
96         Assert.assertEquals("Wrong type",
97                 BundleControlType.forValue(
98                         ofpMessage.getSalControlData().getType().getIntValue()),
99                         ofjMessage.getOnfControlGroupingData().getType()
100         );
101         Assert.assertEquals("Wrong flags",
102                 new BundleFlags(
103                         ofpMessage.getSalControlData().getFlags().isAtomic(),
104                         ofpMessage.getSalControlData().getFlags().isOrdered()),
105                         ofjMessage.getOnfControlGroupingData().getFlags()
106         );
107         if (withProperty) {
108             final BundlePropertyExperimenter originalProperty = (BundlePropertyExperimenter) ofpMessage
109                     .getSalControlData()
110                     .getBundleProperty()
111                     .get(0)
112                     .getBundlePropertyEntry();
113             final BundlePropertyExperimenter convertedProperty = (BundlePropertyExperimenter) ofjMessage
114                     .getOnfControlGroupingData()
115                     .getBundleProperty()
116                     .get(0)
117                     .getBundlePropertyEntry();
118             Assert.assertEquals("Wrong property ExperimenterId", new ExperimenterId(originalProperty.getExperimenter()),
119                     convertedProperty.getExperimenter());
120             Assert.assertEquals("Wrong property experimenter type", originalProperty.getExpType(),
121                     convertedProperty.getExpType());
122             Assert.assertEquals("Wrong property data", originalProperty.getBundlePropertyExperimenterData(),
123                     convertedProperty.getBundlePropertyExperimenterData());
124         } else {
125             Assert.assertTrue("Properties not empty",
126                     ofjMessage
127                             .getOnfControlGroupingData()
128                             .nonnullBundleProperty()
129                             .isEmpty());
130         }
131     }
132
133     private static BundleControlSal createOFPMessage(final boolean withProperty) {
134         final SalControlDataBuilder dataBuilder = new SalControlDataBuilder();
135         dataBuilder.setBundleId(new BundleId(Uint32.ONE));
136         dataBuilder.setType(BundleControlType.ONFBCTOPENREQUEST);
137         dataBuilder.setFlags(new BundleFlags(true, false));
138         List<BundleProperty> properties = new ArrayList<>();
139         if (withProperty) {
140             properties.add(BundleTestUtils.createExperimenterProperty(
141                     Mockito.mock(BundlePropertyExperimenterData.class)));
142         }
143         dataBuilder.setBundleProperty(properties);
144         return new BundleControlSalBuilder().setSalControlData(dataBuilder.build()).build();
145     }
146
147     private static BundleControlOnf createOFJMessage(final boolean withProperty) {
148         final BundleControlOnfBuilder builder = new BundleControlOnfBuilder();
149         final OnfControlGroupingDataBuilder dataBuilder = new OnfControlGroupingDataBuilder();
150         dataBuilder.setBundleId(new BundleId(Uint32.ONE));
151         dataBuilder.setType(BundleControlType.ONFBCTOPENREPLY);
152         dataBuilder.setFlags(new BundleFlags(false, false));
153         List<BundleProperty> properties = new ArrayList<>();
154         if (withProperty) {
155             properties.add(BundleTestUtils.createExperimenterProperty(
156                     Mockito.mock(BundlePropertyExperimenterData.class)));
157         }
158         dataBuilder.setBundleProperty(properties);
159         return new BundleControlOnfBuilder().setOnfControlGroupingData(dataBuilder.build()).build();
160     }
161 }