9d7d8efb657b199d17197502ff404e07f86a68cf
[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.ServiceLevelPreparable;
30 import org.springframework.stereotype.Component;
31
32 @Component
33 public class ServiceLevelUpgrade extends ServiceLevelPreparable implements JavaDelegate {
34
35     private static final List<String> PNF_SOFTWARE_UP_PARAMS = Arrays.asList(ServiceLevelConstants.SERVICE_INSTANCE_ID,
36             ServiceLevelConstants.RESOURCE_TYPE, ServiceLevelConstants.BPMN_REQUEST, ServiceLevelConstants.PNF_NAME);
37
38     // TODO Update the list with vnf software upgrade parameters if any validation needed
39     private static final List<String> VNF_SOFTWARE_UP_PARAMS = Collections.emptyList();
40
41     private static final Map<String, List<String>> SOFTWARE_UP_PARAMS_MAP = Map.of(ServiceLevelConstants.PNF,
42             PNF_SOFTWARE_UP_PARAMS, ServiceLevelConstants.VNF, VNF_SOFTWARE_UP_PARAMS);
43
44
45     @Override
46     public void execute(DelegateExecution execution) throws Exception {
47         if (execution.hasVariable(ServiceLevelConstants.RESOURCE_TYPE)
48                 && execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE) != null) {
49             final String controllerScope = (String) execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE);
50             LOG.debug("Scope retrieved from delegate execution: " + controllerScope);
51             if (ServiceLevelConstants.VALID_CONTROLLER_SCOPE.contains(controllerScope)) {
52                 final String wflName = fetchWorkflowUsingScope(controllerScope, ServiceLevelConstants.SW_UP_OPERATION);
53                 LOG.debug("Software Upgrade workflow fetched for the scope: {} is: {}", controllerScope, wflName);
54                 validateParamsWithScope(execution, controllerScope, SOFTWARE_UP_PARAMS_MAP.get(controllerScope));
55                 LOG.info("Parameters validated successfully for {}", wflName);
56                 execution.setVariable(ServiceLevelConstants.SOFTWARE_WORKFLOW_TO_INVOKE, wflName);
57                 execution.setVariable(ServiceLevelConstants.CONTROLLER_STATUS, "");
58             } else {
59                 exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
60                         "Invalid Controller scope for resource level software upgrade");
61             }
62         } else {
63             exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
64                     "Resource type not found in the execution to invoke resource level software upgrade");
65         }
66     }
67 }