d834c4dca4c45524d3c187c0cbb7f7204e1feac9
[so/adapters/so-cnf-adapter.git] /
1 /*-
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
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.cnfm.lcm.bpmn.flows.tasks;
22
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;
28
29 import java.nio.file.Path;
30 import java.util.List;
31 import java.util.Set;
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;
46
47 /**
48  *
49  * @author Waqas Ikram (waqas.ikram@est.tech)
50  *
51  */
52 @Component
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";
59
60     @Autowired
61     protected TerminateAsTask(final DatabaseServiceProvider databaseServiceProvider,
62                               final KubConfigProvider kubConfigProvider, final AaiServiceProvider aaiServiceProvider) {
63         super(databaseServiceProvider);
64         this.kubConfigProvider = kubConfigProvider;
65         this.aaiServiceProvider = aaiServiceProvider;
66     }
67
68     public void setJobStatusToStarted(final DelegateExecution execution) {
69         setJobStatus(execution, STARTED, "Terminate AS workflow process started");
70     }
71
72     public void setJobStatusToFinished(final DelegateExecution execution) {
73         setJobStatus(execution, FINISHED, "Terminate AS workflow process finished");
74     }
75
76     public void setJobStatusToError(final DelegateExecution execution) {
77         setJobStatusToError(execution, "Terminate AS workflow process failed");
78     }
79
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  ...");
85     }
86
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  ...");
92     }
93
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().forEach(asDeploymentItem -> {
101                 logger.info("Current status {} of terminating asDeploymentItem: {}", asDeploymentItem.getStatus(),
102                         asDeploymentItem.getName());
103             });
104         }
105     }
106
107     public void checkifKubConfigFileAvailable(final DelegateExecution execution) {
108         logger.info("Executing checkifKubConfigFileAvailable");
109         try {
110             setJobStatus(execution, IN_PROGRESS, "Checking if kubeconfig file is available or not");
111             final AsInst asInst = getAsInst(execution);
112
113             final Path kubeConfigFile = kubConfigProvider.getKubeConfigFile(asInst.getCloudOwner(),
114                     asInst.getCloudRegion(), asInst.getTenantId());
115
116             execution.setVariable(KUBE_CONFIG_FILE_PARAM_NAME, kubeConfigFile.toString());
117
118         } catch (final KubeConfigFileNotFoundException exception) {
119             final String message = "Unable to find kube-config file on filesystem";
120             logger.error(message, exception);
121             abortOperation(execution, message);
122
123         }
124
125         logger.info("Finished executing checkifKubConfigFileAvailable  ...");
126
127     }
128
129     public void prepareTerminateDeploymentItemRequests(final DelegateExecution execution) {
130         logger.info("Executing prepareTerminateDeploymentItemRequests ...");
131         setJobStatus(execution, IN_PROGRESS, "Preparing TerminateDeploymentItemRequest requests");
132
133         final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME);
134         final String kubeConfigFile = (String) execution.getVariable(KUBE_CONFIG_FILE_PARAM_NAME);
135
136         final List<AsDeploymentItem> asDeploymentItems =
137                 databaseServiceProvider.getAsDeploymentItemByAsInstId(asInstId);
138
139         final Set<TerminateDeploymentItemRequest> requests = new TreeSet<>();
140
141         asDeploymentItems.forEach(asDeploymentItem -> {
142
143             final String asDeploymentItemInstId = asDeploymentItem.getAsDeploymentItemInstId();
144
145             final TerminateDeploymentItemRequest terminatedeploymentitemrequest = new TerminateDeploymentItemRequest();
146             terminatedeploymentitemrequest.setAsInstId(asInstId);
147             terminatedeploymentitemrequest.setAsDeploymentItemInstId(asDeploymentItemInstId);
148             terminatedeploymentitemrequest.setDeploymentOrder(asDeploymentItem.getDeploymentOrder());
149             terminatedeploymentitemrequest.setKubeConfigFile(kubeConfigFile);
150             terminatedeploymentitemrequest.setReleaseName(asDeploymentItem.getReleaseName());
151
152             requests.add(terminatedeploymentitemrequest);
153
154         });
155
156         execution.setVariable(DEPLOYMENT_ITEM_TERMINATE_REQUESTS, requests);
157
158         logger.info("Finished executing prepareTerminateDeploymentItemRequests ...");
159     }
160
161     public void checkIfDeploymentItemsTerminationWasSuccessful(final DelegateExecution execution) {
162         logger.info("Executing checkIfDeploymentItemsTerminationWasSuccessful");
163
164         @SuppressWarnings("unchecked")
165         final Set<TerminateDeploymentItemRequest> requests =
166                 (Set<TerminateDeploymentItemRequest>) execution.getVariable(DEPLOYMENT_ITEM_TERMINATE_REQUESTS);
167
168         final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME);
169         final List<AsDeploymentItem> asDeploymentItems =
170                 databaseServiceProvider.getAsDeploymentItemByAsInstId(asInstId);
171
172
173         if (asDeploymentItems == null || asDeploymentItems.isEmpty()) {
174             final String message = "Found empty asDeploymentItems";
175             abortOperation(execution, message);
176         } else if (requests.size() != asDeploymentItems.size()) {
177             final String message = "Missing asDeploymentItems. Request triggered has: " + requests.size()
178                     + " asDeploymentItems but database has: " + asDeploymentItems.size();
179             abortOperation(execution, message);
180         } else {
181             execution.setVariable(IS_AS_TERMINATION_SUCCESSFUL_PARAM_NAME, true);
182             asDeploymentItems.forEach(asDeploymentItem -> {
183                 logger.info("Checking AsDeploymentItem {} termination status: {}",
184                         asDeploymentItem.getAsDeploymentItemInstId(), asDeploymentItem.getStatus());
185                 if (!State.NOT_INSTANTIATED.equals(asDeploymentItem.getStatus())) {
186                     logger.error("AsDeploymentItem : {} {} termination failed",
187                             asDeploymentItem.getAsDeploymentItemInstId(), asDeploymentItem.getName());
188                     execution.setVariable(IS_AS_TERMINATION_SUCCESSFUL_PARAM_NAME, false);
189                 }
190             });
191         }
192         logger.info("Finished executing checkIfDeploymentItemsTerminationWasSuccessful  ...");
193     }
194
195     public void updateGenericVnfStatustoDeActivated(final DelegateExecution execution) {
196
197         logger.debug("Executing updateGenericVnfStatustoDeActivated");
198         final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME);
199         final boolean result = aaiServiceProvider.updateGenericVnfStatus(asInstId,
200                 OrchestrationStatusEnum.DEACTIVATED);
201         if (!result) {
202             abortOperation(execution, "Failed to update GenericVnf status to Deactivated as there"
203                     + "is no GenericVnf Found in AAI of ID: " + asInstId);
204         }
205         logger.info("Finished updating vnf status to Deactivated");
206     }
207
208 }