Picking workflow names dynamically for schema update procedure
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / service / level / impl / ServiceLevelPreparation.java
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.impl;
22
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27 import org.camunda.bpm.engine.delegate.DelegateExecution;
28 import org.camunda.bpm.engine.delegate.JavaDelegate;
29 import org.onap.so.bpmn.infrastructure.service.level.ServiceLevelPreparable;
30 import org.springframework.stereotype.Component;
31
32
33 /**
34  * Fetches health check workflow based on the controller_scope. Invoke the corresponding health check workflow after
35  * validation.
36  */
37 @Component("ServiceLevelPreparation")
38 public class ServiceLevelPreparation extends ServiceLevelPreparable implements JavaDelegate {
39
40     private static final List<String> PNF_HEALTH_CHECK_PARAMS = Arrays.asList(ServiceLevelConstants.SERVICE_INSTANCE_ID,
41             ServiceLevelConstants.RESOURCE_TYPE, ServiceLevelConstants.BPMN_REQUEST, ServiceLevelConstants.PNF_NAME);
42
43     // TODO Update the list with vnf health check parameters if any validation needed
44     private static final List<String> VNF_HEALTH_CHECK_PARAMS = Collections.emptyList();
45
46     private static final Map<String, List<String>> HEALTH_CHECK_PARAMS_MAP = Map.of(ServiceLevelConstants.PNF,
47             PNF_HEALTH_CHECK_PARAMS, ServiceLevelConstants.VNF, VNF_HEALTH_CHECK_PARAMS);
48
49     @Override
50     public void execute(DelegateExecution execution) throws Exception {
51         if (execution.hasVariable(ServiceLevelConstants.RESOURCE_TYPE)
52                 && execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE) != null) {
53             final String controllerScope = (String) execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE);
54             LOG.debug("Scope retrieved from delegate execution: " + controllerScope);
55             if (ServiceLevelConstants.VALID_CONTROLLER_SCOPE.contains(controllerScope)) {
56                 final String wflName =
57                         fetchWorkflowUsingScope(controllerScope, ServiceLevelConstants.HEALTH_CHECK_OPERATION);
58                 LOG.debug("Health check workflow fetched for the scope: {} is: {}", controllerScope, wflName);
59                 validateParamsWithScope(execution, controllerScope, HEALTH_CHECK_PARAMS_MAP.get(controllerScope));
60                 LOG.info("Parameters validated successfully for {}", wflName);
61                 execution.setVariable(ServiceLevelConstants.HEALTH_CHECK_WORKFLOW_TO_INVOKE, wflName);
62                 execution.setVariable(ServiceLevelConstants.CONTROLLER_STATUS, ServiceLevelConstants.EMPTY_STRING);
63             } else {
64                 exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
65                         "Invalid Controller scope to prepare resource level health check");
66             }
67         } else {
68             exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
69                     "Resource type not found in the execution to invoke resource level health check");
70         }
71     }
72
73 }