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=========================================================
 
  21 package org.onap.so.bpmn.infrastructure.service.level;
 
  23 import java.util.ArrayList;
 
  24 import java.util.List;
 
  25 import java.util.Optional;
 
  26 import org.camunda.bpm.engine.delegate.DelegateExecution;
 
  27 import org.onap.so.bpmn.infrastructure.service.level.impl.ServiceLevelConstants;
 
  28 import org.onap.so.client.exception.ExceptionBuilder;
 
  29 import org.onap.so.db.catalog.beans.Workflow;
 
  30 import org.onap.so.db.catalog.client.CatalogDbClient;
 
  31 import org.slf4j.Logger;
 
  32 import org.slf4j.LoggerFactory;
 
  33 import org.springframework.beans.factory.annotation.Autowired;
 
  36  * Parent class for Service level upgrade Execution, it should be extended for service level upgrade tasks.
 
  38 public class ServiceLevelPreparable {
 
  40     protected static final Logger LOG = LoggerFactory.getLogger(ServiceLevelPreparable.class);
 
  43     protected ExceptionBuilder exceptionBuilder;
 
  46     protected CatalogDbClient catalogDbClient;
 
  49      * Fetches workflow names based on the controller scope and operation name.
 
  51      * @param scope Controller scope
 
  52      * @param operationName healthcheck/softwareUpgrade
 
  53      * @return String value of Workflow name
 
  55     protected String fetchWorkflowUsingScope(final String scope, String operationName) {
 
  56         Optional<String> wflName = Optional.empty();
 
  58             List<Workflow> workflows = catalogDbClient.findWorkflowByOperationName(operationName);
 
  59             if (!workflows.isEmpty()) {
 
  60                 wflName = Optional.ofNullable(
 
  61                         workflows.stream().filter(workflow -> workflow.getResourceTarget().equalsIgnoreCase(scope))
 
  62                                 .findFirst().get().getName());
 
  64         } catch (Exception e) {
 
  65             // do nothing and assign the default workflow in finally
 
  66             LOG.error("Error occurred while fetching workflow name from CatalogDb {}", e);
 
  68             if (wflName.isEmpty()) {
 
  69                 wflName = Optional.of(ServiceLevelConstants.WORKFLOW_OPERATIONS_MAP.get(operationName).get(scope));
 
  77      * This method validates the execution parameters to be passed for health check workflow.
 
  79      * @param execution Delegate execution obj
 
  80      * @param scope Controller scope * Throws workflow exception if validation fails
 
  82     protected void validateParamsWithScope(DelegateExecution execution, final String scope, List<String> params) {
 
  83         List<String> invalidVariables = new ArrayList<>();
 
  84         for (String param : params) {
 
  85             if (!execution.hasVariable(param) || execution.getVariable(param) == null
 
  86                     || String.valueOf(execution.getVariable(param)).isEmpty()) {
 
  87                 invalidVariables.add(param);
 
  90         if (invalidVariables.size() > 0) {
 
  91             LOG.error("Validation error for the {} health check attributes: {}", scope, invalidVariables);
 
  92             exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
 
  93                     "Validation of health check workflow parameters failed for the scope: " + scope);