Service-notification handling for serviceHandler
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / impl / ServicehandlerImpl.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.impl;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11
12 import java.time.OffsetDateTime;
13 import java.time.ZoneOffset;
14 import java.time.format.DateTimeFormatter;
15 import java.util.Optional;
16
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
19 import org.opendaylight.transportpce.common.OperationResult;
20 import org.opendaylight.transportpce.common.ResponseCodes;
21 import org.opendaylight.transportpce.pce.service.PathComputationService;
22 import org.opendaylight.transportpce.renderer.NetworkModelWavelengthService;
23 import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperations;
24 import org.opendaylight.transportpce.servicehandler.ModelMappingUtils;
25 import org.opendaylight.transportpce.servicehandler.ServiceInput;
26 import org.opendaylight.transportpce.servicehandler.listeners.PceListenerImpl;
27 import org.opendaylight.transportpce.servicehandler.listeners.RendererListenerImpl;
28 import org.opendaylight.transportpce.servicehandler.service.PCEServiceWrapper;
29 import org.opendaylight.transportpce.servicehandler.service.RendererServiceWrapper;
30 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperations;
31 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperationsImpl;
32 import org.opendaylight.transportpce.servicehandler.validation.ServiceCreateValidation;
33 import org.opendaylight.transportpce.servicehandler.validation.checks.ComplianceCheckResult;
34 import org.opendaylight.transportpce.servicehandler.validation.checks.ServicehandlerCompliancyCheck;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.pce.rev171017.PathComputationRequestOutput;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.RpcActions;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.ServiceNotificationTypes;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.configuration.response.common.ConfigurationResponseCommon;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.sdnc.request.header.SdncRequestHeaderBuilder;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.RpcStatus;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.EquipmentNotificationInput;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.EquipmentNotificationOutput;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.NetworkReOptimizationInput;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.NetworkReOptimizationOutput;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.OrgOpenroadmServiceService;
46 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateInput;
47 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateOutput;
48 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteInput;
49 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteInputBuilder;
50 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteOutput;
51 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceFeasibilityCheckInput;
52 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceFeasibilityCheckOutput;
53 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceReconfigureInput;
54 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceReconfigureOutput;
55 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRerouteConfirmInput;
56 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRerouteConfirmOutput;
57 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRerouteInput;
58 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRerouteOutput;
59 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRestorationInput;
60 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRestorationOutput;
61 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceReversionInput;
62 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceReversionOutput;
63 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRollInput;
64 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRollOutput;
65 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceCreateInput;
66 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceCreateOutput;
67 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceDeleteInput;
68 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceDeleteOutput;
69 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.delete.input.ServiceDeleteReqInfo.TailRetention;
70 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.delete.input.ServiceDeleteReqInfoBuilder;
71 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.Services;
72 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
73 import org.opendaylight.yangtools.yang.common.RpcResult;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
76
77
78 /**
79  * Top level service interface providing main OpenROADM controller services.
80  */
81 public class ServicehandlerImpl implements OrgOpenroadmServiceService {
82
83     private static final Logger LOG = LoggerFactory.getLogger(ServicehandlerImpl.class);
84
85     private DataBroker db;
86     private ServiceDataStoreOperations serviceDataStoreOperations;
87     private PCEServiceWrapper pceServiceWrapper;
88     private RendererServiceWrapper rendererServiceWrapper;
89     private PceListenerImpl pceListenerImpl;
90     private RendererListenerImpl rendererListenerImpl;
91
92     //TODO: remove private request fields as they are in global scope
93
94     public ServicehandlerImpl(DataBroker databroker, PathComputationService pathComputationService,
95             RendererServiceOperations rendererServiceOperations, NotificationPublishService notificationPublishService,
96             PceListenerImpl pceListenerImpl, RendererListenerImpl rendererListenerImpl,
97             NetworkModelWavelengthService networkModelWavelengthService) {
98         this.db = databroker;
99         this.serviceDataStoreOperations = new ServiceDataStoreOperationsImpl(this.db);
100         this.serviceDataStoreOperations.initialize();
101         this.pceServiceWrapper = new PCEServiceWrapper(pathComputationService, notificationPublishService);
102         this.rendererServiceWrapper = new RendererServiceWrapper(rendererServiceOperations, notificationPublishService);
103         this.pceListenerImpl = pceListenerImpl;
104         this.rendererListenerImpl = rendererListenerImpl;
105     }
106
107     @Override
108     public ListenableFuture<RpcResult<ServiceCreateOutput>> serviceCreate(ServiceCreateInput input) {
109         LOG.info("RPC service creation received");
110         // Validation
111         OperationResult validationResult = ServiceCreateValidation.validateServiceCreateRequest(
112                 new ServiceInput(input), RpcActions.ServiceCreate);
113         if (! validationResult.isSuccess()) {
114             LOG.warn("Aborting service create because validation of service create request failed: {}",
115                     validationResult.getResultMessage());
116             return ModelMappingUtils.createCreateServiceReply(input, ResponseCodes.FINAL_ACK_YES,
117                     validationResult.getResultMessage(), ResponseCodes.RESPONSE_FAILED);
118         }
119         this.pceListenerImpl.setInput(new ServiceInput(input));
120         this.pceListenerImpl.setServiceReconfigure(false);
121         this.pceListenerImpl.setserviceDataStoreOperations(this.serviceDataStoreOperations);
122         this.rendererListenerImpl.setserviceDataStoreOperations(serviceDataStoreOperations);
123         this.rendererListenerImpl.setServiceInput(new ServiceInput(input));
124         LOG.info("Commencing PCE");
125         PathComputationRequestOutput output = this.pceServiceWrapper.performPCE(input, true);
126         if (output != null) {
127             LOG.info("Service compliant, serviceCreate in progress...");
128             ConfigurationResponseCommon common = output.getConfigurationResponseCommon();
129             return ModelMappingUtils.createCreateServiceReply(input, common.getAckFinalIndicator(),
130                     common.getResponseMessage(), common.getResponseCode());
131         } else {
132             return ModelMappingUtils.createCreateServiceReply(input, ResponseCodes.FINAL_ACK_YES,
133                     "PCE calculation failed", ResponseCodes.RESPONSE_FAILED);
134         }
135     }
136
137     @Override
138     public ListenableFuture<RpcResult<ServiceDeleteOutput>> serviceDelete(ServiceDeleteInput input) {
139         LOG.info("RPC serviceDelete request received for {}", input.getServiceDeleteReqInfo().getServiceName());
140         String message = "";
141
142         /*
143          * Upon receipt of service-deleteService RPC, service header and sdnc-request
144          * header compliancy are verified.
145          */
146         LOG.info("checking Service Compliancy ...");
147         ComplianceCheckResult serviceHandlerCheckResult = ServicehandlerCompliancyCheck.check(
148                 input.getServiceDeleteReqInfo().getServiceName(),
149                 input.getSdncRequestHeader(), null, RpcActions.ServiceDelete, false, true);
150         if (serviceHandlerCheckResult.hasPassed()) {
151             LOG.info("Service compliant !");
152         } else {
153             LOG.info("Service is not compliant !");
154             return ModelMappingUtils
155                     .createDeleteServiceReply(input, ResponseCodes.FINAL_ACK_YES,
156                             "Service not compliant !", ResponseCodes.RESPONSE_FAILED);
157         }
158
159         //Check presence of service to be deleted
160         String serviceName = input.getServiceDeleteReqInfo().getServiceName();
161         LOG.info("serviceName : {}", serviceName);
162         try {
163             Optional<Services> service = this.serviceDataStoreOperations.getService(serviceName);
164             if (!service.isPresent()) {
165                 message = "Service '" + serviceName + "' does not exist in datastore";
166                 LOG.error(message);
167                 return ModelMappingUtils.createDeleteServiceReply(input, ResponseCodes.FINAL_ACK_YES,
168                         message, ResponseCodes.RESPONSE_FAILED);
169             }
170         } catch (NullPointerException e) {
171             LOG.error("failed to get service '{}' from datastore : ", serviceName, e);
172             message = "Service '" + serviceName + "' does not exist in datastore";
173             LOG.error(message);
174             return ModelMappingUtils.createDeleteServiceReply(input, ResponseCodes.FINAL_ACK_YES, message,
175                     ResponseCodes.RESPONSE_FAILED);
176         }
177         LOG.info("Service '{}' present in datastore !", serviceName);
178         this.pceListenerImpl.setInput(new ServiceInput(input));
179         this.pceListenerImpl.setServiceReconfigure(false);
180         this.pceListenerImpl.setserviceDataStoreOperations(this.serviceDataStoreOperations);
181         this.rendererListenerImpl.setserviceDataStoreOperations(serviceDataStoreOperations);
182         this.rendererListenerImpl.setServiceInput(new ServiceInput(input));
183         org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017
184             .ServiceDeleteInput serviceDeleteInput = ModelMappingUtils.createServiceDeleteInput(
185                     new ServiceInput(input));
186         org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceDeleteOutput output =
187                 this.rendererServiceWrapper.performRenderer(serviceDeleteInput,
188                 ServiceNotificationTypes.ServiceDeleteResult);
189         if (output != null) {
190             LOG.info("Service present in datastore, service-delete in progress...");
191             ConfigurationResponseCommon common = output.getConfigurationResponseCommon();
192             return ModelMappingUtils.createDeleteServiceReply(input, common.getAckFinalIndicator(),
193                     common.getResponseMessage(), common.getResponseCode());
194         } else {
195             return ModelMappingUtils.createDeleteServiceReply(input, ResponseCodes.FINAL_ACK_YES,
196                     "Renderer service delete failed !", ResponseCodes.RESPONSE_FAILED);
197         }
198     }
199
200     @Override
201     public ListenableFuture<RpcResult<ServiceFeasibilityCheckOutput>> serviceFeasibilityCheck(
202             ServiceFeasibilityCheckInput input) {
203         throw new UnsupportedOperationException("Not implemented yet");
204     }
205
206     @Override
207     public ListenableFuture<RpcResult<ServiceReconfigureOutput>> serviceReconfigure(ServiceReconfigureInput input) {
208         throw new UnsupportedOperationException("Not implemented yet");
209     }
210
211     @Override
212     public ListenableFuture<RpcResult<ServiceRestorationOutput>> serviceRestoration(ServiceRestorationInput input) {
213         throw new UnsupportedOperationException("Not implemented yet");
214     }
215
216     @Override
217     public ListenableFuture<RpcResult<EquipmentNotificationOutput>>
218             equipmentNotification(EquipmentNotificationInput input) {
219         // TODO Auto-generated method stub
220         return null;
221     }
222
223     @Override
224     public ListenableFuture<RpcResult<ServiceRerouteConfirmOutput>>
225             serviceRerouteConfirm(ServiceRerouteConfirmInput input) {
226         // TODO Auto-generated method stub
227         return null;
228     }
229
230     @Override
231     public ListenableFuture<RpcResult<ServiceRerouteOutput>> serviceReroute(ServiceRerouteInput input) {
232         LOG.info("RPC service reroute received");
233         String message = "";
234         Optional<Services> servicesObject = this.serviceDataStoreOperations.getService(input.getServiceName());
235         if (servicesObject.isPresent()) {
236             Services service = servicesObject.get();
237             ServiceDeleteInputBuilder deleteInputBldr = new ServiceDeleteInputBuilder();
238             DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssxxx");
239             OffsetDateTime offsetDateTime = OffsetDateTime.now(ZoneOffset.UTC);
240             DateAndTime datetime = new DateAndTime(dtf.format(offsetDateTime));
241             deleteInputBldr.setServiceDeleteReqInfo(new ServiceDeleteReqInfoBuilder()
242                 .setServiceName(input.getServiceName()).setDueDate(datetime)
243                 .setTailRetention(TailRetention.No).build());
244             SdncRequestHeaderBuilder sdncBuilder = new SdncRequestHeaderBuilder();
245             sdncBuilder.setNotificationUrl(service.getSdncRequestHeader().getNotificationUrl());
246             sdncBuilder.setRequestId(service.getSdncRequestHeader().getRequestId());
247             sdncBuilder.setRequestSystemId(service.getSdncRequestHeader().getRequestSystemId());
248             sdncBuilder.setRpcAction(RpcActions.ServiceDelete);
249             deleteInputBldr.setSdncRequestHeader(sdncBuilder.build());
250             ServiceInput serviceInput = new ServiceInput(deleteInputBldr.build());
251             serviceInput.setServiceAEnd(service.getServiceAEnd());
252             serviceInput.setServiceZEnd(service.getServiceZEnd());
253             serviceInput.setConnectionType(service.getConnectionType());
254             this.pceListenerImpl.setInput(serviceInput);
255             this.pceListenerImpl.setServiceReconfigure(true);
256             this.pceListenerImpl.setserviceDataStoreOperations(this.serviceDataStoreOperations);
257             this.rendererListenerImpl.setServiceInput(serviceInput);
258             this.rendererListenerImpl.setserviceDataStoreOperations(this.serviceDataStoreOperations);
259             org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017
260                 .ServiceDeleteInput serviceDeleteInput = ModelMappingUtils.createServiceDeleteInput(
261                         new ServiceInput(deleteInputBldr.build()));
262             org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017
263                 .ServiceDeleteOutput output = this.rendererServiceWrapper.performRenderer(serviceDeleteInput,
264                     ServiceNotificationTypes.ServiceDeleteResult);
265             if (output != null) {
266                 LOG.info("Service present in datastore, service-reroute in progress...");
267                 ConfigurationResponseCommon common = output.getConfigurationResponseCommon();
268                 return ModelMappingUtils.createRerouteServiceReply(input, common.getAckFinalIndicator(),
269                         common.getResponseMessage(), RpcStatus.Successful);
270             } else {
271                 return ModelMappingUtils.createRerouteServiceReply(input, ResponseCodes.FINAL_ACK_YES,
272                         "Renderer service delete failed !", RpcStatus.Failed);
273             }
274         } else {
275             LOG.error("Service '{}' is not present", input.getServiceName());
276             message = "Service '" + input.getServiceName() + "' is not present";
277             return ModelMappingUtils.createRerouteServiceReply(input, ResponseCodes.FINAL_ACK_NO, message,
278                     RpcStatus.Failed);
279         }
280     }
281
282     @Override
283     public ListenableFuture<RpcResult<ServiceReversionOutput>> serviceReversion(ServiceReversionInput input) {
284         // TODO Auto-generated method stub
285         return null;
286     }
287
288     @Override
289     public ListenableFuture<RpcResult<ServiceRollOutput>> serviceRoll(ServiceRollInput input) {
290         // TODO Auto-generated method stub
291         return null;
292     }
293
294     @Override
295     public ListenableFuture<RpcResult<NetworkReOptimizationOutput>>
296             networkReOptimization(NetworkReOptimizationInput input) {
297         // TODO Auto-generated method stub
298         return null;
299     }
300
301     @Override
302     public ListenableFuture<RpcResult<TempServiceDeleteOutput>> tempServiceDelete(TempServiceDeleteInput input) {
303         LOG.info("RPC temp serviceDelete request received for {}", input.getCommonId());
304         String message = "";
305
306         /*
307          * Upon receipt of service-deleteService RPC, service header and sdnc-request
308          * header compliancy are verified.
309          */
310         LOG.info("checking Service Compliancy ...");
311         ComplianceCheckResult serviceHandlerCheckResult = ServicehandlerCompliancyCheck.check(input.getCommonId(),
312                 null, null, RpcActions.ServiceDelete, false, false);
313         if (serviceHandlerCheckResult.hasPassed()) {
314             LOG.info("Service compliant !");
315         } else {
316             LOG.info("Service is not compliant !");
317             return ModelMappingUtils.createDeleteServiceReply(input, ResponseCodes.FINAL_ACK_YES,
318                     "Service not compliant !", ResponseCodes.RESPONSE_FAILED);
319         }
320
321         //Check presence of service to be deleted
322         String commonId = input.getCommonId();
323         LOG.info("service common-id : {}", commonId);
324         try {
325             Optional<org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.temp.service.list
326                 .Services> service = this.serviceDataStoreOperations.getTempService(commonId);
327             if (!service.isPresent()) {
328                 message = "Service '" + commonId + "' does not exist in datastore";
329                 LOG.error(message);
330                 return ModelMappingUtils.createDeleteServiceReply(input, ResponseCodes.FINAL_ACK_YES,
331                         message, ResponseCodes.RESPONSE_FAILED);
332             }
333         } catch (NullPointerException e) {
334             LOG.info("failed to get service '{}' from datastore : ", commonId, e);
335         }
336
337         LOG.info("Service '{}' present in datastore !", commonId);
338         this.pceListenerImpl.setInput(new ServiceInput(input));
339         this.pceListenerImpl.setServiceReconfigure(false);
340         this.pceListenerImpl.setserviceDataStoreOperations(this.serviceDataStoreOperations);
341         this.rendererListenerImpl.setserviceDataStoreOperations(this.serviceDataStoreOperations);
342         this.rendererListenerImpl.setServiceInput(new ServiceInput(input));
343         this.rendererListenerImpl.setTempService(true);
344         org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.renderer.rev171017.ServiceDeleteOutput output =
345                 this.rendererServiceWrapper.performRenderer(input, ServiceNotificationTypes.ServiceDeleteResult);
346         if (output != null) {
347             LOG.info("Temp Service present in datastore, service-delete in progress...");
348             ConfigurationResponseCommon common = output.getConfigurationResponseCommon();
349             return ModelMappingUtils.createDeleteServiceReply(input, common.getAckFinalIndicator(),
350                     common.getResponseMessage(), common.getResponseCode());
351         } else {
352             return ModelMappingUtils.createDeleteServiceReply(input, ResponseCodes.FINAL_ACK_YES,
353                     "Renderer service delete failed !", ResponseCodes.RESPONSE_FAILED);
354         }
355     }
356
357     @Override
358     public ListenableFuture<RpcResult<TempServiceCreateOutput>> tempServiceCreate(TempServiceCreateInput input) {
359         LOG.info("RPC temp service creation received");
360         // Validation
361         OperationResult validationResult = ServiceCreateValidation.validateServiceCreateRequest(
362                 new ServiceInput(input), RpcActions.TempServiceCreate);
363         if (! validationResult.isSuccess()) {
364             LOG.warn("Aborting service create because validation of service create request failed: {}",
365                     validationResult.getResultMessage());
366             return ModelMappingUtils.createCreateServiceReply(input, ResponseCodes.FINAL_ACK_YES,
367                     validationResult.getResultMessage(), ResponseCodes.RESPONSE_FAILED);
368         }
369
370         // Starting service create operation
371         LOG.info("Commencing PCE");
372         this.pceListenerImpl.setInput(new ServiceInput(input));
373         this.pceListenerImpl.setServiceReconfigure(false);
374         this.pceListenerImpl.setserviceDataStoreOperations(this.serviceDataStoreOperations);
375         this.pceListenerImpl.setTempService(true);
376         this.rendererListenerImpl.setserviceDataStoreOperations(serviceDataStoreOperations);
377         this.rendererListenerImpl.setServiceInput(new ServiceInput(input));
378         this.rendererListenerImpl.setTempService(true);
379         this.pceServiceWrapper.performPCE(input, true);
380         PathComputationRequestOutput output = this.pceServiceWrapper.performPCE(input, true);
381         if (output != null) {
382             LOG.info("Service compliant, temp serviceCreate in progress...");
383             ConfigurationResponseCommon common = output.getConfigurationResponseCommon();
384             return ModelMappingUtils.createCreateServiceReply(input, common.getAckFinalIndicator(),
385                     common.getResponseMessage(), common.getResponseCode());
386         } else {
387             return ModelMappingUtils.createCreateServiceReply(input, ResponseCodes.FINAL_ACK_YES,
388                     "PCE calculation failed", ResponseCodes.RESPONSE_FAILED);
389         }
390     }
391
392 }