a6180b0e52dac3fdd4c3b9336f22f5981b6a490a
[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 / service / WorkflowQueryService.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.service;
21
22 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.CREATE_NS_RESPONSE_PARAM_NAME;
23 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME;
24 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.Constants.TENANT_ID;
25 import static org.slf4j.LoggerFactory.getLogger;
26 import java.util.Optional;
27 import org.camunda.bpm.engine.HistoryService;
28 import org.camunda.bpm.engine.ProcessEngineException;
29 import org.camunda.bpm.engine.history.HistoricVariableInstance;
30 import org.onap.so.etsi.nfvo.ns.lcm.model.InlineResponse400;
31 import org.onap.so.etsi.nfvo.ns.lcm.model.NsInstancesNsInstance;
32 import org.slf4j.Logger;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.stereotype.Service;
35 import com.google.common.base.Strings;
36
37 /**
38  * @author Waqas Ikram (waqas.ikram@est.tech)
39  *
40  */
41 @Service
42 public class WorkflowQueryService {
43
44     private static final Logger logger = getLogger(WorkflowQueryService.class);
45
46     private final HistoryService camundaHistoryService;
47
48     @Autowired
49     public WorkflowQueryService(final HistoryService camundaHistoryService) {
50         this.camundaHistoryService = camundaHistoryService;
51     }
52
53     public Optional<NsInstancesNsInstance> getCreateNsResponse(final String processInstanceId) {
54         try {
55
56             if (Strings.isNullOrEmpty(processInstanceId)) {
57                 logger.error("Invalid processInstanceId: {}", processInstanceId);
58                 return Optional.empty();
59             }
60
61             final HistoricVariableInstance historicVariableInstance =
62                     getVariable(processInstanceId, CREATE_NS_RESPONSE_PARAM_NAME);
63
64             if (historicVariableInstance != null) {
65                 logger.info("Found HistoricVariableInstance : {}", historicVariableInstance);
66                 final Object variableValue = historicVariableInstance.getValue();
67                 if (variableValue instanceof NsInstancesNsInstance) {
68                     return Optional.ofNullable((NsInstancesNsInstance) variableValue);
69                 }
70                 logger.error("Unknown CreateNsResponse object type {} received value: {}",
71                         historicVariableInstance.getValue() != null ? variableValue.getClass() : null, variableValue);
72             }
73         } catch (final ProcessEngineException processEngineException) {
74             logger.error("Unable to find {} variable using processInstanceId: {}", CREATE_NS_RESPONSE_PARAM_NAME,
75                     processInstanceId, processEngineException);
76         }
77         logger.error("Unable to find {} variable using processInstanceId: {}", CREATE_NS_RESPONSE_PARAM_NAME,
78                 processInstanceId);
79         return Optional.empty();
80
81     }
82
83     public Optional<InlineResponse400> getProblemDetails(final String processInstanceId) {
84         try {
85             final HistoricVariableInstance historicVariableInstance =
86                     getVariable(processInstanceId, NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME);
87
88             logger.info("Found HistoricVariableInstance : {}", historicVariableInstance);
89             final Object variableValue = historicVariableInstance.getValue();
90             if (variableValue instanceof InlineResponse400) {
91                 return Optional.ofNullable((InlineResponse400) variableValue);
92             }
93             logger.error("Unknown ProblemDetails object type {} received value: {}",
94                     historicVariableInstance.getValue() != null ? variableValue.getClass() : null, variableValue);
95         } catch (final ProcessEngineException processEngineException) {
96             logger.error("Unable to find {} variable using processInstanceId: {}",
97                     NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, processInstanceId, processEngineException);
98         }
99         return Optional.empty();
100     }
101
102
103     private HistoricVariableInstance getVariable(final String processInstanceId, final String name) {
104         return camundaHistoryService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId)
105                 .variableName(name).tenantIdIn(TENANT_ID).singleResult();
106     }
107
108 }