36db549486c76386485468aca4003721ea106a26
[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.client.exception.ExceptionBuilder;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 /**
32  * Abstract class for Service level upgrade Execution, it should be extended for service level upgrade tasks.
33  */
34 public abstract class AbstractServiceLevelPreparable {
35
36     protected static final String WORKFLOW_TO_INVOKE = "healthCheckWorkflow";
37     protected static final String GENERIC_PNF_HEALTH_CHECK_WORKFLOW = "GenericPnfHealthCheck";
38     protected static final String GENERIC_PNF_SOFTWARE_UPGRADE_WORKFLOW = "GenericPnfSoftwareUpgrade";
39     protected static final String RESOURCE_TYPE = "RESOURCE_TYPE";
40     protected static final int ERROR_CODE = 601;
41
42     // TODO This value needs to be updated once vnf health check workflow is available
43     protected static final String GENERIC_VNF_HEALTH_CHECK_WORKFLOW = "GenericVNFHealthCheck";
44
45     protected static final Logger LOG = LoggerFactory.getLogger(AbstractServiceLevelPreparable.class);
46
47     @Autowired
48     protected ExceptionBuilder exceptionBuilder;
49
50     /**
51      * This method fetches workflow names to be invoked based on the controller scope .
52      *
53      * @param scope Controller scope
54      * @return String value of Workflow name
55      */
56     protected abstract String fetchWorkflowUsingScope(DelegateExecution execution, final String scope);
57
58     /**
59      * This method validates the execution parameters to be passed for health check workflow.
60      *
61      * @param execution Delegate execution obj
62      * @param scope Controller scope * Throws workflow exception if validation fails
63      */
64     protected void validateParamsWithScope(DelegateExecution execution, final String scope, List<String> params)
65             throws Exception {
66         List<String> invalidVariables = new ArrayList<>();
67         for (String param : params) {
68             if (!execution.hasVariable(param) || execution.getVariable(param) == null
69                     || String.valueOf(execution.getVariable(param)).isEmpty()) {
70                 invalidVariables.add(param);
71             }
72         }
73         if (invalidVariables.size() > 0) {
74             LOG.error("Validation error for the {} health check attributes: {}", scope, invalidVariables);
75             exceptionBuilder.buildAndThrowWorkflowException(execution, ERROR_CODE,
76                     "Validation of health check workflow parameters failed for the scope: " + scope);
77         }
78
79     }
80
81 }