Don't use NotificationListener (TapiProvider)
[transportpce.git] / tapi / src / test / java / org / opendaylight / transportpce / tapi / connectivity / TapiConnectivityImplTest.java
1 /*
2  * Copyright © 2021 Nokia, 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.transportpce.tapi.connectivity;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.when;
14
15 import com.google.common.util.concurrent.Futures;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.ListeningExecutorService;
18 import com.google.common.util.concurrent.MoreExecutors;
19 import java.util.HashMap;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.Executors;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.jupiter.MockitoExtension;
28 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
29 import org.opendaylight.mdsal.binding.api.RpcProviderService;
30 import org.opendaylight.transportpce.common.InstanceIdentifiers;
31 import org.opendaylight.transportpce.common.network.NetworkTransactionImpl;
32 import org.opendaylight.transportpce.common.network.NetworkTransactionService;
33 import org.opendaylight.transportpce.pce.service.PathComputationService;
34 import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperations;
35 import org.opendaylight.transportpce.servicehandler.catalog.CatalogDataStoreOperations;
36 import org.opendaylight.transportpce.servicehandler.impl.ServicehandlerImpl;
37 import org.opendaylight.transportpce.servicehandler.listeners.NetworkListener;
38 import org.opendaylight.transportpce.servicehandler.listeners.PceListener;
39 import org.opendaylight.transportpce.servicehandler.listeners.RendererListener;
40 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
41 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperationsImpl;
42 import org.opendaylight.transportpce.tapi.listeners.TapiPceNotificationHandler;
43 import org.opendaylight.transportpce.tapi.listeners.TapiRendererNotificationHandler;
44 import org.opendaylight.transportpce.tapi.listeners.TapiServiceNotificationHandler;
45 import org.opendaylight.transportpce.tapi.topology.TopologyUtils;
46 import org.opendaylight.transportpce.tapi.utils.TapiConnectivityDataUtils;
47 import org.opendaylight.transportpce.tapi.utils.TapiContext;
48 import org.opendaylight.transportpce.tapi.utils.TapiInitialORMapping;
49 import org.opendaylight.transportpce.tapi.utils.TapiLink;
50 import org.opendaylight.transportpce.tapi.utils.TapiLinkImpl;
51 import org.opendaylight.transportpce.tapi.utils.TapiTopologyDataUtils;
52 import org.opendaylight.transportpce.test.AbstractTest;
53 import org.opendaylight.transportpce.test.utils.TopologyDataUtils;
54 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.OrgOpenroadmServiceService;
55 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceCreateInput;
56 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.CreateConnectivityServiceInput;
57 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.CreateConnectivityServiceInputBuilder;
58 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.CreateConnectivityServiceOutput;
59 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.DeleteConnectivityServiceInput;
60 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.DeleteConnectivityServiceInputBuilder;
61 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev181210.DeleteConnectivityServiceOutput;
62 import org.opendaylight.yangtools.yang.common.ErrorType;
63 import org.opendaylight.yangtools.yang.common.RpcResult;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66
67 @ExtendWith(MockitoExtension.class)
68 public class TapiConnectivityImplTest extends AbstractTest {
69
70     @Mock
71     private PathComputationService pathComputationService;
72     @Mock
73     private RendererServiceOperations rendererServiceOperations;
74     @Mock
75     private NotificationPublishService notificationPublishService;
76     @Mock
77     private TapiPceNotificationHandler tapipceNotificationHandler;
78     @Mock
79     private TapiRendererNotificationHandler tapirendererNotificationHandler;
80     @Mock
81     private TapiServiceNotificationHandler tapiserviceNotificationHandler;
82     @Mock
83     private PceListener pceListenerImpl;
84     @Mock
85     private RendererListener rendererListenerImpl;
86     @Mock
87     private NetworkListener networkModelListenerImpl;
88     @Mock
89     public CatalogDataStoreOperations catalogDataStoreOperations;
90     @Mock
91     private RpcProviderService rpcProviderService;
92
93     private static final Logger LOG = LoggerFactory.getLogger(TapiConnectivityImplTest.class);
94     private static ServiceDataStoreOperations serviceDataStoreOperations;
95     private static TapiContext tapiContext;
96     private static TopologyUtils topologyUtils;
97     private static ConnectivityUtils connectivityUtils;
98     private static TapiInitialORMapping tapiInitialORMapping;
99     private static NetworkTransactionService networkTransactionService;
100     private static TapiLink tapilink;
101     private ListeningExecutorService executorService;
102     private CountDownLatch endSignal;
103     private static final int NUM_THREADS = 5;
104
105     @BeforeEach
106     void setUp() throws InterruptedException, ExecutionException {
107         executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
108         endSignal = new CountDownLatch(1);
109         // Need to have datastore populated to enable the mapping from TAPI to OR
110         TopologyDataUtils.writeTopologyFromFileToDatastore(getDataStoreContextUtil(),
111             TapiTopologyDataUtils.OPENROADM_TOPOLOGY_FILE, InstanceIdentifiers.OVERLAY_NETWORK_II);
112         TopologyDataUtils.writeTopologyFromFileToDatastore(getDataStoreContextUtil(),
113             TapiTopologyDataUtils.OPENROADM_NETWORK_FILE, InstanceIdentifiers.UNDERLAY_NETWORK_II);
114         TopologyDataUtils.writeTopologyFromFileToDatastore(getDataStoreContextUtil(),
115             TapiTopologyDataUtils.OTN_TOPOLOGY_FILE, InstanceIdentifiers.OTN_NETWORK_II);
116         TopologyDataUtils.writePortmappingFromFileToDatastore(getDataStoreContextUtil(),
117             TapiTopologyDataUtils.PORTMAPPING_FILE);
118
119         networkTransactionService = new NetworkTransactionImpl(getDataBroker());
120         tapilink = new TapiLinkImpl(networkTransactionService);
121         serviceDataStoreOperations = new ServiceDataStoreOperationsImpl(getDataStoreContextUtil().getDataBroker());
122         tapiContext = new TapiContext(networkTransactionService);
123         topologyUtils = new TopologyUtils(networkTransactionService, getDataStoreContextUtil().getDataBroker(),
124             tapilink);
125         connectivityUtils = new ConnectivityUtils(serviceDataStoreOperations, new HashMap<>(), tapiContext,
126             networkTransactionService);
127         tapiInitialORMapping = new TapiInitialORMapping(topologyUtils, connectivityUtils,
128             tapiContext, serviceDataStoreOperations);
129         tapiInitialORMapping.performTopoInitialMapping();
130         LOG.info("setup done");
131     }
132
133     @Test
134     void createConnServiceShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
135         OrgOpenroadmServiceService serviceHandler = new ServicehandlerImpl(rpcProviderService, pathComputationService,
136             rendererServiceOperations, notificationPublishService, pceListenerImpl, rendererListenerImpl,
137             networkModelListenerImpl, serviceDataStoreOperations, catalogDataStoreOperations);
138
139         TapiConnectivityImpl tapiConnectivity = new TapiConnectivityImpl(serviceHandler, tapiContext, connectivityUtils,
140             tapipceNotificationHandler, tapirendererNotificationHandler);
141
142         ListenableFuture<RpcResult<CreateConnectivityServiceOutput>> result =
143             tapiConnectivity.createConnectivityService(new CreateConnectivityServiceInputBuilder().build());
144         result.addListener(new Runnable() {
145             @Override
146             public void run() {
147                 endSignal.countDown();
148             }
149         }, executorService);
150
151         endSignal.await();
152
153         RpcResult<CreateConnectivityServiceOutput> rpcResult = result.get();
154         assertEquals(ErrorType.RPC, rpcResult.getErrors().get(0).getErrorType());
155     }
156
157     @Test
158     void createConnServiceShouldBeSuccessfulWhenPerformPCESuccessful()
159             throws ExecutionException, InterruptedException {
160         OrgOpenroadmServiceService serviceHandler = new ServicehandlerImpl(rpcProviderService, pathComputationService,
161             rendererServiceOperations, notificationPublishService, pceListenerImpl, rendererListenerImpl,
162             networkModelListenerImpl, serviceDataStoreOperations, catalogDataStoreOperations);
163
164         CreateConnectivityServiceInput input = TapiConnectivityDataUtils.buildConnServiceCreateInput();
165         when(pathComputationService.pathComputationRequest(any())).thenReturn(Futures.immediateFuture(any()));
166
167         TapiConnectivityImpl tapiConnectivity = new TapiConnectivityImpl(serviceHandler, tapiContext, connectivityUtils,
168             tapipceNotificationHandler, tapirendererNotificationHandler);
169         ListenableFuture<RpcResult<CreateConnectivityServiceOutput>> result =
170             tapiConnectivity.createConnectivityService(input);
171         result.addListener(new Runnable() {
172             @Override
173             public void run() {
174                 endSignal.countDown();
175             }
176         }, executorService);
177
178         endSignal.await();
179
180         RpcResult<CreateConnectivityServiceOutput> rpcResult = result.get();
181         assertTrue(rpcResult.isSuccessful());
182     }
183
184     @Test
185     void deleteConnServiceShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
186         OrgOpenroadmServiceService serviceHandler = new ServicehandlerImpl(rpcProviderService, pathComputationService,
187             rendererServiceOperations, notificationPublishService, pceListenerImpl, rendererListenerImpl,
188             networkModelListenerImpl, serviceDataStoreOperations, catalogDataStoreOperations);
189
190         TapiConnectivityImpl tapiConnectivity = new TapiConnectivityImpl(serviceHandler, tapiContext, connectivityUtils,
191             tapipceNotificationHandler, tapirendererNotificationHandler);
192
193         ListenableFuture<RpcResult<DeleteConnectivityServiceOutput>> result =
194             tapiConnectivity.deleteConnectivityService(new DeleteConnectivityServiceInputBuilder().build());
195         result.addListener(new Runnable() {
196             @Override
197             public void run() {
198                 endSignal.countDown();
199             }
200         }, executorService);
201
202         endSignal.await();
203
204         RpcResult<DeleteConnectivityServiceOutput> rpcResult = result.get();
205         assertEquals(ErrorType.RPC, rpcResult.getErrors().get(0).getErrorType());
206     }
207
208     @Test
209     void deleteConnServiceShouldBeFailedWithNonExistService() throws ExecutionException, InterruptedException {
210         DeleteConnectivityServiceInput input = TapiConnectivityDataUtils.buildConnServiceDeleteInput1();
211         OrgOpenroadmServiceService serviceHandler = new ServicehandlerImpl(rpcProviderService, pathComputationService,
212             rendererServiceOperations, notificationPublishService, pceListenerImpl, rendererListenerImpl,
213             networkModelListenerImpl, serviceDataStoreOperations, catalogDataStoreOperations);
214
215         TapiConnectivityImpl tapiConnectivity = new TapiConnectivityImpl(serviceHandler, tapiContext, connectivityUtils,
216             tapipceNotificationHandler, tapirendererNotificationHandler);
217         ListenableFuture<RpcResult<DeleteConnectivityServiceOutput>> result =
218             tapiConnectivity.deleteConnectivityService(input);
219         result.addListener(new Runnable() {
220             @Override
221             public void run() {
222                 endSignal.countDown();
223             }
224         }, executorService);
225
226         endSignal.await();
227
228         RpcResult<DeleteConnectivityServiceOutput> rpcResult = result.get();
229         assertEquals(ErrorType.RPC, rpcResult.getErrors().get(0).getErrorType());
230     }
231
232     @Test
233     void deleteConnServiceShouldBeSuccessForExistingService() throws ExecutionException, InterruptedException {
234         when(rendererServiceOperations.serviceDelete(any(), any())).thenReturn(Futures.immediateFuture(any()));
235
236         OrgOpenroadmServiceService serviceHandler = new ServicehandlerImpl(rpcProviderService, pathComputationService,
237             rendererServiceOperations, notificationPublishService, pceListenerImpl, rendererListenerImpl,
238             networkModelListenerImpl, serviceDataStoreOperations, catalogDataStoreOperations);
239
240         TapiConnectivityImpl tapiConnectivity = new TapiConnectivityImpl(serviceHandler, tapiContext, connectivityUtils,
241             tapipceNotificationHandler, tapirendererNotificationHandler);
242
243         ServiceCreateInput createInput = TapiConnectivityDataUtils.buildServiceCreateInput();
244         serviceDataStoreOperations.createService(createInput);
245         tapiContext.updateConnectivityContext(TapiConnectivityDataUtils.createConnService(), new HashMap<>());
246
247         DeleteConnectivityServiceInput input = TapiConnectivityDataUtils.buildConnServiceDeleteInput();
248         ListenableFuture<RpcResult<DeleteConnectivityServiceOutput>> result =
249             tapiConnectivity.deleteConnectivityService(input);
250         result.addListener(new Runnable() {
251             @Override
252             public void run() {
253                 endSignal.countDown();
254             }
255         }, executorService);
256
257         endSignal.await();
258
259         RpcResult<DeleteConnectivityServiceOutput> rpcResult = result.get();
260         assertTrue(rpcResult.isSuccessful());
261     }
262 }