Refactor SOL003 Adapter to organize its modules
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / vnfm / tasks / EtsiVnfDeleteTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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.vnfm.tasks;
22
23 import static org.onap.so.bpmn.servicedecomposition.entities.ResourceKey.GENERIC_VNF_ID;
24 import org.onap.so.adapters.etsisol003adapter.lcm.v1.model.DeleteVnfResponse;
25 import org.onap.so.bpmn.common.BuildingBlockExecution;
26 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
27 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
28 import org.onap.so.client.exception.ExceptionBuilder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
33 import com.google.common.base.Optional;
34
35 /**
36  *
37  * @author Lathishbabu Ganesan (lathishbabu.ganesan@est.tech)
38  */
39 @Component
40 public class EtsiVnfDeleteTask {
41
42     private static final Logger LOGGER = LoggerFactory.getLogger(EtsiVnfDeleteTask.class);
43     private final ExtractPojosForBB extractPojosForBB;
44     private final ExceptionBuilder exceptionUtil;
45     private final VnfmAdapterServiceProvider vnfmAdapterServiceProvider;
46
47     @Autowired
48     public EtsiVnfDeleteTask(final ExceptionBuilder exceptionUtil, final ExtractPojosForBB extractPojosForBB,
49             final VnfmAdapterServiceProvider vnfmAdapterServiceProvider) {
50         this.exceptionUtil = exceptionUtil;
51         this.extractPojosForBB = extractPojosForBB;
52         this.vnfmAdapterServiceProvider = vnfmAdapterServiceProvider;
53     }
54
55     /**
56      * Invoke VNFM adapter to delete the VNF
57      *
58      * @param execution {@link org.onap.so.bpmn.common.DelegateExecutionImpl}
59      */
60     public void invokeVnfmAdapter(final BuildingBlockExecution execution) {
61         try {
62             LOGGER.debug("Executing invokeVnfmAdapter  ...");
63             final GenericVnf vnf = extractPojosForBB.extractByKey(execution, GENERIC_VNF_ID);
64
65             final Optional<DeleteVnfResponse> response = vnfmAdapterServiceProvider.invokeDeleteRequest(vnf.getVnfId());
66
67             if (!response.isPresent()) {
68                 final String errorMessage = "Unexpected error while processing delete request";
69                 LOGGER.error(errorMessage);
70                 exceptionUtil.buildAndThrowWorkflowException(execution, 1211, errorMessage);
71             }
72
73             final DeleteVnfResponse vnfResponse = response.get();
74
75             LOGGER.debug("Vnf delete response: {}", vnfResponse);
76             execution.setVariable(Constants.DELETE_VNF_RESPONSE_PARAM_NAME, vnfResponse);
77
78             LOGGER.debug("Finished executing invokeVnfmAdapter ...");
79         } catch (final Exception exception) {
80             LOGGER.error("Unable to invoke delete request", exception);
81             exceptionUtil.buildAndThrowWorkflowException(execution, 1212, exception);
82         }
83     }
84 }