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_INSTANCE_ID_PARAM_NAME;
24 import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.DEPLOYMENT_ITEM_TERMINATE_REQUESTS;
25 import static org.onap.so.cnfm.lcm.database.beans.JobStatusEnum.FINISHED;
26 import static org.onap.so.cnfm.lcm.database.beans.JobStatusEnum.IN_PROGRESS;
27 import static org.onap.so.cnfm.lcm.database.beans.JobStatusEnum.STARTED;
29 import java.nio.file.Path;
30 import java.util.List;
32 import java.util.TreeSet;
33 import org.camunda.bpm.engine.delegate.DelegateExecution;
34 import org.onap.so.beans.nsmf.OrchestrationStatusEnum;
35 import org.onap.so.cnfm.lcm.bpmn.flows.exceptions.KubeConfigFileNotFoundException;
36 import org.onap.so.cnfm.lcm.bpmn.flows.extclients.aai.AaiServiceProvider;
37 import org.onap.so.cnfm.lcm.bpmn.flows.service.KubConfigProvider;
38 import org.onap.so.cnfm.lcm.database.beans.AsDeploymentItem;
39 import org.onap.so.cnfm.lcm.database.beans.AsInst;
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 Waqas Ikram (waqas.ikram@est.tech)
53 public class TerminateAsTask extends AbstractServiceTask {
54 private static final Logger logger = LoggerFactory.getLogger(TerminateAsTask.class);
55 private final KubConfigProvider kubConfigProvider;
56 private final AaiServiceProvider aaiServiceProvider;
57 private static final String KUBE_CONFIG_FILE_PARAM_NAME = "kubeConfigFile";
58 private static final String IS_AS_TERMINATION_SUCCESSFUL_PARAM_NAME = "isAsTerminationSuccessful";
61 protected TerminateAsTask(final DatabaseServiceProvider databaseServiceProvider,
62 final KubConfigProvider kubConfigProvider, final AaiServiceProvider aaiServiceProvider) {
63 super(databaseServiceProvider);
64 this.kubConfigProvider = kubConfigProvider;
65 this.aaiServiceProvider = aaiServiceProvider;
68 public void setJobStatusToStarted(final DelegateExecution execution) {
69 setJobStatus(execution, STARTED, "Terminate AS workflow process started");
72 public void setJobStatusToFinished(final DelegateExecution execution) {
73 setJobStatus(execution, FINISHED, "Terminate AS workflow process finished");
76 public void setJobStatusToError(final DelegateExecution execution) {
77 setJobStatusToError(execution, "Terminate AS workflow process failed");
80 public void updateAsInstanceStatusToTerminating(final DelegateExecution execution) {
81 logger.info("Executing updateAsInstanceStatusToTerminating");
82 setJobStatus(execution, IN_PROGRESS, "Updating AsInst Status to " + State.TERMINATING);
83 updateAsInstanceStatus(execution, State.TERMINATING);
84 logger.info("Finished executing updateAsInstanceStatusToTerminating ...");
87 public void updateAsInstanceStatusToNotInstantiated(final DelegateExecution execution) {
88 logger.info("Executing updateAsInstanceStatusToNotInstantiated");
89 setJobStatus(execution, IN_PROGRESS, "Updating AsInst Status to " + State.NOT_INSTANTIATED);
90 updateAsInstanceStatus(execution, State.NOT_INSTANTIATED);
91 logger.info("Finished executing updateAsInstanceStatusToNotInstantiated ...");
94 public void logTimeOut(final DelegateExecution execution) {
95 logger.error("Deployment items Termination timedOut ...");
96 final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME);
97 final List<AsDeploymentItem> asDeploymentItems =
98 databaseServiceProvider.getAsDeploymentItemByAsInstId(asInstId);
99 if (asDeploymentItems != null) {
100 asDeploymentItems.stream()
101 .forEach(asDeploymentItem -> logger.info("Current status {} of terminating asDeploymentItem: {}",
102 asDeploymentItem.getStatus(), asDeploymentItem.getName()));
106 public void checkifKubConfigFileAvailable(final DelegateExecution execution) {
107 logger.info("Executing checkifKubConfigFileAvailable");
109 setJobStatus(execution, IN_PROGRESS, "Checking if kubeconfig file is available or not");
110 final AsInst asInst = getAsInst(execution);
112 final Path kubeConfigFile = kubConfigProvider.getKubeConfigFile(asInst.getCloudOwner(),
113 asInst.getCloudRegion(), asInst.getTenantId());
115 execution.setVariable(KUBE_CONFIG_FILE_PARAM_NAME, kubeConfigFile.toString());
117 } catch (final KubeConfigFileNotFoundException exception) {
118 final String message = "Unable to find kube-config file on filesystem";
119 logger.error(message, exception);
120 abortOperation(execution, message);
124 logger.info("Finished executing checkifKubConfigFileAvailable ...");
128 public void prepareTerminateDeploymentItemRequests(final DelegateExecution execution) {
129 logger.info("Executing prepareTerminateDeploymentItemRequests ...");
130 setJobStatus(execution, IN_PROGRESS, "Preparing TerminateDeploymentItemRequest requests");
132 final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME);
133 final String kubeConfigFile = (String) execution.getVariable(KUBE_CONFIG_FILE_PARAM_NAME);
135 final List<AsDeploymentItem> asDeploymentItems =
136 databaseServiceProvider.getAsDeploymentItemByAsInstId(asInstId);
138 final Set<TerminateDeploymentItemRequest> requests = new TreeSet<>();
140 asDeploymentItems.forEach(asDeploymentItem -> {
142 final String asDeploymentItemInstId = asDeploymentItem.getAsDeploymentItemInstId();
144 final TerminateDeploymentItemRequest terminatedeploymentitemrequest = new TerminateDeploymentItemRequest();
145 terminatedeploymentitemrequest.setAsInstId(asInstId);
146 terminatedeploymentitemrequest.setAsDeploymentItemInstId(asDeploymentItemInstId);
147 terminatedeploymentitemrequest.setDeploymentOrder(asDeploymentItem.getDeploymentOrder());
148 terminatedeploymentitemrequest.setKubeConfigFile(kubeConfigFile);
149 terminatedeploymentitemrequest.setReleaseName(asDeploymentItem.getReleaseName());
151 requests.add(terminatedeploymentitemrequest);
155 execution.setVariable(DEPLOYMENT_ITEM_TERMINATE_REQUESTS, requests);
157 logger.info("Finished executing prepareTerminateDeploymentItemRequests ...");
160 public void checkIfDeploymentItemsTerminationWasSuccessful(final DelegateExecution execution) {
161 logger.info("Executing checkIfDeploymentItemsTerminationWasSuccessful");
163 @SuppressWarnings("unchecked")
164 final Set<TerminateDeploymentItemRequest> requests =
165 (Set<TerminateDeploymentItemRequest>) execution.getVariable(DEPLOYMENT_ITEM_TERMINATE_REQUESTS);
167 final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME);
168 final List<AsDeploymentItem> asDeploymentItems =
169 databaseServiceProvider.getAsDeploymentItemByAsInstId(asInstId);
172 if (asDeploymentItems == null || asDeploymentItems.isEmpty()) {
173 final String message = "Found empty asDeploymentItems";
174 abortOperation(execution, message);
175 } else if (requests.size() != asDeploymentItems.size()) {
176 final String message = "Missing asDeploymentItems. Request triggered has: " + requests.size()
177 + " asDeploymentItems but database has: " + asDeploymentItems.size();
178 abortOperation(execution, message);
180 execution.setVariable(IS_AS_TERMINATION_SUCCESSFUL_PARAM_NAME, true);
181 asDeploymentItems.forEach(asDeploymentItem -> {
182 logger.info("Checking AsDeploymentItem {} termination status: {}",
183 asDeploymentItem.getAsDeploymentItemInstId(), asDeploymentItem.getStatus());
184 if (!State.NOT_INSTANTIATED.equals(asDeploymentItem.getStatus())) {
185 logger.error("AsDeploymentItem : {} {} termination failed",
186 asDeploymentItem.getAsDeploymentItemInstId(), asDeploymentItem.getName());
187 execution.setVariable(IS_AS_TERMINATION_SUCCESSFUL_PARAM_NAME, false);
191 logger.info("Finished executing checkIfDeploymentItemsTerminationWasSuccessful ...");
194 public void updateGenericVnfStatustoDeActivated(final DelegateExecution execution) {
196 logger.debug("Executing updateGenericVnfStatustoDeActivated");
197 final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME);
198 final boolean result = aaiServiceProvider.updateGenericVnfStatus(asInstId, OrchestrationStatusEnum.DEACTIVATED);
200 abortOperation(execution, "Failed to update GenericVnf status to Deactivated as there"
201 + "is no GenericVnf Found in AAI of ID: " + asInstId);
203 logger.info("Finished updating vnf status to Deactivated");