Migration to TAPI 2.4 Step3
[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.opendaylight.transportpce.renderer.rev210915.ServiceDeleteOutput;
55 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceDeleteOutputBuilder;
56 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.configuration.response.common.ConfigurationResponseCommon;
57 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.configuration.response.common.ConfigurationResponseCommonBuilder;
58 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.OrgOpenroadmServiceService;
59 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceCreateInput;
60 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.CreateConnectivityServiceInput;
61 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.CreateConnectivityServiceInputBuilder;
62 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.CreateConnectivityServiceOutput;
63 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.DeleteConnectivityServiceInput;
64 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.DeleteConnectivityServiceInputBuilder;
65 import org.opendaylight.yang.gen.v1.urn.onf.otcc.yang.tapi.connectivity.rev221121.DeleteConnectivityServiceOutput;
66 import org.opendaylight.yangtools.yang.common.ErrorType;
67 import org.opendaylight.yangtools.yang.common.RpcResult;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70
71 @ExtendWith(MockitoExtension.class)
72 public class TapiConnectivityImplTest extends AbstractTest {
73
74     @Mock
75     private PathComputationService pathComputationService;
76     @Mock
77     private RendererServiceOperations rendererServiceOperations;
78     @Mock
79     private NotificationPublishService notificationPublishService;
80     @Mock
81     private TapiPceNotificationHandler tapipceNotificationHandler;
82     @Mock
83     private TapiRendererNotificationHandler tapirendererNotificationHandler;
84     @Mock
85     private TapiServiceNotificationHandler tapiserviceNotificationHandler;
86     @Mock
87     private PceListener pceListenerImpl;
88     @Mock
89     private RendererListener rendererListenerImpl;
90     @Mock
91     private NetworkListener networkModelListenerImpl;
92     @Mock
93     public CatalogDataStoreOperations catalogDataStoreOperations;
94     @Mock
95     private RpcProviderService rpcProviderService;
96
97     private static final Logger LOG = LoggerFactory.getLogger(TapiConnectivityImplTest.class);
98     private static ServiceDataStoreOperations serviceDataStoreOperations;
99     private static TapiContext tapiContext;
100     private static TopologyUtils topologyUtils;
101     private static ConnectivityUtils connectivityUtils;
102     private static TapiInitialORMapping tapiInitialORMapping;
103     private static NetworkTransactionService networkTransactionService;
104     private static TapiLink tapilink;
105     private ListeningExecutorService executorService;
106     private CountDownLatch endSignal;
107     private static final int NUM_THREADS = 5;
108
109     @BeforeEach
110     void setUp() throws InterruptedException, ExecutionException {
111         executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
112         endSignal = new CountDownLatch(1);
113         // Need to have datastore populated to enable the mapping from TAPI to OR
114         TopologyDataUtils.writeTopologyFromFileToDatastore(getDataStoreContextUtil(),
115             TapiTopologyDataUtils.OPENROADM_TOPOLOGY_FILE, InstanceIdentifiers.OVERLAY_NETWORK_II);
116         TopologyDataUtils.writeTopologyFromFileToDatastore(getDataStoreContextUtil(),
117             TapiTopologyDataUtils.OPENROADM_NETWORK_FILE, InstanceIdentifiers.UNDERLAY_NETWORK_II);
118         TopologyDataUtils.writeTopologyFromFileToDatastore(getDataStoreContextUtil(),
119             TapiTopologyDataUtils.OTN_TOPOLOGY_FILE, InstanceIdentifiers.OTN_NETWORK_II);
120         TopologyDataUtils.writePortmappingFromFileToDatastore(getDataStoreContextUtil(),
121             TapiTopologyDataUtils.PORTMAPPING_FILE);
122
123         networkTransactionService = new NetworkTransactionImpl(getDataBroker());
124         tapilink = new TapiLinkImpl(networkTransactionService);
125         serviceDataStoreOperations = new ServiceDataStoreOperationsImpl(getDataStoreContextUtil().getDataBroker());
126         tapiContext = new TapiContext(networkTransactionService);
127         topologyUtils = new TopologyUtils(networkTransactionService, getDataStoreContextUtil().getDataBroker(),
128             tapilink);
129         connectivityUtils = new ConnectivityUtils(serviceDataStoreOperations, new HashMap<>(), tapiContext,
130             networkTransactionService);
131         tapiInitialORMapping = new TapiInitialORMapping(topologyUtils, connectivityUtils,
132             tapiContext, serviceDataStoreOperations);
133         tapiInitialORMapping.performTopoInitialMapping();
134         LOG.info("setup done");
135     }
136
137     @Test
138     void createConnServiceShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
139         OrgOpenroadmServiceService serviceHandler = new ServicehandlerImpl(rpcProviderService, pathComputationService,
140             rendererServiceOperations, notificationPublishService, pceListenerImpl, rendererListenerImpl,
141             networkModelListenerImpl, serviceDataStoreOperations, catalogDataStoreOperations);
142
143         TapiConnectivityImpl tapiConnectivity = new TapiConnectivityImpl(serviceHandler, tapiContext, connectivityUtils,
144             tapipceNotificationHandler, tapirendererNotificationHandler, networkTransactionService);
145
146         ListenableFuture<RpcResult<CreateConnectivityServiceOutput>> result =
147             tapiConnectivity.createConnectivityService(new CreateConnectivityServiceInputBuilder().build());
148         result.addListener(new Runnable() {
149             @Override
150             public void run() {
151                 endSignal.countDown();
152             }
153         }, executorService);
154
155         endSignal.await();
156
157         RpcResult<CreateConnectivityServiceOutput> rpcResult = result.get();
158         assertEquals(ErrorType.RPC, rpcResult.getErrors().get(0).getErrorType());
159     }
160
161     @Test
162     void createConnServiceShouldBeSuccessfulWhenPerformPCESuccessful()
163             throws ExecutionException, InterruptedException {
164         OrgOpenroadmServiceService serviceHandler = new ServicehandlerImpl(rpcProviderService, pathComputationService,
165             rendererServiceOperations, notificationPublishService, pceListenerImpl, rendererListenerImpl,
166             networkModelListenerImpl, serviceDataStoreOperations, catalogDataStoreOperations);
167
168         CreateConnectivityServiceInput input = TapiConnectivityDataUtils.buildConnServiceCreateInput();
169
170         when(pathComputationService.pathComputationRequest(any())).thenReturn(Futures.immediateFuture(any()));
171
172         TapiConnectivityImpl tapiConnectivity = new TapiConnectivityImpl(serviceHandler, tapiContext, connectivityUtils,
173             tapipceNotificationHandler, tapirendererNotificationHandler, networkTransactionService);
174         ListenableFuture<RpcResult<CreateConnectivityServiceOutput>> result =
175             tapiConnectivity.createConnectivityService(input);
176         result.addListener(new Runnable() {
177             @Override
178             public void run() {
179                 endSignal.countDown();
180             }
181         }, executorService);
182
183         endSignal.await();
184         LOG.info("INPUT= {}", input.toString());
185         RpcResult<CreateConnectivityServiceOutput> rpcResult = result.get();
186         assertTrue(rpcResult.isSuccessful());
187     }
188
189     @Test
190     void deleteConnServiceShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
191         OrgOpenroadmServiceService serviceHandler = new ServicehandlerImpl(rpcProviderService, pathComputationService,
192             rendererServiceOperations, notificationPublishService, pceListenerImpl, rendererListenerImpl,
193             networkModelListenerImpl, serviceDataStoreOperations, catalogDataStoreOperations);
194
195         TapiConnectivityImpl tapiConnectivity = new TapiConnectivityImpl(serviceHandler, tapiContext, connectivityUtils,
196             tapipceNotificationHandler, tapirendererNotificationHandler, networkTransactionService);
197
198         ListenableFuture<RpcResult<DeleteConnectivityServiceOutput>> result =
199             tapiConnectivity.deleteConnectivityService(new DeleteConnectivityServiceInputBuilder().build());
200         result.addListener(new Runnable() {
201             @Override
202             public void run() {
203                 endSignal.countDown();
204             }
205         }, executorService);
206
207         endSignal.await();
208
209         RpcResult<DeleteConnectivityServiceOutput> rpcResult = result.get();
210         assertEquals(ErrorType.RPC, rpcResult.getErrors().get(0).getErrorType());
211     }
212
213     @Test
214     void deleteConnServiceShouldBeFailedWithNonExistService() throws ExecutionException, InterruptedException {
215         DeleteConnectivityServiceInput input = TapiConnectivityDataUtils.buildConnServiceDeleteInput1();
216         OrgOpenroadmServiceService serviceHandler = new ServicehandlerImpl(rpcProviderService, pathComputationService,
217             rendererServiceOperations, notificationPublishService, pceListenerImpl, rendererListenerImpl,
218             networkModelListenerImpl, serviceDataStoreOperations, catalogDataStoreOperations);
219         TapiConnectivityImpl tapiConnectivity = new TapiConnectivityImpl(serviceHandler, tapiContext, connectivityUtils,
220             tapipceNotificationHandler, tapirendererNotificationHandler, networkTransactionService);
221         ListenableFuture<RpcResult<DeleteConnectivityServiceOutput>> result =
222             tapiConnectivity.deleteConnectivityService(input);
223         result.addListener(new Runnable() {
224             @Override
225             public void run() {
226                 endSignal.countDown();
227             }
228         }, executorService);
229
230         endSignal.await();
231
232         RpcResult<DeleteConnectivityServiceOutput> rpcResult = result.get();
233         assertEquals(ErrorType.RPC, rpcResult.getErrors().get(0).getErrorType());
234     }
235
236     @Test
237     void deleteConnServiceShouldBeSuccessForExistingService() throws ExecutionException, InterruptedException {
238         ConfigurationResponseCommon crc = new ConfigurationResponseCommonBuilder()
239             .setRequestId("request 1")
240             .setResponseCode("OK")
241             .setAckFinalIndicator("requestProcessed").build();
242         ServiceDeleteOutput sdo = new ServiceDeleteOutputBuilder()
243             .setConfigurationResponseCommon(crc)
244             .build();
245         when(rendererServiceOperations.serviceDelete(any(), any()))
246             .thenReturn(Futures.immediateFuture(sdo));
247
248         OrgOpenroadmServiceService serviceHandler = new ServicehandlerImpl(rpcProviderService, pathComputationService,
249             rendererServiceOperations, notificationPublishService, pceListenerImpl, rendererListenerImpl,
250             networkModelListenerImpl, serviceDataStoreOperations, catalogDataStoreOperations);
251
252         TapiConnectivityImpl tapiConnectivity = new TapiConnectivityImpl(serviceHandler, tapiContext, connectivityUtils,
253             tapipceNotificationHandler, tapirendererNotificationHandler, networkTransactionService);
254         ServiceCreateInput createInput = TapiConnectivityDataUtils.buildServiceCreateInput();
255         serviceDataStoreOperations.createService(createInput);
256         tapiContext.updateConnectivityContext(TapiConnectivityDataUtils.createConnService(), new HashMap<>());
257         DeleteConnectivityServiceInput input = TapiConnectivityDataUtils.buildConnServiceDeleteInput();
258         ListenableFuture<RpcResult<DeleteConnectivityServiceOutput>> result =
259             tapiConnectivity.deleteConnectivityService(input);
260         result.addListener(new Runnable() {
261             @Override
262             public void run() {
263                 endSignal.countDown();
264             }
265         }, executorService);
266
267         endSignal.await();
268         LOG.debug("RESULT = {}", result.toString());
269         RpcResult<DeleteConnectivityServiceOutput> rpcResult = result.get();
270         assertTrue(rpcResult.isSuccessful());
271     }
272 }