323141365d352e40ba5f05b121a81ebca0cf3003
[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.Optional;
27
28 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.CreateVnfResponse;
29 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.QueryJobResponse;
30 import org.onap.so.bpmn.common.BuildingBlockExecution;
31 import org.onap.so.client.exception.ExceptionBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36
37 import com.google.common.collect.ImmutableSet;
38
39 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
40 import org.onap.so.cnfm.lcm.model.AsLcmOpOcc.OperationStateEnum;
41
42
43 /**
44  * @author sagar.shetty@est.tech
45  *
46  */
47 @Component
48 public class MonitorCnfmCreateJobTask {
49
50     private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
51     public static final ImmutableSet<OperationStateEnum> OPERATION_FINISHED_STATES =
52             ImmutableSet.of(OperationStateEnum.COMPLETED, OperationStateEnum.FAILED, OperationStateEnum.ROLLED_BACK);
53     private static final Logger LOGGER = LoggerFactory.getLogger(MonitorCnfmCreateJobTask.class);
54     protected final ExceptionBuilder exceptionUtil;
55     private final CnfmHttpServiceProvider cnfmHttpServiceProvider;
56
57     @Autowired
58     public MonitorCnfmCreateJobTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
59             final ExceptionBuilder exceptionUtil) {
60         this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
61         this.exceptionUtil = exceptionUtil;
62     }
63
64     /**
65      * Get the current operation status of instantiation job
66      * 
67      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
68      */
69     public void getCurrentOperationStatus(final BuildingBlockExecution execution) {
70         try {
71             LOGGER.debug("Executing getCurrentOperationStatus  ...");
72             final URI operation_status_url = execution.getVariable(CNFM_REQUEST_STATUS_CHECK_URL);
73             LOGGER.debug("Executing getCurrentOperationStatus for CNF... :{}", operation_status_url.toString());
74             final Optional<AsLcmOpOcc> instantiateOperationJobStatus =
75                     cnfmHttpServiceProvider.getInstantiateOperationJobStatus(operation_status_url.toString());
76             if (instantiateOperationJobStatus.isPresent()) {
77                 final AsLcmOpOcc asLcmOpOccResponse = instantiateOperationJobStatus.get();
78                 if (asLcmOpOccResponse.getOperationState() != null) {
79                     final OperationStateEnum operationStatus = asLcmOpOccResponse.getOperationState();
80                     LOGGER.debug("Operation {} with {} and operation retrieval status : {}", asLcmOpOccResponse.getId(),
81                             operationStatus, asLcmOpOccResponse.getOperationState());
82                     execution.setVariable(OPERATION_STATUS_PARAM_NAME, asLcmOpOccResponse.getOperationState());
83                 }
84
85                 LOGGER.debug("Operation {} without operationStatus and operation retrieval status :{}",
86                         asLcmOpOccResponse.getId(), asLcmOpOccResponse.getOperationState());
87             }
88             execution.setVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME, instantiateOperationJobStatus.get());
89             LOGGER.debug("Finished executing getCurrentOperationStatus for CNF...");
90         } catch (final Exception exception) {
91             final String message = "Unable to invoke get current Operation status";
92             LOGGER.error(message);
93             exceptionUtil.buildAndThrowWorkflowException(execution, 1209, message);
94
95         }
96     }
97
98     /**
99      * Log and throw exception on timeout for job status
100      * 
101      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
102      */
103     public void timeOutLogFailue(final BuildingBlockExecution execution) {
104         final String message = "CNF Instantiation operation time out";
105         LOGGER.error(message);
106         exceptionUtil.buildAndThrowWorkflowException(execution, 1205, message);
107     }
108
109     /**
110      * Check the final status of instantiation throw exception if not completed successfully
111      * 
112      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
113      */
114     public void checkIfOperationWasSuccessful(final BuildingBlockExecution execution) {
115         LOGGER.debug("Executing CNF checkIfOperationWasSuccessful  ...");
116         final OperationStateEnum operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
117         final AsLcmOpOcc cnfInstantiateStautusResponse = execution.getVariable(CREATE_CNF_STATUS_RESPONSE_PARAM_NAME);
118         if (operationStatusOption == null) {
119             final String message = "Unable to instantiate CNF jobId: "
120                     + (cnfInstantiateStautusResponse != null ? cnfInstantiateStautusResponse.getId() : "null")
121                     + "Unable to retrieve OperationStatus";
122             LOGGER.error(message);
123             exceptionUtil.buildAndThrowWorkflowException(execution, 1206, message);
124         } else {
125             final OperationStateEnum operationStatus = operationStatusOption;
126             if (operationStatus != OperationStateEnum.COMPLETED) {
127                 final String message = "Unable to instantiate jobId: "
128                         + (cnfInstantiateStautusResponse != null ? cnfInstantiateStautusResponse.getId() : "null")
129                         + " OperationStatus: " + operationStatus;
130                 LOGGER.error(message);
131                 exceptionUtil.buildAndThrowWorkflowException(execution, 1207, message);
132             }
133             LOGGER.debug("Successfully completed CNF instatiation of job status {}", cnfInstantiateStautusResponse);
134         }
135     }
136
137     /**
138      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
139      * @return boolean to indicate whether job has competed or not
140      */
141     public boolean hasOperationFinished(final BuildingBlockExecution execution) {
142         LOGGER.debug("Executing hasOperationFinished  ...");
143
144         final OperationStateEnum operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
145         if (operationStatusOption != null) {
146             return OPERATION_FINISHED_STATES.contains(operationStatusOption);
147         }
148         LOGGER.debug("OperationStatus is not present yet... ");
149         LOGGER.debug("Finished executing hasOperationFinished ...");
150         return false;
151     }
152 }