a3a8f6e714bfb85358c8d7c923b9563560b5550a
[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 @Component
33 public class ServiceLevelUpgrade extends ServiceLevel 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);
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         LOG.debug("Running execute block for activity id: {}, name: {}", execution.getCurrentActivityId(),
48                 execution.getCurrentActivityName());
49
50         if (execution.hasVariable(ServiceLevelConstants.RESOURCE_TYPE)
51                 && execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE) != null) {
52             final String controllerScope = (String) execution.getVariable(ServiceLevelConstants.RESOURCE_TYPE);
53             LOG.debug("Scope retrieved from delegate execution: " + controllerScope);
54             if (ServiceLevelConstants.VALID_CONTROLLER_SCOPE.contains(controllerScope)) {
55                 final String wflName = fetchWorkflowUsingScope(controllerScope, ServiceLevelConstants.SW_UP_OPERATION);
56                 LOG.debug("Software Upgrade workflow fetched for the scope: {} is: {}", controllerScope, wflName);
57                 validateParamsWithScope(execution, controllerScope, SOFTWARE_UP_PARAMS_MAP.get(controllerScope));
58                 LOG.info("Parameters validated successfully for {}", wflName);
59                 execution.setVariable(ServiceLevelConstants.SOFTWARE_WORKFLOW_TO_INVOKE, wflName);
60                 execution.setVariable(ServiceLevelConstants.CONTROLLER_STATUS, ServiceLevelConstants.EMPTY_STRING);
61                 execution.setVariable(ServiceLevelConstants.PNF_COUNTER, ServiceLevelConstants.COUNT_ZERO);
62             } else {
63                 exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
64                         "Invalid Controller scope for resource level software upgrade");
65             }
66         } else {
67             exceptionBuilder.buildAndThrowWorkflowException(execution, ServiceLevelConstants.ERROR_CODE,
68                     "Resource type not found in the execution to invoke resource level software upgrade");
69         }
70     }
71 }