54b87ec2fe6cb4e4221a572634fb6de548e7c6a6
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Ericsson. All rights reserved.
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.bpmn.infrastructure.adapter.cnfm.tasks;
21
22 import static org.onap.so.bpmn.servicedecomposition.entities.ResourceKey.GENERIC_VNF_ID;
23
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 /**
41  * This class performs CNF Delete
42  *
43  * @author raviteja.karumuri@est.tech
44  */
45 @Component
46 public class CnfDeleteTask {
47     private static final Logger LOGGER = LoggerFactory.getLogger(CnfDeleteTask.class);
48     private final ExceptionBuilder exceptionUtil;
49     private final CnfmHttpServiceProvider cnfmHttpServiceProvider;
50     private final ExtractPojosForBB extractPojosForBB;
51     private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
52     private static final String TERMINATE_AS_REQUEST_OBJECT = "TerminateAsRequest";
53     private static final String MONITOR_JOB_NAME = "MonitorJobName";
54
55     @Autowired
56     public CnfDeleteTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider, final ExceptionBuilder exceptionUtil,
57             ExtractPojosForBB extractPojosForBB) {
58         this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
59         this.exceptionUtil = exceptionUtil;
60         this.extractPojosForBB = extractPojosForBB;
61     }
62
63     public void createTerminateAsRequest(final BuildingBlockExecution execution) {
64         try {
65             LOGGER.debug("Executing createTerminateAsRequest task  ...");
66
67             final TerminateAsRequest terminateAsRequest = new TerminateAsRequest();
68             terminateAsRequest.setTerminationType(TerminateAsRequest.TerminationTypeEnum.GRACEFUL);
69             terminateAsRequest.setGracefulTerminationTimeout(0);
70             terminateAsRequest.setAdditionalParams(new HashMap<>());
71
72             LOGGER.debug("Adding TerminateAsRequest to execution {}", terminateAsRequest);
73
74             execution.setVariable(TERMINATE_AS_REQUEST_OBJECT, terminateAsRequest);
75             LOGGER.debug("Finished executing terminateAsRequest task ...");
76
77         } catch (final Exception exception) {
78             LOGGER.error("Unable to create TerminateAsRequest", exception);
79             exceptionUtil.buildAndThrowWorkflowException(execution, 2001, exception);
80         }
81     }
82
83     public void invokeCnfmToTerminateAsInstance(final BuildingBlockExecution execution) {
84         try {
85             LOGGER.debug("Executing TerminateAsInstance task  ...");
86
87             final TerminateAsRequest terminateAsRequest = execution.getVariable(TERMINATE_AS_REQUEST_OBJECT);
88             final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
89             final String asInstanceId = vnf.getVnfId();
90
91             Optional<URI> terminateStatusCheck =
92                     cnfmHttpServiceProvider.invokeTerminateAsRequest(asInstanceId, terminateAsRequest);
93             execution.setVariable(CNFM_REQUEST_STATUS_CHECK_URL,
94                     terminateStatusCheck.orElseThrow(() -> new NoSuchElementException("Status check url Not found")));
95             execution.setVariable(MONITOR_JOB_NAME, "Terminate");
96             LOGGER.debug("Successfully invoked CNFM terminate AS request: {}", asInstanceId);
97
98         } catch (final Exception exception) {
99             LOGGER.error("Unable to invoke CNFM TerminateAsRequest", exception);
100             exceptionUtil.buildAndThrowWorkflowException(execution, 2004, exception);
101         }
102     }
103
104     public void invokeCnfmToDeleteAsInstance(final BuildingBlockExecution execution) {
105         try {
106             LOGGER.debug("Executing DelteAsInstance task  ...");
107
108             final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
109             final String asInstanceId = vnf.getVnfId();
110
111             Optional<Boolean> response = cnfmHttpServiceProvider.invokeDeleteAsRequest(asInstanceId);
112             if (Boolean.TRUE.equals(response
113                     .orElseThrow(() -> new BpmnError("Unable to complete DeleteAsRequest of ID: " + asInstanceId)))) {
114                 LOGGER.debug("Successfully invoked CNFM delete AS request with ID: {}", asInstanceId);
115             }
116
117         } catch (final Exception exception) {
118             LOGGER.error("Unable to invoke CNFM DeleteAsRequest", exception);
119             exceptionUtil.buildAndThrowWorkflowException(execution, 2004, exception);
120         }
121     }
122 }