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.so.bpmn.common.BuildingBlockExecution;
26 import org.onap.so.client.exception.ExceptionBuilder;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Component;
31 import com.google.common.collect.ImmutableSet;
32 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
33 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc.OperationStateEnum;
37 * @author sagar.shetty@est.tech
40 public class MonitorCnfmCreateJobTask {
42 public static final ImmutableSet<OperationStateEnum> OPERATION_FINISHED_STATES =
43 ImmutableSet.of(OperationStateEnum.COMPLETED, OperationStateEnum.FAILED, OperationStateEnum.ROLLED_BACK);
44 private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
45 private static final Logger LOGGER = LoggerFactory.getLogger(MonitorCnfmCreateJobTask.class);
46 protected final ExceptionBuilder exceptionUtil;
47 private final CnfmHttpServiceProvider cnfmHttpServiceProvider;
50 public MonitorCnfmCreateJobTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
51 final ExceptionBuilder exceptionUtil) {
52 this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
53 this.exceptionUtil = exceptionUtil;
57 * Get the current operation status of instantiation job
59 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
61 public void getCurrentOperationStatus(final BuildingBlockExecution execution) {
63 LOGGER.debug("Executing getCurrentOperationStatus ...");
64 final URI operation_status_url = execution.getVariable(CNFM_REQUEST_STATUS_CHECK_URL);
65 LOGGER.debug("Executing getCurrentOperationStatus for CNF... :{}", operation_status_url.toString());
66 final Optional<AsLcmOpOcc> instantiateOperationJobStatus =
67 cnfmHttpServiceProvider.getInstantiateOperationJobStatus(operation_status_url.toString());
68 if (instantiateOperationJobStatus.isPresent()) {
69 final AsLcmOpOcc asLcmOpOccResponse = instantiateOperationJobStatus.get();
70 if (asLcmOpOccResponse.getOperationState() != null) {
71 final OperationStateEnum operationStatus = asLcmOpOccResponse.getOperationState();
72 LOGGER.debug("Operation {} with {} and operation retrieval status : {}", asLcmOpOccResponse.getId(),
73 operationStatus, asLcmOpOccResponse.getOperationState());
74 execution.setVariable(OPERATION_STATUS_PARAM_NAME, asLcmOpOccResponse.getOperationState());
77 LOGGER.debug("Operation {} without operationStatus and operation retrieval status :{}",
78 asLcmOpOccResponse.getId(), asLcmOpOccResponse.getOperationState());
80 execution.setVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME, instantiateOperationJobStatus.get());
81 LOGGER.debug("Finished executing getCurrentOperationStatus for CNF...");
82 } catch (final Exception exception) {
83 final String message = "Unable to invoke get current Operation status";
84 LOGGER.error(message);
85 exceptionUtil.buildAndThrowWorkflowException(execution, 1209, message);
91 * Log and throw exception on timeout for job status
93 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
95 public void timeOutLogFailue(final BuildingBlockExecution execution) {
96 final String message = "CNF Instantiation operation time out";
97 LOGGER.error(message);
98 exceptionUtil.buildAndThrowWorkflowException(execution, 1205, message);
102 * Check the final status of instantiation throw exception if not completed successfully
104 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
106 public void checkIfOperationWasSuccessful(final BuildingBlockExecution execution) {
107 LOGGER.debug("Executing CNF checkIfOperationWasSuccessful ...");
108 final OperationStateEnum operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
109 final AsLcmOpOcc cnfInstantiateStautusResponse = execution.getVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME);
110 if (operationStatusOption == null) {
111 final String message = "Unable to instantiate CNF jobId: "
112 + (cnfInstantiateStautusResponse != null ? cnfInstantiateStautusResponse.getId() : "null")
113 + "Unable to retrieve OperationStatus";
114 LOGGER.error(message);
115 exceptionUtil.buildAndThrowWorkflowException(execution, 1206, message);
117 final OperationStateEnum operationStatus = operationStatusOption;
118 if (operationStatus != OperationStateEnum.COMPLETED) {
119 final String message = "Unable to instantiate jobId: "
120 + (cnfInstantiateStautusResponse != null ? cnfInstantiateStautusResponse.getId() : "null")
121 + " OperationStatus: " + operationStatus;
122 LOGGER.error(message);
123 exceptionUtil.buildAndThrowWorkflowException(execution, 1207, message);
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 ...");