eb1cd1c1b68e272777cb77d02ec969e32415deea
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / cnfm / tasks / MonitorCnfmCreateJobTask.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.CREATE_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 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;
35
36
37 /**
38  * @author sagar.shetty@est.tech
39  */
40 @Component
41 public class MonitorCnfmCreateJobTask {
42
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;
49
50     @Autowired
51     public MonitorCnfmCreateJobTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
52             final ExceptionBuilder exceptionUtil) {
53         this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
54         this.exceptionUtil = exceptionUtil;
55     }
56
57     /**
58      * Get the current operation status of instantiation job
59      *
60      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
61      */
62     public void getCurrentOperationStatus(final BuildingBlockExecution execution) {
63         try {
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());
76                 }
77
78                 LOGGER.debug("Operation {} without operationStatus and operation retrieval status :{}",
79                         asLcmOpOccResponse.getId(), asLcmOpOccResponse.getOperationState());
80             }
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);
87
88         }
89     }
90
91     /**
92      * Log and throw exception on timeout for job status
93      *
94      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
95      */
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);
100     }
101
102     /**
103      * Check the final status of instantiation throw exception if not completed successfully
104      *
105      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
106      */
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);
117         } else {
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);
124             }
125             LOGGER.debug("Successfully completed CNF instatiation of job status {}", cnfInstantiateStautusResponse);
126         }
127     }
128
129     /**
130      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
131      * @return boolean to indicate whether job has competed or not
132      */
133     public boolean hasOperationFinished(final BuildingBlockExecution execution) {
134         LOGGER.debug("Executing hasOperationFinished  ...");
135
136         final OperationStateEnum operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
137         if (operationStatusOption != null) {
138             return OPERATION_FINISHED_STATES.contains(operationStatusOption);
139         }
140         LOGGER.debug("OperationStatus is not present yet... ");
141         LOGGER.debug("Finished executing hasOperationFinished ...");
142         return false;
143     }
144 }