2 * Copyright © 2019 Orange, Inc. and others. All rights reserved.
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
8 package org.opendaylight.transportpce.servicehandler.impl;
10 import static org.mockito.ArgumentMatchers.any;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 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;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.MockitoAnnotations;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
29 import org.opendaylight.transportpce.common.ResponseCodes;
30 import org.opendaylight.transportpce.pce.service.PathComputationService;
31 import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperations;
32 import org.opendaylight.transportpce.servicehandler.listeners.PceListenerImpl;
33 import org.opendaylight.transportpce.servicehandler.listeners.RendererListenerImpl;
34 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperationsImpl;
35 import org.opendaylight.transportpce.servicehandler.utils.ServiceDataUtils;
36 import org.opendaylight.transportpce.test.AbstractTest;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateInput;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateInputBuilder;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateOutput;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteInput;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteInputBuilder;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteOutput;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceFeasibilityCheckInput;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceFeasibilityCheckInputBuilder;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceFeasibilityCheckOutput;
46 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.delete.input.ServiceDeleteReqInfoBuilder;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
49 public class ServicehandlerImplTest extends AbstractTest {
52 private PathComputationService pathComputationService;
55 private RendererServiceOperations rendererServiceOperations;
58 private NotificationPublishService notificationPublishService;
61 private PceListenerImpl pceListenerImpl;
64 private RendererListenerImpl rendererListenerImpl;
66 private ListeningExecutorService executorService;
67 private CountDownLatch endSignal;
68 private static final int NUM_THREADS = 5;
69 private boolean callbackRan;
73 executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
74 endSignal = new CountDownLatch(1);
76 MockitoAnnotations.initMocks(this);
80 public void createServiceShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
81 ServicehandlerImpl servicehandlerImpl =
82 new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
83 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
84 ListenableFuture<RpcResult<ServiceCreateOutput>> result =
85 servicehandlerImpl.serviceCreate(new ServiceCreateInputBuilder().build());
86 result.addListener(new Runnable() {
90 endSignal.countDown();
96 RpcResult<ServiceCreateOutput> rpcResult = result.get();
98 ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
102 public void createServiceShouldBeSuccessfulWhenPreformPCESuccessful()
103 throws ExecutionException, InterruptedException {
104 ServiceCreateInput input = ServiceDataUtils.buildServiceCreateInput();
105 Mockito.when(pathComputationService.pathComputationRequest(any())).thenReturn(Futures.immediateFuture(any()));
106 ServicehandlerImpl servicehandlerImpl =
107 new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
108 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
109 ListenableFuture<RpcResult<ServiceCreateOutput>> result = servicehandlerImpl.serviceCreate(input);
110 result.addListener(new Runnable() {
114 endSignal.countDown();
120 RpcResult<ServiceCreateOutput> rpcResult = result.get();
122 ResponseCodes.RESPONSE_OK, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
126 public void deleteServiceShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
127 ServicehandlerImpl servicehandlerImpl =
128 new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
129 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
130 ListenableFuture<RpcResult<ServiceDeleteOutput>> result =
131 servicehandlerImpl.serviceDelete(new ServiceDeleteInputBuilder()
132 .setServiceDeleteReqInfo(new ServiceDeleteReqInfoBuilder().setServiceName("").build()).build());
133 result.addListener(new Runnable() {
137 endSignal.countDown();
143 RpcResult<ServiceDeleteOutput> rpcResult = result.get();
145 ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
149 public void deleteServiceShouldBeFailedWithNonExistService() throws ExecutionException, InterruptedException {
150 ServiceDeleteInput input = ServiceDataUtils.buildServiceDeleteInput();
151 ServicehandlerImpl servicehandlerImpl =
152 new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
153 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
154 ListenableFuture<RpcResult<ServiceDeleteOutput>> result = servicehandlerImpl.serviceDelete(input);
155 result.addListener(new Runnable() {
159 endSignal.countDown();
165 RpcResult<ServiceDeleteOutput> rpcResult = result.get();
167 ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
171 public void deleteServiceShouldBeSuccessForExistingService() throws ExecutionException, InterruptedException {
172 DataBroker dataBroker = getNewDataBroker();
173 Mockito.when(rendererServiceOperations.serviceDelete(any())).thenReturn(Futures.immediateFuture(any()));
174 ServicehandlerImpl servicehandlerImpl =
175 new ServicehandlerImpl(dataBroker, pathComputationService, rendererServiceOperations,
176 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
177 ServiceDataStoreOperationsImpl serviceDataStoreOperations = new ServiceDataStoreOperationsImpl(dataBroker);
178 ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
179 serviceDataStoreOperations.createService(createInput);
180 ServiceDeleteInput input = ServiceDataUtils.buildServiceDeleteInput();
181 ListenableFuture<RpcResult<ServiceDeleteOutput>> result = servicehandlerImpl.serviceDelete(input);
182 result.addListener(new Runnable() {
186 endSignal.countDown();
192 RpcResult<ServiceDeleteOutput> rpcResult = result.get();
194 ResponseCodes.RESPONSE_OK, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
199 public void serviceFeasibilityCheckShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
200 ServicehandlerImpl servicehandlerImpl =
201 new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
202 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
203 ListenableFuture<RpcResult<ServiceFeasibilityCheckOutput>> result =
204 servicehandlerImpl.serviceFeasibilityCheck(new ServiceFeasibilityCheckInputBuilder().build());
205 result.addListener(new Runnable() {
209 endSignal.countDown();
215 RpcResult<ServiceFeasibilityCheckOutput> rpcResult = result.get();
217 ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
221 public void serviceFeasibilityCheckShouldBeSuccessfulWhenPreformPCESuccessful()
222 throws ExecutionException, InterruptedException {
223 ServiceFeasibilityCheckInput input = ServiceDataUtils.buildServiceFeasibilityCheckInput();
224 Mockito.when(pathComputationService.pathComputationRequest(any())).thenReturn(Futures.immediateFuture(any()));
225 ServicehandlerImpl servicehandlerImpl =
226 new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
227 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
228 ListenableFuture<RpcResult<ServiceFeasibilityCheckOutput>> result =
229 servicehandlerImpl.serviceFeasibilityCheck(input);
230 result.addListener(new Runnable() {
234 endSignal.countDown();
240 RpcResult<ServiceFeasibilityCheckOutput> rpcResult = result.get();
242 ResponseCodes.RESPONSE_OK, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());