e3992428cb0a636bf13d88d7d950301d7844e51c
[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.so.bpmn.common.BuildingBlockExecution;
26 import org.onap.so.client.exception.ExceptionBuilder;
27 import org.onap.vnfmadapter.v1.model.OperationStateEnum;
28 import org.onap.vnfmadapter.v1.model.QueryJobResponse;
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    * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
74    * @param jobId unique job id
75    * @return Operation State
76    */
77   protected Optional<OperationStateEnum> getOperationStatus(final BuildingBlockExecution execution,
78       final String jobId) {
79
80     final Optional<QueryJobResponse> instantiateOperationJobStatus =
81         vnfmAdapterServiceProvider.getInstantiateOperationJobStatus(jobId);
82
83     if (instantiateOperationJobStatus.isPresent()) {
84       final QueryJobResponse queryJobResponse = instantiateOperationJobStatus.get();
85
86       if (!OPERATION_RETRIEVAL_STATES.contains(queryJobResponse.getOperationStatusRetrievalStatus())) {
87         final String message =
88             "Recevied invalid operation reterivel state: " + queryJobResponse.getOperationStatusRetrievalStatus();
89         LOGGER.error(message);
90         exceptionUtil.buildAndThrowWorkflowException(execution, 1203, message);
91       }
92       if (queryJobResponse.getOperationState() != null) {
93         final OperationStateEnum operationStatus = queryJobResponse.getOperationState();
94         LOGGER.debug("Operation {} with {} and operation retrieval status : {}", queryJobResponse.getId(),
95             operationStatus, queryJobResponse.getOperationStatusRetrievalStatus());
96         return Optional.of(queryJobResponse.getOperationState());
97       }
98
99       LOGGER.debug("Operation {} without operationStatus and operation retrieval status :{}", queryJobResponse.getId(),
100           queryJobResponse.getOperationStatusRetrievalStatus());
101     }
102     return Optional.absent();
103   }
104 }