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
 
   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.etsi.nfvo.ns.lcm.bpmn.flows.service;
 
  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.CREATE_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;
 
  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<NsInstancesNsInstance> 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_NS_RESPONSE_PARAM_NAME);
 
  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);
 
  70                 logger.error("Unknown CreateNsResponse 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_NS_RESPONSE_PARAM_NAME,
 
  75                     processInstanceId, processEngineException);
 
  77         logger.error("Unable to find {} variable using processInstanceId: {}", CREATE_NS_RESPONSE_PARAM_NAME,
 
  79         return Optional.empty();
 
  83     public Optional<InlineResponse400> getProblemDetails(final String processInstanceId) {
 
  85             final HistoricVariableInstance historicVariableInstance =
 
  86                     getVariable(processInstanceId, CREATE_NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME);
 
  88             logger.info("Found HistoricVariableInstance : {}", historicVariableInstance);
 
  89             final Object variableValue = historicVariableInstance.getValue();
 
  90             if (variableValue instanceof InlineResponse400) {
 
  91                 return Optional.ofNullable((InlineResponse400) variableValue);
 
  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                     CREATE_NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, processInstanceId, processEngineException);
 
  99         return Optional.empty();
 
 103     private HistoricVariableInstance getVariable(final String processInstanceId, final String name) {
 
 104         return camundaHistoryService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId)
 
 105                 .variableName(name).tenantIdIn(TENANT_ID).singleResult();