d154d9bc6772f7af8781493bad1b290a021d7aa9
[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.rev200128.PathComputationRequestOutput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.PathComputationRequestOutputBuilder;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.ServicePathRpcResult;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.TransportpcePceListener;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.service.path.rpc.result.PathDescription;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev200128.service.path.rpc.result.PathDescriptionBuilder;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev201125.ServiceImplementationRequestInput;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev181130.State;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev190531.service.list.Services;
28 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.RpcStatusEx;
29 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp.ResponseParameters;
30 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.service.types.rev200128.response.parameters.sp.ResponseParametersBuilder;
31 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.PublishNotificationService;
32 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.PublishNotificationServiceBuilder;
33 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.notification.service.ServiceAEndBuilder;
34 import org.opendaylight.yang.gen.v1.nbi.notifications.rev201130.notification.service.ServiceZEndBuilder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class PceListenerImpl implements TransportpcePceListener {
39
40     private static final Logger LOG = LoggerFactory.getLogger(PceListenerImpl.class);
41     private static final String TOPIC = "PceListener";
42
43     private ServicePathRpcResult servicePathRpcResult;
44     private RendererServiceOperations rendererServiceOperations;
45     private ServiceDataStoreOperations serviceDataStoreOperations;
46     private PCEServiceWrapper pceServiceWrapper;
47     private ServiceInput input;
48     private Boolean serviceReconfigure;
49     private Boolean tempService;
50     private Boolean serviceFeasiblity;
51     private NotificationPublishService notificationPublishService;
52
53     public PceListenerImpl(RendererServiceOperations rendererServiceOperations,
54             PathComputationService pathComputationService, 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 = new PathDescriptionBuilder()
102                 .setAToZDirection(servicePathRpcResult.getPathDescription().getAToZDirection())
103                 .setZToADirection(servicePathRpcResult.getPathDescription().getZToADirection())
104                 .build();
105         LOG.info("PathDescription gets : {}", pathDescription);
106         if (serviceFeasiblity) {
107             LOG.warn("service-feasibility-check RPC ");
108             return;
109         }
110         if (input == null) {
111             LOG.error("Input is null !");
112             return;
113         }
114         OperationResult operationResult = null;
115         if (tempService) {
116             operationResult = this.serviceDataStoreOperations.createTempService(input.getTempServiceCreateInput());
117             if (!operationResult.isSuccess()) {
118                 LOG.error("Temp Service not created in datastore !");
119             }
120         } else {
121             operationResult = this.serviceDataStoreOperations.createService(input.getServiceCreateInput());
122             if (!operationResult.isSuccess()) {
123                 LOG.error("Service not created in datastore !");
124             }
125         }
126         ResponseParameters responseParameters = new ResponseParametersBuilder()
127                 .setPathDescription(new org.opendaylight.yang.gen.v1.http
128                         .org.transportpce.b.c._interface.service.types.rev200128
129                         .response.parameters.sp.response.parameters.PathDescriptionBuilder(pathDescription).build())
130                 .build();
131         PathComputationRequestOutput pceResponse = new PathComputationRequestOutputBuilder()
132                 .setResponseParameters(responseParameters).build();
133         OperationResult operationServicePathSaveResult = this.serviceDataStoreOperations
134                 .createServicePath(input, pceResponse);
135         if (!operationServicePathSaveResult.isSuccess()) {
136             LOG.error("Service Path not created in datastore !");
137         }
138         ServiceImplementationRequestInput serviceImplementationRequest = ModelMappingUtils
139                 .createServiceImplementationRequest(input, pathDescription);
140         LOG.info("Sending serviceImplementation request : {}", serviceImplementationRequest);
141         this.rendererServiceOperations.serviceImplementation(serviceImplementationRequest);
142     }
143
144     /**
145      * Check status of notification and send nbi notification.
146      * @param notification ServicePathRpcResult the notification to check.
147      * @return true is status is Successful, false otherwise.
148      */
149     private boolean checkStatus(ServicePathRpcResult notification) {
150         PublishNotificationService nbiNotification = getPublishNotificationService(notification);
151         PublishNotificationServiceBuilder publishNotificationServiceBuilder = new PublishNotificationServiceBuilder(
152                 nbiNotification);
153         switch (servicePathRpcResult.getStatus()) {
154             case Failed:
155                 LOG.error("PCE path computation failed !");
156                 nbiNotification = publishNotificationServiceBuilder.setMessage("ServiceCreate request failed ...")
157                         .setResponseFailed("PCE path computation failed !")
158                         .setOperationalState(State.Degraded).build();
159                 sendNbiNotification(nbiNotification);
160                 return false;
161             case Pending:
162                 LOG.warn("PCE path computation returned a Pending RpcStatusEx code!");
163                 return false;
164             case Successful:
165                 LOG.info("PCE calculation done OK !");
166                 return true;
167             default:
168                 LOG.error("PCE path computation returned an unknown RpcStatusEx code {}",
169                         servicePathRpcResult.getStatus());
170                 nbiNotification = publishNotificationServiceBuilder.setMessage("ServiceCreate request failed ...")
171                         .setResponseFailed("PCE path computation returned an unknown RpcStatusEx code!")
172                         .setOperationalState(State.Degraded).build();
173                 sendNbiNotification(nbiNotification);
174                 return false;
175         }
176     }
177
178     private PublishNotificationService getPublishNotificationService(ServicePathRpcResult notification) {
179         PublishNotificationServiceBuilder nbiNotificationBuilder = new PublishNotificationServiceBuilder();
180         if (input != null) {
181             nbiNotificationBuilder.setServiceName(input.getServiceName())
182                     .setServiceAEnd(new ServiceAEndBuilder(input.getServiceAEnd()).build())
183                     .setServiceZEnd(new ServiceZEndBuilder(input.getServiceZEnd()).build())
184                     .setCommonId(input.getCommonId()).setConnectionType(input.getConnectionType());
185         } else {
186             nbiNotificationBuilder.setServiceName(notification.getServiceName());
187         }
188         nbiNotificationBuilder.setTopic(TOPIC);
189         return nbiNotificationBuilder.build();
190     }
191
192     /**
193      * Process cancel resource result.
194      */
195     private void onCancelResourceResult() {
196         if (servicePathRpcResult.getStatus() == RpcStatusEx.Pending) {
197             LOG.warn("PCE cancel returned a Pending RpcStatusEx code !");
198             return;
199         } else if (servicePathRpcResult.getStatus() != RpcStatusEx.Successful
200                 && servicePathRpcResult.getStatus() != RpcStatusEx.Failed) {
201             LOG.error("PCE cancel returned an unknown RpcStatusEx code !");
202             return;
203         }
204         Services service = serviceDataStoreOperations.getService(input.getServiceName()).get();
205         PublishNotificationServiceBuilder nbiNotificationBuilder = new PublishNotificationServiceBuilder()
206                 .setServiceName(service.getServiceName())
207                 .setServiceAEnd(new ServiceAEndBuilder(service.getServiceAEnd()).build())
208                 .setServiceZEnd(new ServiceZEndBuilder(service.getServiceZEnd()).build())
209                 .setCommonId(service.getCommonId())
210                 .setConnectionType(service.getConnectionType())
211                 .setTopic(TOPIC);
212         if (servicePathRpcResult.getStatus() == RpcStatusEx.Failed) {
213             LOG.info("PCE cancel resource failed !");
214             sendNbiNotification(nbiNotificationBuilder
215                     .setResponseFailed("PCE cancel resource failed !")
216                     .setMessage("ServiceDelete request failed ...")
217                     .setOperationalState(service.getOperationalState())
218                     .build());
219             return;
220         }
221         LOG.info("PCE cancel resource done OK !");
222         OperationResult deleteServicePathOperationResult =
223                 this.serviceDataStoreOperations.deleteServicePath(input.getServiceName());
224         if (!deleteServicePathOperationResult.isSuccess()) {
225             LOG.warn("Service path was not removed from datastore !");
226         }
227         OperationResult deleteServiceOperationResult;
228         String serviceType = "";
229         if (tempService) {
230             deleteServiceOperationResult = this.serviceDataStoreOperations.deleteTempService(input.getServiceName());
231             serviceType = "Temp ";
232         } else {
233             deleteServiceOperationResult = this.serviceDataStoreOperations.deleteService(input.getServiceName());
234         }
235         if (deleteServiceOperationResult.isSuccess()) {
236             sendNbiNotification(nbiNotificationBuilder
237                     .setResponseFailed("")
238                     .setMessage("Service deleted !")
239                     .setOperationalState(State.Degraded)
240                     .build());
241         } else {
242             LOG.warn("{}Service was not removed from datastore !", serviceType);
243             sendNbiNotification(nbiNotificationBuilder
244                     .setResponseFailed(serviceType + "Service was not removed from datastore !")
245                     .setMessage("ServiceDelete request failed ...")
246                     .setOperationalState(service.getOperationalState())
247                     .build());
248         }
249         /**
250          * if it was an RPC serviceReconfigure, re-launch PCR.
251          */
252         if (this.serviceReconfigure) {
253             LOG.info("cancel resource reserve done, relaunching PCE path computation ...");
254             this.pceServiceWrapper.performPCE(input.getServiceCreateInput(), true);
255             this.serviceReconfigure = false;
256         }
257     }
258
259     @SuppressFBWarnings(
260         value = "ES_COMPARING_STRINGS_WITH_EQ",
261         justification = "false positives, not strings but real object references comparisons")
262     private Boolean compareServicePathRpcResult(ServicePathRpcResult notification) {
263         if (servicePathRpcResult == null) {
264             return false;
265         }
266         if (servicePathRpcResult.getNotificationType() != notification.getNotificationType()) {
267             return false;
268         }
269         if (servicePathRpcResult.getServiceName() != notification.getServiceName()) {
270             return false;
271         }
272         if (servicePathRpcResult.getStatus() != notification.getStatus()) {
273             return false;
274         }
275         if (servicePathRpcResult.getStatusMessage() != notification.getStatusMessage()) {
276             return false;
277         }
278         return true;
279     }
280
281     public void setInput(ServiceInput serviceInput) {
282         this.input = serviceInput;
283     }
284
285     public void setServiceReconfigure(Boolean serv) {
286         this.serviceReconfigure = serv;
287     }
288
289     public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
290         this.serviceDataStoreOperations = serviceData;
291     }
292
293     public void setTempService(Boolean tempService) {
294         this.tempService = tempService;
295     }
296
297     public void setServiceFeasiblity(Boolean serviceFeasiblity) {
298         this.serviceFeasiblity = serviceFeasiblity;
299     }
300
301     /**
302      * Send notification to NBI notification in order to publish message.
303      * @param service PublishNotificationService
304      */
305     private void sendNbiNotification(PublishNotificationService service) {
306         try {
307             notificationPublishService.putNotification(service);
308         } catch (InterruptedException e) {
309             LOG.warn("Cannot send notification to nbi", e);
310             Thread.currentThread().interrupt();
311         }
312     }
313 }