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