aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-tasks/src/main/java/org/onap/so/bpmn/infrastructure/adapter/cnfm/tasks/CnfDeleteTask.java
blob: 54b87ec2fe6cb4e4221a572634fb6de548e7c6a6 (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2022 Ericsson. All rights reserved.
 * ================================================================================
 * 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.bpmn.infrastructure.adapter.cnfm.tasks;

import static org.onap.so.bpmn.servicedecomposition.entities.ResourceKey.GENERIC_VNF_ID;

import java.net.URI;
import java.util.HashMap;
import java.util.NoSuchElementException;
import java.util.Optional;
import org.camunda.bpm.engine.delegate.BpmnError;
import org.onap.so.bpmn.common.BuildingBlockExecution;
import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
import org.onap.so.client.exception.ExceptionBuilder;
import org.onap.so.cnfm.lcm.model.TerminateAsRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * This class performs CNF Delete
 *
 * @author raviteja.karumuri@est.tech
 */
@Component
public class CnfDeleteTask {
    private static final Logger LOGGER = LoggerFactory.getLogger(CnfDeleteTask.class);
    private final ExceptionBuilder exceptionUtil;
    private final CnfmHttpServiceProvider cnfmHttpServiceProvider;
    private final ExtractPojosForBB extractPojosForBB;
    private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
    private static final String TERMINATE_AS_REQUEST_OBJECT = "TerminateAsRequest";
    private static final String MONITOR_JOB_NAME = "MonitorJobName";

    @Autowired
    public CnfDeleteTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider, final ExceptionBuilder exceptionUtil,
            ExtractPojosForBB extractPojosForBB) {
        this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
        this.exceptionUtil = exceptionUtil;
        this.extractPojosForBB = extractPojosForBB;
    }

    public void createTerminateAsRequest(final BuildingBlockExecution execution) {
        try {
            LOGGER.debug("Executing createTerminateAsRequest task  ...");

            final TerminateAsRequest terminateAsRequest = new TerminateAsRequest();
            terminateAsRequest.setTerminationType(TerminateAsRequest.TerminationTypeEnum.GRACEFUL);
            terminateAsRequest.setGracefulTerminationTimeout(0);
            terminateAsRequest.setAdditionalParams(new HashMap<>());

            LOGGER.debug("Adding TerminateAsRequest to execution {}", terminateAsRequest);

            execution.setVariable(TERMINATE_AS_REQUEST_OBJECT, terminateAsRequest);
            LOGGER.debug("Finished executing terminateAsRequest task ...");

        } catch (final Exception exception) {
            LOGGER.error("Unable to create TerminateAsRequest", exception);
            exceptionUtil.buildAndThrowWorkflowException(execution, 2001, exception);
        }
    }

    public void invokeCnfmToTerminateAsInstance(final BuildingBlockExecution execution) {
        try {
            LOGGER.debug("Executing TerminateAsInstance task  ...");

            final TerminateAsRequest terminateAsRequest = execution.getVariable(TERMINATE_AS_REQUEST_OBJECT);
            final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
            final String asInstanceId = vnf.getVnfId();

            Optional<URI> terminateStatusCheck =
                    cnfmHttpServiceProvider.invokeTerminateAsRequest(asInstanceId, terminateAsRequest);
            execution.setVariable(CNFM_REQUEST_STATUS_CHECK_URL,
                    terminateStatusCheck.orElseThrow(() -> new NoSuchElementException("Status check url Not found")));
            execution.setVariable(MONITOR_JOB_NAME, "Terminate");
            LOGGER.debug("Successfully invoked CNFM terminate AS request: {}", asInstanceId);

        } catch (final Exception exception) {
            LOGGER.error("Unable to invoke CNFM TerminateAsRequest", exception);
            exceptionUtil.buildAndThrowWorkflowException(execution, 2004, exception);
        }
    }

    public void invokeCnfmToDeleteAsInstance(final BuildingBlockExecution execution) {
        try {
            LOGGER.debug("Executing DelteAsInstance task  ...");

            final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
            final String asInstanceId = vnf.getVnfId();

            Optional<Boolean> response = cnfmHttpServiceProvider.invokeDeleteAsRequest(asInstanceId);
            if (Boolean.TRUE.equals(response
                    .orElseThrow(() -> new BpmnError("Unable to complete DeleteAsRequest of ID: " + asInstanceId)))) {
                LOGGER.debug("Successfully invoked CNFM delete AS request with ID: {}", asInstanceId);
            }

        } catch (final Exception exception) {
            LOGGER.error("Unable to invoke CNFM DeleteAsRequest", exception);
            exceptionUtil.buildAndThrowWorkflowException(execution, 2004, exception);
        }
    }
}