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.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;
42 * @author Waqas Ikram (waqas.ikram@est.tech)
43 * @author Andrew Lamb (andrew.a.lamb@est.tech)
46 public abstract class AbstractNetworkServiceTask {
47 private final Logger logger = LoggerFactory.getLogger(getClass());
48 protected final DatabaseServiceProvider databaseServiceProvider;
50 protected AbstractNetworkServiceTask(final DatabaseServiceProvider jobServiceProvider) {
51 this.databaseServiceProvider = jobServiceProvider;
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);
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());
73 if (JobStatusEnum.FINISHED.equals(jobStatus)) {
74 nfvoJob.endTime(LocalDateTime.now());
77 nfvoJob.nfvoJobStatus(
78 new NfvoJobStatus().status(jobStatus).description(description).updatedTime(LocalDateTime.now()));
79 databaseServiceProvider.addJob(nfvoJob);
83 public void setJobStatusToError(final DelegateExecution execution, final String description) {
84 logger.info("Setting Job Status to {}", JobStatusEnum.ERROR);
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);
92 final NfvoJob nfvoJob = optional.get();
93 nfvoJob.status(JobStatusEnum.ERROR).endTime(LocalDateTime.now());
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()));
100 nfvoJob.nfvoJobStatus(new NfvoJobStatus().status(JobStatusEnum.ERROR).description(description)
101 .updatedTime(LocalDateTime.now()));
103 databaseServiceProvider.addJob(nfvoJob);
105 logger.info("Finished setting Job Status to {}", JobStatusEnum.ERROR);
109 public void updateNsLcmOpOccStatusToCompleted(final DelegateExecution execution) {
110 logger.info("Executing updateNsLcmOpOccStatusToCompleted ...");
112 updateNsLcmOpOccOperationState(execution, OperationStateEnum.COMPLETED);
114 logger.info("Finished executing updateNsLcmOpOccStatusToCompleted ...");
118 public void updateNsLcmOpOccStatusToFailed(final DelegateExecution execution) {
119 logger.info("Executing updateNsLcmOpOccStatusToFailed ...");
121 updateNsLcmOpOccOperationState(execution, OperationStateEnum.FAILED);
123 logger.info("Finished executing updateNsLcmOpOccStatusToFailed ...");
127 protected void abortOperation(final DelegateExecution execution, final String message) {
128 abortOperation(execution, message, new InlineResponse400().detail(message));
131 private void updateNsLcmOpOccOperationState(final DelegateExecution execution,
132 final OperationStateEnum operationState) {
133 final String occId = (String) execution.getVariable(OCC_ID_PARAM_NAME);
135 final boolean isSuccessful = databaseServiceProvider.updateNsLcmOpOccOperationState(occId, operationState);
137 final String message =
138 "Unable to update NsLcmOpOcc " + occId + " operationState to" + operationState + " in database";
139 logger.error(message);
140 abortOperation(execution, message);
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");
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");
161 return optional.get();
164 protected void updateNsInstanceStatus(final DelegateExecution execution, final State nsStatus) {
165 final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
167 logger.info("Updating NfvoNsInst Status to {} and saving to DB", nsStatus);
168 databaseServiceProvider.updateNsInstState(nsInstId, nsStatus);
171 protected NfvoNsInst getNfvoNsInst(final DelegateExecution execution) {
172 final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
173 return getNfvoNsInst(execution, nsInstId);
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);
180 if (optionalNfvoNsInst.isEmpty()) {
181 final String message = "Unable to find NS Instance in database using id: " + nsInstId;
182 abortOperation(execution, message);
185 return optionalNfvoNsInst.get();