f89931063c4f538220a6c894d23d78a42eb6cdcf
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Ericsson. All rights reserved.
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  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks;
21
22 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.CREATE_VNF_RESPONSE_PARAM_NAME;
23 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.OPERATION_FINISHED_STATES;
24 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.OPERATION_RETRIEVAL_STATES;
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.onap.vnfmadapter.v1.model.QueryJobResponse;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Component;
35 import com.google.common.base.Optional;
36
37
38 /**
39  * @author waqas.ikram@est.tech
40  *
41  */
42 @Component
43 public class MonitorVnfmCreateJobTask {
44
45   private static final Logger LOGGER = LoggerFactory.getLogger(MonitorVnfmCreateJobTask.class);
46   private final ExceptionBuilder exceptionUtil;
47   private final VnfmAdapterServiceProvider vnfmAdapterServiceProvider;
48
49   @Autowired
50   public MonitorVnfmCreateJobTask(final VnfmAdapterServiceProvider vnfmAdapterServiceProvider,
51       final ExceptionBuilder exceptionUtil) {
52     this.vnfmAdapterServiceProvider = vnfmAdapterServiceProvider;
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     LOGGER.debug("Executing getCurrentOperationStatus  ...");
63     final CreateVnfResponse vnfInstantiateResponse = execution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME);
64     execution.setVariable(OPERATION_STATUS_PARAM_NAME, getOperationStatus(execution, vnfInstantiateResponse));
65     LOGGER.debug("Finished executing getCurrentOperationStatus ...");
66   }
67
68   /**
69    * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
70    * @return boolean to indicate whether job has competed or not
71    */
72   public boolean hasOperationFinished(final BuildingBlockExecution execution) {
73     LOGGER.debug("Executing hasOperationFinished  ...");
74
75     final Optional<OperationStateEnum> operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
76     if (operationStatusOption != null && operationStatusOption.isPresent()) {
77       return OPERATION_FINISHED_STATES.contains(operationStatusOption.get());
78     }
79     LOGGER.debug("OperationStatus is not present yet... ");
80     LOGGER.debug("Finished executing hasOperationFinished ...");
81     return false;
82
83   }
84
85   /**
86    * Log and throw exception on timeout for job status
87    * 
88    * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
89    */
90   public void timeOutLogFailue(final BuildingBlockExecution execution) {
91     final String message = "Instantiation operation time out";
92     LOGGER.error(message);
93     exceptionUtil.buildAndThrowWorkflowException(execution, 1205, message);
94   }
95
96   /**
97    * Check the final status of instantiation throw exception if not completed successfully
98    * 
99    * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
100    */
101   public void checkIfOperationWasSuccessful(final BuildingBlockExecution execution) {
102     LOGGER.debug("Executing checkIfOperationWasSuccessful  ...");
103     final Optional<OperationStateEnum> operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
104     final CreateVnfResponse vnfInstantiateResponse = execution.getVariable(CREATE_VNF_RESPONSE_PARAM_NAME);
105     if (operationStatusOption == null || !operationStatusOption.isPresent()) {
106       final String message = "Unable to instantiate jobId: "
107           + (vnfInstantiateResponse != null ? vnfInstantiateResponse.getJobId() : "null")
108           + "Unable to retrieve OperationStatus";
109       LOGGER.error(message);
110       exceptionUtil.buildAndThrowWorkflowException(execution, 1206, message);
111     }
112     if (operationStatusOption.isPresent()) {
113       final OperationStateEnum operationStatus = operationStatusOption.get();
114       if (operationStatus != OperationStateEnum.COMPLETED) {
115         final String message = "Unable to instantiate jobId: "
116             + (vnfInstantiateResponse != null ? vnfInstantiateResponse.getJobId() : "null") + " OperationStatus: "
117             + operationStatus;
118         LOGGER.error(message);
119         exceptionUtil.buildAndThrowWorkflowException(execution, 1207, message);
120       }
121       LOGGER.debug("Successfully completed instatiation of job {}", vnfInstantiateResponse);
122     }
123   }
124
125   private Optional<OperationStateEnum> getOperationStatus(final BuildingBlockExecution execution,
126       final CreateVnfResponse vnfInstantiateResponse) {
127
128     final Optional<QueryJobResponse> instantiateOperationJobStatus =
129         vnfmAdapterServiceProvider.getInstantiateOperationJobStatus(vnfInstantiateResponse.getJobId());
130
131     if (instantiateOperationJobStatus.isPresent()) {
132       final QueryJobResponse queryJobResponse = instantiateOperationJobStatus.get();
133
134       if (!OPERATION_RETRIEVAL_STATES.contains(queryJobResponse.getOperationStatusRetrievalStatus())) {
135         final String message =
136             "Recevied invalid operation reterivel state: " + queryJobResponse.getOperationStatusRetrievalStatus();
137         LOGGER.error(message);
138         exceptionUtil.buildAndThrowWorkflowException(execution, 1203, message);
139       }
140
141       if (queryJobResponse.getOperationState() != null) {
142         final OperationStateEnum operationStatus = queryJobResponse.getOperationState();
143         LOGGER.debug("Operation {} with {} and operation retrieval status : {}", queryJobResponse.getId(),
144             operationStatus, queryJobResponse.getOperationStatusRetrievalStatus());
145         return Optional.of(queryJobResponse.getOperationState());
146       }
147
148       LOGGER.debug("Operation {} without operationStatus and operation retrieval status :{}", queryJobResponse.getId(),
149           queryJobResponse.getOperationStatusRetrievalStatus());
150
151     }
152     return Optional.absent();
153   }
154 }