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