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.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;
37 * @author Waqas Ikram (waqas.ikram@est.tech)
40 public abstract class AbstractNetworkServiceTask {
41 private final Logger logger = LoggerFactory.getLogger(getClass());
42 protected final DatabaseServiceProvider databaseServiceProvider;
44 public AbstractNetworkServiceTask(final DatabaseServiceProvider jobServiceProvider) {
45 this.databaseServiceProvider = jobServiceProvider;
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());
57 if (JobStatusEnum.FINISHED.equals(jobStatus)) {
58 nfvoJob.endTime(LocalDateTime.now());
61 nfvoJob.nfvoJobStatus(
62 new NfvoJobStatus().status(jobStatus).description(description).updatedTime(LocalDateTime.now()));
63 databaseServiceProvider.addJob(nfvoJob);
67 public void setJobStatusToError(final DelegateExecution execution, final String description) {
68 logger.info("Setting Job Status to {}", JobStatusEnum.ERROR);
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);
76 final NfvoJob nfvoJob = optional.get();
77 nfvoJob.status(JobStatusEnum.ERROR).endTime(LocalDateTime.now());
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()));
84 nfvoJob.nfvoJobStatus(new NfvoJobStatus().status(JobStatusEnum.ERROR).description(description)
85 .updatedTime(LocalDateTime.now()));
87 databaseServiceProvider.addJob(nfvoJob);
89 logger.info("Finished setting Job Status to {}", JobStatusEnum.ERROR);
93 protected void abortOperation(final DelegateExecution execution, final String message) {
94 abortOperation(execution, message, new InlineResponse400().detail(message));
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");
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");
115 return optional.get();