Terminate NS Backend Service
[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 / TerminateNsTask.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 org.camunda.bpm.engine.delegate.DelegateExecution;
23 import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants;
24 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoNfInst;
25 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoNsInst;
26 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.State;
27 import org.onap.so.etsi.nfvo.ns.lcm.database.service.DatabaseServiceProvider;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Component;
32 import java.util.ArrayList;
33 import java.util.List;
34 import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.NS_INSTANCE_ID_PARAM_NAME;
35 import static org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum.FINISHED;
36 import static org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum.IN_PROGRESS;
37 import static org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum.STARTED;
38
39 /**
40  * @author Andrew Lamb (andrew.a.lamb@est.tech)
41  *
42  */
43 @Component
44 public class TerminateNsTask extends AbstractNetworkServiceTask {
45
46     private static final String IS_NS_TERMINATION_SUCCESSFUL_PARAM_NAME = "isNsTerminationSuccessful";
47     private static final Logger logger = LoggerFactory.getLogger(TerminateNsTask.class);
48
49     @Autowired
50     public TerminateNsTask(final DatabaseServiceProvider databaseServiceProvider) {
51         super(databaseServiceProvider);
52     }
53
54     public void setJobStatusToStarted(final DelegateExecution execution) {
55         setJobStatus(execution, STARTED, "Terminate NS workflow process started");
56     }
57
58     public void setJobStatusToFinished(final DelegateExecution execution) {
59         setJobStatus(execution, FINISHED, "Terminate NS workflow process finished");
60     }
61
62     public void setJobStatusToError(final DelegateExecution execution) {
63         updateNsInstanceStatus(execution, State.FAILED);
64         setJobStatusToError(execution, "Terminate NS workflow process failed");
65     }
66
67     public void updateNsInstanceStatusToTerminating(final DelegateExecution execution) {
68         logger.info("Executing updateNsInstanceStatusToTerminating");
69         setJobStatus(execution, IN_PROGRESS, "Updating NfvoNsInst Status to " + State.TERMINATING);
70         updateNsInstanceStatus(execution, State.TERMINATING);
71         logger.info("Finished executing updateNsInstanceStatusToTerminating  ...");
72     }
73
74     public void updateNsInstanceStatusToNotInstantiated(final DelegateExecution execution) {
75         logger.info("Executing updateNsInstanceStatusToNotInstantiated");
76         setJobStatus(execution, IN_PROGRESS, "Updating NfvoNsInst Status to " + State.NOT_INSTANTIATED);
77         updateNsInstanceStatus(execution, State.NOT_INSTANTIATED);
78         logger.info("Finished executing updateNsInstanceStatusToNotInstantiated  ...");
79     }
80
81     public void getVnfIdsInNs(final DelegateExecution execution) {
82         logger.info("Executing getVnfIdsInNs ...");
83         setJobStatus(execution, IN_PROGRESS, "Getting Each VnfId In Ns");
84         final List<String> nfvoNfInstIds = getNfvoNfInstIds(execution);
85         execution.setVariable(CamundaVariableNameConstants.NFVO_NF_INST_IDS_PARAM_NAME, nfvoNfInstIds);
86         logger.info("Finished executing getVnfIdsInNs ...");
87     }
88
89     public void checkIfVnfTerminationWasSuccessful(final DelegateExecution execution) {
90         logger.info("Executing checkIfVnfTerminationWasSuccessful");
91
92         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
93         final List<NfvoNfInst> nfInstances = databaseServiceProvider.getNfvoNfInstByNsInstId(nsInstId);
94
95         if ((nfInstances != null) && !(nfInstances.isEmpty())) {
96             final String message = "Found NF Instances";
97             nfInstances.stream().forEach(instance -> {
98                 logger.error("VNF : {} {} termination failed", instance.getNfInstId(), instance.getName());
99                 execution.setVariable(IS_NS_TERMINATION_SUCCESSFUL_PARAM_NAME, false);
100             });
101             abortOperation(execution, message);
102         }
103
104         execution.setVariable(IS_NS_TERMINATION_SUCCESSFUL_PARAM_NAME, true);
105         logger.info("Finished executing checkIfVnfTerminationWasSuccessful");
106     }
107
108     public void logTimeOut(final DelegateExecution execution) {
109         logger.error("Vnf termination timedOut ...");
110         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
111         final List<NfvoNfInst> nfInstances = databaseServiceProvider.getNfvoNfInstByNsInstId(nsInstId);
112         if (nfInstances != null) {
113             nfInstances.stream().forEach(instance -> {
114                 logger.info("Current status {} of vnf: {}", instance.getStatus(), instance.getName());
115             });
116         }
117     }
118
119     private void updateNsInstanceStatus(final DelegateExecution execution, final State nsStatus) {
120         final NfvoNsInst nfvoNsInst = getNfvoNsInst(execution);
121         logger.info("Updating NfvoNsInst Status to {} and saving to DB", nsStatus);
122         nfvoNsInst.setStatus(nsStatus);
123         databaseServiceProvider.saveNfvoNsInst(nfvoNsInst);
124     }
125
126     private List<String> getNfvoNfInstIds(final DelegateExecution execution) {
127         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
128         logger.info("Getting NfvoNfInstList using nsInstId: {}", nsInstId);
129         final List<NfvoNfInst> nfvoNfInstList = databaseServiceProvider.getNfvoNfInstByNsInstId(nsInstId);
130
131         if (nfvoNfInstList.size() == 0) {
132             final String message = "Unable to find NF Instances in database using id: " + nsInstId;
133             abortOperation(execution, message);
134         }
135
136         final List<String> vnfIdsList = new ArrayList<>();
137
138         nfvoNfInstList.stream().forEach(nfvoNfInst -> {
139             vnfIdsList.add(nfvoNfInst.getNfInstId());
140         });
141
142         return vnfIdsList;
143     }
144
145 }