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