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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.tasks;
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;
43 * @author Waqas Ikram (waqas.ikram@est.tech)
44 * @author Andrew Lamb (andrew.a.lamb@est.tech)
47 public abstract class AbstractNetworkServiceTask {
48 private final Logger logger = LoggerFactory.getLogger(getClass());
49 protected final DatabaseServiceProvider databaseServiceProvider;
51 protected AbstractNetworkServiceTask(final DatabaseServiceProvider jobServiceProvider) {
52 this.databaseServiceProvider = jobServiceProvider;
55 public void addJobStatus(final DelegateExecution execution, final JobStatusEnum jobStatus,
56 final String description) {
57 final NfvoJobStatus nfvoJobStatus =
58 new NfvoJobStatus().status(jobStatus).description(description).updatedTime(LocalDateTime.now());
59 logger.info("Adding NfvoJobStatus {}", nfvoJobStatus);
60 final NfvoJob nfvoJob = getNfvoJob(execution);
61 nfvoJob.nfvoJobStatus(nfvoJobStatus);
62 databaseServiceProvider.addJob(nfvoJob);
65 public void setJobStatus(final DelegateExecution execution, final JobStatusEnum jobStatus,
66 final String description) {
67 logger.info("Setting Job Status to {}", jobStatus);
68 final NfvoJob nfvoJob = getNfvoJob(execution);
69 nfvoJob.status(jobStatus);
70 if (JobStatusEnum.STARTED.equals(jobStatus)) {
71 nfvoJob.processInstanceId(execution.getProcessInstanceId());
74 if (JobStatusEnum.FINISHED.equals(jobStatus)) {
75 nfvoJob.endTime(LocalDateTime.now());
78 nfvoJob.nfvoJobStatus(
79 new NfvoJobStatus().status(jobStatus).description(description).updatedTime(LocalDateTime.now()));
80 databaseServiceProvider.addJob(nfvoJob);
84 public void setJobStatusToError(final DelegateExecution execution, final String description) {
85 logger.info("Setting Job Status to {}", JobStatusEnum.ERROR);
87 final String jobId = (String) execution.getVariable(JOB_ID_PARAM_NAME);
88 final Optional<NfvoJob> optional = databaseServiceProvider.getJob(jobId);
89 if (optional.isPresent()) {
90 final InlineResponse400 problemDetails =
91 (InlineResponse400) execution.getVariable(NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME);
93 final NfvoJob nfvoJob = optional.get();
94 nfvoJob.status(JobStatusEnum.ERROR).endTime(LocalDateTime.now());
96 if (problemDetails != null) {
97 logger.error("Found failed reason: {}", problemDetails);
98 nfvoJob.nfvoJobStatus(new NfvoJobStatus().status(JobStatusEnum.ERROR)
99 .description(problemDetails.getDetail()).updatedTime(LocalDateTime.now()));
101 nfvoJob.nfvoJobStatus(new NfvoJobStatus().status(JobStatusEnum.ERROR).description(description)
102 .updatedTime(LocalDateTime.now()));
104 databaseServiceProvider.addJob(nfvoJob);
106 logger.info("Finished setting Job Status to {}", JobStatusEnum.ERROR);
110 public void updateNsLcmOpOccStatusToCompleted(final DelegateExecution execution) {
111 logger.info("Executing updateNsLcmOpOccStatusToCompleted ...");
112 final String occId = (String) execution.getVariable(OCC_ID_PARAM_NAME);
114 final Optional<NsLcmOpOcc> optional = databaseServiceProvider.getNsLcmOpOcc(occId);
116 if (optional.isEmpty()) {
117 final String message = "Unable to find record for NSLcmOpOcc in database using id: " + occId;
118 logger.error(message);
119 abortOperation(execution, message);
122 final NsLcmOpOcc nsLcmOpOcc = optional.get();
123 final OperationStateEnum operationStateCompleted = OperationStateEnum.COMPLETED;
124 logger.info("Setting operation state to {} for id: {}", operationStateCompleted, occId);
125 nsLcmOpOcc.setOperationState(operationStateCompleted);
126 databaseServiceProvider.addNSLcmOpOcc(nsLcmOpOcc);
128 logger.info("Finished executing updateNsLcmOpOccStatusToCompleted ...");
132 public void updateNsLcmOpOccStatusToFailed(final DelegateExecution execution) {
133 logger.info("Executing updateNsLcmOpOccStatusToFailed ...");
134 final String occId = (String) execution.getVariable(OCC_ID_PARAM_NAME);
136 final Optional<NsLcmOpOcc> optional = databaseServiceProvider.getNsLcmOpOcc(occId);
138 if (optional.isPresent()) {
139 final NsLcmOpOcc nsLcmOpOcc = optional.get();
140 final OperationStateEnum operationStateFailed = OperationStateEnum.FAILED;
141 logger.info("Setting operation state to {} for id: {}", operationStateFailed, occId);
142 nsLcmOpOcc.setOperationState(operationStateFailed);
144 databaseServiceProvider.addNSLcmOpOcc(nsLcmOpOcc);
146 logger.error("Unable to find record for NSLcmOpOcc in database using id: {}", occId);
149 logger.info("Finished executing updateNsLcmOpOccStatusToFailed ...");
153 protected void abortOperation(final DelegateExecution execution, final String message) {
154 abortOperation(execution, message, new InlineResponse400().detail(message));
157 protected void abortOperation(final DelegateExecution execution, final String message,
158 final InlineResponse400 problemDetails) {
159 logger.error(message);
160 execution.setVariable(NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, problemDetails);
161 throw new BpmnError("WORKFLOW_FAILED");
164 private NfvoJob getNfvoJob(final DelegateExecution execution) {
165 final String jobId = (String) execution.getVariable(JOB_ID_PARAM_NAME);
166 final Optional<NfvoJob> optional = databaseServiceProvider.getJob(jobId);
167 if (optional.isEmpty()) {
168 final String message = "Unable to find job using job id: " + jobId;
169 logger.error(message);
170 execution.setVariable(NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, new InlineResponse400().detail(message));
171 throw new BpmnError("WORKFLOW_FAILED");
174 return optional.get();
177 protected void updateNsInstanceStatus(final DelegateExecution execution, final State nsStatus) {
178 final NfvoNsInst nfvoNsInst = getNfvoNsInst(execution);
179 logger.info("Updating NfvoNsInst Status to {} and saving to DB", nsStatus);
180 nfvoNsInst.setStatus(nsStatus);
181 databaseServiceProvider.saveNfvoNsInst(nfvoNsInst);
184 protected NfvoNsInst getNfvoNsInst(final DelegateExecution execution) {
185 final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
186 return getNfvoNsInst(execution, nsInstId);
189 protected NfvoNsInst getNfvoNsInst(final DelegateExecution execution, final String nsInstId) {
190 logger.info("Getting NfvoNsInst to update with nsInstId: {}", nsInstId);
191 final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstId);
193 if (optionalNfvoNsInst.isEmpty()) {
194 final String message = "Unable to find NS Instance in database using id: " + nsInstId;
195 abortOperation(execution, message);
198 return optionalNfvoNsInst.get();