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