a785a5199b4b118878394f2d2994f6583b2482ea
[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.JOB_ID_PARAM_NAME;
23 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.NS_INSTANCE_ID_PARAM_NAME;
24 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME;
25 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.OCC_ID_PARAM_NAME;
26 import java.time.LocalDateTime;
27 import java.util.Optional;
28 import org.camunda.bpm.engine.delegate.BpmnError;
29 import org.camunda.bpm.engine.delegate.DelegateExecution;
30 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum;
31 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoJob;
32 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoJobStatus;
33 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoNsInst;
34 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.OperationStateEnum;
35 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.State;
36 import org.onap.so.etsi.nfvo.ns.lcm.database.service.DatabaseServiceProvider;
37 import org.onap.so.etsi.nfvo.ns.lcm.model.InlineResponse400;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * @author Waqas Ikram (waqas.ikram@est.tech)
43  * @author Andrew Lamb (andrew.a.lamb@est.tech)
44  *
45  */
46 public abstract class AbstractNetworkServiceTask {
47     private final Logger logger = LoggerFactory.getLogger(getClass());
48     protected final DatabaseServiceProvider databaseServiceProvider;
49
50     protected AbstractNetworkServiceTask(final DatabaseServiceProvider jobServiceProvider) {
51         this.databaseServiceProvider = jobServiceProvider;
52     }
53
54     public void addJobStatus(final DelegateExecution execution, final JobStatusEnum jobStatus,
55             final String description) {
56         final NfvoJobStatus nfvoJobStatus =
57                 new NfvoJobStatus().status(jobStatus).description(description).updatedTime(LocalDateTime.now());
58         logger.info("Adding NfvoJobStatus {}", nfvoJobStatus);
59         final NfvoJob nfvoJob = getNfvoJob(execution);
60         nfvoJob.nfvoJobStatus(nfvoJobStatus);
61         databaseServiceProvider.addJob(nfvoJob);
62     }
63
64     public void setJobStatus(final DelegateExecution execution, final JobStatusEnum jobStatus,
65             final String description) {
66         logger.info("Setting Job Status to {}", jobStatus);
67         final NfvoJob nfvoJob = getNfvoJob(execution);
68         nfvoJob.status(jobStatus);
69         if (JobStatusEnum.STARTED.equals(jobStatus)) {
70             nfvoJob.processInstanceId(execution.getProcessInstanceId());
71         }
72
73         if (JobStatusEnum.FINISHED.equals(jobStatus)) {
74             nfvoJob.endTime(LocalDateTime.now());
75         }
76
77         nfvoJob.nfvoJobStatus(
78                 new NfvoJobStatus().status(jobStatus).description(description).updatedTime(LocalDateTime.now()));
79         databaseServiceProvider.addJob(nfvoJob);
80
81     }
82
83     public void setJobStatusToError(final DelegateExecution execution, final String description) {
84         logger.info("Setting Job Status to {}", JobStatusEnum.ERROR);
85
86         final String jobId = (String) execution.getVariable(JOB_ID_PARAM_NAME);
87         final Optional<NfvoJob> optional = databaseServiceProvider.getJob(jobId);
88         if (optional.isPresent()) {
89             final InlineResponse400 problemDetails =
90                     (InlineResponse400) execution.getVariable(NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME);
91
92             final NfvoJob nfvoJob = optional.get();
93             nfvoJob.status(JobStatusEnum.ERROR).endTime(LocalDateTime.now());
94
95             if (problemDetails != null) {
96                 logger.error("Found failed reason: {}", problemDetails);
97                 nfvoJob.nfvoJobStatus(new NfvoJobStatus().status(JobStatusEnum.ERROR)
98                         .description(problemDetails.getDetail()).updatedTime(LocalDateTime.now()));
99             }
100             nfvoJob.nfvoJobStatus(new NfvoJobStatus().status(JobStatusEnum.ERROR).description(description)
101                     .updatedTime(LocalDateTime.now()));
102
103             databaseServiceProvider.addJob(nfvoJob);
104         }
105         logger.info("Finished setting Job Status to {}", JobStatusEnum.ERROR);
106
107     }
108
109     public void updateNsLcmOpOccStatusToCompleted(final DelegateExecution execution) {
110         logger.info("Executing updateNsLcmOpOccStatusToCompleted ...");
111
112         updateNsLcmOpOccOperationState(execution, OperationStateEnum.COMPLETED);
113
114         logger.info("Finished executing updateNsLcmOpOccStatusToCompleted ...");
115
116     }
117
118     public void updateNsLcmOpOccStatusToFailed(final DelegateExecution execution) {
119         logger.info("Executing updateNsLcmOpOccStatusToFailed ...");
120
121         updateNsLcmOpOccOperationState(execution, OperationStateEnum.FAILED);
122
123         logger.info("Finished executing updateNsLcmOpOccStatusToFailed ...");
124
125     }
126
127     protected void abortOperation(final DelegateExecution execution, final String message) {
128         abortOperation(execution, message, new InlineResponse400().detail(message));
129     }
130
131     private void updateNsLcmOpOccOperationState(final DelegateExecution execution,
132             final OperationStateEnum operationState) {
133         final String occId = (String) execution.getVariable(OCC_ID_PARAM_NAME);
134
135         final boolean isSuccessful = databaseServiceProvider.updateNsLcmOpOccOperationState(occId, operationState);
136         if (!isSuccessful) {
137             final String message =
138                     "Unable to update NsLcmOpOcc " + occId + " operationState to" + operationState + " in database";
139             logger.error(message);
140             abortOperation(execution, message);
141         }
142     }
143
144     protected void abortOperation(final DelegateExecution execution, final String message,
145             final InlineResponse400 problemDetails) {
146         logger.error(message);
147         execution.setVariable(NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, problemDetails);
148         throw new BpmnError("WORKFLOW_FAILED");
149     }
150
151     private NfvoJob getNfvoJob(final DelegateExecution execution) {
152         final String jobId = (String) execution.getVariable(JOB_ID_PARAM_NAME);
153         final Optional<NfvoJob> optional = databaseServiceProvider.getJob(jobId);
154         if (optional.isEmpty()) {
155             final String message = "Unable to find job using job id: " + jobId;
156             logger.error(message);
157             execution.setVariable(NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, new InlineResponse400().detail(message));
158             throw new BpmnError("WORKFLOW_FAILED");
159
160         }
161         return optional.get();
162     }
163
164     protected void updateNsInstanceStatus(final DelegateExecution execution, final State nsStatus) {
165         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
166
167         logger.info("Updating NfvoNsInst Status to {} and saving to DB", nsStatus);
168         databaseServiceProvider.updateNsInstState(nsInstId, nsStatus);
169     }
170
171     protected NfvoNsInst getNfvoNsInst(final DelegateExecution execution) {
172         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
173         return getNfvoNsInst(execution, nsInstId);
174     }
175
176     protected NfvoNsInst getNfvoNsInst(final DelegateExecution execution, final String nsInstId) {
177         logger.info("Getting NfvoNsInst to update with nsInstId: {}", nsInstId);
178         final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstId);
179
180         if (optionalNfvoNsInst.isEmpty()) {
181             final String message = "Unable to find NS Instance in database using id: " + nsInstId;
182             abortOperation(execution, message);
183         }
184
185         return optionalNfvoNsInst.get();
186     }
187
188 }