6cb3461001ee57a75ab0007f55f6fc56ca4d6a96
[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.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.ServiceLevel;
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 ServiceLevel 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);
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         LOG.debug("Running execute block for activity id: {}, name: {}", execution.getCurrentActivityId(),
52                 execution.getCurrentActivityName());
53
54         if (execution.hasVariable(ServiceLevelConstants.RESOURCE_TYPE)
55                 && execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE) != null) {
56             final String controllerScope = (String) execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE);
57             LOG.debug("Scope retrieved from delegate execution: " + controllerScope);
58             if (ServiceLevelConstants.VALID_CONTROLLER_SCOPE.contains(controllerScope)) {
59                 final String wflName =
60                         fetchWorkflowUsingScope(controllerScope, ServiceLevelConstants.HEALTH_CHECK_OPERATION);
61                 LOG.debug("Health check workflow fetched for the scope: {} is: {}", controllerScope, wflName);
62                 validateParamsWithScope(execution, controllerScope, HEALTH_CHECK_PARAMS_MAP.get(controllerScope));
63                 LOG.info("Parameters validated successfully for {}", wflName);
64                 execution.setVariable(ServiceLevelConstants.HEALTH_CHECK_WORKFLOW_TO_INVOKE, wflName);
65                 execution.setVariable(ServiceLevelConstants.CONTROLLER_STATUS, ServiceLevelConstants.EMPTY_STRING);
66                 execution.setVariable(ServiceLevelConstants.PNF_COUNTER, ServiceLevelConstants.COUNT_ZERO);
67             } else {
68                 exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
69                         "Invalid Controller scope to prepare resource level health check");
70             }
71         } else {
72             exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
73                     "Resource type not found in the execution to invoke resource level health check");
74         }
75     }
76
77 }