Adding logic to communicate with SOL003 Adpater
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-bpmn-flows / src / main / java / org / onap / so / etsi / nfvo / ns / lcm / bpmn / flows / tasks / MonitorSol003AdapterJobTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
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.etsi.nfvo.ns.lcm.bpmn.flows.tasks;
21
22 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.OPERATION_STATUS_PARAM_NAME;
23 import java.util.Optional;
24 import org.camunda.bpm.engine.delegate.DelegateExecution;
25 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.OperationStateEnum;
26 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.OperationStatusRetrievalStatusEnum;
27 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.QueryJobResponse;
28 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.vnfm.Sol003AdapterServiceProvider;
29 import org.onap.so.etsi.nfvo.ns.lcm.database.service.DatabaseServiceProvider;
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.collect.ImmutableSet;
35
36 /**
37  * @author Waqas Ikram (waqas.ikram@est.tech)
38  *
39  */
40 @Component
41 public class MonitorSol003AdapterJobTask extends AbstractNetworkServiceTask {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(MonitorSol003AdapterJobTask.class);
44     public static final ImmutableSet<OperationStateEnum> OPERATION_FINISHED_STATES =
45             ImmutableSet.of(OperationStateEnum.COMPLETED, OperationStateEnum.FAILED, OperationStateEnum.ROLLED_BACK);
46     public static final ImmutableSet<OperationStatusRetrievalStatusEnum> OPERATION_RETRIEVAL_STATES = ImmutableSet
47             .of(OperationStatusRetrievalStatusEnum.STATUS_FOUND, OperationStatusRetrievalStatusEnum.WAITING_FOR_STATUS);
48     protected final Sol003AdapterServiceProvider sol003AdapterServiceProvider;
49
50     @Autowired
51     public MonitorSol003AdapterJobTask(final Sol003AdapterServiceProvider sol003AdapterServiceProvider,
52             final DatabaseServiceProvider databaseServiceProvider) {
53         super(databaseServiceProvider);
54         this.sol003AdapterServiceProvider = sol003AdapterServiceProvider;
55     }
56
57     public boolean hasOperationFinished(final DelegateExecution execution) {
58         LOGGER.debug("Executing hasOperationFinished  ...");
59
60         final OperationStateEnum operationStatus =
61                 (OperationStateEnum) execution.getVariable(OPERATION_STATUS_PARAM_NAME);
62         if (operationStatus != null) {
63             return OPERATION_FINISHED_STATES.contains(operationStatus);
64         }
65         LOGGER.debug("OperationStatus is not present yet... ");
66         LOGGER.debug("Finished executing hasOperationFinished ...");
67         return false;
68     }
69
70     protected OperationStateEnum getOperationStatus(final DelegateExecution execution, final String jobId) {
71
72         final Optional<QueryJobResponse> instantiateOperationJobStatus =
73                 sol003AdapterServiceProvider.getInstantiateOperationJobStatus(jobId);
74
75         if (instantiateOperationJobStatus.isPresent()) {
76             final QueryJobResponse queryJobResponse = instantiateOperationJobStatus.get();
77
78             if (!OPERATION_RETRIEVAL_STATES.contains(queryJobResponse.getOperationStatusRetrievalStatus())) {
79                 final String message = "Received invalid operation retrieval state: "
80                         + queryJobResponse.getOperationStatusRetrievalStatus();
81                 LOGGER.error(message);
82                 abortOperation(execution, message);
83             }
84             if (queryJobResponse.getOperationState() != null) {
85                 final OperationStateEnum operationStatus = queryJobResponse.getOperationState();
86                 LOGGER.debug("Operation {} with {} and operation retrieval status : {}", queryJobResponse.getId(),
87                         operationStatus, queryJobResponse.getOperationStatusRetrievalStatus());
88                 return queryJobResponse.getOperationState();
89             }
90
91             LOGGER.debug("Operation {} without operationStatus and operation retrieval status :{}",
92                     queryJobResponse.getId(), queryJobResponse.getOperationStatusRetrievalStatus());
93         }
94         return null;
95     }
96 }