40a26cfaf96c3f72e779bdb19da00fd28253f31c
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 Ericsson. All rights reserved.
4  * ================================================================================
5  * Copyright (C) 2019 Samsung
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks;
21
22 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.CREATE_CNF_STATUS_RESPONSE_PARAM_NAME;
23 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.OPERATION_STATUS_PARAM_NAME;
24
25 import java.net.URI;
26 import java.util.NoSuchElementException;
27 import java.util.Optional;
28 import java.util.Set;
29 import org.onap.so.bpmn.common.BuildingBlockExecution;
30 import org.onap.so.client.exception.ExceptionBuilder;
31 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
32 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc.OperationStateEnum;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37
38
39 /**
40  * @author sagar.shetty@est.tech
41  *
42  */
43 @Component
44 public class MonitorCnfmCreateJobTask {
45
46     private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
47     private static final String MONITOR_JOB_NAME = "MonitorJobName";
48     public static final Set<OperationStateEnum> OPERATION_FINISHED_STATES =
49             Set.of(OperationStateEnum.COMPLETED, OperationStateEnum.FAILED, OperationStateEnum.ROLLED_BACK);
50     private static final Logger LOGGER = LoggerFactory.getLogger(MonitorCnfmCreateJobTask.class);
51     protected final ExceptionBuilder exceptionUtil;
52     private final CnfmHttpServiceProvider cnfmHttpServiceProvider;
53
54     @Autowired
55     public MonitorCnfmCreateJobTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
56             final ExceptionBuilder exceptionUtil) {
57         this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
58         this.exceptionUtil = exceptionUtil;
59     }
60
61     /**
62      * Get the current operation status of instantiation job
63      * 
64      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
65      */
66     public void getCurrentOperationStatus(final BuildingBlockExecution execution) {
67         try {
68             LOGGER.debug("Executing getCurrentOperationStatus  ...");
69             final Optional<URI> operationStatusURL = Optional.of(execution.getVariable(CNFM_REQUEST_STATUS_CHECK_URL));
70             LOGGER.debug("Executing getCurrentOperationStatus for CNF... :{}", operationStatusURL);
71             final Optional<AsLcmOpOcc> instantiateOperationJobStatus =
72                     cnfmHttpServiceProvider.getInstantiateOperationJobStatus(String.valueOf(operationStatusURL
73                             .orElseThrow(() -> new NoSuchElementException("Operational Status check url Not found"))));
74             if (instantiateOperationJobStatus.isPresent()) {
75                 final AsLcmOpOcc asLcmOpOccResponse = instantiateOperationJobStatus.get();
76                 if (asLcmOpOccResponse.getOperationState() != null) {
77                     final OperationStateEnum operationStatus = asLcmOpOccResponse.getOperationState();
78                     LOGGER.debug("Operation {} with {} and operation retrieval status : {}", asLcmOpOccResponse.getId(),
79                             operationStatus, asLcmOpOccResponse.getOperationState());
80                     execution.setVariable(OPERATION_STATUS_PARAM_NAME, asLcmOpOccResponse.getOperationState());
81                 }
82                 LOGGER.debug("Operation {} without operationStatus and operation retrieval status :{}",
83                         asLcmOpOccResponse.getId(), asLcmOpOccResponse.getOperationState());
84             }
85             execution.setVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME,
86                     instantiateOperationJobStatus.isPresent() ? instantiateOperationJobStatus.get() : " ");
87             LOGGER.debug("Finished executing getCurrentOperationStatus for CNF...");
88         } catch (final Exception exception) {
89             LOGGER.error("Unable to invoke get current Operation status");
90             exceptionUtil.buildAndThrowWorkflowException(execution, 1209, exception);
91         }
92     }
93
94     /**
95      * Log and throw exception on timeout for job status
96      * 
97      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
98      */
99     public void timeOutLogFailure(final BuildingBlockExecution execution) {
100         String message = "CNF" + execution.getVariable(MONITOR_JOB_NAME) + "operation time out";
101         LOGGER.error(message);
102         exceptionUtil.buildAndThrowWorkflowException(execution, 1205, new Exception(message));
103     }
104
105     /**
106      * Check the final status of instantiation throw exception if not completed successfully
107      * 
108      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
109      */
110     public void checkIfOperationWasSuccessful(final BuildingBlockExecution execution) {
111         LOGGER.debug("Executing CNF checkIfOperationWasSuccessful  ...");
112         final OperationStateEnum operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
113         final AsLcmOpOcc cnfInstantiateStautusResponse = execution.getVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME);
114         if ((operationStatusOption == OperationStateEnum.FAILED)
115                 || (operationStatusOption == OperationStateEnum.FAILED_TEMP)) {
116             final String message = "Unable to" + execution.getVariable(MONITOR_JOB_NAME) + "CNF jobId: "
117                     + (cnfInstantiateStautusResponse != null ? cnfInstantiateStautusResponse.getId() : "null");
118             LOGGER.error(message);
119             exceptionUtil.buildAndThrowWorkflowException(execution, 1206, new Exception());
120         } else if ((operationStatusOption == OperationStateEnum.COMPLETED)) {
121             String monitorJobName = execution.getVariable(MONITOR_JOB_NAME);
122             LOGGER.debug("Successfully completed CNF {} job", monitorJobName);
123         }
124     }
125
126     /**
127      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
128      * @return boolean to indicate whether job has competed or not
129      */
130     public boolean hasOperationFinished(final BuildingBlockExecution execution) {
131         LOGGER.debug("Executing hasOperationFinished  ...");
132
133         final OperationStateEnum operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
134         if (operationStatusOption != null) {
135             return OPERATION_FINISHED_STATES.contains(operationStatusOption);
136         }
137         LOGGER.debug("OperationStatus is not present yet... ");
138         LOGGER.debug("Finished executing hasOperationFinished ...");
139         return false;
140     }
141 }