Merge "Removing so-monitoring module"
[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 / DeleteNsTask.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.extclients.aai.AaiServiceProvider;
24 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoNsInst;
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.onap.so.etsi.nfvo.ns.lcm.model.InlineResponse400;
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.Optional;
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.bpmn.flows.CamundaVariableNameConstants.NS_WORKFLOW_PROCESSING_EXCEPTION_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 import static org.onap.so.etsi.nfvo.ns.lcm.database.beans.State.NOT_INSTANTIATED;
39
40 /**
41  * @author Andrew Lamb (andrew.a.lamb@est.tech)
42  *
43  */
44 @Component
45 public class DeleteNsTask extends AbstractNetworkServiceTask {
46
47     private static final String NS_INSTANCE_EXISTS_PARAM_NAME = "nsInstanceExists";
48     private static final String NS_INSTANCE_IS_IN_NOT_INSTANTIATED_STATE_PARAM_NAME = "isInNotInstantiatedState";
49     private static final Logger logger = LoggerFactory.getLogger(DeleteNsTask.class);
50     private final AaiServiceProvider aaiServiceProvider;
51
52     @Autowired
53     public DeleteNsTask(final DatabaseServiceProvider databaseServiceProvider,
54             final AaiServiceProvider aaiServiceProvider) {
55         super(databaseServiceProvider);
56         this.aaiServiceProvider = aaiServiceProvider;
57     }
58
59     public void setJobStatusToStarted(final DelegateExecution execution) {
60         setJobStatus(execution, STARTED, "Delete NS workflow process started");
61     }
62
63     public void setJobStatusToFinished(final DelegateExecution execution) {
64         setJobStatus(execution, FINISHED, "Delete NS workflow process finished");
65     }
66
67     public void setJobStatusInProgress(final DelegateExecution execution, final String message) {
68         setJobStatus(execution, IN_PROGRESS, message);
69     }
70
71     public void setJobStatusToError(final DelegateExecution execution) {
72         setJobStatusToError(execution, "Delete NS workflow process failed");
73     }
74
75     public void checkIfNsInstanceExistsInDb(final DelegateExecution execution) {
76         logger.info("Executing checkIfNsInstanceExistsInDb  ...");
77         setJobStatusInProgress(execution, "Checking that NS Instance Exists in DB");
78
79         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
80         final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstId);
81         final boolean nsInstanceExists = optionalNfvoNsInst.isPresent();
82         logger.info("NS Instance entry with id: {} {} exist in database", nsInstId,
83                 nsInstanceExists ? "does" : "doesn't");
84         execution.setVariable(NS_INSTANCE_EXISTS_PARAM_NAME, nsInstanceExists);
85
86         if (!nsInstanceExists) {
87             final String message =
88                     "NS Instance with id: " + nsInstId + " does not exist in database, so will not be deleted.";
89             logger.info(message);
90             execution.setVariable(NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, new InlineResponse400().detail(message));
91         }
92
93         logger.info("Finished executing checkIfNsInstanceExistsInDb ...");
94     }
95
96     public void checkthatNsInstanceInDbIsInNotInstantiatedState(final DelegateExecution execution) {
97         logger.info("Executing checkthatNsInstanceInDbIsInNotInstantiatedState ...");
98         setJobStatusInProgress(execution, "Checking that NS Instance is in NOT_INSTANTIATED state in Db");
99
100         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
101         final NfvoNsInst nfvoNsInst = getNfvoNsInst(execution, nsInstId);
102         final State nfvoNsInstState = nfvoNsInst.getStatus();
103         final boolean nsInstanceIsNotInstantiated = NOT_INSTANTIATED.equals(nfvoNsInstState);
104         logger.info("Ns Instance entry with nsInstId: {} is in state: {}", nsInstId, nfvoNsInstState);
105         execution.setVariable(NS_INSTANCE_IS_IN_NOT_INSTANTIATED_STATE_PARAM_NAME, nsInstanceIsNotInstantiated);
106
107         if (!nsInstanceIsNotInstantiated) {
108             final String message =
109                     "Cannot Delete NS Instance with id: " + nsInstId + " in the state: " + nfvoNsInstState;
110             logger.info(message);
111             execution.setVariable(NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, new InlineResponse400().detail(message));
112         }
113
114         logger.info("Finished executing checkthatNsInstanceInDbIsInNotInstantiatedState ...");
115     }
116
117     public void deleteNsInstanceFromAai(final DelegateExecution execution) {
118         logger.info("Executing deleteNsInstanceFromAAI ...");
119         setJobStatusInProgress(execution, "Deleting NS Instance from AAI");
120
121         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
122         final NfvoNsInst nfvoNsInst = getNfvoNsInst(execution, nsInstId);
123         final String globalCustomerId = nfvoNsInst.getGlobalCustomerId();
124         final String serviceType = nfvoNsInst.getServiceType();
125
126         aaiServiceProvider.deleteServiceInstance(globalCustomerId, serviceType, nsInstId);
127
128         logger.info("Finished executing deleteNsInstanceFromAAI ...");
129     }
130
131     public void deleteNsInstanceFromDb(final DelegateExecution execution) {
132         logger.info("Executing deleteNsInstanceFromDb ...");
133         setJobStatusInProgress(execution, "Deleting NS Instance from Db");
134
135         final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
136         databaseServiceProvider.deleteNfvoNsInst(nsInstId);
137
138         logger.info("Finished executing deleteNsInstanceFromDb ...");
139     }
140 }