2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.cnfm.lcm.bpmn.flows.tasks;
23 import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.AS_DEPLOYMENT_ITEM_INST_ID_PARAM_NAME;
24 import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.KUBE_CONFIG_FILE_PATH_PARAM_NAME;
25 import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.KUBE_KINDS_PARAM_NAME;
26 import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.KUBE_KINDS_RESULT_PARAM_NAME;
27 import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.RELEASE_NAME_PARAM_NAME;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.util.HashMap;
31 import java.util.List;
33 import org.camunda.bpm.engine.delegate.DelegateExecution;
34 import org.onap.so.cnfm.lcm.bpmn.flows.extclients.aai.AaiServiceProvider;
35 import org.onap.so.cnfm.lcm.bpmn.flows.extclients.helm.HelmClient;
36 import org.onap.so.cnfm.lcm.bpmn.flows.extclients.kubernetes.KubernetesClientProvider;
37 import org.onap.so.cnfm.lcm.bpmn.flows.extclients.kubernetes.KubernetesResource;
38 import org.onap.so.cnfm.lcm.database.beans.AsInst;
39 import org.onap.so.cnfm.lcm.database.beans.JobStatusEnum;
40 import org.onap.so.cnfm.lcm.database.beans.State;
41 import org.onap.so.cnfm.lcm.database.service.DatabaseServiceProvider;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Component;
49 * @author Raviteja karumuri (raviteja.karumuri@est.tech)
54 public class TerminateDeploymentItemTask extends AbstractServiceTask {
56 private static final Logger logger = LoggerFactory.getLogger(TerminateDeploymentItemTask.class);
58 private static final String IS_DELETED_PARAM_NAME = "isDeleted";
59 private static final String TERMINATE_REQUEST_PARAM_NAME = "request";
61 private final AaiServiceProvider aaiServiceProvider;
62 private final HelmClient helmClient;
63 private final KubernetesClientProvider kubernetesClientProvider;
66 protected TerminateDeploymentItemTask(final DatabaseServiceProvider databaseServiceProvider,
67 final AaiServiceProvider aaiServiceProvider, final HelmClient helmClient,
68 final KubernetesClientProvider kubernetesClientProvider) {
69 super(databaseServiceProvider);
70 this.aaiServiceProvider = aaiServiceProvider;
71 this.helmClient = helmClient;
72 this.kubernetesClientProvider = kubernetesClientProvider;
75 public void checkIfDeploymentItemExistsInDb(final DelegateExecution execution) {
76 logger.info("Executing checkIfDeploymentItemExistsInDb");
77 final TerminateDeploymentItemRequest request =
78 (TerminateDeploymentItemRequest) execution.getVariable(TERMINATE_REQUEST_PARAM_NAME);
79 logger.info("Terminate request: {}", request);
81 final String asDeploymentItemInstId = request.getAsDeploymentItemInstId();
82 addJobStatus(execution, JobStatusEnum.IN_PROGRESS,
83 "Checking if Deployment item record exists in database for asDeploymentItemInstId: "
84 + asDeploymentItemInstId);
86 if (!databaseServiceProvider.isAsDeploymentItemExists(request.getAsDeploymentItemInstId())) {
87 abortOperation(execution, "Deployment Item does not exists in database for asDeploymentItemInstId: "
88 + asDeploymentItemInstId);
90 execution.setVariable(AS_DEPLOYMENT_ITEM_INST_ID_PARAM_NAME, request.getAsDeploymentItemInstId());
91 execution.setVariable(KUBE_CONFIG_FILE_PATH_PARAM_NAME, request.getKubeConfigFile());
92 logger.info("Finished executing checkIfDeploymentItemExistsInDb ...");
96 public void unInstantiateHelmChart(final DelegateExecution execution) {
97 logger.info("Executing unInstantiateHelmChart");
99 final TerminateDeploymentItemRequest request =
100 (TerminateDeploymentItemRequest) execution.getVariable(TERMINATE_REQUEST_PARAM_NAME);
101 final String releaseName = request.getReleaseName();
104 final Path kubeConfigFilePath = Paths.get(request.getKubeConfigFile());
105 helmClient.unInstallHelmChart(releaseName, kubeConfigFilePath);
106 } catch (final Exception exception) {
107 final String message = "Failed to uninstall helm chart: " + " using kube-config file: "
108 + request.getKubeConfigFile() + "for reason: " + exception.getMessage();
109 logger.error(message, exception);
110 abortOperation(execution, message);
112 logger.info("Finished executing unInstantiateHelmChart ...");
117 public void updateDeploymentItemStatusToNotInstantiated(final DelegateExecution execution) {
119 logger.info("Executing updateDeploymentItemStatusToNotInstantiated");
120 final TerminateDeploymentItemRequest request =
121 (TerminateDeploymentItemRequest) execution.getVariable(TERMINATE_REQUEST_PARAM_NAME);
123 updateDeploymentItemStatus(execution, request.getAsDeploymentItemInstId(), State.NOT_INSTANTIATED);
125 addJobStatus(execution, JobStatusEnum.FINISHED, "Successfully Terminated Deployment Item with "
126 + "releaseName: " + request.getReleaseName() + " and will set status to " + State.NOT_INSTANTIATED);
128 logger.info("Finished executing updateDeploymentItemStatusToNotInstantiated ...");
131 public void getKubeKindsUsingManifestCommand(final DelegateExecution execution) {
133 logger.info("Executing getKubeKindsFromReleaseHistory");
135 final TerminateDeploymentItemRequest request =
136 (TerminateDeploymentItemRequest) execution.getVariable(TERMINATE_REQUEST_PARAM_NAME);
137 final String releaseName = request.getReleaseName();
138 final Path kubeConfigFilePath = Paths.get(request.getKubeConfigFile());
139 final Map<String, Boolean> kubeKindsMap = new HashMap<>();
140 final List<String> kinds = helmClient.getKubeKindsUsingManifestCommand(releaseName, kubeConfigFilePath);
141 if (kinds.isEmpty()) {
142 abortOperation(execution,
143 "Unable to retrieve kinds from helm release history for releaseName: " + releaseName);
145 kinds.forEach(kind -> kubeKindsMap.put(kind, false));
147 execution.setVariable(RELEASE_NAME_PARAM_NAME, releaseName);
148 execution.setVariable(KUBE_KINDS_RESULT_PARAM_NAME, kubeKindsMap);
149 execution.setVariable(KUBE_KINDS_PARAM_NAME, kinds);
152 public void checkIfHelmUnInstallWasSuccessful(final DelegateExecution execution) {
153 logger.info("Executing checkIfHelmUnInstallWasSuccessful");
155 @SuppressWarnings("unchecked")
156 final Map<String, Boolean> kubeKindResult =
157 (Map<String, Boolean>) execution.getVariable(KUBE_KINDS_RESULT_PARAM_NAME);
159 execution.setVariable(IS_DELETED_PARAM_NAME, true);
161 kubeKindResult.forEach((key, value) -> {
162 logger.info("Checking if resource type {} was deleted Status: {}", key, value);
164 if (Boolean.FALSE.equals(value)) {
165 logger.error("resource type {} failed to delete", key);
166 execution.setVariable(IS_DELETED_PARAM_NAME, false);
170 final String kubeConfigFile = (String) execution.getVariable(KUBE_CONFIG_FILE_PATH_PARAM_NAME);
171 kubernetesClientProvider.closeApiClient(kubeConfigFile);
173 logger.info("Finished executing checkIfHelmUnInstallWasSuccessful ...");
177 public void deleteK8ResourcesinAAI(final DelegateExecution execution) {
178 logger.info("Executing deleteK8ResourcesinAai");
179 final TerminateDeploymentItemRequest request =
180 (TerminateDeploymentItemRequest) execution.getVariable(TERMINATE_REQUEST_PARAM_NAME);
181 final List<KubernetesResource> resources =
182 aaiServiceProvider.getK8sResources(request.getAsInstId(), request.getAsDeploymentItemInstId());
183 final AsInst asInst = getAsInst(execution);
184 resources.forEach(resource -> aaiServiceProvider.deleteK8SResource(resource.getId(), asInst.getCloudOwner(),
185 asInst.getCloudRegion(), asInst.getTenantId()));
188 public void deleteVFModuleinAai(final DelegateExecution execution) {
189 logger.info("Executing deleteVFModuleinAai");
191 final TerminateDeploymentItemRequest request =
192 (TerminateDeploymentItemRequest) execution.getVariable(TERMINATE_REQUEST_PARAM_NAME);
193 aaiServiceProvider.deleteVfModule(request.getAsInstId(), request.getAsDeploymentItemInstId());
194 logger.info("Delete VfModule in AAi is Done");