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.CREATE_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;
25 import org.onap.logging.filter.base.ONAPComponents;
26 import org.onap.so.bpmn.common.BuildingBlockExecution;
27 import org.onap.so.client.exception.ExceptionBuilder;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Component;
32 import com.google.common.collect.ImmutableSet;
33 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
34 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc.OperationStateEnum;
38 * @author sagar.shetty@est.tech
41 public class MonitorCnfmCreateJobTask {
43 public static final ImmutableSet<OperationStateEnum> OPERATION_FINISHED_STATES =
44 ImmutableSet.of(OperationStateEnum.COMPLETED, OperationStateEnum.FAILED, OperationStateEnum.ROLLED_BACK);
45 private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
46 private static final Logger LOGGER = LoggerFactory.getLogger(MonitorCnfmCreateJobTask.class);
47 protected final ExceptionBuilder exceptionUtil;
48 private final CnfmHttpServiceProvider cnfmHttpServiceProvider;
51 public MonitorCnfmCreateJobTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
52 final ExceptionBuilder exceptionUtil) {
53 this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
54 this.exceptionUtil = exceptionUtil;
58 * Get the current operation status of instantiation job
60 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
62 public void getCurrentOperationStatus(final BuildingBlockExecution execution) {
64 LOGGER.debug("Executing getCurrentOperationStatus ...");
65 final URI operationStatusURL = execution.getVariable(CNFM_REQUEST_STATUS_CHECK_URL);
66 LOGGER.debug("Executing getCurrentOperationStatus for CNF... :{}", operationStatusURL);
67 final Optional<AsLcmOpOcc> instantiateOperationJobStatus =
68 cnfmHttpServiceProvider.getInstantiateOperationJobStatus(operationStatusURL.toString());
69 if (instantiateOperationJobStatus.isPresent()) {
70 final AsLcmOpOcc asLcmOpOccResponse = instantiateOperationJobStatus.get();
71 if (asLcmOpOccResponse.getOperationState() != null) {
72 final OperationStateEnum operationStatus = asLcmOpOccResponse.getOperationState();
73 LOGGER.debug("Operation {} with {} and operation retrieval status : {}", asLcmOpOccResponse.getId(),
74 operationStatus, asLcmOpOccResponse.getOperationState());
75 execution.setVariable(OPERATION_STATUS_PARAM_NAME, asLcmOpOccResponse.getOperationState());
78 LOGGER.debug("Operation {} without operationStatus and operation retrieval status :{}",
79 asLcmOpOccResponse.getId(), asLcmOpOccResponse.getOperationState());
81 execution.setVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME, instantiateOperationJobStatus.orElseThrow());
82 LOGGER.debug("Finished executing getCurrentOperationStatus for CNF...");
83 } catch (final Exception exception) {
84 final String message = "Unable to invoke get current Operation status";
85 LOGGER.error(message);
86 exceptionUtil.buildAndThrowWorkflowException(execution, 1209, message, ONAPComponents.SO);
92 * Log and throw exception on timeout for job status
94 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
96 public void timeOutLogFailue(final BuildingBlockExecution execution) {
97 final String message = "CNF Instantiation operation time out";
98 LOGGER.error(message);
99 exceptionUtil.buildAndThrowWorkflowException(execution, 1205, message, ONAPComponents.SO);
103 * Check the final status of instantiation throw exception if not completed successfully
105 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
107 public void checkIfOperationWasSuccessful(final BuildingBlockExecution execution) {
108 LOGGER.debug("Executing CNF checkIfOperationWasSuccessful ...");
109 final OperationStateEnum operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
110 final AsLcmOpOcc cnfInstantiateStautusResponse = execution.getVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME);
111 if (operationStatusOption == null) {
112 final String message = "Unable to instantiate CNF jobId: "
113 + (cnfInstantiateStautusResponse != null ? cnfInstantiateStautusResponse.getId() : "null")
114 + "Unable to retrieve OperationStatus";
115 LOGGER.error(message);
116 exceptionUtil.buildAndThrowWorkflowException(execution, 1206, message, ONAPComponents.SO);
118 if (operationStatusOption != OperationStateEnum.COMPLETED) {
119 final String message = "Unable to instantiate jobId: "
120 + (cnfInstantiateStautusResponse != null ? cnfInstantiateStautusResponse.getId() : "null")
121 + " OperationStatus: " + operationStatusOption;
122 LOGGER.error(message);
123 exceptionUtil.buildAndThrowWorkflowException(execution, 1207, message, ONAPComponents.SO);
125 LOGGER.debug("Successfully completed CNF instatiation of job status {}", cnfInstantiateStautusResponse);
130 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
131 * @return boolean to indicate whether job has competed or not
133 public boolean hasOperationFinished(final BuildingBlockExecution execution) {
134 LOGGER.debug("Executing hasOperationFinished ...");
136 final OperationStateEnum operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
137 if (operationStatusOption != null) {
138 return OPERATION_FINISHED_STATES.contains(operationStatusOption);
140 LOGGER.debug("OperationStatus is not present yet... ");
141 LOGGER.debug("Finished executing hasOperationFinished ...");