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 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());
64 if (JobStatusEnum.FINISHED.equals(jobStatus)) {
65 nfvoJob.endTime(LocalDateTime.now());
68 nfvoJob.nfvoJobStatus(
69 new NfvoJobStatus().status(jobStatus).description(description).updatedTime(LocalDateTime.now()));
70 databaseServiceProvider.addJob(nfvoJob);
74 public void setJobStatusToError(final DelegateExecution execution, final String description) {
75 logger.info("Setting Job Status to {}", JobStatusEnum.ERROR);
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);
83 final NfvoJob nfvoJob = optional.get();
84 nfvoJob.status(JobStatusEnum.ERROR).endTime(LocalDateTime.now());
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()));
91 nfvoJob.nfvoJobStatus(new NfvoJobStatus().status(JobStatusEnum.ERROR).description(description)
92 .updatedTime(LocalDateTime.now()));
94 databaseServiceProvider.addJob(nfvoJob);
96 logger.info("Finished setting Job Status to {}", JobStatusEnum.ERROR);
100 public void updateNsLcmOpOccStatusToCompleted(final DelegateExecution execution) {
101 logger.info("Executing updateNsLcmOpOccStatusToCompleted ...");
102 final String occId = (String) execution.getVariable(OCC_ID_PARAM_NAME);
104 final Optional<NsLcmOpOcc> optional = databaseServiceProvider.getNsLcmOpOcc(occId);
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);
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);
118 logger.info("Finished executing updateNsLcmOpOccStatusToCompleted ...");
122 public void updateNsLcmOpOccStatusToFailed(final DelegateExecution execution) {
123 logger.info("Executing updateNsLcmOpOccStatusToFailed ...");
124 final String occId = (String) execution.getVariable(OCC_ID_PARAM_NAME);
126 final Optional<NsLcmOpOcc> optional = databaseServiceProvider.getNsLcmOpOcc(occId);
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);
134 databaseServiceProvider.addNSLcmOpOcc(nsLcmOpOcc);
136 logger.error("Unable to find record for NSLcmOpOcc in database using id: {}", occId);
139 logger.info("Finished executing updateNsLcmOpOccStatusToFailed ...");
143 protected void abortOperation(final DelegateExecution execution, final String message) {
144 abortOperation(execution, message, new InlineResponse400().detail(message));
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");
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");
164 return optional.get();
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);
174 protected NfvoNsInst getNfvoNsInst(final DelegateExecution execution) {
175 final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
176 return getNfvoNsInst(execution, nsInstId);
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);
183 if (optionalNfvoNsInst.isEmpty()) {
184 final String message = "Unable to find NS Instance in database using id: " + nsInstId;
185 abortOperation(execution, message);
188 return optionalNfvoNsInst.get();