06b44e451299d054620c0c7b0763a6c73abc8f3f
[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.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;
34
35
36 /**
37  * @author sagar.shetty@est.tech
38  */
39 @Component
40 public class MonitorCnfmCreateJobTask {
41
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;
48
49     @Autowired
50     public MonitorCnfmCreateJobTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
51             final ExceptionBuilder exceptionUtil) {
52         this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
53         this.exceptionUtil = exceptionUtil;
54     }
55
56     /**
57      * Get the current operation status of instantiation job
58      *
59      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
60      */
61     public void getCurrentOperationStatus(final BuildingBlockExecution execution) {
62         try {
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());
75                 }
76
77                 LOGGER.debug("Operation {} without operationStatus and operation retrieval status :{}",
78                         asLcmOpOccResponse.getId(), asLcmOpOccResponse.getOperationState());
79             }
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);
86
87         }
88     }
89
90     /**
91      * Log and throw exception on timeout for job status
92      *
93      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
94      */
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);
99     }
100
101     /**
102      * Check the final status of instantiation throw exception if not completed successfully
103      *
104      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
105      */
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);
116         } else {
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);
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 }