Servicehandler Tests
[transportpce.git] / tests / stubrenderer / src / main / java / org / opendaylight / transportpce / stubrenderer / impl / StubrendererImpl.java
1 /*
2  * Copyright © 2017 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
9
10 package org.opendaylight.transportpce.stubrenderer.impl;
11
12 import com.google.common.util.concurrent.FutureCallback;
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
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.Future;
20
21 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
22 import org.opendaylight.transportpce.stubrenderer.SendingRendererRPCs;
23 import org.opendaylight.transportpce.stubrenderer.StubrendererCompliancyCheck;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceDeleteInput;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceDeleteOutput;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceDeleteOutputBuilder;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceImplementationRequestInput;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceImplementationRequestOutput;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceImplementationRequestOutputBuilder;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceRpcResultSp;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.ServiceRpcResultSpBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.StubrendererService;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.service.rpc.result.sp.PathTopology;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.stubrenderer.rev170426.service.rpc.result.sp.PathTopologyBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.configuration.response.common.ConfigurationResponseCommonBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.service.TopologyBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.RpcStatusEx;
38 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev170426.ServicePathNotificationTypes;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44
45
46 /**
47  * Class to implement StubrendererService.
48  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
49  *
50  */
51 public class StubrendererImpl implements StubrendererService {
52     /** Logging. */
53     private static final Logger LOG = LoggerFactory.getLogger(StubrendererImpl.class);
54     /** send notification. */
55     private NotificationPublishService notificationPublishService;
56     private ServiceRpcResultSp notification;
57     private final ListeningExecutorService executor = MoreExecutors
58             .listeningDecorator(Executors.newFixedThreadPool(10));
59     /** check service sdnc-request-header compliancy. */
60     private StubrendererCompliancyCheck compliancyCheck;
61
62     public StubrendererImpl(NotificationPublishService notificationPublishService) {
63         this.notificationPublishService = notificationPublishService;
64     }
65
66     @Override
67     public Future<RpcResult<ServiceImplementationRequestOutput>> serviceImplementationRequest(
68             ServiceImplementationRequestInput input) {
69         LOG.info("RPC  serviceImplementationRequest request received");
70         String responseCode = "";
71         String message = "";
72         ConfigurationResponseCommonBuilder configurationResponseCommon = null;
73
74         compliancyCheck = new StubrendererCompliancyCheck(input.getServiceName(), input.getServiceHandlerHeader());
75         if (compliancyCheck.check(false, true)) {
76             LOG.info("Service compliant !");
77             /**
78              * If compliant, service-request parameters are verified in order to
79              * check if there is no missing parameter that prevents calculating
80              * a path and implement a service.
81              */
82
83             notification = new ServiceRpcResultSpBuilder()
84                     .setNotificationType(ServicePathNotificationTypes.ServiceImplementationRequest)
85                     .setServiceName(input.getServiceName())
86                     .setStatus(RpcStatusEx.Pending)
87                     .setStatusMessage("Service compliant, submitting serviceImplementation Request ...")
88                     .build();
89             try {
90                 notificationPublishService.putNotification(notification);
91             } catch (InterruptedException e) {
92                 LOG.info("notification offer rejected : {}", e);
93             }
94
95             SendingRendererRPCs sendingRenderer = new SendingRendererRPCs(executor);
96             FutureCallback<Boolean> rendererCallback =
97                     new FutureCallback<Boolean>() {
98                 String message = "";
99                 ServiceRpcResultSp notification = null;
100
101                 @Override
102                 public void onFailure(Throwable arg0) {
103                     LOG.error("Failure message : {}", arg0.toString());
104                     LOG.error("Service implementation failed !");
105                     notification = new ServiceRpcResultSpBuilder()
106                             .setNotificationType(ServicePathNotificationTypes.ServiceImplementationRequest)
107                             .setServiceName(input.getServiceName()).setStatus(RpcStatusEx.Failed)
108                             .setStatusMessage("PCR Request failed  : {}" + arg0.getMessage()).build();
109                     try {
110                         notificationPublishService.putNotification(notification);
111                     } catch (InterruptedException e) {
112                         LOG.info("notification offer rejected : {}", e);
113                     }
114                 }
115
116                 @Override
117                 public void onSuccess(Boolean response) {
118                     LOG.info("response : {}", response);
119                     if (response) {
120                         message = "Service implemented !";
121                         TopologyBuilder topo = sendingRenderer.getTopology();
122                         ServiceRpcResultSpBuilder tmp = new ServiceRpcResultSpBuilder()
123                                 .setNotificationType(ServicePathNotificationTypes.ServiceImplementationRequest)
124                                 .setServiceName(input.getServiceName())
125                                 .setStatus(RpcStatusEx.Successful)
126                                 .setStatusMessage(message);
127                         if (topo != null) {
128                             PathTopology value = new PathTopologyBuilder()
129                                     .setAToZ(topo.getAToZ())
130                                     .setZToA(topo.getZToA())
131                                     .build();
132                             tmp.setPathTopology(value);
133                         }
134                         notification = tmp.build();
135                     } else {
136                         message = "Service implementation failed : " + sendingRenderer.getError();
137                         notification = new ServiceRpcResultSpBuilder()
138                                 .setNotificationType(ServicePathNotificationTypes.ServiceImplementationRequest)
139                                 .setServiceName("")
140                                 .setStatus(RpcStatusEx.Failed).setStatusMessage(message)
141                                 .build();
142                     }
143                     LOG.info(notification.toString());
144                     try {
145                         notificationPublishService.putNotification(notification);
146                     } catch (InterruptedException e) {
147                         LOG.info("notification offer rejected : {}", e);
148                     }
149                     LOG.info(message);
150                 }
151             };
152             ListenableFuture<Boolean> renderer = sendingRenderer.serviceImplementation();
153             Futures.addCallback(renderer, rendererCallback, executor);
154             LOG.info("Service implmentation Request in progress ");
155             configurationResponseCommon = new ConfigurationResponseCommonBuilder()
156                     .setAckFinalIndicator("Yes")
157                     .setRequestId(input.getServiceHandlerHeader().getRequestId())
158                     .setResponseCode("200")
159                     .setResponseMessage("Service implementation Request in progress ");
160
161             ServiceImplementationRequestOutput output = new ServiceImplementationRequestOutputBuilder()
162                     .setConfigurationResponseCommon(configurationResponseCommon.build())
163                     .build();
164             return RpcResultBuilder.success(output).buildFuture();
165         } else {
166             message = compliancyCheck.getMessage();
167             responseCode = "500";
168             LOG.info("Service not compliant caused by : {}", message);
169             notification = new ServiceRpcResultSpBuilder()
170                     .setNotificationType(ServicePathNotificationTypes.ServiceDelete)
171                     .setServiceName(input.getServiceName()).setStatus(RpcStatusEx.Failed)
172                     .setStatusMessage("Service not compliant caused by : " + message)
173                     .build();
174             try {
175                 notificationPublishService.putNotification(notification);
176             } catch (InterruptedException e) {
177                 LOG.info("notification offer rejected : {}", e);
178             }
179         }
180         configurationResponseCommon = new ConfigurationResponseCommonBuilder()
181                 .setAckFinalIndicator("yes")
182                 .setRequestId(input.getServiceHandlerHeader().getRequestId())
183                 .setResponseCode(responseCode)
184                 .setResponseMessage(message);
185         ServiceImplementationRequestOutput output = new ServiceImplementationRequestOutputBuilder()
186                 .setConfigurationResponseCommon(configurationResponseCommon.build())
187                 .build();
188
189         return RpcResultBuilder.success(output).buildFuture();
190     }
191
192     @Override
193     public Future<RpcResult<ServiceDeleteOutput>> serviceDelete(ServiceDeleteInput input) {
194         String message = "";
195         LOG.info("RPC serviceDelete request received");
196         String responseCode = "";
197         ConfigurationResponseCommonBuilder configurationResponseCommon = null;
198         compliancyCheck = new StubrendererCompliancyCheck(input.getServiceName(), input.getServiceHandlerHeader());
199         if (compliancyCheck.check(false, true)) {
200             LOG.info("Service compliant !");
201             /**
202              * If compliant, service-request parameters are verified in order to
203              * check if there is no missing parameter that prevents calculating
204              * a path and implement a service.
205              */
206
207             notification = new ServiceRpcResultSpBuilder()
208                     .setNotificationType(ServicePathNotificationTypes.ServiceDelete)
209                     .setServiceName(input.getServiceName())
210                     .setStatus(RpcStatusEx.Pending)
211                     .setStatusMessage("Service compliant, submitting serviceDelete Request ...")
212                     .build();
213             try {
214                 notificationPublishService.putNotification(notification);
215             } catch (InterruptedException e) {
216                 LOG.info("notification offer rejected : {}", e);
217             }
218             SendingRendererRPCs sendingRenderer = new SendingRendererRPCs(executor);
219             FutureCallback<Boolean> rendererCallback = new FutureCallback<Boolean>() {
220                 String message = "";
221                 ServiceRpcResultSp notification = null;
222
223                 @Override
224                 public void onFailure(Throwable arg0) {
225                     LOG.error("Failure message : {}", arg0.toString());
226                     LOG.error("Service delete failed !");
227                     notification = new ServiceRpcResultSpBuilder()
228                             .setNotificationType(ServicePathNotificationTypes.ServiceDelete)
229                             .setServiceName(input.getServiceName()).setStatus(RpcStatusEx.Failed)
230                             .setStatusMessage("PCR Request failed  : " + arg0.getMessage()).build();
231                     try {
232                         notificationPublishService.putNotification(notification);
233                     } catch (InterruptedException e) {
234                         LOG.info("notification offer rejected : {}", e);
235                     }
236                 }
237
238                 @Override
239                 public void onSuccess(Boolean response) {
240                     LOG.info("response : {}", response);
241                     if (response) {
242                         message = "Service deleted !";
243                         notification = new ServiceRpcResultSpBuilder()
244                                 .setNotificationType(ServicePathNotificationTypes.ServiceDelete)
245                                 .setServiceName(input.getServiceName()).setStatus(RpcStatusEx.Successful)
246                                 .setStatusMessage(message).build();
247                     } else {
248                         message = "Service delete failed : " + sendingRenderer.getError();
249                         notification = new ServiceRpcResultSpBuilder()
250                                 .setNotificationType(ServicePathNotificationTypes.ServiceDelete)
251                                 .setServiceName("")
252                                 .setStatus(RpcStatusEx.Failed).setStatusMessage(message)
253                                 .build();
254                     }
255                     LOG.info(notification.toString());
256                     try {
257                         notificationPublishService.putNotification(notification);
258                     } catch (InterruptedException e) {
259                         LOG.info("notification offer rejected : {}", e);
260                     }
261                     LOG.info(message);
262                 }
263             };
264             ListenableFuture<Boolean> renderer = sendingRenderer.serviceDelete();
265             Futures.addCallback(renderer, rendererCallback, executor);
266             message = "Service delete Request in progress ...";
267             LOG.info(message);
268             configurationResponseCommon = new ConfigurationResponseCommonBuilder()
269                     .setAckFinalIndicator("Yes")
270                     .setRequestId(input.getServiceHandlerHeader().getRequestId())
271                     .setResponseCode("200")
272                     .setResponseMessage(message);
273             ServiceDeleteOutput output = new ServiceDeleteOutputBuilder()
274                     .setConfigurationResponseCommon(configurationResponseCommon.build())
275                     .build();
276             return RpcResultBuilder.success(output).buildFuture();
277         } else {
278             message = compliancyCheck.getMessage();
279             LOG.info("Service not compliant caused by : {}", message);
280             responseCode = "500";
281             notification = new ServiceRpcResultSpBuilder()
282                     .setNotificationType(ServicePathNotificationTypes.ServiceDelete)
283                     .setServiceName(input.getServiceName()).setStatus(RpcStatusEx.Failed)
284                     .setStatusMessage("Service not compliant caused by : " + message)
285                     .build();
286             try {
287                 notificationPublishService.putNotification(notification);
288             } catch (InterruptedException e) {
289                 LOG.info("notification offer rejected : {}", e);
290             }
291         }
292         configurationResponseCommon = new ConfigurationResponseCommonBuilder()
293                 .setAckFinalIndicator("yes")
294                 .setRequestId(input.getServiceHandlerHeader().getRequestId())
295                 .setResponseCode(responseCode)
296                 .setResponseMessage(message);
297         ServiceDeleteOutput output = new ServiceDeleteOutputBuilder()
298                 .setConfigurationResponseCommon(configurationResponseCommon.build())
299                 .build();
300         return RpcResultBuilder.success(output).buildFuture();
301     }
302 }