Fixing SQL deadlock issue
[so.git] / so-etsi-nfvo / so-etsi-nfvo-ns-lcm / so-etsi-nfvo-ns-lcm-bpmn-flows / src / main / java / org / onap / so / etsi / nfvo / ns / lcm / bpmn / flows / tasks / TerminateVnfTask.java
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 package org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.tasks;
21
22 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.DELETE_VNF_RESPONSE_PARAM_NAME;
23 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.NF_INST_ID_PARAM_NAME;
24 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.TERMINATE_VNF_VNFID_PARAM_NAME;
25 import static org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum.ERROR;
26 import static org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum.FINISHED;
27 import static org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum.IN_PROGRESS;
28 import java.util.Optional;
29 import org.camunda.bpm.engine.delegate.DelegateExecution;
30 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.DeleteVnfResponse;
31 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.aai.AaiServiceProvider;
32 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.vnfm.Sol003AdapterServiceProvider;
33 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum;
34 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoNfInst;
35 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.State;
36 import org.onap.so.etsi.nfvo.ns.lcm.database.service.DatabaseServiceProvider;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41
42 /**
43  * @author Andrew Lamb (andrew.a.lamb@est.tech)
44  */
45 @Component
46 public class TerminateVnfTask extends AbstractNetworkServiceTask {
47     private static final Logger logger = LoggerFactory.getLogger(TerminateVnfTask.class);
48     private final AaiServiceProvider aaiServiceProvider;
49     private final Sol003AdapterServiceProvider sol003AdapterServiceProvider;
50
51     @Autowired
52     public TerminateVnfTask(final DatabaseServiceProvider databaseServiceProvider,
53             final AaiServiceProvider aaiServiceProvider,
54             final Sol003AdapterServiceProvider sol003AdapterServiceProvider) {
55         super(databaseServiceProvider);
56         this.aaiServiceProvider = aaiServiceProvider;
57         this.sol003AdapterServiceProvider = sol003AdapterServiceProvider;
58     }
59
60     public void checkIfNfInstanceExistsInDb(final DelegateExecution execution) {
61         logger.info("Executing checkIfNfInstanceInDb");
62         final String vnfId = (String) execution.getVariable(TERMINATE_VNF_VNFID_PARAM_NAME);
63         logger.info("vnfId: {}", vnfId);
64         execution.setVariable(NF_INST_ID_PARAM_NAME, vnfId);
65
66         addJobStatus(execution, JobStatusEnum.IN_PROGRESS,
67                 "Checking if VNF Instance with id: " + vnfId + " exists in database.");
68         if (!databaseServiceProvider.isNfInstExists(vnfId)) {
69             abortOperation(execution,
70                     "VNF instance with id: " + vnfId + " does not exist in database, so will not be terminated.");
71         }
72         logger.info("Finished executing checkIfNfInstanceInDb  ...");
73
74     }
75
76     public void invokeTerminateRequest(final DelegateExecution execution) {
77         logger.info("Executing invokeTerminateRequest");
78         final String vnfId = (String) execution.getVariable(TERMINATE_VNF_VNFID_PARAM_NAME);
79
80         try {
81             addJobStatus(execution, IN_PROGRESS, "Invoking SOL003 adapter for terminating VNF with vnfId: " + vnfId);
82
83             final Optional<DeleteVnfResponse> optional = sol003AdapterServiceProvider.invokeTerminationRequest(vnfId);
84
85             if (optional.isEmpty()) {
86                 final String errorMessage = "Unexpected error while processing terminate request for vnfId: " + vnfId;
87                 logger.error(errorMessage);
88                 abortOperation(execution, errorMessage);
89             }
90
91             final DeleteVnfResponse vnfResponse = optional.get();
92
93             logger.info("Vnf delete response: {}", vnfResponse);
94             execution.setVariable(DELETE_VNF_RESPONSE_PARAM_NAME, vnfResponse);
95             addJobStatus(execution, IN_PROGRESS, "Successfully invoked SOL003 adapter terminate VNF with vnfId: "
96                     + vnfId + " DeleteVnfResponse Job Id: " + vnfResponse.getJobId());
97             logger.debug("Finished executing invokeTerminateRequest ...");
98         } catch (final Exception exception) {
99             final String message = "Unable to invoke terminate request for vnfId: " + vnfId;
100             logger.error(message, exception);
101             abortOperation(execution, message);
102         }
103
104     }
105
106     public void deleteGenericVnfFromAai(final DelegateExecution execution) {
107         logger.info("Executing deleteGenericVnfFromAai");
108         final String vnfId = (String) execution.getVariable(TERMINATE_VNF_VNFID_PARAM_NAME);
109
110         try {
111             addJobStatus(execution, IN_PROGRESS, "Deleting GenericVnf record from AAI for vnfId: " + vnfId);
112             aaiServiceProvider.deleteGenericVnf(vnfId);
113
114         } catch (final Exception exception) {
115             final String message = "Unable to Delete GenericVnf from AAI for vnfId: " + vnfId;
116             logger.error(message, exception);
117             abortOperation(execution, message);
118         }
119
120         logger.info("Finished executing deleteGenericVnfFromAai ...");
121     }
122
123     public void deleteNfInstanceFromDb(final DelegateExecution execution) {
124         logger.info("Executing deleteNfInstanceFromDb");
125         final String vnfId = (String) execution.getVariable(TERMINATE_VNF_VNFID_PARAM_NAME);
126
127         addJobStatus(execution, IN_PROGRESS, "Deleting NF Instance record from Database for vnfId: " + vnfId);
128         databaseServiceProvider.deleteNfvoNfInst(vnfId);
129
130         addJobStatus(execution, FINISHED, "Successfully finished terminating VNF with vnfId: " + vnfId);
131         logger.info("Finished executing deleteNfInstanceFromDb ...");
132     }
133
134     public void updateNfInstanceStatusToTerminating(final DelegateExecution execution) {
135         logger.info("Executing updateNfInstanceStatusToTerminating");
136
137         updateNfInstanceStatus(execution, State.TERMINATING);
138         final String vnfId = (String) execution.getVariable(TERMINATE_VNF_VNFID_PARAM_NAME);
139         addJobStatus(execution, IN_PROGRESS,
140                 "Terminating VNF with vnfId: " + vnfId + " will set status to " + State.TERMINATING);
141
142         logger.info("Finished executing updateNfInstanceStatusToTerminating  ...");
143
144     }
145
146     public void updateNfInstanceStatusToNotInstantiated(final DelegateExecution execution) {
147         logger.info("Executing updateNfInstanceStatusToNotInstantiated");
148
149         updateNfInstanceStatus(execution, State.NOT_INSTANTIATED);
150         final String vnfId = (String) execution.getVariable(TERMINATE_VNF_VNFID_PARAM_NAME);
151         addJobStatus(execution, IN_PROGRESS,
152                 "Successfully terminated VNF with vnfId: " + vnfId + " will set status to " + State.NOT_INSTANTIATED);
153
154         logger.info("Finished executing updateNfInstanceStatusToInstantiated  ...");
155
156     }
157
158     public void updateNfInstanceStatusToFailed(final DelegateExecution execution) {
159         logger.info("Executing updateNfInstanceStatusToFailed");
160
161         updateNfInstanceStatus(execution, State.FAILED);
162         final String vnfId = (String) execution.getVariable(TERMINATE_VNF_VNFID_PARAM_NAME);
163         addJobStatus(execution, ERROR,
164                 "Failed to terminate VNF with vnfId: " + vnfId + " will set status to " + State.FAILED);
165
166         logger.info("Finished executing updateNfInstanceStatusToFailed  ...");
167
168     }
169
170     private void updateNfInstanceStatus(final DelegateExecution execution, final State vnfStatus) {
171         final String vnfId = (String) execution.getVariable(TERMINATE_VNF_VNFID_PARAM_NAME);
172
173         final Optional<NfvoNfInst> optional = databaseServiceProvider.getNfvoNfInst(vnfId);
174         if (optional.isEmpty()) {
175             final String message = "Unable to find NfvoNfInst record in database using vnfId: " + vnfId;
176             logger.error(message);
177             abortOperation(execution, message);
178         }
179
180         final NfvoNfInst nfvoNfInst = optional.get();
181         nfvoNfInst.setStatus(vnfStatus);
182         databaseServiceProvider.saveNfvoNfInst(nfvoNfInst);
183     }
184
185 }