e26195158d498d353d88d1fc2d6a92b932fc6210
[so.git] /
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
21 package org.onap.so.bpmn.infrastructure.service.level;
22
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;
34
35 /**
36  * Parent class for Service level upgrade Execution, it should be extended for service level upgrade tasks.
37  */
38 public class ServiceLevelPreparable {
39
40     protected static final Logger LOG = LoggerFactory.getLogger(ServiceLevelPreparable.class);
41
42     @Autowired
43     protected ExceptionBuilder exceptionBuilder;
44
45     @Autowired
46     protected CatalogDbClient catalogDbClient;
47
48     /**
49      * Fetches workflow names based on the controller scope and operation name.
50      *
51      * @param scope Controller scope
52      * @param operationName healthcheck/softwareUpgrade
53      * @return String value of Workflow name
54      */
55     protected String fetchWorkflowUsingScope(final String scope, String operationName) {
56         Optional<String> wflName = Optional.empty();
57         try {
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());
63             }
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);
67         } finally {
68             if (wflName.isEmpty()) {
69                 wflName = Optional.of(ServiceLevelConstants.WORKFLOW_OPERATIONS_MAP.get(operationName).get(scope));
70             }
71         }
72         return wflName.get();
73
74     }
75
76     /**
77      * This method validates the execution parameters to be passed for health check workflow.
78      *
79      * @param execution Delegate execution obj
80      * @param scope Controller scope * Throws workflow exception if validation fails
81      */
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);
88             }
89         }
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);
94         }
95
96     }
97
98 }