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 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.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 public AbstractNetworkServiceTask(final DatabaseServiceProvider jobServiceProvider) {
51 this.databaseServiceProvider = jobServiceProvider;
54 public void setJobStatus(final DelegateExecution execution, final JobStatusEnum jobStatus,
55 final String description) {
56 logger.info("Setting Job Status to {}", jobStatus);
57 final NfvoJob nfvoJob = getNfvoJob(execution);
58 nfvoJob.status(jobStatus);
59 if (JobStatusEnum.STARTED.equals(jobStatus)) {
60 nfvoJob.processInstanceId(execution.getProcessInstanceId());
63 if (JobStatusEnum.FINISHED.equals(jobStatus)) {
64 nfvoJob.endTime(LocalDateTime.now());
67 nfvoJob.nfvoJobStatus(
68 new NfvoJobStatus().status(jobStatus).description(description).updatedTime(LocalDateTime.now()));
69 databaseServiceProvider.addJob(nfvoJob);
73 public void setJobStatusToError(final DelegateExecution execution, final String description) {
74 logger.info("Setting Job Status to {}", JobStatusEnum.ERROR);
76 final String jobId = (String) execution.getVariable(JOB_ID_PARAM_NAME);
77 final Optional<NfvoJob> optional = databaseServiceProvider.getJob(jobId);
78 if (optional.isPresent()) {
79 final InlineResponse400 problemDetails =
80 (InlineResponse400) execution.getVariable(CREATE_NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME);
82 final NfvoJob nfvoJob = optional.get();
83 nfvoJob.status(JobStatusEnum.ERROR).endTime(LocalDateTime.now());
85 if (problemDetails != null) {
86 logger.error("Found failed reason: {}", problemDetails);
87 nfvoJob.nfvoJobStatus(new NfvoJobStatus().status(JobStatusEnum.ERROR)
88 .description(problemDetails.getDetail()).updatedTime(LocalDateTime.now()));
90 nfvoJob.nfvoJobStatus(new NfvoJobStatus().status(JobStatusEnum.ERROR).description(description)
91 .updatedTime(LocalDateTime.now()));
93 databaseServiceProvider.addJob(nfvoJob);
95 logger.info("Finished setting Job Status to {}", JobStatusEnum.ERROR);
99 public void updateNsLcmOpOccStatusToCompleted(final DelegateExecution execution) {
100 logger.info("Executing updateNsLcmOpOccStatusToCompleted ...");
101 final String occId = (String) execution.getVariable(OCC_ID_PARAM_NAME);
103 final Optional<NsLcmOpOcc> optional = databaseServiceProvider.getNsLcmOpOcc(occId);
105 if (optional.isEmpty()) {
106 final String message = "Unable to find record for NSLcmOpOcc in database using id: " + occId;
107 logger.error(message);
108 abortOperation(execution, message);
111 final NsLcmOpOcc nsLcmOpOcc = optional.get();
112 final OperationStateEnum operationStateCompleted = OperationStateEnum.COMPLETED;
113 logger.info("Setting operation state to {} for id: {}", operationStateCompleted, occId);
114 nsLcmOpOcc.setOperationState(operationStateCompleted);
115 databaseServiceProvider.addNSLcmOpOcc(nsLcmOpOcc);
117 logger.info("Finished executing updateNsLcmOpOccStatusToCompleted ...");
121 public void updateNsLcmOpOccStatusToFailed(final DelegateExecution execution) {
122 logger.info("Executing updateNsLcmOpOccStatusToFailed ...");
123 final String occId = (String) execution.getVariable(OCC_ID_PARAM_NAME);
125 final Optional<NsLcmOpOcc> optional = databaseServiceProvider.getNsLcmOpOcc(occId);
127 if (optional.isPresent()) {
128 final NsLcmOpOcc nsLcmOpOcc = optional.get();
129 final OperationStateEnum operationStateFailed = OperationStateEnum.FAILED;
130 logger.info("Setting operation state to {} for id: {}", operationStateFailed, occId);
131 nsLcmOpOcc.setOperationState(operationStateFailed);
133 databaseServiceProvider.addNSLcmOpOcc(nsLcmOpOcc);
135 logger.error("Unable to find record for NSLcmOpOcc in database using id: {}", occId);
138 logger.info("Finished executing updateNsLcmOpOccStatusToFailed ...");
142 protected void abortOperation(final DelegateExecution execution, final String message) {
143 abortOperation(execution, message, new InlineResponse400().detail(message));
146 protected void abortOperation(final DelegateExecution execution, final String message,
147 final InlineResponse400 problemDetails) {
148 logger.error(message);
149 execution.setVariable(CREATE_NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, problemDetails);
150 throw new BpmnError("WORKFLOW_FAILED");
153 private NfvoJob getNfvoJob(final DelegateExecution execution) {
154 final String jobId = (String) execution.getVariable(JOB_ID_PARAM_NAME);
155 final Optional<NfvoJob> optional = databaseServiceProvider.getJob(jobId);
156 if (optional.isEmpty()) {
157 final String message = "Unable to find job using job id: " + jobId;
158 logger.error(message);
159 execution.setVariable(CREATE_NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME,
160 new InlineResponse400().detail(message));
161 throw new BpmnError("WORKFLOW_FAILED");
164 return optional.get();
167 protected NfvoNsInst getNfvoNsInst(final DelegateExecution execution) {
168 final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
169 return getNfvoNsInst(execution, nsInstId);
172 protected NfvoNsInst getNfvoNsInst(final DelegateExecution execution, final String nsInstId) {
173 logger.info("Getting NfvoNsInst to update with nsInstId: {}", nsInstId);
174 final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstId);
176 if (optionalNfvoNsInst.isEmpty()) {
177 final String message = "Unable to find NS Instance in database using id: " + nsInstId;
178 abortOperation(execution, message);
181 return optionalNfvoNsInst.get();