665b18f52415e035bbc876dd13f938489f94eaa7
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / listeners / PceListenerImpl.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 package org.opendaylight.transportpce.servicehandler.listeners;
9
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
12 import org.opendaylight.transportpce.common.OperationResult;
13 import org.opendaylight.transportpce.pce.service.PathComputationService;
14 import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperations;
15 import org.opendaylight.transportpce.servicehandler.ModelMappingUtils;
16 import org.opendaylight.transportpce.servicehandler.ServiceInput;
17 import org.opendaylight.transportpce.servicehandler.service.PCEServiceWrapper;
18 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.PathComputationRequestOutputBuilder;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.ServicePathRpcResult;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.TransportpcePceListener;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.service.path.rpc.result.PathDescription;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev220808.service.path.rpc.result.PathDescriptionBuilder;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.ServiceImplementationRequestInput;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev211210.service.list.Services;
27 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.RpcStatusEx;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev220118.response.parameters.sp.ResponseParametersBuilder;
29 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationProcessService;
30 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationProcessServiceBuilder;
31 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.notification.process.service.ServiceAEndBuilder;
32 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.notification.process.service.ServiceZEndBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class PceListenerImpl implements TransportpcePceListener {
37
38     private static final Logger LOG = LoggerFactory.getLogger(PceListenerImpl.class);
39     private static final String PUBLISHER = "PceListener";
40
41     private ServicePathRpcResult servicePathRpcResult;
42     private RendererServiceOperations rendererServiceOperations;
43     private ServiceDataStoreOperations serviceDataStoreOperations;
44     private PCEServiceWrapper pceServiceWrapper;
45     private ServiceInput input;
46     private Boolean serviceReconfigure;
47     private Boolean tempService;
48     private Boolean serviceFeasiblity;
49     private NotificationPublishService notificationPublishService;
50
51     public PceListenerImpl(
52             RendererServiceOperations rendererServiceOperations,
53             PathComputationService pathComputationService,
54             NotificationPublishService notificationPublishService,
55             ServiceDataStoreOperations serviceDataStoreOperations) {
56         this.rendererServiceOperations = rendererServiceOperations;
57         this.pceServiceWrapper = new PCEServiceWrapper(pathComputationService, notificationPublishService);
58         this.serviceDataStoreOperations = serviceDataStoreOperations;
59         setServiceReconfigure(false);
60         setInput(null);
61         setTempService(false);
62         setServiceFeasiblity(false);
63         this.notificationPublishService = notificationPublishService;
64     }
65
66     @Override
67     public void onServicePathRpcResult(ServicePathRpcResult notification) {
68         if (compareServicePathRpcResult(notification)) {
69             LOG.warn("ServicePathRpcResult already wired !");
70             return;
71         }
72         servicePathRpcResult = notification;
73         switch (servicePathRpcResult.getNotificationType().getIntValue()) {
74             /* path-computation-request. */
75             case 1:
76                 onPathComputationResult(notification);
77                 break;
78             /* cancel-resource-reserve. */
79             case 2:
80                 onCancelResourceResult();
81                 break;
82             default:
83                 break;
84         }
85     }
86
87     /**
88      * Process path computation request result.
89      * @param notification the result notification.
90      */
91     private void onPathComputationResult(ServicePathRpcResult notification) {
92         LOG.info("PCE '{}' Notification received : {}", servicePathRpcResult.getNotificationType().getName(),
93                 notification);
94         if (!checkStatus(notification)) {
95             return;
96         }
97         if (servicePathRpcResult.getPathDescription() == null) {
98             LOG.error("'PathDescription' parameter is null ");
99             return;
100         }
101         PathDescription pathDescription =
102             new PathDescriptionBuilder()
103                 .setAToZDirection(servicePathRpcResult.getPathDescription().getAToZDirection())
104                 .setZToADirection(servicePathRpcResult.getPathDescription().getZToADirection())
105                 .build();
106         LOG.info("PathDescription gets : {}", pathDescription);
107         if (serviceFeasiblity) {
108             LOG.warn("service-feasibility-check RPC ");
109             return;
110         }
111         if (input == null) {
112             LOG.error("Input is null !");
113             return;
114         }
115         OperationResult operationResult = null;
116         if (tempService) {
117             operationResult = this.serviceDataStoreOperations.createTempService(input.getTempServiceCreateInput());
118             if (!operationResult.isSuccess()) {
119                 LOG.error("Temp Service not created in datastore !");
120             }
121         } else {
122             operationResult = this.serviceDataStoreOperations.createService(input.getServiceCreateInput());
123             if (!operationResult.isSuccess()) {
124                 LOG.error("Service not created in datastore !");
125             }
126         }
127         if (!this.serviceDataStoreOperations
128                 .createServicePath(
129                     input,
130                     //pceResponse
131                     new PathComputationRequestOutputBuilder()
132                         .setResponseParameters(
133                             new ResponseParametersBuilder()
134                                 .setPathDescription(
135                                     new org.opendaylight.yang.gen.v1
136                                             .http.org.transportpce.b.c._interface.service.types.rev220118
137                                                 .response.parameters.sp.response.parameters
138                                                     .PathDescriptionBuilder(pathDescription)
139                                         .build())
140                                 .build())
141                         .build())
142                 .isSuccess()) {
143             LOG.error("Service Path not created in datastore !");
144         }
145         ServiceImplementationRequestInput serviceImplementationRequest =
146             ModelMappingUtils.createServiceImplementationRequest(input, pathDescription);
147         LOG.info("Sending serviceImplementation request : {}", serviceImplementationRequest);
148         this.rendererServiceOperations.serviceImplementation(serviceImplementationRequest);
149     }
150
151     /**
152      * Check status of notification and send nbi notification.
153      * @param notification ServicePathRpcResult the notification to check.
154      * @return true is status is Successful, false otherwise.
155      */
156     private boolean checkStatus(ServicePathRpcResult notification) {
157         PublishNotificationProcessService nbiNotification = getPublishNotificationProcessService(notification);
158         PublishNotificationProcessServiceBuilder publishNotificationProcessServiceBuilder =
159                 new PublishNotificationProcessServiceBuilder(nbiNotification);
160         //TODO is it worth to instantiate the 2 variables above if status is 'Pending' or 'Successful' ?
161         switch (servicePathRpcResult.getStatus()) {
162             case Failed:
163                 LOG.error("PCE path computation failed !");
164                 nbiNotification = publishNotificationProcessServiceBuilder
165                         .setMessage("ServiceCreate request failed ...")
166                         .setResponseFailed("PCE path computation failed !")
167                         .setOperationalState(State.Degraded).build();
168                 sendNbiNotification(nbiNotification);
169                 return false;
170             case Pending:
171                 LOG.warn("PCE path computation returned a Pending RpcStatusEx code!");
172                 return false;
173             case Successful:
174                 LOG.info("PCE calculation done OK !");
175                 return true;
176             default:
177                 LOG.error("PCE path computation returned an unknown RpcStatusEx code {}",
178                         servicePathRpcResult.getStatus());
179                 nbiNotification = publishNotificationProcessServiceBuilder
180                         .setMessage("ServiceCreate request failed ...")
181                         .setResponseFailed("PCE path computation returned an unknown RpcStatusEx code!")
182                         .setOperationalState(State.Degraded).build();
183                 sendNbiNotification(nbiNotification);
184                 return false;
185         }
186     }
187
188     private PublishNotificationProcessService getPublishNotificationProcessService(ServicePathRpcResult notification) {
189         if (input == null) {
190             return new PublishNotificationProcessServiceBuilder()
191                 .setServiceName(notification.getServiceName())
192                 .setPublisherName(PUBLISHER)
193                 .build();
194         }
195         return new PublishNotificationProcessServiceBuilder()
196             .setServiceName(input.getServiceName())
197             .setServiceAEnd(new ServiceAEndBuilder(input.getServiceAEnd()).build())
198             .setServiceZEnd(new ServiceZEndBuilder(input.getServiceZEnd()).build())
199             .setCommonId(input.getCommonId())
200             .setConnectionType(input.getConnectionType())
201             .setPublisherName(PUBLISHER)
202             .build();
203     }
204
205     /**
206      * Process cancel resource result.
207      */
208     private void onCancelResourceResult() {
209         if (servicePathRpcResult.getStatus() == RpcStatusEx.Pending) {
210             LOG.warn("PCE cancel returned a Pending RpcStatusEx code !");
211             return;
212         } else if (servicePathRpcResult.getStatus() != RpcStatusEx.Successful
213                 && servicePathRpcResult.getStatus() != RpcStatusEx.Failed) {
214             LOG.error("PCE cancel returned an unknown RpcStatusEx code !");
215             return;
216         }
217         Services service = serviceDataStoreOperations.getService(input.getServiceName()).get();
218         PublishNotificationProcessServiceBuilder nbiNotificationBuilder =
219             new PublishNotificationProcessServiceBuilder()
220                 .setServiceName(service.getServiceName())
221                 .setServiceAEnd(new ServiceAEndBuilder(service.getServiceAEnd()).build())
222                 .setServiceZEnd(new ServiceZEndBuilder(service.getServiceZEnd()).build())
223                 .setCommonId(service.getCommonId())
224                 .setConnectionType(service.getConnectionType())
225                 .setPublisherName(PUBLISHER);
226         if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
227             LOG.info("PCE cancel resource failed !");
228             sendNbiNotification(
229                 nbiNotificationBuilder
230                     .setResponseFailed("PCE cancel resource failed !")
231                     .setMessage("ServiceDelete request failed ...")
232                     .setOperationalState(service.getOperationalState())
233                     .build());
234             return;
235         }
236         LOG.info("PCE cancel resource done OK !");
237         OperationResult deleteServicePathOperationResult =
238                 this.serviceDataStoreOperations.deleteServicePath(input.getServiceName());
239         if (!deleteServicePathOperationResult.isSuccess()) {
240             LOG.warn("Service path was not removed from datastore !");
241         }
242         OperationResult deleteServiceOperationResult;
243         String serviceType = "";
244         if (tempService) {
245             deleteServiceOperationResult = this.serviceDataStoreOperations.deleteTempService(input.getServiceName());
246             serviceType = "Temp ";
247         } else {
248             deleteServiceOperationResult = this.serviceDataStoreOperations.deleteService(input.getServiceName());
249         }
250         if (deleteServiceOperationResult.isSuccess()) {
251             sendNbiNotification(
252                 nbiNotificationBuilder
253                     .setResponseFailed("")
254                     .setMessage("Service deleted !")
255                     .setOperationalState(State.Degraded)
256                     .build());
257         } else {
258             LOG.warn("{}Service was not removed from datastore !", serviceType);
259             sendNbiNotification(
260                 nbiNotificationBuilder
261                     .setResponseFailed(serviceType + "Service was not removed from datastore !")
262                     .setMessage("ServiceDelete request failed ...")
263                     .setOperationalState(service.getOperationalState())
264                     .build());
265         }
266         /**
267          * if it was an RPC serviceReconfigure, re-launch PCR.
268          */
269         if (this.serviceReconfigure) {
270             LOG.info("cancel resource reserve done, relaunching PCE path computation ...");
271             this.pceServiceWrapper.performPCE(input.getServiceCreateInput(), true);
272             this.serviceReconfigure = false;
273         }
274     }
275
276     @SuppressFBWarnings(
277         value = "ES_COMPARING_STRINGS_WITH_EQ",
278         justification = "false positives, not strings but real object references comparisons")
279     private Boolean compareServicePathRpcResult(ServicePathRpcResult notification) {
280         if (servicePathRpcResult == null) {
281             return false;
282         }
283         if (servicePathRpcResult.getNotificationType() != notification.getNotificationType()) {
284             return false;
285         }
286         if (servicePathRpcResult.getServiceName() != notification.getServiceName()) {
287             return false;
288         }
289         if (servicePathRpcResult.getStatus() != notification.getStatus()) {
290             return false;
291         }
292         if (servicePathRpcResult.getStatusMessage() != notification.getStatusMessage()) {
293             return false;
294         }
295         return true;
296     }
297
298     public void setInput(ServiceInput serviceInput) {
299         this.input = serviceInput;
300     }
301
302     public void setServiceReconfigure(Boolean serv) {
303         this.serviceReconfigure = serv;
304     }
305
306     public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
307         this.serviceDataStoreOperations = serviceData;
308     }
309
310     public void setTempService(Boolean tempService) {
311         this.tempService = tempService;
312     }
313
314     public void setServiceFeasiblity(Boolean serviceFeasiblity) {
315         this.serviceFeasiblity = serviceFeasiblity;
316     }
317
318     /**
319      * Send notification to NBI notification in order to publish message.
320      * @param service PublishNotificationService
321      */
322     private void sendNbiNotification(PublishNotificationProcessService service) {
323         try {
324             notificationPublishService.putNotification(service);
325         } catch (InterruptedException e) {
326             LOG.warn("Cannot send notification to nbi", e);
327             Thread.currentThread().interrupt();
328         }
329     }
330 }