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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks;
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;
39 * @author waqas.ikram@est.tech
43 public class MonitorVnfmCreateJobTask {
45 private static final Logger LOGGER = LoggerFactory.getLogger(MonitorVnfmCreateJobTask.class);
46 private final ExceptionBuilder exceptionUtil;
47 private final VnfmAdapterServiceProvider vnfmAdapterServiceProvider;
50 public MonitorVnfmCreateJobTask(final VnfmAdapterServiceProvider vnfmAdapterServiceProvider,
51 final ExceptionBuilder exceptionUtil) {
52 this.vnfmAdapterServiceProvider = vnfmAdapterServiceProvider;
53 this.exceptionUtil = exceptionUtil;
57 * Get the current operation status of instantiation job
59 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
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 ...");
69 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
70 * @return boolean to indicate whether job has competed or not
72 public boolean hasOperationFinished(final BuildingBlockExecution execution) {
73 LOGGER.debug("Executing hasOperationFinished ...");
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());
79 LOGGER.debug("OperationStatus is not present yet... ");
80 LOGGER.debug("Finished executing hasOperationFinished ...");
86 * Log and throw exception on timeout for job status
88 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
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);
97 * Check the final status of instantiation throw exception if not completed successfully
99 * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
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);
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: "
118 LOGGER.error(message);
119 exceptionUtil.buildAndThrowWorkflowException(execution, 1207, message);
121 LOGGER.debug("Successfully completed instatiation of job {}", vnfInstantiateResponse);
125 private Optional<OperationStateEnum> getOperationStatus(final BuildingBlockExecution execution,
126 final CreateVnfResponse vnfInstantiateResponse) {
128 final Optional<QueryJobResponse> instantiateOperationJobStatus =
129 vnfmAdapterServiceProvider.getInstantiateOperationJobStatus(vnfInstantiateResponse.getJobId());
131 if (instantiateOperationJobStatus.isPresent()) {
132 final QueryJobResponse queryJobResponse = instantiateOperationJobStatus.get();
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);
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());
148 LOGGER.debug("Operation {} without operationStatus and operation retrieval status :{}", queryJobResponse.getId(),
149 queryJobResponse.getOperationStatusRetrievalStatus());
152 return Optional.absent();