2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 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.
16 * ============LICENSE_END=========================================================
19 package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks;
21 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.CNF_STATUS_RESPONSE_PARAM_NAME;
22 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.OPERATION_STATUS_PARAM_NAME;
24 import java.util.Optional;
26 import org.camunda.bpm.engine.delegate.BpmnError;
27 import org.onap.so.bpmn.common.BuildingBlockExecution;
28 import org.onap.so.client.exception.ExceptionBuilder;
29 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
30 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc.OperationStateEnum;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
37 * @author sagar.shetty@est.tech
38 * @author Raviteja Karumuri (raviteja.karumuri@est.tech)
41 public class MonitorCnfmJobTask {
43 private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
44 private static final String MONITOR_JOB_NAME = "MonitorJobName";
45 public static final Set<OperationStateEnum> OPERATION_FINISHED_STATES =
46 Set.of(OperationStateEnum.COMPLETED, OperationStateEnum.FAILED, OperationStateEnum.ROLLED_BACK);
47 private static final Logger LOGGER = LoggerFactory.getLogger(MonitorCnfmJobTask.class);
48 protected final ExceptionBuilder exceptionUtil;
49 private final CnfmHttpServiceProvider cnfmHttpServiceProvider;
52 public MonitorCnfmJobTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
53 final ExceptionBuilder exceptionUtil) {
54 this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
55 this.exceptionUtil = exceptionUtil;
59 * Get the current operation status of cnfm job
61 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
63 public void getCurrentOperationStatus(final BuildingBlockExecution execution) {
65 LOGGER.debug("Executing getCurrentOperationStatus ...");
66 final Optional<URI> operationStatusURL = Optional.of(execution.getVariable(CNFM_REQUEST_STATUS_CHECK_URL));
67 LOGGER.debug("Executing getCurrentOperationStatus for CNF... :{}", operationStatusURL);
68 final Optional<AsLcmOpOcc> operationalJobStatus =
69 cnfmHttpServiceProvider.getOperationJobStatus(String.valueOf(operationStatusURL
70 .orElseThrow(() -> new BpmnError("Operational Status check url Not found"))));
72 final AsLcmOpOcc asLcmOpOccResponse = operationalJobStatus
73 .orElseThrow(() -> new BpmnError("Unable to get operational Job status from the CNFM"));
75 if (asLcmOpOccResponse.getOperationState() != null) {
76 final OperationStateEnum operationStatus = asLcmOpOccResponse.getOperationState();
77 LOGGER.debug("Operation {} with {} and operation retrieval status : {}", asLcmOpOccResponse.getId(),
78 operationStatus, operationStatus);
79 execution.setVariable(OPERATION_STATUS_PARAM_NAME, operationStatus);
81 LOGGER.debug("Operation {} without operationStatus", asLcmOpOccResponse.getId());
82 exceptionUtil.buildAndThrowWorkflowException(execution, 1206,
83 new Exception("Operation Status is empty"));
85 execution.setVariable(CNF_STATUS_RESPONSE_PARAM_NAME, asLcmOpOccResponse);
86 LOGGER.debug("Finished executing getCurrentOperationStatus for CNF...");
87 } catch (final Exception exception) {
88 LOGGER.error("Unable to invoke get current Operation status");
89 exceptionUtil.buildAndThrowWorkflowException(execution, 1209, exception);
94 * Log and throw exception on timeout for job status
96 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
98 public void timeOutLogFailure(final BuildingBlockExecution execution) {
99 String message = "CNF" + execution.getVariable(MONITOR_JOB_NAME) + "operation time out";
100 LOGGER.error(message);
101 exceptionUtil.buildAndThrowWorkflowException(execution, 1205, new Exception(message));
105 * Check the final status of cnfm job throw exception if not completed successfully
107 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
109 public void checkIfOperationWasSuccessful(final BuildingBlockExecution execution) {
110 LOGGER.debug("Executing CNF checkIfOperationWasSuccessful ...");
111 final OperationStateEnum operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
112 final AsLcmOpOcc cnfStautusResponse = execution.getVariable(CNF_STATUS_RESPONSE_PARAM_NAME);
113 if ((operationStatusOption == OperationStateEnum.FAILED)
114 || (operationStatusOption == OperationStateEnum.FAILED_TEMP)) {
115 final String message = "Unable to" + execution.getVariable(MONITOR_JOB_NAME) + "CNF jobId: "
116 + (cnfStautusResponse != null ? cnfStautusResponse.getId() : "null");
117 LOGGER.error(message);
118 exceptionUtil.buildAndThrowWorkflowException(execution, 1206, new Exception());
119 } else if ((operationStatusOption == OperationStateEnum.COMPLETED)) {
120 String monitorJobName = execution.getVariable(MONITOR_JOB_NAME);
121 LOGGER.debug("Successfully completed CNF {} job", monitorJobName);
126 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
127 * @return boolean to indicate whether job has competed or not
129 public boolean hasOperationFinished(final BuildingBlockExecution execution) {
130 LOGGER.debug("Executing hasOperationFinished ...");
132 final OperationStateEnum operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
133 if (operationStatusOption != null) {
134 return OPERATION_FINISHED_STATES.contains(operationStatusOption);
136 LOGGER.debug("OperationStatus is not present yet... ");
137 LOGGER.debug("Finished executing hasOperationFinished ...");