aboutsummaryrefslogtreecommitdiffstats
path: root/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
blob: cd711fb3e8ca16e1645341b0b8559c40bb140594 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */
package org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.tasks;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.extclients.aai.AaiServiceProvider;
import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoNsInst;
import org.onap.so.etsi.nfvo.ns.lcm.database.beans.State;
import org.onap.so.etsi.nfvo.ns.lcm.database.service.DatabaseServiceProvider;
import org.onap.so.etsi.nfvo.ns.lcm.model.InlineResponse400;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Optional;
import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.NS_INSTANCE_ID_PARAM_NAME;
import static org.onap.so.etsi.nfvo.ns.lcm.bpmn.flows.CamundaVariableNameConstants.NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME;
import static org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum.FINISHED;
import static org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum.IN_PROGRESS;
import static org.onap.so.etsi.nfvo.ns.lcm.database.beans.JobStatusEnum.STARTED;
import static org.onap.so.etsi.nfvo.ns.lcm.database.beans.State.NOT_INSTANTIATED;

/**
 * @author Andrew Lamb (andrew.a.lamb@est.tech)
 *
 */
@Component
public class DeleteNsTask extends AbstractNetworkServiceTask {

    private static final String NS_INSTANCE_EXISTS_PARAM_NAME = "nsInstanceExists";
    private static final String NS_INSTANCE_IS_IN_NOT_INSTANTIATED_STATE_PARAM_NAME = "isInNotInstantiatedState";
    private static final Logger logger = LoggerFactory.getLogger(DeleteNsTask.class);
    private final AaiServiceProvider aaiServiceProvider;

    @Autowired
    public DeleteNsTask(final DatabaseServiceProvider databaseServiceProvider,
            final AaiServiceProvider aaiServiceProvider) {
        super(databaseServiceProvider);
        this.aaiServiceProvider = aaiServiceProvider;
    }

    public void setJobStatusToStarted(final DelegateExecution execution) {
        setJobStatus(execution, STARTED, "Delete NS workflow process started");
    }

    public void setJobStatusToFinished(final DelegateExecution execution) {
        setJobStatus(execution, FINISHED, "Delete NS workflow process finished");
    }

    public void setJobStatusInProgress(final DelegateExecution execution, final String message) {
        setJobStatus(execution, IN_PROGRESS, message);
    }

    public void setJobStatusToError(final DelegateExecution execution) {
        setJobStatusToError(execution, "Delete NS workflow process failed");
    }

    public void checkIfNsInstanceExistsInDb(final DelegateExecution execution) {
        logger.info("Executing checkIfNsInstanceExistsInDb  ...");
        setJobStatusInProgress(execution, "Checking that NS Instance Exists in DB");

        final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
        final Optional<NfvoNsInst> optionalNfvoNsInst = databaseServiceProvider.getNfvoNsInst(nsInstId);
        final boolean nsInstanceExists = optionalNfvoNsInst.isPresent();
        logger.info("NS Instance entry with id: {} {} exist in database", nsInstId,
                nsInstanceExists ? "does" : "doesn't");
        execution.setVariable(NS_INSTANCE_EXISTS_PARAM_NAME, nsInstanceExists);

        if (!nsInstanceExists) {
            final String message =
                    "NS Instance with id: " + nsInstId + " does not exist in database, so will not be deleted.";
            logger.info(message);
            execution.setVariable(NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, new InlineResponse400().detail(message));
        }

        logger.info("Finished executing checkIfNsInstanceExistsInDb ...");
    }

    public void checkthatNsInstanceInDbIsInNotInstantiatedState(final DelegateExecution execution) {
        logger.info("Executing checkthatNsInstanceInDbIsInNotInstantiatedState ...");
        setJobStatusInProgress(execution, "Checking that NS Instance is in NOT_INSTANTIATED state in Db");

        final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
        final NfvoNsInst nfvoNsInst = getNfvoNsInst(execution, nsInstId);
        final State nfvoNsInstState = nfvoNsInst.getStatus();
        final boolean nsInstanceIsNotInstantiated = NOT_INSTANTIATED.equals(nfvoNsInstState);
        logger.info("Ns Instance entry with nsInstId: {} is in state: {}", nsInstId, nfvoNsInstState);
        execution.setVariable(NS_INSTANCE_IS_IN_NOT_INSTANTIATED_STATE_PARAM_NAME, nsInstanceIsNotInstantiated);

        if (!nsInstanceIsNotInstantiated) {
            final String message =
                    "Cannot Delete NS Instance with id: " + nsInstId + " in the state: " + nfvoNsInstState;
            logger.info(message);
            execution.setVariable(NS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, new InlineResponse400().detail(message));
        }

        logger.info("Finished executing checkthatNsInstanceInDbIsInNotInstantiatedState ...");
    }

    public void deleteNsInstanceFromAai(final DelegateExecution execution) {
        logger.info("Executing deleteNsInstanceFromAAI ...");
        setJobStatusInProgress(execution, "Deleting NS Instance from AAI");

        final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
        final NfvoNsInst nfvoNsInst = getNfvoNsInst(execution, nsInstId);
        final String globalCustomerId = nfvoNsInst.getGlobalCustomerId();
        final String serviceType = nfvoNsInst.getServiceType();

        aaiServiceProvider.deleteServiceInstance(globalCustomerId, serviceType, nsInstId);

        logger.info("Finished executing deleteNsInstanceFromAAI ...");
    }

    public void deleteNsInstanceFromDb(final DelegateExecution execution) {
        logger.info("Executing deleteNsInstanceFromDb ...");
        setJobStatusInProgress(execution, "Deleting NS Instance from Db");

        final String nsInstId = (String) execution.getVariable(NS_INSTANCE_ID_PARAM_NAME);
        databaseServiceProvider.deleteNfvoNsInst(nsInstId);

        logger.info("Finished executing deleteNsInstanceFromDb ...");
    }
}