[SO] SO changes to support Delete AS
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / cnfm / tasks / MonitorCnfmJobTask.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  */
18
19 package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks;
20
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;
23 import java.net.URI;
24 import java.util.Optional;
25 import java.util.Set;
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;
35
36 /**
37  * @author sagar.shetty@est.tech
38  * @author Raviteja Karumuri (raviteja.karumuri@est.tech)
39  */
40 @Component
41 public class MonitorCnfmJobTask {
42
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;
50
51     @Autowired
52     public MonitorCnfmJobTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
53             final ExceptionBuilder exceptionUtil) {
54         this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
55         this.exceptionUtil = exceptionUtil;
56     }
57
58     /**
59      * Get the current operation status of cnfm job
60      * 
61      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
62      */
63     public void getCurrentOperationStatus(final BuildingBlockExecution execution) {
64         try {
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"))));
71
72             final AsLcmOpOcc asLcmOpOccResponse = operationalJobStatus
73                     .orElseThrow(() -> new BpmnError("Unable to get operational Job status from the CNFM"));
74
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);
80             } else {
81                 LOGGER.debug("Operation {} without operationStatus", asLcmOpOccResponse.getId());
82                 exceptionUtil.buildAndThrowWorkflowException(execution, 1206,
83                         new Exception("Operation Status is empty"));
84             }
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);
90         }
91     }
92
93     /**
94      * Log and throw exception on timeout for job status
95      *
96      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
97      */
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));
102     }
103
104     /**
105      * Check the final status of cnfm job throw exception if not completed successfully
106      * 
107      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
108      */
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);
122         }
123     }
124
125     /**
126      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
127      * @return boolean to indicate whether job has competed or not
128      */
129     public boolean hasOperationFinished(final BuildingBlockExecution execution) {
130         LOGGER.debug("Executing hasOperationFinished  ...");
131
132         final OperationStateEnum operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
133         if (operationStatusOption != null) {
134             return OPERATION_FINISHED_STATES.contains(operationStatusOption);
135         }
136         LOGGER.debug("OperationStatus is not present yet... ");
137         LOGGER.debug("Finished executing hasOperationFinished ...");
138         return false;
139     }
140 }