remove stubs bundles from build process
[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.rev170426.PathComputationRequestOutput;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.RpcActions;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.service.types.rev161014.sdnc.request.header.SdncRequestHeaderBuilder;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.RpcStatus;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.State;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.EquipmentNotificationInput;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.EquipmentNotificationOutput;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.NetworkReOptimizationInput;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.NetworkReOptimizationOutput;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.OrgOpenroadmServiceService;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateInput;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateInputBuilder;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateOutput;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteInput;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteInputBuilder;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteOutput;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceFeasibilityCheckInput;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceFeasibilityCheckOutput;
46 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceReconfigureInput;
47 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceReconfigureOutput;
48 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRerouteConfirmInput;
49 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRerouteConfirmOutput;
50 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRerouteInput;
51 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRerouteOutput;
52 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRerouteOutputBuilder;
53 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRestorationInput;
54 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRestorationOutput;
55 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceReversionInput;
56 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceReversionOutput;
57 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRollInput;
58 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceRollOutput;
59 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceCreateInput;
60 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceCreateOutput;
61 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceDeleteInput;
62 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.TempServiceDeleteOutput;
63 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.create.input.ServiceAEndBuilder;
64 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.create.input.ServiceZEndBuilder;
65 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.delete.input.ServiceDeleteReqInfo.TailRetention;
66 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.delete.input.ServiceDeleteReqInfoBuilder;
67 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.list.Services;
68 import org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev170426.ServiceImplementationRequestInput;
69 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
70 import org.opendaylight.yangtools.yang.common.RpcResult;
71 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
72 import org.slf4j.Logger;
73 import org.slf4j.LoggerFactory;
74
75
76 /**
77  * Top level service interface providing main OpenROADM controller services.
78  */
79 public class ServicehandlerImpl implements OrgOpenroadmServiceService {
80
81     private static final Logger LOG = LoggerFactory.getLogger(ServicehandlerImpl.class);
82
83     private DataBroker db;
84     private ServiceDataStoreOperations serviceDataStoreOperations;
85     private RendererServiceOperations rendererServiceOperations;
86     private PCEServiceWrapper pceServiceWrapper;
87
88     //TODO: remove private request fields as they are in global scope
89
90     public ServicehandlerImpl(DataBroker databroker, PathComputationService pathComputationService,
91                               RendererServiceOperations rendererServiceOperations) {
92         this.db = databroker;
93         this.rendererServiceOperations = rendererServiceOperations;
94         this.serviceDataStoreOperations = new ServiceDataStoreOperationsImpl(this.db);
95         this.serviceDataStoreOperations.initialize();
96         this.pceServiceWrapper = new PCEServiceWrapper(pathComputationService);
97     }
98
99     @Override
100     public ListenableFuture<RpcResult<ServiceCreateOutput>> serviceCreate(ServiceCreateInput input) {
101         LOG.info("RPC service creation received");
102         // Validation
103         OperationResult validationResult = ServiceCreateValidation.validateServiceCreateRequest(input);
104         if (! validationResult.isSuccess()) {
105             LOG.warn("Aborting service create because validation of service create request failed: {}",
106                     validationResult.getResultMessage());
107             return ModelMappingUtils.createCreateServiceReply(input, ResponseCodes.FINAL_ACK_YES,
108                     validationResult.getResultMessage(), ResponseCodes.RESPONSE_FAILED);
109         }
110
111         // Starting service create operation
112         LOG.info("Commencing PCE");
113         //TODO: createService service status into datastore
114         PathComputationRequestOutput pceResponse = this.pceServiceWrapper.performPCE(input, true);
115         String pceResponseCode = pceResponse.getConfigurationResponseCommon().getResponseCode();
116         if (!ResponseCodes.RESPONSE_OK.equals(pceResponseCode)) {
117             LOG.info("PCE calculation failed {}", pceResponseCode);
118             return ModelMappingUtils.createCreateServiceReply(input, ResponseCodes.FINAL_ACK_YES,
119                     pceResponse.getConfigurationResponseCommon().getResponseMessage(), ResponseCodes.RESPONSE_FAILED);
120         }
121
122         LOG.info("PCE calculation done OK {}", pceResponseCode);
123
124         OperationResult operationResult = this.serviceDataStoreOperations.createService(input, pceResponse);
125         if (!operationResult.isSuccess()) {
126             String message = "Service status not updated in datastore !";
127             LOG.info(message);
128             return ModelMappingUtils.createCreateServiceReply(input, ResponseCodes.FINAL_ACK_YES, message,
129                     ResponseCodes.RESPONSE_FAILED);
130         }
131
132         OperationResult operationServicePathSaveResult = this.serviceDataStoreOperations.createServicePath(input,
133             pceResponse);
134         if (!operationServicePathSaveResult.isSuccess()) {
135             String message = "Service Path not updated in datastore !";
136             LOG.info(message);
137             return ModelMappingUtils.createCreateServiceReply(input, ResponseCodes.FINAL_ACK_YES, message,
138                     ResponseCodes.RESPONSE_FAILED);
139         }
140
141         ServiceImplementationRequestInput serviceImplementationRequest =
142                 ModelMappingUtils.createServiceImplementationRequest(input, pceResponse);
143         org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev170426
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.transportpce.b.c._interface.servicepath.rev170426.ServiceDeleteInput
217                 serviceDeleteInput = ModelMappingUtils.createServiceDeleteInput(input);
218         org.opendaylight.yang.gen.v1.http.org.transportpce.b.c._interface.servicepath.rev170426
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                 ServiceRerouteOutputBuilder output = new ServiceRerouteOutputBuilder()
330                     .setHardConstraints(null).setSoftConstraints(null).setStatus(
331                     org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.RpcStatus.Successful)
332                     .setStatusMessage("Service reroute successfully !");
333                 return RpcResultBuilder.success(output).buildFuture();
334             } else {
335                 LOG.error("Service '{}' is not present", input.getServiceName());
336                 message = "Service '" + input.getServiceName() + "' is not present";
337             }
338         } catch (InterruptedException | ExecutionException e) {
339             LOG.info("Exception caught" , e);
340         }
341         ServiceRerouteOutputBuilder output = new ServiceRerouteOutputBuilder()
342             .setHardConstraints(null).setSoftConstraints(null).setStatus(RpcStatus.Failed).setStatusMessage(message);
343         return RpcResultBuilder.success(output).buildFuture();
344     }
345
346     @Override
347     public ListenableFuture<RpcResult<ServiceReversionOutput>> serviceReversion(ServiceReversionInput input) {
348         // TODO Auto-generated method stub
349         return null;
350     }
351
352     @Override
353     public ListenableFuture<RpcResult<ServiceRollOutput>> serviceRoll(ServiceRollInput input) {
354         // TODO Auto-generated method stub
355         return null;
356     }
357
358     @Override
359     public ListenableFuture<RpcResult<NetworkReOptimizationOutput>>
360             networkReOptimization(NetworkReOptimizationInput input) {
361         // TODO Auto-generated method stub
362         return null;
363     }
364
365     @Override
366     public ListenableFuture<RpcResult<TempServiceDeleteOutput>> tempServiceDelete(TempServiceDeleteInput input) {
367         // TODO Auto-generated method stub
368         return null;
369     }
370
371     @Override
372     public ListenableFuture<RpcResult<TempServiceCreateOutput>> tempServiceCreate(TempServiceCreateInput input) {
373         // TODO Auto-generated method stub
374         return null;
375     }
376
377 }