Merge "Refactor TAPI Connectivity Utils"
[transportpce.git] / servicehandler / src / test / java / org / opendaylight / transportpce / servicehandler / impl / ServicehandlerImplTest.java
1 /*
2  * Copyright © 2019 Orange, 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.servicehandler.impl;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.jupiter.api.Assertions.assertEquals;
13 import static org.junit.jupiter.api.Assertions.assertNull;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
19 import static org.opendaylight.transportpce.servicehandler.impl.ServicehandlerImpl.LogMessages;
20
21 import com.google.common.util.concurrent.Futures;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import com.google.common.util.concurrent.ListeningExecutorService;
24 import com.google.common.util.concurrent.MoreExecutors;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.concurrent.CountDownLatch;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.Executors;
30 import org.junit.Assert;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockito.Mock;
35 import org.mockito.junit.jupiter.MockitoExtension;
36 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
37 import org.opendaylight.mdsal.binding.api.RpcProviderService;
38 import org.opendaylight.transportpce.common.ResponseCodes;
39 import org.opendaylight.transportpce.common.network.NetworkTransactionImpl;
40 import org.opendaylight.transportpce.pce.service.PathComputationService;
41 import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperations;
42 import org.opendaylight.transportpce.servicehandler.ServiceInput;
43 import org.opendaylight.transportpce.servicehandler.catalog.CatalogDataStoreOperations;
44 import org.opendaylight.transportpce.servicehandler.catalog.CatalogDataStoreOperationsImpl;
45 import org.opendaylight.transportpce.servicehandler.listeners.NetworkListener;
46 import org.opendaylight.transportpce.servicehandler.listeners.PceListener;
47 import org.opendaylight.transportpce.servicehandler.listeners.RendererListener;
48 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
49 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperationsImpl;
50 import org.opendaylight.transportpce.servicehandler.utils.CatalogDataUtils;
51 import org.opendaylight.transportpce.servicehandler.utils.ServiceDataUtils;
52 import org.opendaylight.transportpce.test.AbstractTest;
53 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev240205.PathComputationRequestOutputBuilder;
54 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev240205.PathComputationRerouteRequestOutputBuilder;
55 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev240205.service.path.rpc.result.PathDescription;
56 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.configuration.response.common.ConfigurationResponseCommonBuilder;
57 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
58 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.AddOpenroadmOperationalModesToCatalogInputBuilder;
59 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.AddOpenroadmOperationalModesToCatalogOutput;
60 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.AddSpecificOperationalModesToCatalogInputBuilder;
61 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.AddSpecificOperationalModesToCatalogOutput;
62 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceCreateInput;
63 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceCreateInputBuilder;
64 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceCreateOutput;
65 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceDeleteInput;
66 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceDeleteInputBuilder;
67 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceDeleteOutput;
68 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceFeasibilityCheckInputBuilder;
69 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceFeasibilityCheckOutput;
70 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceReconfigureInput;
71 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceReconfigureInputBuilder;
72 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceReconfigureOutput;
73 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceRerouteInput;
74 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceRerouteInputBuilder;
75 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceRerouteOutput;
76 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceRestorationInput;
77 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceRestorationInputBuilder;
78 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceRestorationOutput;
79 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceSrlgGetInputBuilder;
80 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceSrlgGetOutput;
81 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.TempServiceCreateInput;
82 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.TempServiceCreateInputBuilder;
83 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.TempServiceCreateOutput;
84 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.TempServiceDeleteInputBuilder;
85 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.TempServiceDeleteOutput;
86 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.delete.input.ServiceDeleteReqInfoBuilder;
87 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.list.ServicesBuilder;
88 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.AToZDirectionBuilder;
89 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.ZToADirectionBuilder;
90 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.atoz.direction.AToZ;
91 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.atoz.direction.AToZBuilder;
92 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.path.description.atoz.direction.AToZKey;
93 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce.resource.ResourceBuilder;
94 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.pathdescription.rev230501.pce.resource.resource.resource.TerminationPointBuilder;
95 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.response.parameters.sp.ResponseParametersBuilder;
96 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.response.parameters.sp.response.parameters.PathDescriptionBuilder;
97 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
98 import org.opendaylight.yangtools.yang.common.ErrorTag;
99 import org.opendaylight.yangtools.yang.common.ErrorType;
100 import org.opendaylight.yangtools.yang.common.RpcResult;
101 import org.opendaylight.yangtools.yang.common.Uint32;
102
103 @ExtendWith(MockitoExtension.class)
104 public class ServicehandlerImplTest extends AbstractTest {
105     @Mock
106     private RpcProviderService rpcProviderService;
107     @Mock
108     private PathComputationService pathComputationService;
109     @Mock
110     private RendererServiceOperations rendererServiceOperations;
111     @Mock
112     private NotificationPublishService notificationPublishService;
113     @Mock
114     private PceListener pceListenerImpl;
115     @Mock
116     private RendererListener rendererListenerImpl;
117     @Mock
118     private NetworkListener networkModelListenerImpl;
119
120     @Mock
121     private PathDescription pathDescription;
122     private ServiceDataStoreOperations serviceDataStoreOperations;
123     private CatalogDataStoreOperations catalogDataStoreOperations;
124     private ServiceCreateInput serviceCreateInput;
125     private ServiceDeleteInput serviceDeleteInput;
126     private ServiceReconfigureInput serviceReconfigureInput;
127     private ServiceRestorationInput serviceRestorationInput;
128     private ServiceRerouteInput serviceRerouteInput;
129     private ListeningExecutorService executorService;
130     private CountDownLatch endSignal;
131     private static final int NUM_THREADS = 5;
132
133     @BeforeEach
134     void setUp() {
135         executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
136         endSignal = new CountDownLatch(1);
137         this.serviceDataStoreOperations = new ServiceDataStoreOperationsImpl(getNewDataBroker());
138         this.catalogDataStoreOperations =
139             new CatalogDataStoreOperationsImpl(new NetworkTransactionImpl(getDataBroker()));
140         serviceCreateInput = ServiceDataUtils.buildServiceCreateInput();
141         serviceDeleteInput = ServiceDataUtils.buildServiceDeleteInput();
142         serviceReconfigureInput = ServiceDataUtils.buildServiceReconfigureInput();
143         serviceRestorationInput = ServiceDataUtils.buildServiceRestorationInput();
144         serviceRerouteInput = ServiceDataUtils.buildServiceRerouteInput();
145         pathDescription = ServiceDataUtils.createPathDescription(0,1,0,1);
146     }
147
148     @Test
149     void testRpcRegistration() {
150         new ServicehandlerImpl(rpcProviderService,
151             pathComputationService, rendererServiceOperations, notificationPublishService,
152             pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
153             serviceDataStoreOperations, catalogDataStoreOperations);
154         verify(rpcProviderService, times(1)).registerRpcImplementations(any());
155     }
156
157     @Test
158     void testNotImplementedRpc() throws InterruptedException, ExecutionException {
159         ListenableFuture<RpcResult<ServiceSrlgGetOutput>> result = new ServicehandlerImpl(rpcProviderService,
160                 pathComputationService, rendererServiceOperations, notificationPublishService,
161                 pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
162                 serviceDataStoreOperations, catalogDataStoreOperations)
163             .serviceSrlgGet(new ServiceSrlgGetInputBuilder().build());
164         result.addListener(() -> endSignal.countDown(), executorService);
165         endSignal.await();
166         assertNotNull(result.get());
167         assertFalse(result.get().isSuccessful());
168         assertNull(result.get().getResult());
169         assertEquals(ErrorType.RPC, result.get().getErrors().get(0).getErrorType());
170         assertEquals(ErrorSeverity.ERROR, result.get().getErrors().get(0).getSeverity());
171         assertEquals(ErrorTag.OPERATION_NOT_SUPPORTED, result.get().getErrors().get(0).getTag());
172         assertEquals("RPC not implemented yet", result.get().getErrors().get(0).getMessage());
173     }
174
175     @Test
176     void createServiceShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
177         ListenableFuture<RpcResult<ServiceCreateOutput>> result =
178             new ServicehandlerImpl(rpcProviderService,
179                     pathComputationService, rendererServiceOperations, notificationPublishService,
180                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
181                     serviceDataStoreOperations, catalogDataStoreOperations)
182                 .serviceCreate(new ServiceCreateInputBuilder().build());
183         result.addListener(() -> endSignal.countDown(), executorService);
184         endSignal.await();
185         assertEquals(
186             ResponseCodes.RESPONSE_FAILED,
187             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
188     }
189
190     @Test
191     void createServiceShouldBeFailedWithServiceAlreadyExist() throws ExecutionException, InterruptedException {
192         final ServiceDataStoreOperations serviceDSOperations = mock(ServiceDataStoreOperations.class);
193         when(serviceDSOperations.getService(serviceCreateInput.getServiceName()))
194                 .thenReturn(Optional.of(
195                         new ServicesBuilder()
196                                 .setServiceName(serviceCreateInput.getServiceName())
197                                 .build()));
198         ListenableFuture<RpcResult<ServiceCreateOutput>> result =
199             new ServicehandlerImpl(rpcProviderService,
200                     pathComputationService, rendererServiceOperations, notificationPublishService,
201                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
202                     serviceDSOperations, catalogDataStoreOperations)
203                 .serviceCreate(serviceCreateInput);
204         result.addListener(() -> endSignal.countDown(), executorService);
205         endSignal.await();
206         assertEquals(
207             ResponseCodes.RESPONSE_FAILED,
208             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
209     }
210
211     @Test
212     void createServiceShouldBeSuccessfulWhenPerformPCESuccessful() throws ExecutionException, InterruptedException {
213         when(pathComputationService.pathComputationRequest(any())).thenReturn(Futures.immediateFuture(any()));
214         ListenableFuture<RpcResult<ServiceCreateOutput>> result =
215             new ServicehandlerImpl(rpcProviderService,
216                     pathComputationService, rendererServiceOperations, notificationPublishService,
217                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
218                     serviceDataStoreOperations, catalogDataStoreOperations)
219                 .serviceCreate(serviceCreateInput);
220         result.addListener(() -> endSignal.countDown(), executorService);
221         endSignal.await();
222         assertEquals(
223             ResponseCodes.RESPONSE_OK,
224             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
225     }
226
227     @Test
228     void deleteServiceShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
229         ListenableFuture<RpcResult<ServiceDeleteOutput>> result =
230             new ServicehandlerImpl(rpcProviderService,
231                     pathComputationService, rendererServiceOperations, notificationPublishService,
232                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
233                     serviceDataStoreOperations, catalogDataStoreOperations)
234                 .serviceDelete(
235                     new ServiceDeleteInputBuilder()
236                         .setServiceDeleteReqInfo(new ServiceDeleteReqInfoBuilder()
237                                 .setServiceName("")
238                                 .build())
239                         .build());
240         result.addListener(() -> endSignal.countDown(), executorService);
241         endSignal.await();
242         assertEquals(
243             ResponseCodes.RESPONSE_FAILED,
244             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
245     }
246
247     @Test
248     void deleteServiceShouldBeFailedWithNonExistService() throws ExecutionException, InterruptedException {
249         ListenableFuture<RpcResult<ServiceDeleteOutput>> result =
250             new ServicehandlerImpl(rpcProviderService,
251                     pathComputationService, rendererServiceOperations, notificationPublishService,
252                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
253                     serviceDataStoreOperations, catalogDataStoreOperations)
254                 .serviceDelete(serviceDeleteInput);
255         result.addListener(() -> endSignal.countDown(), executorService);
256         endSignal.await();
257         assertEquals(
258             ResponseCodes.RESPONSE_FAILED,
259             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
260     }
261
262     @Test
263     void deleteServiceShouldBeSuccessForExistingService() throws ExecutionException, InterruptedException {
264         when(rendererServiceOperations.serviceDelete(any(), any())).thenReturn(Futures.immediateFuture(any()));
265         serviceDataStoreOperations.createService(serviceCreateInput);
266         ListenableFuture<RpcResult<ServiceDeleteOutput>> result =
267             new ServicehandlerImpl(rpcProviderService,
268                     pathComputationService, rendererServiceOperations, notificationPublishService,
269                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
270                     serviceDataStoreOperations, catalogDataStoreOperations)
271                 .serviceDelete(serviceDeleteInput);
272         result.addListener(() -> endSignal.countDown(), executorService);
273         endSignal.await();
274         assertEquals(
275             ResponseCodes.RESPONSE_OK,
276             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
277     }
278
279     @Test
280     void serviceFeasibilityCheckShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
281         ServicehandlerImpl servicehandlerImpl = new ServicehandlerImpl(rpcProviderService, pathComputationService,
282                 rendererServiceOperations, notificationPublishService, pceListenerImpl, rendererListenerImpl,
283                 networkModelListenerImpl, serviceDataStoreOperations, catalogDataStoreOperations);
284         ListenableFuture<RpcResult<ServiceFeasibilityCheckOutput>> result =
285                 servicehandlerImpl.serviceFeasibilityCheck(new ServiceFeasibilityCheckInputBuilder().build());
286         result.addListener(() -> endSignal.countDown(), executorService);
287         endSignal.await();
288         assertEquals(
289             ResponseCodes.RESPONSE_FAILED,
290             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
291     }
292
293     @Test
294     void serviceFeasibilityCheckShouldBeSuccessfulWhenPerformPCESuccessful()
295             throws ExecutionException, InterruptedException {
296         when(pathComputationService.pathComputationRequest(any())).thenReturn(Futures.immediateFuture(any()));
297         ListenableFuture<RpcResult<ServiceFeasibilityCheckOutput>> result =
298             new ServicehandlerImpl(rpcProviderService,
299                     pathComputationService, rendererServiceOperations, notificationPublishService,
300                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
301                     serviceDataStoreOperations, catalogDataStoreOperations)
302                 .serviceFeasibilityCheck(ServiceDataUtils.buildServiceFeasibilityCheckInput());
303         result.addListener(() -> endSignal.countDown(), executorService);
304         endSignal.await();
305         assertEquals(
306             ResponseCodes.RESPONSE_OK,
307             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
308     }
309
310     @Test
311     void serviceReconfigureShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
312         ListenableFuture<RpcResult<ServiceReconfigureOutput>> result =
313             new ServicehandlerImpl(rpcProviderService,
314                     pathComputationService, rendererServiceOperations, notificationPublishService,
315                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
316                     serviceDataStoreOperations, catalogDataStoreOperations)
317                 .serviceReconfigure(new ServiceReconfigureInputBuilder().setServiceName("").build());
318         result.addListener(() -> endSignal.countDown(), executorService);
319         endSignal.await();
320     }
321
322
323     @Test
324     void serviceReconfigureShouldBeFailedWithNonExistService() throws ExecutionException, InterruptedException {
325         //action -> service reconfigure
326         ListenableFuture<RpcResult<ServiceReconfigureOutput>> result =
327             new ServicehandlerImpl(rpcProviderService,
328                     pathComputationService, rendererServiceOperations, notificationPublishService,
329                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
330                     serviceDataStoreOperations, catalogDataStoreOperations)
331                 .serviceReconfigure(serviceReconfigureInput);
332         result.addListener(() -> endSignal.countDown(), executorService);
333         endSignal.await();
334     }
335
336     @Test
337     void serviceReconfigureShouldBeSuccessForExistingService() throws ExecutionException, InterruptedException {
338         // serviceReconfigure is calling service delete method in renderer
339         when(rendererServiceOperations.serviceDelete(any(), any())).thenReturn(Futures.immediateFuture(any()));
340         //create service to reconfigure
341         serviceDataStoreOperations.createService(serviceCreateInput);
342         //service reconfigure test action
343         //ServiceReconfigureInput is created with the same service information that is created before
344         ListenableFuture<RpcResult<ServiceReconfigureOutput>> result =
345             new ServicehandlerImpl(rpcProviderService,
346                     pathComputationService, rendererServiceOperations, notificationPublishService,
347                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
348                     serviceDataStoreOperations, catalogDataStoreOperations)
349                 .serviceReconfigure(serviceReconfigureInput);
350         result.addListener(() -> endSignal.countDown(), executorService);
351         endSignal.await();
352     }
353
354     @Test
355     void serviceReRestorationShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
356         ListenableFuture<RpcResult<ServiceRestorationOutput>> result =
357             new ServicehandlerImpl(rpcProviderService,
358                     pathComputationService, rendererServiceOperations, notificationPublishService,
359                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
360                     serviceDataStoreOperations, catalogDataStoreOperations)
361                 .serviceRestoration(new ServiceRestorationInputBuilder()
362                 .setServiceName("")
363                 .build());
364         result.addListener(() -> endSignal.countDown(), executorService);
365         endSignal.await();
366     }
367
368     @Test
369     void serviceRestorationShouldBeFailedWithNonExistService() throws ExecutionException, InterruptedException {
370         //action -> service restore
371         ListenableFuture<RpcResult<ServiceRestorationOutput>> result =
372             new ServicehandlerImpl(rpcProviderService,
373                     pathComputationService, rendererServiceOperations, notificationPublishService,
374                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
375                     serviceDataStoreOperations, catalogDataStoreOperations)
376                 .serviceRestoration(serviceRestorationInput);
377         result.addListener(() -> endSignal.countDown(), executorService);
378         endSignal.await();
379     }
380
381     @Test
382     void serviceRestorationShouldBeSuccessForExistingService() throws ExecutionException, InterruptedException {
383         // serviceRestoration is calling service delete method in renderer
384         when(rendererServiceOperations.serviceDelete(any(), any())).thenReturn(Futures.immediateFuture(any()));
385         //create service to restore
386         serviceDataStoreOperations.createService(serviceCreateInput);
387         //service Restoration test action
388         //ServiceRestorationInput is created with the same service information that is created before
389         ListenableFuture<RpcResult<ServiceRestorationOutput>> result =
390             new ServicehandlerImpl(rpcProviderService,
391                     pathComputationService, rendererServiceOperations, notificationPublishService,
392                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
393                     serviceDataStoreOperations, catalogDataStoreOperations)
394                 .serviceRestoration(serviceRestorationInput);
395         result.addListener(() -> endSignal.countDown(), executorService);
396         endSignal.await();
397     }
398
399     @Test
400     void serviceRerouteShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
401         ListenableFuture<RpcResult<ServiceRerouteOutput>> result =
402             new ServicehandlerImpl(rpcProviderService,
403                     pathComputationService, rendererServiceOperations, notificationPublishService,
404                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
405                     serviceDataStoreOperations, catalogDataStoreOperations)
406                 .serviceReroute(new ServiceRerouteInputBuilder()
407                 .setServiceName("")
408                 .build());
409         result.addListener(() -> endSignal.countDown(), executorService);
410         endSignal.await();
411         assertEquals(
412             ResponseCodes.RESPONSE_FAILED,
413             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
414     }
415
416     @Test
417     void serviceRerouteShouldBeFailedWithNonExistService() throws ExecutionException, InterruptedException {
418         //action -> service reconfigure
419         ListenableFuture<RpcResult<ServiceRerouteOutput>> result =
420             new ServicehandlerImpl(rpcProviderService,
421                     pathComputationService, rendererServiceOperations, notificationPublishService,
422                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
423                     serviceDataStoreOperations, catalogDataStoreOperations)
424                 .serviceReroute(serviceRerouteInput);
425         result.addListener(() -> endSignal.countDown(), executorService);
426         endSignal.await();
427         assertEquals(
428             ResponseCodes.RESPONSE_FAILED,
429             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
430     }
431
432     @Test
433     void serviceRerouteShouldBeSuccessForExistingService() throws ExecutionException, InterruptedException {
434         when(pathComputationService.pathComputationRerouteRequest(any()))
435             .thenReturn(Futures.immediateFuture(new PathComputationRerouteRequestOutputBuilder()
436                     .setConfigurationResponseCommon(new ConfigurationResponseCommonBuilder()
437                             .setResponseCode(ResponseCodes.RESPONSE_OK)
438                             .build())
439                     .build()));
440         Map<AToZKey, AToZ> atoz = Map.of(
441                 new AToZKey("0"),
442                 new AToZBuilder()
443                         .setId("0")
444                         .setResource(new ResourceBuilder()
445                                 .setResource(new TerminationPointBuilder()
446                                         .setTpNodeId("tpNodeIdC")
447                                         .setTpId("TpIdC1")
448                                         .build())
449                                 .setState(State.InService)
450                                 .build())
451                         .build(),
452                 new AToZKey("1"),
453                 new AToZBuilder()
454                         .setId("1")
455                         .setResource(new ResourceBuilder()
456                                 .setResource(new TerminationPointBuilder()
457                                         .setTpNodeId("tpNodeIdD")
458                                         .setTpId("TpIdCD")
459                                         .build())
460                                 .setState(State.InService)
461                                 .build())
462                         .build(), new AToZKey("2"),
463                 new AToZBuilder()
464                         .setId("2")
465                         .setResource(new ResourceBuilder()
466                                 .setResource(new TerminationPointBuilder()
467                                         .setTpNodeId("tpNodeIdA")
468                                         .setTpId("TpIdA1")
469                                         .build())
470                                 .setState(State.InService)
471                                 .build())
472                         .build()
473
474         );
475         serviceDataStoreOperations.createServicePath(new ServiceInput(serviceCreateInput),
476                 new PathComputationRequestOutputBuilder()
477                         .setResponseParameters(new ResponseParametersBuilder()
478                                 .setPathDescription(new PathDescriptionBuilder()
479                                         .setAToZDirection(new AToZDirectionBuilder()
480                                                 .setAToZ(atoz)
481                                                 .setRate(Uint32.valueOf(1))
482                                                 .build())
483                                         .setZToADirection(new ZToADirectionBuilder()
484                                                 .setRate(Uint32.valueOf(1))
485                                                 .build())
486                                         .build())
487                                 .build())
488                         .build());
489         serviceDataStoreOperations.createService(serviceCreateInput);
490         ListenableFuture<RpcResult<ServiceRerouteOutput>> result =
491             new ServicehandlerImpl(rpcProviderService,
492                     pathComputationService, rendererServiceOperations, notificationPublishService,
493                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
494                     serviceDataStoreOperations, catalogDataStoreOperations)
495                 .serviceReroute(serviceRerouteInput);
496         result.addListener(() -> endSignal.countDown(), executorService);
497         endSignal.await();
498         assertEquals(
499             ResponseCodes.RESPONSE_OK,
500             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
501     }
502
503     @Test
504     void tempServiceDeleteShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
505         ListenableFuture<RpcResult<TempServiceDeleteOutput>> result =
506             new ServicehandlerImpl(rpcProviderService,
507                     pathComputationService, rendererServiceOperations, notificationPublishService,
508                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
509                     serviceDataStoreOperations, catalogDataStoreOperations)
510                 .tempServiceDelete(new TempServiceDeleteInputBuilder()
511                 .setCommonId("")
512                 .build());
513         result.addListener(() -> endSignal.countDown(), executorService);
514         endSignal.await();
515         RpcResult<TempServiceDeleteOutput> rpcResult = result.get();
516         assertEquals(
517             ResponseCodes.RESPONSE_FAILED,
518             rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
519         assertEquals(
520             LogMessages.SERVICE_NON_COMPLIANT,
521             rpcResult.getResult().getConfigurationResponseCommon().getResponseMessage());
522     }
523
524     @Test
525     void tempServiceDeleteShouldBeFailedWithNonExistService() throws ExecutionException, InterruptedException {
526         ListenableFuture<RpcResult<TempServiceDeleteOutput>> result =
527             new ServicehandlerImpl(rpcProviderService,
528                     pathComputationService, rendererServiceOperations, notificationPublishService,
529                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
530                     serviceDataStoreOperations, catalogDataStoreOperations)
531                 .tempServiceDelete(ServiceDataUtils.buildTempServiceDeleteInput());
532         result.addListener(() -> endSignal.countDown(), executorService);
533         endSignal.await();
534         assertEquals(
535             ResponseCodes.RESPONSE_FAILED,
536             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
537     }
538
539     @Test
540     void tempServiceDeleteShouldBeSuccessForExistingService() throws ExecutionException, InterruptedException {
541         when(rendererServiceOperations.serviceDelete(any(), any())).thenReturn(Futures.immediateFuture(any()));
542         //create temp service to delete in the temp delete action
543         TempServiceCreateInput createInput = ServiceDataUtils.buildTempServiceCreateInput();
544         serviceDataStoreOperations.createTempService(createInput, pathDescription);
545         ListenableFuture<RpcResult<TempServiceDeleteOutput>> result =
546                 new ServicehandlerImpl(rpcProviderService,
547                         pathComputationService, rendererServiceOperations, notificationPublishService,
548                         pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
549                         serviceDataStoreOperations, catalogDataStoreOperations)
550                     .tempServiceDelete(ServiceDataUtils.buildTempServiceDeleteInput(createInput.getCommonId()));
551         result.addListener(() -> endSignal.countDown(), executorService);
552         endSignal.await();
553         assertEquals(
554             ResponseCodes.RESPONSE_OK,
555             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
556     }
557
558     @Test
559     void tempServiceCreateShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
560         ListenableFuture<RpcResult<TempServiceCreateOutput>> result =
561             new ServicehandlerImpl(rpcProviderService,
562                     pathComputationService, rendererServiceOperations, notificationPublishService,
563                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
564                     serviceDataStoreOperations, catalogDataStoreOperations)
565                 .tempServiceCreate(new TempServiceCreateInputBuilder().build());
566         result.addListener(() -> endSignal.countDown(), executorService);
567         endSignal.await();
568         assertEquals(
569             ResponseCodes.RESPONSE_FAILED,
570             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
571     }
572
573     @Test
574     void tempServiceCreateShouldBeFailedWithServiceAlreadyExist() throws ExecutionException, InterruptedException {
575         final ServiceDataStoreOperations serviceDSOperations = mock(ServiceDataStoreOperations.class);
576         when(serviceDSOperations.getTempService(any()))
577             .thenReturn(Optional.of(
578                     new org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.temp.service.list
579                             .ServicesBuilder()
580                         .setCommonId("bad_commonId")
581                         .build()));
582         ListenableFuture<RpcResult<TempServiceCreateOutput>> result =
583             new ServicehandlerImpl(rpcProviderService,
584                     pathComputationService, rendererServiceOperations, notificationPublishService,
585                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
586                     serviceDSOperations, catalogDataStoreOperations)
587                 .tempServiceCreate(ServiceDataUtils.buildTempServiceCreateInput());
588         result.addListener(() -> endSignal.countDown(), executorService);
589         endSignal.await();
590         assertEquals(
591             ResponseCodes.RESPONSE_FAILED,
592             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
593         assertEquals(
594             "Service 'Temp (commonId)' already exists in datastore",
595             result.get().getResult().getConfigurationResponseCommon().getResponseMessage());
596     }
597
598     @Test
599     void tempServiceCreateShouldBeSuccessfulWhenPerformPCESuccessful()
600             throws ExecutionException, InterruptedException {
601         when(pathComputationService.pathComputationRequest(any())).thenReturn(Futures.immediateFuture(any()));
602         ListenableFuture<RpcResult<TempServiceCreateOutput>> result =
603             new ServicehandlerImpl(rpcProviderService,
604                     pathComputationService, rendererServiceOperations, notificationPublishService,
605                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
606                     serviceDataStoreOperations, catalogDataStoreOperations)
607                 .tempServiceCreate(ServiceDataUtils.buildTempServiceCreateInput());
608         result.addListener(() -> endSignal.countDown(), executorService);
609         endSignal.await();
610         assertEquals(
611             ResponseCodes.RESPONSE_OK,
612             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
613     }
614
615     @Test
616     public void addOpenroadmOperationalModesToCatalogShouldBeFailedWithEmptyInput()
617             throws ExecutionException, InterruptedException {
618         ListenableFuture<RpcResult<AddOpenroadmOperationalModesToCatalogOutput>> result =
619             new ServicehandlerImpl(rpcProviderService,
620                     pathComputationService, rendererServiceOperations, notificationPublishService,
621                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
622                     serviceDataStoreOperations, catalogDataStoreOperations)
623                 .addOpenroadmOperationalModesToCatalog(new AddOpenroadmOperationalModesToCatalogInputBuilder().build());
624         Assert.assertEquals(
625             ResponseCodes.RESPONSE_FAILED,
626             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
627     }
628
629     @Test
630     public void addSpecificOperationalModesToCatalogShouldBeFailedWithEmptyInput()
631             throws ExecutionException, InterruptedException {
632         ListenableFuture<RpcResult<AddSpecificOperationalModesToCatalogOutput>> result =
633             new ServicehandlerImpl(rpcProviderService,
634                     pathComputationService, rendererServiceOperations, notificationPublishService,
635                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
636                     serviceDataStoreOperations, catalogDataStoreOperations)
637                 .addSpecificOperationalModesToCatalog(new AddSpecificOperationalModesToCatalogInputBuilder().build());
638         Assert.assertEquals(
639             ResponseCodes.RESPONSE_FAILED,
640             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
641     }
642
643     @Test
644     public void addOpenroadmOperationalModesToCatalogShouldBeSuccessfulWhenAddORToCatalog()
645             throws ExecutionException, InterruptedException {
646         ListenableFuture<RpcResult<AddOpenroadmOperationalModesToCatalogOutput>> result =
647             new ServicehandlerImpl(rpcProviderService,
648                     pathComputationService, rendererServiceOperations, notificationPublishService,
649                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
650                     serviceDataStoreOperations, catalogDataStoreOperations)
651                 .addOpenroadmOperationalModesToCatalog(CatalogDataUtils.buildAddORToCatalogInput());
652         Assert.assertEquals(
653             ResponseCodes.RESPONSE_OK,
654             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
655     }
656
657     @Test
658     public void addSpecificOperationalModesToCatalogShouldBeSuccessfulWhenAddSpecificToCatalog()
659             throws ExecutionException, InterruptedException {
660         ListenableFuture<RpcResult<AddSpecificOperationalModesToCatalogOutput>> result =
661             new ServicehandlerImpl(rpcProviderService,
662                     pathComputationService, rendererServiceOperations, notificationPublishService,
663                     pceListenerImpl, rendererListenerImpl, networkModelListenerImpl,
664                     serviceDataStoreOperations, catalogDataStoreOperations)
665                 .addSpecificOperationalModesToCatalog(CatalogDataUtils.buildAddSpecificToCatalogInput());
666         Assert.assertEquals(
667             ResponseCodes.RESPONSE_OK,
668             result.get().getResult().getConfigurationResponseCommon().getResponseCode());
669     }
670 }