[SO] SO changes to support Delete AS
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / cnfm / tasks / CnfDeleteTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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
21 package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks;
22
23 import static org.onap.so.bpmn.servicedecomposition.entities.ResourceKey.GENERIC_VNF_ID;
24 import java.net.URI;
25 import java.util.HashMap;
26 import java.util.NoSuchElementException;
27 import java.util.Optional;
28 import org.camunda.bpm.engine.delegate.BpmnError;
29 import org.onap.so.bpmn.common.BuildingBlockExecution;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
31 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
32 import org.onap.so.client.exception.ExceptionBuilder;
33 import org.onap.so.cnfm.lcm.model.TerminateAsRequest;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
38
39 /**
40  * This class performs CNF Delete
41  *
42  * @author Raviteja Karumuri (raviteja.karumuri@est.tech)
43  */
44 @Component
45 public class CnfDeleteTask {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(CnfInstantiateTask.class);
48     private final ExceptionBuilder exceptionUtil;
49     private final CnfmHttpServiceProvider cnfmHttpServiceProvider;
50     private final ExtractPojosForBB extractPojosForBB;
51     private static final String MONITOR_JOB_NAME = "MonitorJobName";
52     private static final String AS_INSTANCE_ID = "asInstanceid";
53     private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
54     private static final String TERMINATE_AS_REQUEST_OBJECT = "TerminateAsRequest";
55
56     @Autowired
57     public CnfDeleteTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider, final ExceptionBuilder exceptionUtil,
58             ExtractPojosForBB extractPojosForBB) {
59         this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
60         this.exceptionUtil = exceptionUtil;
61         this.extractPojosForBB = extractPojosForBB;
62     }
63
64     public void createTerminateAsRequest(final BuildingBlockExecution execution) {
65         try {
66             LOGGER.debug("Executing createTerminateAsRequest task  ...");
67
68             final TerminateAsRequest terminateAsRequest = new TerminateAsRequest();
69             terminateAsRequest.setTerminationType(TerminateAsRequest.TerminationTypeEnum.GRACEFUL);
70             terminateAsRequest.setGracefulTerminationTimeout(0);
71             terminateAsRequest.setAdditionalParams(new HashMap<>());
72
73             LOGGER.debug("Adding TerminateAsRequest to execution {}", terminateAsRequest);
74
75             execution.setVariable(TERMINATE_AS_REQUEST_OBJECT, terminateAsRequest);
76             LOGGER.debug("Finished executing terminateAsRequest task ...");
77
78         } catch (final Exception exception) {
79             LOGGER.error("Unable to create TerminateAsRequest", exception);
80             exceptionUtil.buildAndThrowWorkflowException(execution, 2001, exception);
81         }
82     }
83
84     public void invokeCnfmToTerminateAsInstance(final BuildingBlockExecution execution) {
85         try {
86             LOGGER.debug("Executing TerminateAsInstance task  ...");
87
88             final TerminateAsRequest terminateAsRequest = execution.getVariable(TERMINATE_AS_REQUEST_OBJECT);
89             final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
90             final String asInstanceId = vnf.getVnfId();
91
92             Optional<URI> terminateStatusCheck =
93                     cnfmHttpServiceProvider.invokeTerminateAsRequest(asInstanceId, terminateAsRequest);
94             execution.setVariable(CNFM_REQUEST_STATUS_CHECK_URL,
95                     terminateStatusCheck.orElseThrow(() -> new NoSuchElementException("Status check url Not found")));
96             execution.setVariable(MONITOR_JOB_NAME, "Terminate");
97             LOGGER.debug("Successfully invoked CNFM terminate AS request: {}", asInstanceId);
98
99         } catch (final Exception exception) {
100             LOGGER.error("Unable to invoke CNFM TerminateAsRequest", exception);
101             exceptionUtil.buildAndThrowWorkflowException(execution, 2002, exception);
102         }
103     }
104
105     public void invokeCnfmToDeleteAsInstance(final BuildingBlockExecution execution) {
106         try {
107             LOGGER.debug("Executing DeleteAsInstance task  ...");
108
109             final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
110             final String asInstanceId = vnf.getVnfId();
111
112             Optional<Boolean> response = cnfmHttpServiceProvider.invokeDeleteAsRequest(asInstanceId);
113             if (Boolean.TRUE.equals(response
114                     .orElseThrow(() -> new BpmnError("Unable to complete DeleteAsRequest of ID: " + asInstanceId)))) {
115                 LOGGER.debug("Successfully invoked CNFM delete AS request with ID: {}", asInstanceId);
116             }
117
118         } catch (final Exception exception) {
119             LOGGER.error("Unable to invoke CNFM DeleteAsRequest", exception);
120             exceptionUtil.buildAndThrowWorkflowException(execution, 2003, exception);
121         }
122     }
123 }