2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 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
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.cnfm.lcm.bpmn.flows.service;
22 import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.AS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME;
23 import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.CREATE_AS_RESPONSE_PARAM_NAME;
24 import static org.onap.so.cnfm.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.cnfm.lcm.model.AsInstance;
31 import org.onap.so.cnfm.lcm.model.ErrorDetails;
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;
38 * @author Waqas Ikram (waqas.ikram@est.tech)
42 public class WorkflowQueryService {
44 private static final Logger logger = getLogger(WorkflowQueryService.class);
46 private final HistoryService camundaHistoryService;
49 public WorkflowQueryService(final HistoryService camundaHistoryService) {
50 this.camundaHistoryService = camundaHistoryService;
53 public Optional<AsInstance> getCreateNsResponse(final String processInstanceId) {
56 if (Strings.isNullOrEmpty(processInstanceId)) {
57 logger.error("Invalid processInstanceId: {}", processInstanceId);
58 return Optional.empty();
61 final HistoricVariableInstance historicVariableInstance =
62 getVariable(processInstanceId, CREATE_AS_RESPONSE_PARAM_NAME);
64 if (historicVariableInstance != null) {
65 logger.info("Found HistoricVariableInstance : {}", historicVariableInstance);
66 final Object variableValue = historicVariableInstance.getValue();
67 if (variableValue instanceof AsInstance) {
68 return Optional.ofNullable((AsInstance) variableValue);
70 logger.error("Unknown CreateAsResponse object type {} received value: {}",
71 historicVariableInstance.getValue() != null ? variableValue.getClass() : null, variableValue);
73 } catch (final ProcessEngineException processEngineException) {
74 logger.error("Unable to find {} variable using processInstanceId: {}", CREATE_AS_RESPONSE_PARAM_NAME,
75 processInstanceId, processEngineException);
77 logger.error("Unable to find {} variable using processInstanceId: {}", CREATE_AS_RESPONSE_PARAM_NAME,
79 return Optional.empty();
83 public Optional<ErrorDetails> getErrorDetails(final String processInstanceId) {
85 final HistoricVariableInstance historicVariableInstance =
86 getVariable(processInstanceId, AS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME);
88 logger.info("Found HistoricVariableInstance : {}", historicVariableInstance);
89 if (historicVariableInstance != null) {
90 final Object variableValue = historicVariableInstance.getValue();
91 if (variableValue instanceof ErrorDetails) {
92 return Optional.ofNullable((ErrorDetails) variableValue);
94 logger.error("Unknown ErrorContents object type {} received value: {}",
95 historicVariableInstance.getValue() != null ? variableValue.getClass() : null, variableValue);
97 logger.error("Unable to retrieve HistoricVariableInstance value was null");
98 } catch (final ProcessEngineException processEngineException) {
99 logger.error("Unable to find {} variable using processInstanceId: {}",
100 AS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, processInstanceId, processEngineException);
102 return Optional.empty();
106 private HistoricVariableInstance getVariable(final String processInstanceId, final String name) {
107 return camundaHistoryService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId)
108 .variableName(name).tenantIdIn(TENANT_ID).singleResult();