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
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=========================================================
20 package org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.tasks;
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;
42 * @author Andrew Lamb (andrew.a.lamb@est.tech)
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;
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;
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);
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.");
71 logger.info("Finished executing checkIfNfInstanceInDb ...");
75 public void invokeTerminateRequest(final DelegateExecution execution) {
76 logger.info("Executing invokeTerminateRequest");
77 final String vnfId = (String) execution.getVariable(TERMINATE_VNF_VNFID_PARAM_NAME);
80 addJobStatus(execution, IN_PROGRESS, "Invoking SOL003 adapter for terminating VNF with vnfId: " + vnfId);
82 final Optional<DeleteVnfResponse> optional = sol003AdapterServiceProvider.invokeTerminationRequest(vnfId);
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);
90 final DeleteVnfResponse vnfResponse = optional.get();
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);
105 public void deleteGenericVnfFromAai(final DelegateExecution execution) {
106 logger.info("Executing deleteGenericVnfFromAai");
107 final String vnfId = (String) execution.getVariable(TERMINATE_VNF_VNFID_PARAM_NAME);
110 addJobStatus(execution, IN_PROGRESS, "Deleting GenericVnf record from AAI for vnfId: " + vnfId);
111 aaiServiceProvider.deleteGenericVnf(vnfId);
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);
119 logger.info("Finished executing deleteGenericVnfFromAai ...");
122 public void deleteNfInstanceFromDb(final DelegateExecution execution) {
123 logger.info("Executing deleteNfInstanceFromDb");
124 final String vnfId = (String) execution.getVariable(TERMINATE_VNF_VNFID_PARAM_NAME);
126 addJobStatus(execution, IN_PROGRESS, "Deleting NF Instance record from Database for vnfId: " + vnfId);
127 databaseServiceProvider.deleteNfvoNfInst(vnfId);
129 addJobStatus(execution, FINISHED, "Successfully finished terminating VNF with vnfId: " + vnfId);
130 logger.info("Finished executing deleteNfInstanceFromDb ...");
133 public void updateNfInstanceStatusToTerminating(final DelegateExecution execution) {
134 logger.info("Executing updateNfInstanceStatusToTerminating");
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);
141 logger.info("Finished executing updateNfInstanceStatusToTerminating ...");
145 public void updateNfInstanceStatusToNotInstantiated(final DelegateExecution execution) {
146 logger.info("Executing updateNfInstanceStatusToNotInstantiated");
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);
153 logger.info("Finished executing updateNfInstanceStatusToInstantiated ...");
157 public void updateNfInstanceStatusToFailed(final DelegateExecution execution) {
158 logger.info("Executing updateNfInstanceStatusToFailed");
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);
165 logger.info("Finished executing updateNfInstanceStatusToFailed ...");
169 private void updateNfInstanceStatus(final DelegateExecution execution, final State vnfStatus) {
170 final String vnfId = (String) execution.getVariable(TERMINATE_VNF_VNFID_PARAM_NAME);
172 final boolean isSuccessful = databaseServiceProvider.updateNfInstState(vnfId, vnfStatus);
174 final String message = "Unable to update NfvoNfInst " + vnfId + " status to" + vnfStatus + " in database";
175 logger.error(message);
176 abortOperation(execution, message);