1de11948fb34a071101fa130a08754552175a1d9
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / listeners / RendererListenerImpl.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 java.util.ArrayList;
12 import java.util.List;
13 import java.util.Map;
14 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
15 import org.opendaylight.transportpce.common.OperationResult;
16 import org.opendaylight.transportpce.common.StringConstants;
17 import org.opendaylight.transportpce.networkmodel.service.NetworkModelService;
18 import org.opendaylight.transportpce.pce.service.PathComputationService;
19 import org.opendaylight.transportpce.servicehandler.ServiceInput;
20 import org.opendaylight.transportpce.servicehandler.service.PCEServiceWrapper;
21 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.networkutils.rev220630.OtnLinkType;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.RendererRpcResultSp;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.TransportpceRendererListener;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev210915.renderer.rpc.result.sp.Link;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev201125.ServiceRpcResultSh;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.servicehandler.rev201125.ServiceRpcResultShBuilder;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev230526.ServiceNotificationTypes;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.state.types.rev191129.State;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.equipment.states.types.rev191129.AdminStates;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceRpcResult;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.ServiceRpcResultBuilder;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.list.Services;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.rpc.result.PathComputationResultBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.rpc.result.path.computation.result.AToZBuilder;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev230526.service.rpc.result.path.computation.result.ZToABuilder;
37 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationProcessService;
38 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.PublishNotificationProcessServiceBuilder;
39 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.notification.process.service.ServiceAEndBuilder;
40 import org.opendaylight.yang.gen.v1.nbi.notifications.rev211013.notification.process.service.ServiceZEndBuilder;
41 import org.osgi.service.component.annotations.Activate;
42 import org.osgi.service.component.annotations.Component;
43 import org.osgi.service.component.annotations.Reference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * Calls to listen to Renderer notifications.
49  *
50  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
51  *
52  */
53 @Component
54 public class RendererListenerImpl implements TransportpceRendererListener, RendererListener {
55
56     private static final String PUBLISHER = "RendererListener";
57     private static final Logger LOG = LoggerFactory.getLogger(RendererListenerImpl.class);
58     private RendererRpcResultSp serviceRpcResultSp;
59     private ServiceDataStoreOperations serviceDataStoreOperations;
60     private ServiceInput input;
61     private PCEServiceWrapper pceServiceWrapper;
62     private Boolean tempService;
63     private NotificationPublishService notificationPublishService;
64     private final NetworkModelService networkModelService;
65
66
67     @Activate
68     public RendererListenerImpl(@Reference PathComputationService pathComputationService,
69             @Reference NotificationPublishService notificationPublishService,
70             @Reference NetworkModelService networkModelService) {
71         this.pceServiceWrapper = new PCEServiceWrapper(pathComputationService, notificationPublishService);
72         setServiceInput(null);
73         setTempService(false);
74         this.notificationPublishService = notificationPublishService;
75         this.networkModelService = networkModelService;
76     }
77
78     @Override
79     public void onRendererRpcResultSp(RendererRpcResultSp notification) {
80         if (compareServiceRpcResultSp(notification)) {
81             LOG.warn("ServiceRpcResultSp already wired !");
82             return;
83         }
84         serviceRpcResultSp = notification;
85         int notifType = serviceRpcResultSp.getNotificationType().getIntValue();
86         LOG.info("Renderer '{}' Notification received : {}", serviceRpcResultSp.getNotificationType().getName(),
87                 notification);
88         switch (notifType) {
89             /* service-implementation-request. */
90             case 3:
91                 onServiceImplementationResult(notification);
92                 break;
93             /* service-delete. */
94             case 4:
95                 onServiceDeleteResult(notification);
96                 break;
97             default:
98                 break;
99         }
100     }
101
102     /**
103      * Process service delete result for serviceName.
104      * @param notification RendererRpcResultSp
105      */
106     private void onServiceDeleteResult(RendererRpcResultSp notification) {
107         switch (serviceRpcResultSp.getStatus()) {
108             case Successful:
109                 updateOtnTopology(notification, true);
110                 break;
111             case Failed:
112                 LOG.error("Renderer service delete failed !");
113                 Services service = serviceDataStoreOperations.getService(input.getServiceName()).orElseThrow();
114                 sendNbiNotification(new PublishNotificationProcessServiceBuilder()
115                         .setServiceName(service.getServiceName())
116                         .setServiceAEnd(new ServiceAEndBuilder(service.getServiceAEnd()).build())
117                         .setServiceZEnd(new ServiceZEndBuilder(service.getServiceZEnd()).build())
118                         .setCommonId(service.getCommonId())
119                         .setConnectionType(service.getConnectionType())
120                         .setResponseFailed("Renderer service delete failed !")
121                         .setMessage("ServiceDelete request failed ...")
122                         .setOperationalState(service.getOperationalState())
123                         .setPublisherName(PUBLISHER)
124                         .build());
125                 return;
126             case Pending:
127                 LOG.warn("Renderer service delete returned a Pending RpcStatusEx code!");
128                 return;
129             default:
130                 LOG.error("Renderer service delete returned an unknown RpcStatusEx code!");
131                 return;
132         }
133         LOG.info("Service '{}' deleted !", notification.getServiceName());
134         if (this.input == null) {
135             LOG.error("ServiceInput parameter is null !");
136             return;
137         }
138         LOG.info("sending PCE cancel resource reserve for '{}'", this.input.getServiceName());
139         this.pceServiceWrapper.cancelPCEResource(this.input.getServiceName(),
140                 ServiceNotificationTypes.ServiceDeleteResult);
141         sendServiceHandlerNotification(notification, ServiceNotificationTypes.ServiceDeleteResult);
142     }
143
144     /**
145      * Process service implementation result for serviceName.
146      * @param notification RendererRpcResultSp
147      */
148     private void onServiceImplementationResult(RendererRpcResultSp notification) {
149         switch (serviceRpcResultSp.getStatus()) {
150             case Successful:
151                 onSuccededServiceImplementation(notification);
152                 break;
153             case Failed:
154                 onFailedServiceImplementation(notification.getServiceName());
155                 break;
156             case Pending:
157                 LOG.warn("Service Implementation still pending according to RpcStatusEx");
158                 break;
159             default:
160                 LOG.warn("Service Implementation has an unknown RpcStatusEx code");
161                 break;
162         }
163     }
164
165     /**
166      * Process succeeded service implementation for service.
167      * @param notification RendererRpcResultSp
168      */
169     private void onSuccededServiceImplementation(RendererRpcResultSp notification) {
170         LOG.info("Service implemented !");
171         if (serviceDataStoreOperations == null) {
172             LOG.debug("serviceDataStoreOperations is null");
173             return;
174         }
175         updateOtnTopology(notification, false);
176         PublishNotificationProcessServiceBuilder nbiNotificationBuilder =
177             new PublishNotificationProcessServiceBuilder()
178                 .setServiceName(input.getServiceName())
179                 .setServiceAEnd(new ServiceAEndBuilder(input.getServiceAEnd()).build())
180                 .setServiceZEnd(new ServiceZEndBuilder(input.getServiceZEnd()).build())
181                 .setCommonId(input.getCommonId()).setConnectionType(input.getConnectionType())
182                 .setPublisherName(PUBLISHER);
183         String serviceTemp = "";
184         if (tempService) {
185             OperationResult operationResult = this.serviceDataStoreOperations.modifyTempService(
186                     serviceRpcResultSp.getServiceName(), State.InService, AdminStates.InService);
187             serviceTemp = "Temp ";
188             if (operationResult.isSuccess()) {
189                 sendNbiNotification(nbiNotificationBuilder
190                     .setResponseFailed("")
191                     .setMessage("Temp Service implemented !")
192                     .setOperationalState(State.InService)
193                     .build());
194                 LOG.debug("For the Temp service, sending notification on service-result-rpc");
195                 sendServiceRpcResultNotification(notification, ServiceNotificationTypes.ServiceCreateResult);
196                 return;
197             }
198         } else {
199             OperationResult operationResult = this.serviceDataStoreOperations.modifyService(
200                     serviceRpcResultSp.getServiceName(), State.InService, AdminStates.InService);
201             // Here the service is implemented and the tempService has to be deleted if present
202             String commonId = input.getCommonId();
203             if (commonId != null) {
204                 if (this.serviceDataStoreOperations.getTempService(commonId).isPresent()) {
205                     LOG.info("Temp-service exists with the common-Id {}", commonId);
206                     // Delete the common-id from this temp-service-list here
207                     OperationResult tempServiceListDelete = serviceDataStoreOperations.deleteTempService(commonId);
208                     //TODO: Also need to delete the service-path from the transportpce-service-path list
209                     this.serviceDataStoreOperations.deleteServicePath(commonId);
210                     LOG.info("Result for temp-service-list with {} is {}", commonId, tempServiceListDelete);
211                 }
212             }
213
214             if (operationResult.isSuccess()) {
215                 sendNbiNotification(nbiNotificationBuilder
216                     .setResponseFailed("")
217                     .setMessage("Service implemented !")
218                     .setOperationalState(State.InService)
219                     .build());
220                 sendServiceHandlerNotification(notification, ServiceNotificationTypes.ServiceCreateResult);
221                 return;
222             }
223         }
224         LOG.warn("{}Service status not updated in datastore !", serviceTemp);
225         sendNbiNotification(nbiNotificationBuilder
226             .setResponseFailed(serviceTemp + "Service status not updated in datastore !")
227             .setMessage("ServiceCreate request failed ...")
228             .setOperationalState(State.OutOfService)
229             .build());
230     }
231
232     /**
233      * Create and send service handler notification.
234      * @param notification RendererRpcResultSp
235      * @param type ServiceNotificationTypes
236      */
237     private void sendServiceHandlerNotification(RendererRpcResultSp notification, ServiceNotificationTypes type) {
238         try {
239             ServiceRpcResultSh serviceHandlerNotification = new ServiceRpcResultShBuilder()
240                     .setAToZDirection(notification.getAToZDirection())
241                     .setZToADirection(notification.getZToADirection())
242                     .setServiceName(notification.getServiceName())
243                     .setStatus(notification.getStatus())
244                     .setStatusMessage(notification.getStatusMessage())
245                     .setNotificationType(type)
246                     .build();
247             LOG.debug("Service update in datastore OK, sending notification {}", serviceHandlerNotification);
248             notificationPublishService.putNotification(
249                     serviceHandlerNotification);
250         } catch (InterruptedException e) {
251             LOG.warn("Something went wrong while sending notification for service {}",
252                     serviceRpcResultSp.getServiceName(), e);
253             Thread.currentThread().interrupt();
254         }
255     }
256
257     private void sendServiceRpcResultNotification(RendererRpcResultSp notification, ServiceNotificationTypes type) {
258         try {
259             ServiceRpcResult serviceRpcResult = new ServiceRpcResultBuilder()
260                     .setServiceName(notification.getServiceName())
261                     .setNotificationType(type)
262                     .setStatusMessage(notification.getStatusMessage())
263                     .setCommonId(notification.getCommonId())
264                     .setPathComputationResult(new PathComputationResultBuilder()
265                             .setAToZ(new AToZBuilder()
266                                             .setFrequency(notification
267                                                             .getAToZDirection()
268                                                             .getCentralFrequency())
269                                             .setWidth(notification
270                                                             .getAToZDirection()
271                                                             .getWidth())
272                                             // TODO: here the optical operational mode should be set
273                                             // TODO: also set the GNSR, OSNR, power values
274                                             .setOpticalOperationalMode("test")
275                                             .build())
276                             .setZToA(new ZToABuilder()
277                                             .setFrequency(notification
278                                                             .getZToADirection()
279                                                             .getCentralFrequency())
280                                             .setWidth(notification
281                                                             .getZToADirection()
282                                                             .getWidth())
283                                             // TODO: here the optical operational mode should be set
284                                             // TODO: also set the GNSR, OSNR, power values
285                                             .setOpticalOperationalMode("test")
286                                             .build())
287                             .build())
288                     .build();
289             LOG.info("Sending the notification for service-rpc-result {}", serviceRpcResult);
290             notificationPublishService.putNotification(
291                     serviceRpcResult);
292         } catch (InterruptedException e) {
293             LOG.warn("Something went wrong while sending notification for service {}",
294                     serviceRpcResultSp.getServiceName(), e);
295             Thread.currentThread().interrupt();
296         }
297     }
298
299
300
301     /**
302      * Process failed service implementation for serviceName.
303      * @param serviceName String
304      */
305     private void onFailedServiceImplementation(String serviceName) {
306         LOG.error("Renderer implementation failed !");
307         Services service = serviceDataStoreOperations.getService(input.getServiceName()).orElseThrow();
308         sendNbiNotification(new PublishNotificationProcessServiceBuilder()
309                 .setServiceName(service.getServiceName())
310                 .setServiceAEnd(new ServiceAEndBuilder(service.getServiceAEnd()).build())
311                 .setServiceZEnd(new ServiceZEndBuilder(service.getServiceZEnd()).build())
312                 .setCommonId(service.getCommonId())
313                 .setConnectionType(service.getConnectionType())
314                 .setResponseFailed("Renderer implementation failed !")
315                 .setMessage("ServiceCreate request failed ...")
316                 .setOperationalState(service.getOperationalState())
317                 .setPublisherName(PUBLISHER)
318                 .build());
319         OperationResult deleteServicePathOperationResult =
320                 this.serviceDataStoreOperations.deleteServicePath(serviceName);
321         if (!deleteServicePathOperationResult.isSuccess()) {
322             LOG.warn("Service path was not removed from datastore!");
323         }
324         OperationResult deleteServiceOperationResult;
325         String serviceType = "";
326         if (tempService) {
327             deleteServiceOperationResult = this.serviceDataStoreOperations.deleteTempService(serviceName);
328             serviceType = "Temp ";
329         } else {
330             deleteServiceOperationResult = this.serviceDataStoreOperations.deleteService(serviceName);
331         }
332         if (deleteServiceOperationResult.isSuccess()) {
333             LOG.warn("{}Service was not removed from datastore!", serviceType);
334         }
335     }
336
337     @SuppressFBWarnings(
338         value = "ES_COMPARING_STRINGS_WITH_EQ",
339         justification = "false positives, not strings but real object references comparisons")
340     private Boolean compareServiceRpcResultSp(RendererRpcResultSp notification) {
341         if (serviceRpcResultSp == null
342                 || serviceRpcResultSp.getNotificationType() != notification.getNotificationType()
343                 || serviceRpcResultSp.getServiceName() != notification.getServiceName()
344                 || serviceRpcResultSp.getStatus() != notification.getStatus()
345                 || serviceRpcResultSp.getStatusMessage() != notification.getStatusMessage()) {
346             return false;
347         }
348         return true;
349     }
350
351     @Override
352     public void setServiceInput(ServiceInput serviceInput) {
353         this.input = serviceInput;
354     }
355
356     @Override
357     public void setserviceDataStoreOperations(ServiceDataStoreOperations serviceData) {
358         this.serviceDataStoreOperations = serviceData;
359     }
360
361     @Override
362     public void setTempService(Boolean tempService) {
363         this.tempService = tempService;
364     }
365
366     /**
367      * Send notification to NBI notification in order to publish message.
368      * @param service PublishNotificationService
369      */
370     private void sendNbiNotification(PublishNotificationProcessService service) {
371         try {
372             notificationPublishService.putNotification(service);
373         } catch (InterruptedException e) {
374             LOG.warn("Cannot send notification to nbi", e);
375             Thread.currentThread().interrupt();
376         }
377     }
378
379
380     private void updateOtnTopology(RendererRpcResultSp notification, boolean isDeletion) {
381         Link link = notification.getLink();
382         if (link == null && notification.getLinkId() == null) {
383             return;
384         }
385         List<String> supportedLinkIds = null;
386         if (notification.getLinkId() != null) {
387             supportedLinkIds = new ArrayList<>(notification.getLinkId());
388         }
389         String serviceType = notification.getServiceType();
390         switch (serviceType) {
391             case StringConstants.SERVICE_TYPE_OTU4:
392             case StringConstants.SERVICE_TYPE_OTUC2:
393             case StringConstants.SERVICE_TYPE_OTUC3:
394             case StringConstants.SERVICE_TYPE_OTUC4:
395             case StringConstants.SERVICE_TYPE_ODU4:
396             case StringConstants.SERVICE_TYPE_ODUC2:
397             case StringConstants.SERVICE_TYPE_ODUC3:
398             case StringConstants.SERVICE_TYPE_ODUC4:
399                 Map<String, OtnLinkType> otnLinkTypeMap = Map.of(
400                     StringConstants.SERVICE_TYPE_OTU4, OtnLinkType.OTU4,
401                     // TODO: need to change it when OtnLinkType is updated with enum
402                     StringConstants.SERVICE_TYPE_OTUC2, OtnLinkType.OTUC4,
403                     StringConstants.SERVICE_TYPE_OTUC3, OtnLinkType.OTUC4,
404                     StringConstants.SERVICE_TYPE_OTUC4, OtnLinkType.OTUC4,
405                     StringConstants.SERVICE_TYPE_ODU4, OtnLinkType.ODTU4,
406                     // TODO: need to change it when OtnLinkType is updated with enum
407                     StringConstants.SERVICE_TYPE_ODUC2, OtnLinkType.ODUC4,
408                     StringConstants.SERVICE_TYPE_ODUC3, OtnLinkType.ODUC4,
409                     StringConstants.SERVICE_TYPE_ODUC4, OtnLinkType.ODUC4);
410                 if (isDeletion) {
411                     LOG.info("updating otn-topology removing links");
412                     this.networkModelService.deleteOtnLinks(link, supportedLinkIds, otnLinkTypeMap.get(serviceType));
413                 } else {
414                     LOG.info("updating otn-topology adding links");
415                     this.networkModelService.createOtnLinks(link, supportedLinkIds, otnLinkTypeMap.get(serviceType));
416                 }
417                 break;
418             case StringConstants.SERVICE_TYPE_1GE:
419             case StringConstants.SERVICE_TYPE_10GE:
420             case StringConstants.SERVICE_TYPE_100GE_M:
421                 Short tribPort = Short.valueOf(notification.getAToZDirection().getMinTribSlot().getValue()
422                     .split("\\.")[0]);
423                 Short minTribSlot = Short.valueOf(notification.getAToZDirection().getMinTribSlot().getValue()
424                     .split("\\.")[1]);
425                 Short maxTribSlot = Short.valueOf(notification.getAToZDirection().getMaxTribSlot().getValue()
426                     .split("\\.")[1]);
427                 LOG.info("updating otn-topology node tps -tps and tpn pools");
428                 this.networkModelService.updateOtnLinks(link, supportedLinkIds,
429                     notification.getAToZDirection().getRate(), tribPort, minTribSlot, maxTribSlot, isDeletion);
430                 break;
431             case StringConstants.SERVICE_TYPE_100GE_S:
432                 this.networkModelService.updateOtnLinks(supportedLinkIds, isDeletion);
433                 break;
434             default:
435                 LOG.warn("service-type {} not managed yet", serviceType);
436                 break;
437         }
438     }
439
440 }