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