Implementing Create NS
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-bpmn-flows / src / main / java / org / onap / so / etsi / nfvo / ns / lcm / bpmn / flows / tasks / AbstractNetworkServiceTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.tasks;
21
22 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.CREATE_NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME;
23 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.JOB_ID_PARAM_NAME;
24 import java.time.LocalDateTime;
25 import java.util.Optional;
26 import org.camunda.bpm.engine.delegate.BpmnError;
27 import org.camunda.bpm.engine.delegate.DelegateExecution;
28 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum;
29 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoJob;
30 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoJobStatus;
31 import org.onap.so.etsi.nfvo.ns.lcm.database.service.DatabaseServiceProvider;
32 import org.onap.so.etsi.nfvo.ns.lcm.model.InlineResponse400;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * @author Waqas Ikram (waqas.ikram@est.tech)
38  *
39  */
40 public abstract class AbstractNetworkServiceTask {
41     private final Logger logger = LoggerFactory.getLogger(getClass());
42     protected final DatabaseServiceProvider databaseServiceProvider;
43
44     public AbstractNetworkServiceTask(final DatabaseServiceProvider jobServiceProvider) {
45         this.databaseServiceProvider = jobServiceProvider;
46     }
47
48     public void setJobStatus(final DelegateExecution execution, final JobStatusEnum jobStatus,
49             final String description) {
50         logger.info("Setting Job Status to {}", jobStatus);
51         final NfvoJob nfvoJob = getNfvoJob(execution);
52         nfvoJob.status(jobStatus);
53         if (JobStatusEnum.STARTED.equals(jobStatus)) {
54             nfvoJob.processInstanceId(execution.getProcessInstanceId());
55         }
56
57         if (JobStatusEnum.FINISHED.equals(jobStatus)) {
58             nfvoJob.endTime(LocalDateTime.now());
59         }
60
61         nfvoJob.nfvoJobStatus(
62                 new NfvoJobStatus().status(jobStatus).description(description).updatedTime(LocalDateTime.now()));
63         databaseServiceProvider.addJob(nfvoJob);
64
65     }
66
67     public void setJobStatusToError(final DelegateExecution execution, final String description) {
68         logger.info("Setting Job Status to {}", JobStatusEnum.ERROR);
69
70         final String jobId = (String) execution.getVariable(JOB_ID_PARAM_NAME);
71         final Optional<NfvoJob> optional = databaseServiceProvider.getJob(jobId);
72         if (optional.isPresent()) {
73             final InlineResponse400 problemDetails =
74                     (InlineResponse400) execution.getVariable(CREATE_NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME);
75
76             final NfvoJob nfvoJob = optional.get();
77             nfvoJob.status(JobStatusEnum.ERROR).endTime(LocalDateTime.now());
78
79             if (problemDetails != null) {
80                 logger.error("Found failed reason: {}", problemDetails);
81                 nfvoJob.nfvoJobStatus(new NfvoJobStatus().status(JobStatusEnum.ERROR)
82                         .description(problemDetails.getDetail()).updatedTime(LocalDateTime.now()));
83             }
84             nfvoJob.nfvoJobStatus(new NfvoJobStatus().status(JobStatusEnum.ERROR).description(description)
85                     .updatedTime(LocalDateTime.now()));
86
87             databaseServiceProvider.addJob(nfvoJob);
88         }
89         logger.info("Finished setting Job Status to {}", JobStatusEnum.ERROR);
90
91     }
92
93     protected void abortOperation(final DelegateExecution execution, final String message) {
94         abortOperation(execution, message, new InlineResponse400().detail(message));
95     }
96
97     protected void abortOperation(final DelegateExecution execution, final String message,
98             final InlineResponse400 problemDetails) {
99         logger.error(message);
100         execution.setVariable(CREATE_NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, problemDetails);
101         throw new BpmnError("WORKFLOW_FAILED");
102     }
103
104     private NfvoJob getNfvoJob(final DelegateExecution execution) {
105         final String jobId = (String) execution.getVariable(JOB_ID_PARAM_NAME);
106         final Optional<NfvoJob> optional = databaseServiceProvider.getJob(jobId);
107         if (!optional.isPresent()) {
108             final String message = "Unable to find job using job id: " + jobId;
109             logger.error(message);
110             execution.setVariable(CREATE_NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME,
111                     new InlineResponse400().detail(message));
112             throw new BpmnError("WORKFLOW_FAILED");
113
114         }
115         return optional.get();
116     }
117 }