5b772760f0ae38eb65330fa2756987d6c2439a82
[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 org.camunda.bpm.engine.delegate.DelegateExecution;
24 import org.camunda.bpm.engine.delegate.JavaDelegate;
25 import org.onap.so.bpmn.infrastructure.service.level.AbstractServiceLevelPreparable;
26 import org.springframework.stereotype.Component;
27 import java.util.Arrays;
28 import java.util.List;
29
30 /**
31  * Fetches health check workflow based on the controller_scope. Invoke the corresponding health check workflow after
32  * validation.
33  */
34 @Component("ServiceLevelPreparation")
35 public class ServiceLevelPreparation extends AbstractServiceLevelPreparable implements JavaDelegate {
36
37     private static final List<String> PNF_HC_PARAMS = Arrays.asList(ServiceLevelConstants.SERVICE_INSTANCE_ID,
38             ServiceLevelConstants.RESOURCE_TYPE, ServiceLevelConstants.BPMN_REQUEST, ServiceLevelConstants.PNF_NAME);
39
40     @Override
41     public void execute(DelegateExecution execution) throws Exception {
42         if (execution.hasVariable(ServiceLevelConstants.RESOURCE_TYPE)
43                 && execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE) != null) {
44             final String controllerScope = (String) execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE);
45             LOG.debug("Scope retrieved from delegate execution: " + controllerScope);
46             final String wflName = fetchWorkflowUsingScope(execution, controllerScope);
47             LOG.debug("Health check workflow fetched for the scope: {} is: {}", controllerScope, wflName);
48
49             if (ServiceLevelConstants.PNF.equalsIgnoreCase(controllerScope)) {
50                 validateParamsWithScope(execution, controllerScope, PNF_HC_PARAMS);
51                 LOG.info("Parameters validated successfully for {}", wflName);
52             }
53             execution.setVariable(ServiceLevelConstants.WORKFLOW_TO_INVOKE, wflName);
54             execution.setVariable(ServiceLevelConstants.CONTROLLER_STATUS, ServiceLevelConstants.EMPTY_STRING);
55         } else {
56             exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
57                     "Controller scope not found to invoke resource level health check");
58         }
59     }
60
61     @Override
62     public String fetchWorkflowUsingScope(DelegateExecution execution, final String scope) {
63         String wflName = null;
64         switch (scope.toLowerCase()) {
65             case ServiceLevelConstants.PNF:
66                 wflName = ServiceLevelConstants.GENERIC_PNF_HEALTH_CHECK_WORKFLOW;
67                 break;
68             case ServiceLevelConstants.VNF:
69                 wflName = ServiceLevelConstants.GENERIC_VNF_HEALTH_CHECK_WORKFLOW;
70                 break;
71             default:
72                 exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
73                         "No valid health check work flow retrieved for the scope: " + scope);
74         }
75         return wflName;
76     }
77
78 }