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