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