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