e5873ef66e5544fb49ebc4964aecb03b5cfd6b72
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / MonitorVnfmJobTask.java
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.OPERATION_FINISHED_STATES;
23 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.OPERATION_RETRIEVAL_STATES;
24 import static org.onap.so.bpmn.infrastructure.adapter.vnfm.tasks.Constants.OPERATION_STATUS_PARAM_NAME;
25 import org.onap.etsi.sol003.adapter.lcm.v1.model.OperationStateEnum;
26 import org.onap.etsi.sol003.adapter.lcm.v1.model.QueryJobResponse;
27 import org.onap.so.bpmn.common.BuildingBlockExecution;
28 import org.onap.so.client.exception.ExceptionBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
33 import com.google.common.base.Optional;
34
35
36 /**
37  * 
38  * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
39  *
40  */
41 @Component
42 public class MonitorVnfmJobTask {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(MonitorVnfmJobTask.class);
45     protected final ExceptionBuilder exceptionUtil;
46     protected final VnfmAdapterServiceProvider vnfmAdapterServiceProvider;
47
48     @Autowired
49     public MonitorVnfmJobTask(final VnfmAdapterServiceProvider vnfmAdapterServiceProvider,
50             final ExceptionBuilder exceptionUtil) {
51         this.vnfmAdapterServiceProvider = vnfmAdapterServiceProvider;
52         this.exceptionUtil = exceptionUtil;
53     }
54
55     /**
56      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
57      * @return boolean to indicate whether job has competed or not
58      */
59     public boolean hasOperationFinished(final BuildingBlockExecution execution) {
60         LOGGER.debug("Executing hasOperationFinished  ...");
61
62         final Optional<OperationStateEnum> operationStatusOption = execution.getVariable(OPERATION_STATUS_PARAM_NAME);
63         if (operationStatusOption != null && operationStatusOption.isPresent()) {
64             return OPERATION_FINISHED_STATES.contains(operationStatusOption.get());
65         }
66         LOGGER.debug("OperationStatus is not present yet... ");
67         LOGGER.debug("Finished executing hasOperationFinished ...");
68         return false;
69     }
70
71     /**
72      * This method calls the Vnfm adapter and gets the Operation status of the job
73      * 
74      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
75      * @param jobId unique job id
76      * @return Operation State
77      */
78     protected Optional<OperationStateEnum> getOperationStatus(final BuildingBlockExecution execution,
79             final String jobId) {
80
81         final Optional<QueryJobResponse> instantiateOperationJobStatus =
82                 vnfmAdapterServiceProvider.getInstantiateOperationJobStatus(jobId);
83
84         if (instantiateOperationJobStatus.isPresent()) {
85             final QueryJobResponse queryJobResponse = instantiateOperationJobStatus.get();
86
87             if (!OPERATION_RETRIEVAL_STATES.contains(queryJobResponse.getOperationStatusRetrievalStatus())) {
88                 final String message = "Recevied invalid operation reterivel state: "
89                         + queryJobResponse.getOperationStatusRetrievalStatus();
90                 LOGGER.error(message);
91                 exceptionUtil.buildAndThrowWorkflowException(execution, 1203, message);
92             }
93             if (queryJobResponse.getOperationState() != null) {
94                 final OperationStateEnum operationStatus = queryJobResponse.getOperationState();
95                 LOGGER.debug("Operation {} with {} and operation retrieval status : {}", queryJobResponse.getId(),
96                         operationStatus, queryJobResponse.getOperationStatusRetrievalStatus());
97                 return Optional.of(queryJobResponse.getOperationState());
98             }
99
100             LOGGER.debug("Operation {} without operationStatus and operation retrieval status :{}",
101                     queryJobResponse.getId(), queryJobResponse.getOperationStatusRetrievalStatus());
102         }
103         return Optional.absent();
104     }
105 }