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