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