Fixing infinte loop of flow
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / MonitorVnfmCreateJobTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Ericsson. All rights reserved.
4  * ================================================================================
5  *  Modifications 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  * 
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks;
23
24 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.CREATE_VNF_RESPONSE_PARAM_NAME;
25 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.OPERATION_STATUS_PARAM_NAME;
26 import org.onap.so.bpmn.common.BuildingBlockExecution;
27 import org.onap.so.client.exception.ExceptionBuilder;
28 import org.onap.vnfmadapter.v1.model.CreateVnfResponse;
29 import org.onap.vnfmadapter.v1.model.OperationStateEnum;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Component;
34 import com.google.common.base.Optional;
35
36
37 /**
38  * @author waqas.ikram@est.tech
39  *
40  */
41 @Component
42 public class MonitorVnfmCreateJobTask extends MonitorVnfmJobTask {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(MonitorVnfmCreateJobTask.class);
45
46     @Autowired
47     public MonitorVnfmCreateJobTask(final VnfmAdapterServiceProvider vnfmAdapterServiceProvider,
48             final ExceptionBuilder exceptionUtil) {
49         super(vnfmAdapterServiceProvider, exceptionUtil);
50     }
51
52     /**
53      * Get the current operation status of instantiation job
54      * 
55      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
56      */
57     public void getCurrentOperationStatus(final BuildingBlockExecution execution) {
58         try {
59             LOGGER.debug("Executing getCurrentOperationStatus  ...");
60             final CreateVnfResponse vnfInstantiateResponse = execution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME);
61             execution.setVariable(OPERATION_STATUS_PARAM_NAME,
62                     getOperationStatus(execution, vnfInstantiateResponse.getJobId()));
63             LOGGER.debug("Finished executing getCurrentOperationStatus ...");
64         } catch (final Exception exception) {
65             final String message = "Unable to invoke get current Operation status";
66             LOGGER.error(message);
67             exceptionUtil.buildAndThrowWorkflowException(execution, 1209, message);
68
69         }
70     }
71
72     /**
73      * Log and throw exception on timeout for job status
74      * 
75      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
76      */
77     public void timeOutLogFailue(final BuildingBlockExecution execution) {
78         final String message = "Instantiation operation time out";
79         LOGGER.error(message);
80         exceptionUtil.buildAndThrowWorkflowException(execution, 1205, message);
81     }
82
83     /**
84      * Check the final status of instantiation throw exception if not completed successfully
85      * 
86      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
87      */
88     public void checkIfOperationWasSuccessful(final BuildingBlockExecution execution) {
89         LOGGER.debug("Executing checkIfOperationWasSuccessful  ...");
90         final Optional<OperationStateEnum> operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
91         final CreateVnfResponse vnfInstantiateResponse = execution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME);
92         if (operationStatusOption == null || !operationStatusOption.isPresent()) {
93             final String message = "Unable to instantiate jobId: "
94                     + (vnfInstantiateResponse != null ? vnfInstantiateResponse.getJobId() : "null")
95                     + "Unable to retrieve OperationStatus";
96             LOGGER.error(message);
97             exceptionUtil.buildAndThrowWorkflowException(execution, 1206, message);
98         } else if (operationStatusOption != null && operationStatusOption.isPresent()) {
99             final OperationStateEnum operationStatus = operationStatusOption.get();
100             if (operationStatus != OperationStateEnum.COMPLETED) {
101                 final String message = "Unable to instantiate jobId: "
102                         + (vnfInstantiateResponse != null ? vnfInstantiateResponse.getJobId() : "null")
103                         + " OperationStatus: " + operationStatus;
104                 LOGGER.error(message);
105                 exceptionUtil.buildAndThrowWorkflowException(execution, 1207, message);
106             }
107             LOGGER.debug("Successfully completed instatiation of job {}", vnfInstantiateResponse);
108         }
109     }
110 }