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