Refactoring of cisco-xr-driver and impl modules.
[unimgr.git] / cisco-xr-driver / src / test / java / org / opendaylight / unimgr / mef / nrp / cisco / xr / common / helper / BandwidthProfileHelperTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems Inc 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.unimgr.mef.nrp.cisco.xr.common.helper;
9
10 import org.junit.Test;
11 import org.junit.runner.RunWith;
12 import org.mockito.Mock;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.unimgr.utils.MdsalUtils;
15 import org.opendaylight.yang.gen.v1.urn.mef.nrp.specs.rev160630.AdapterSpec1;
16 import org.opendaylight.yang.gen.v1.urn.mef.nrp.specs.rev160630.TerminationSpec1;
17 import org.opendaylight.yang.gen.v1.urn.mef.nrp.specs.rev160630.g_nrp_connadaptspec.EgressBwpFlow;
18 import org.opendaylight.yang.gen.v1.urn.mef.nrp.specs.rev160630.g_nrp_connadaptspec.IngressBwpFlow;
19 import org.opendaylight.yang.gen.v1.urn.mef.nrp.specs.rev160630.g_nrp_uni_terminationspec.EgressBwpUni;
20 import org.opendaylight.yang.gen.v1.urn.mef.nrp.specs.rev160630.g_nrp_uni_terminationspec.IngressBwpUni;
21 import org.opendaylight.yang.gen.v1.urn.mef.nrp.specs.rev160630.network.topology.topology.node.termination.point.ltp.attrs.lplist.lpspec.adapterspec.NrpConnAdaptSpecAttrs;
22 import org.opendaylight.yang.gen.v1.urn.mef.nrp.specs.rev160630.network.topology.topology.node.termination.point.ltp.attrs.lplist.lpspec.adapterspec.NrpEvcEndpointConnAdaptSpecAttrs;
23 import org.opendaylight.yang.gen.v1.urn.mef.nrp.specs.rev160630.network.topology.topology.node.termination.point.ltp.attrs.lplist.lpspec.terminationspec.NrpUniTerminationAttrs;
24 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.TerminationPoint1;
25 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.g_forwardingconstruct.FcPort;
26 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.g_layerprotocol.LpSpec;
27 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.g_logicalterminationpoint.LpList;
28 import org.opendaylight.yang.gen.v1.urn.onf.core.network.module.rev160630.network.topology.topology.node.termination.point.LtpAttrs;
29 import org.opendaylight.yang.gen.v1.urn.onf.core.specs.rev160630.g_layerprotocolspec.AdapterSpec;
30 import org.opendaylight.yang.gen.v1.urn.onf.core.specs.rev160630.g_layerprotocolspec.TerminationSpec;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntries;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.QosEntriesBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.Queues;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.queues.QueuesOtherConfig;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
36 import org.powermock.api.mockito.PowerMockito;
37 import org.powermock.core.classloader.annotations.PrepareForTest;
38 import org.powermock.modules.junit4.PowerMockRunner;
39
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.Optional;
43
44 import static org.junit.Assert.*;
45 import static org.mockito.Matchers.eq;
46 import static org.mockito.Mockito.mock;
47 import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION;
48 import static org.powermock.api.mockito.PowerMockito.when;
49
50 @RunWith(PowerMockRunner.class)
51 @PrepareForTest(MdsalUtils.class)
52 public class BandwidthProfileHelperTest {
53
54     @Test
55     public void testRetrieveBandwidthProfiles() {
56         //given
57         FcPort fcPort = mock(FcPort.class);
58
59         IngressBwpFlow expectedIngressDefaultBwp = mock(IngressBwpFlow.class);
60         EgressBwpFlow expectedEgressDefaultBwp = mock(EgressBwpFlow.class);
61         IngressBwpFlow expectedIngressEvcBwp = mock(IngressBwpFlow.class);
62         EgressBwpFlow expectedEgressEvcBwp = mock(EgressBwpFlow.class);
63         IngressBwpUni expectedIngressUniBwp = null;
64         EgressBwpUni expectedEgressUniBwp = null;
65
66         DataBroker dataBroker = mockDatastore(fcPort,
67                 Optional.ofNullable(expectedIngressDefaultBwp),
68                 Optional.ofNullable(expectedEgressDefaultBwp),
69                 Optional.ofNullable(expectedIngressEvcBwp),
70                 Optional.ofNullable(expectedEgressEvcBwp),
71                 Optional.ofNullable(expectedIngressUniBwp),
72                 Optional.ofNullable(expectedEgressUniBwp));
73
74         //when
75         List<BandwidthProfileComposition> actual  = new BandwidthProfileHelper(dataBroker, fcPort).getBandwidthProfiles();
76
77         //then
78         assertNotNull(actual);
79         assertEquals(1, actual.size());
80
81         BandwidthProfileComposition actualBpc = actual.get(0);
82         assertTrue(actualBpc.hasAnyProfileDefined());
83
84         assertTrue(actualBpc.getDefaultIngressBwProfile().isPresent());
85         assertEquals(expectedIngressDefaultBwp, actualBpc.getDefaultIngressBwProfile().get());
86
87         assertTrue(actualBpc.getDefaultEgressBwProfile().isPresent());
88         assertEquals(expectedEgressDefaultBwp, actualBpc.getDefaultEgressBwProfile().get());
89
90         assertTrue(actualBpc.getIngressBwProfilePerEvc().isPresent());
91         assertEquals(expectedIngressEvcBwp, actualBpc.getIngressBwProfilePerEvc().get());
92
93         assertTrue(actualBpc.getEgressBwProfilePerEvc().isPresent());
94         assertEquals(expectedEgressEvcBwp, actualBpc.getEgressBwProfilePerEvc().get());
95
96         assertFalse(actualBpc.getIngressBwProfilePerUni().isPresent());
97         assertFalse(actualBpc.getEgressBwProfilePerUni().isPresent());
98     }
99
100     @Test
101     public void testRetrieveBandwidthProfilesNoQos() {
102         //given
103         FcPort fcPort = mock(FcPort.class);
104
105         DataBroker dataBroker = mockDatastore(fcPort,
106                 Optional.empty(),
107                 Optional.empty(),
108                 Optional.empty(),
109                 Optional.empty(),
110                 Optional.empty(),
111                 Optional.empty());
112
113         //when
114         List<BandwidthProfileComposition> actual  = new BandwidthProfileHelper(dataBroker, fcPort).getBandwidthProfiles();
115
116         //then
117         assertNotNull(actual);
118         assertEquals(1, actual.size());
119
120         BandwidthProfileComposition actualBpc = actual.get(0);
121         assertFalse(actualBpc.hasAnyProfileDefined());
122         assertFalse(actualBpc.getDefaultIngressBwProfile().isPresent());
123         assertFalse(actualBpc.getDefaultEgressBwProfile().isPresent());
124         assertFalse(actualBpc.getIngressBwProfilePerEvc().isPresent());
125         assertFalse(actualBpc.getEgressBwProfilePerEvc().isPresent());
126         assertFalse(actualBpc.getIngressBwProfilePerUni().isPresent());
127         assertFalse(actualBpc.getEgressBwProfilePerUni().isPresent());
128     }
129
130     @Test
131     public void testRetrieveBandwidthProfilesEmpty() {
132         //given
133         FcPort fcPort = mock(FcPort.class);
134
135         DataBroker dataBroker = mockDatastoreEmpty(fcPort);
136
137         //when
138         List<BandwidthProfileComposition> actual  = new BandwidthProfileHelper(dataBroker, fcPort).getBandwidthProfiles();
139
140         //then
141         assertNotNull(actual);
142         assertEquals(0, actual.size());
143     }
144
145     private DataBroker mockDatastore(FcPort fcPort,
146                                      Optional<IngressBwpFlow> ingressDefaultBwp,
147                                      Optional<EgressBwpFlow> egressDefaultBwp,
148                                      Optional<IngressBwpFlow> ingressEvcBwp,
149                                      Optional<EgressBwpFlow> egressEvcBwp,
150                                      Optional<IngressBwpUni> ingressUniBwp,
151                                      Optional<EgressBwpUni> egressUniBwp) {
152         DataBroker dataBroker = mock(DataBroker.class);
153
154         TerminationPoint tp = mock(TerminationPoint.class);
155         TerminationPoint1 tp1 = mock(TerminationPoint1.class);
156         LtpAttrs ltpAttrs = mock(LtpAttrs.class);
157         LpList lpList = mock(LpList.class);
158         LpSpec lpSpec = mock(LpSpec.class);
159         List<LpList> lpLists = new ArrayList<>();
160         lpLists.add(lpList);
161
162         when(tp.getAugmentation(TerminationPoint1.class)).thenReturn(tp1);
163         when(tp1.getLtpAttrs()).thenReturn(ltpAttrs);
164         when(ltpAttrs.getLpList()).thenReturn(lpLists);
165         when(lpList.getLpSpec()).thenReturn(lpSpec);
166
167         AdapterSpec adapterSpec = mock(AdapterSpec.class);
168         AdapterSpec1 adapterSpec1 = mock(AdapterSpec1.class);
169         NrpEvcEndpointConnAdaptSpecAttrs evcAttrs = mock(NrpEvcEndpointConnAdaptSpecAttrs.class);
170         NrpConnAdaptSpecAttrs connAdaptSpecAttrs = mock(NrpConnAdaptSpecAttrs.class);
171
172         when(lpSpec.getAdapterSpec()).thenReturn(adapterSpec);
173         when(adapterSpec.getAugmentation(AdapterSpec1.class)).thenReturn(adapterSpec1);
174         when(adapterSpec1.getNrpConnAdaptSpecAttrs()).thenReturn(connAdaptSpecAttrs);
175         when(adapterSpec1.getNrpEvcEndpointConnAdaptSpecAttrs()).thenReturn(evcAttrs);
176
177         if(ingressDefaultBwp.isPresent()) {
178             when(connAdaptSpecAttrs.getIngressBwpFlow()).thenReturn(ingressDefaultBwp.get());
179         }
180
181         if(egressDefaultBwp.isPresent()) {
182             when(connAdaptSpecAttrs.getEgressBwpFlow()).thenReturn(egressDefaultBwp.get());
183         }
184
185         if(ingressEvcBwp.isPresent()) {
186             when(evcAttrs.getIngressBwpFlow()).thenReturn(ingressEvcBwp.get());
187         }
188
189         if(egressEvcBwp.isPresent()) {
190             when(evcAttrs.getEgressBwpFlow()).thenReturn(egressEvcBwp.get());
191         }
192
193         TerminationSpec terminationSpec = mock(TerminationSpec.class);
194         TerminationSpec1 terminationSpec1 = mock(TerminationSpec1.class);
195         NrpUniTerminationAttrs nrpUniTerminationAttrs = mock(NrpUniTerminationAttrs.class);
196
197         when(lpSpec.getTerminationSpec()).thenReturn(terminationSpec);
198         when(terminationSpec.getAugmentation(TerminationSpec1.class)).thenReturn(terminationSpec1);
199         when(terminationSpec1.getNrpUniTerminationAttrs()).thenReturn(nrpUniTerminationAttrs);
200
201         if(ingressUniBwp.isPresent()) {
202             when(nrpUniTerminationAttrs.getIngressBwpUni()).thenReturn(ingressUniBwp.get());
203         }
204
205         if(egressUniBwp.isPresent()) {
206             when(nrpUniTerminationAttrs.getEgressBwpUni()).thenReturn(egressUniBwp.get());
207         }
208
209         PowerMockito.mockStatic(MdsalUtils.class);
210         when(MdsalUtils.readTerminationPoint(eq(dataBroker), eq(CONFIGURATION), eq(fcPort))).thenReturn(com.google.common.base.Optional.of(tp));
211
212         return dataBroker;
213     }
214
215     private DataBroker mockDatastoreEmpty(FcPort fcPort) {
216         DataBroker dataBroker = mock(DataBroker.class);
217
218         PowerMockito.mockStatic(MdsalUtils.class);
219         when(MdsalUtils.readTerminationPoint(eq(dataBroker), eq(CONFIGURATION), eq(fcPort))).thenReturn(com.google.common.base.Optional.absent());
220
221         return dataBroker;
222     }
223 }