0671354bd6c6f35e01e1334faa90865e4ed3b00c
[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 org.camunda.bpm.engine.delegate.DelegateExecution;
24 import org.onap.so.bpmn.infrastructure.service.level.impl.ServiceLevelConstants;
25 import org.onap.so.client.exception.ExceptionBuilder;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 /**
33  * Abstract class for Service level upgrade Execution, it should be extended for service level upgrade tasks.
34  */
35 public abstract class AbstractServiceLevelPreparable {
36
37     protected static final Logger LOG = LoggerFactory.getLogger(AbstractServiceLevelPreparable.class);
38
39     @Autowired
40     protected ExceptionBuilder exceptionBuilder;
41
42     /**
43      * This method fetches workflow names to be invoked based on the controller scope .
44      *
45      * @param scope Controller scope
46      * @return String value of Workflow name
47      */
48     protected abstract String fetchWorkflowUsingScope(DelegateExecution execution, final String scope);
49
50     /**
51      * This method validates the execution parameters to be passed for health check workflow.
52      *
53      * @param execution Delegate execution obj
54      * @param scope Controller scope * Throws workflow exception if validation fails
55      */
56     protected void validateParamsWithScope(DelegateExecution execution, final String scope, List<String> params) {
57         List<String> invalidVariables = new ArrayList<>();
58         for (String param : params) {
59             if (!execution.hasVariable(param) || execution.getVariable(param) == null
60                     || String.valueOf(execution.getVariable(param)).isEmpty()) {
61                 invalidVariables.add(param);
62             }
63         }
64         if (invalidVariables.size() > 0) {
65             LOG.error("Validation error for the {} health check attributes: {}", scope, invalidVariables);
66             exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
67                     "Validation of health check workflow parameters failed for the scope: " + scope);
68         }
69
70     }
71
72 }