b6dbd77f06721ba49b5e7c5f4578819dfbb02eb8
[so/adapters/so-cnf-adapter.git] /
1 /*-
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
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.cnfm.lcm.bpmn.flows.service;
21
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;
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<AsInstance> 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_AS_RESPONSE_PARAM_NAME);
63
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);
69                 }
70                 logger.error("Unknown CreateAsResponse 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_AS_RESPONSE_PARAM_NAME,
75                     processInstanceId, processEngineException);
76         }
77         logger.error("Unable to find {} variable using processInstanceId: {}", CREATE_AS_RESPONSE_PARAM_NAME,
78                 processInstanceId);
79         return Optional.empty();
80
81     }
82
83     public Optional<ErrorDetails> getErrorDetails(final String processInstanceId) {
84         try {
85             final HistoricVariableInstance historicVariableInstance =
86                     getVariable(processInstanceId, AS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME);
87
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);
93                 }
94                 logger.error("Unknown ErrorContents object type {} received value: {}",
95                         historicVariableInstance.getValue() != null ? variableValue.getClass() : null, variableValue);
96             }
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);
101         }
102         return Optional.empty();
103     }
104
105
106     private HistoricVariableInstance getVariable(final String processInstanceId, final String name) {
107         return camundaHistoryService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId)
108                 .variableName(name).tenantIdIn(TENANT_ID).singleResult();
109     }
110
111 }