9e0c26ba20f0091e873de004d38d3ec62caa4302
[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     LOGGER.debug("Executing getCurrentOperationStatus  ...");
59     final CreateVnfResponse vnfInstantiateResponse = execution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME);
60     execution.setVariable(OPERATION_STATUS_PARAM_NAME, getOperationStatus(execution, vnfInstantiateResponse.getJobId()));
61     LOGGER.debug("Finished executing getCurrentOperationStatus ...");
62   }
63
64   /**
65    * Log and throw exception on timeout for job status
66    * 
67    * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
68    */
69   public void timeOutLogFailue(final BuildingBlockExecution execution) {
70     final String message = "Instantiation operation time out";
71     LOGGER.error(message);
72     exceptionUtil.buildAndThrowWorkflowException(execution, 1205, message);
73   }
74
75   /**
76    * Check the final status of instantiation throw exception if not completed successfully
77    * 
78    * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
79    */
80   public void checkIfOperationWasSuccessful(final BuildingBlockExecution execution) {
81     LOGGER.debug("Executing checkIfOperationWasSuccessful  ...");
82     final Optional<OperationStateEnum> operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
83     final CreateVnfResponse vnfInstantiateResponse = execution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME);
84     if (operationStatusOption == null || !operationStatusOption.isPresent()) {
85       final String message = "Unable to instantiate jobId: "
86           + (vnfInstantiateResponse != null ? vnfInstantiateResponse.getJobId() : "null")
87           + "Unable to retrieve OperationStatus";
88       LOGGER.error(message);
89       exceptionUtil.buildAndThrowWorkflowException(execution, 1206, message);
90     } else if (operationStatusOption != null && operationStatusOption.isPresent()) {
91       final OperationStateEnum operationStatus = operationStatusOption.get();
92       if (operationStatus != OperationStateEnum.COMPLETED) {
93         final String message = "Unable to instantiate jobId: "
94             + (vnfInstantiateResponse != null ? vnfInstantiateResponse.getJobId() : "null") + " OperationStatus: "
95             + operationStatus;
96         LOGGER.error(message);
97         exceptionUtil.buildAndThrowWorkflowException(execution, 1207, message);
98       }
99       LOGGER.debug("Successfully completed instatiation of job {}", vnfInstantiateResponse);
100     }
101   }
102 }