[SO] SO changes to support Delete AS
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / adapter / cnfm / tasks / CnfInstantiateTask.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 java.util.Objects.isNull;
24 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.CLOUD_OWNER_PARAM_KEY;
25 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.CLOUD_REGION_PARAM_KEY;
26 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.RESOURCE_ID_KEY;
27 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.SERVICE_INSTANCE_ID_PARAM_KEY;
28 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.SERVICE_INSTANCE_NAME_PARAM_KEY;
29 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.TENANT_ID_PARAM_KEY;
30 import java.net.URI;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.NoSuchElementException;
35 import java.util.Optional;
36 import org.apache.groovy.util.Maps;
37 import org.onap.logging.filter.base.ONAPComponents;
38 import org.onap.so.bpmn.common.BuildingBlockExecution;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
40 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
41 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
42 import org.onap.so.client.exception.ExceptionBuilder;
43 import org.onap.so.cnfm.lcm.model.AsInstance;
44 import org.onap.so.cnfm.lcm.model.CreateAsRequest;
45 import org.onap.so.cnfm.lcm.model.InstantiateAsRequest;
46 import org.onap.so.cnfm.lcm.model.AsInfoModificationRequestDeploymentItems;
47 import org.onap.so.serviceinstancebeans.CloudConfiguration;
48 import org.onap.so.serviceinstancebeans.ModelInfo;
49 import org.onap.so.serviceinstancebeans.RequestDetails;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.springframework.beans.factory.annotation.Autowired;
53 import org.springframework.stereotype.Component;
54
55
56 /**
57  * This class performs CNF Instantiation
58  *
59  * @author sagar.shetty@est.tech
60  * @author Waqas Ikram (waqas.ikram@est.tech)
61  * @author Raviteja Karumuri (raviteja.karumuri@est.tech)
62  */
63 @Component
64 public class CnfInstantiateTask {
65     private static final String CREATE_AS_REQUEST_OBJECT = "CreateAsRequestObject";
66     private static final String INSTANTIATE_AS_REQUEST_OBJECT = "InstantiateAsRequest";
67     private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
68     private static final String MONITOR_JOB_NAME = "MonitorJobName";
69     private static final String AS_INSTANCE_ID = "asInstanceid";
70     private static final Logger LOGGER = LoggerFactory.getLogger(CnfInstantiateTask.class);
71     private final ExceptionBuilder exceptionUtil;
72     private final CnfmHttpServiceProvider cnfmHttpServiceProvider;
73
74     @Autowired
75     public CnfInstantiateTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
76             final ExceptionBuilder exceptionUtil) {
77         this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
78         this.exceptionUtil = exceptionUtil;
79     }
80
81     public void createCreateAsRequest(final BuildingBlockExecution execution) {
82         try {
83             LOGGER.debug("Executing createAsRequest task  ...");
84             final ExecuteBuildingBlock executeBuildingBlock =
85                     (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
86
87             final GeneralBuildingBlock generalBuildingBlock = execution.getGeneralBuildingBlock();
88
89             final RequestDetails requestDetails = executeBuildingBlock.getRequestDetails();
90             LOGGER.debug("RequestDetails: {}", requestDetails);
91
92             if (isNull(requestDetails) || isNull(requestDetails.getModelInfo())
93                     || isNull(requestDetails.getRequestInfo())
94                     || isNull(requestDetails.getCloudConfiguration()) && isNull(generalBuildingBlock)) {
95                 LOGGER.error("Missing Mandatory attribute from RequestDetails: {} or GeneralBuildingBlock: {}",
96                         requestDetails, generalBuildingBlock);
97                 exceptionUtil.buildAndThrowWorkflowException(execution, 2000,
98                         "Missing Mandatory attribute from RequestDetails or GeneralBuildingBlock", ONAPComponents.SO);
99             }
100
101             final ModelInfo modelInfo = requestDetails.getModelInfo();
102             final CloudConfiguration cloudConfiguration = requestDetails.getCloudConfiguration();
103             final ServiceInstance serviceInstance = generalBuildingBlock.getServiceInstance();
104             final String resourceId = executeBuildingBlock.getResourceId();
105
106             final CreateAsRequest createAsRequest = new CreateAsRequest().asdId(modelInfo.getModelVersionId())
107                     .asInstanceName(requestDetails.getRequestInfo().getInstanceName())
108                     .additionalParams(Maps.of(CLOUD_OWNER_PARAM_KEY, cloudConfiguration.getCloudOwner(),
109                             CLOUD_REGION_PARAM_KEY, cloudConfiguration.getLcpCloudRegionId(), TENANT_ID_PARAM_KEY,
110                             cloudConfiguration.getTenantId(), SERVICE_INSTANCE_ID_PARAM_KEY,
111                             serviceInstance.getServiceInstanceId(), SERVICE_INSTANCE_NAME_PARAM_KEY,
112                             serviceInstance.getServiceInstanceName(), RESOURCE_ID_KEY, resourceId));
113
114             LOGGER.debug("Adding CreateAsRequest to execution {}", createAsRequest);
115
116             execution.setVariable(CREATE_AS_REQUEST_OBJECT, createAsRequest);
117             LOGGER.debug("Finished executing createAsRequest task ...");
118
119         } catch (final Exception exception) {
120             LOGGER.error("Unable to create CreateAsRequest", exception);
121             exceptionUtil.buildAndThrowWorkflowException(execution, 2001, exception);
122         }
123     }
124
125     public void invokeCnfmWithCreateAsRequest(final BuildingBlockExecution execution) {
126         try {
127             final CreateAsRequest createAsRequest = execution.getVariable(CREATE_AS_REQUEST_OBJECT);
128
129             final Optional<AsInstance> optional = cnfmHttpServiceProvider.invokeCreateAsRequest(createAsRequest);
130
131             if (optional.isEmpty()) {
132                 LOGGER.error("Unable to invoke CNFM for CreateAsRequest : {}", createAsRequest);
133                 exceptionUtil.buildAndThrowWorkflowException(execution, 2003,
134                         "Unable to invoke CNFM for CreateAsRequest", ONAPComponents.SO);
135             }
136
137             final AsInstance asInstance =
138                     optional.orElseThrow(() -> new NoSuchElementException("AsInstance object is empty"));
139             execution.setVariable(AS_INSTANCE_ID, asInstance.getAsInstanceid());
140             LOGGER.debug("Successfully invoked CNFM response: {}", asInstance);
141
142         } catch (final Exception exception) {
143             LOGGER.error("Unable to invoke CNFM AsCreateRequest", exception);
144             exceptionUtil.buildAndThrowWorkflowException(execution, 2004, exception);
145         }
146     }
147
148     public void createAsInstanceRequest(final BuildingBlockExecution execution) {
149         try {
150             LOGGER.debug("Executing createAsInstanceRequest task  ...");
151             final ExecuteBuildingBlock executeBuildingBlock =
152                     (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
153             final RequestDetails requestDetails = executeBuildingBlock.getRequestDetails();
154             final InstantiateAsRequest instantiateAsRequest = new InstantiateAsRequest();
155             if (requestDetails != null && requestDetails.getRequestParameters() != null) {
156                 List<Map<String, Object>> userParams = requestDetails.getRequestParameters().getUserParams();
157                 if (userParams != null && !userParams.isEmpty()) {
158                     List<Object> deploymentItems = new ArrayList<>();
159                     List<AsInfoModificationRequestDeploymentItems> deploymentItemsReq = new ArrayList<>();
160                     for (Map<String, Object> userParam : userParams) {
161                         if (userParam.containsKey("deploymentItems")) {
162                             deploymentItems = (ArrayList<Object>) userParam.get("deploymentItems");
163                             break;
164                         }
165                     }
166                     for (Object deploymentItem : deploymentItems) {
167                         Map<String, Object> deploymentItemMap = (Map<String, Object>) deploymentItem;
168                         AsInfoModificationRequestDeploymentItems item = new AsInfoModificationRequestDeploymentItems();
169                         item.setDeploymentItemsId(deploymentItemMap.get("deploymentItemsId").toString());
170                         item.setLifecycleParameterKeyValues(deploymentItemMap.get("lifecycleParameterKeyValues"));
171                         deploymentItemsReq.add(item);
172                     }
173                     instantiateAsRequest.setDeploymentItems(deploymentItemsReq);
174                 }
175             }
176             LOGGER.debug("Adding InstantiateAsRequest to execution {}", instantiateAsRequest);
177
178             execution.setVariable(INSTANTIATE_AS_REQUEST_OBJECT, instantiateAsRequest);
179             LOGGER.debug("Finished executing createAsInstanceRequest task ...");
180
181         } catch (final Exception exception) {
182             LOGGER.error("Unable to create CreateAsInstanceRequest", exception);
183             exceptionUtil.buildAndThrowWorkflowException(execution, 2002, exception);
184         }
185     }
186
187     public void invokeCnfmWithInstantiateAsRequest(final BuildingBlockExecution execution) {
188         try {
189             final InstantiateAsRequest instantiateAsRequest = execution.getVariable(INSTANTIATE_AS_REQUEST_OBJECT);
190             final String asInstanceId = execution.getVariable(AS_INSTANCE_ID);
191             Optional<URI> cnfStatusCheckURL =
192                     cnfmHttpServiceProvider.invokeInstantiateAsRequest(instantiateAsRequest, asInstanceId);
193             execution.setVariable(CNFM_REQUEST_STATUS_CHECK_URL, cnfStatusCheckURL.orElseThrow());
194             execution.setVariable(MONITOR_JOB_NAME, "Instantiate");
195             LOGGER.debug("Successfully invoked CNFM instantiate AS request: {}", asInstanceId);
196         } catch (final Exception exception) {
197             LOGGER.error("Unable to invoke CNFM InstantiateAsRequest", exception);
198             exceptionUtil.buildAndThrowWorkflowException(execution, 2005, exception);
199         }
200     }
201
202 }