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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.infrastructure.adapter.cnfm.tasks;
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.SERVICE_INSTANCE_ID_PARAM_KEY;
27 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.SERVICE_INSTANCE_NAME_PARAM_KEY;
28 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.TENANT_ID_PARAM_KEY;
30 import java.util.ArrayList;
31 import java.util.List;
33 import java.util.Optional;
34 import org.apache.groovy.util.Maps;
35 import org.onap.logging.filter.base.ONAPComponents;
36 import org.onap.so.bpmn.common.BuildingBlockExecution;
37 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
38 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
39 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
40 import org.onap.so.client.exception.ExceptionBuilder;
41 import org.onap.so.cnfm.lcm.model.AsInstance;
42 import org.onap.so.cnfm.lcm.model.CreateAsRequest;
43 import org.onap.so.cnfm.lcm.model.InstantiateAsRequest;
44 import org.onap.so.cnfm.lcm.model.AsInfoModificationRequestDeploymentItems;
45 import org.onap.so.serviceinstancebeans.CloudConfiguration;
46 import org.onap.so.serviceinstancebeans.ModelInfo;
47 import org.onap.so.serviceinstancebeans.RequestDetails;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.stereotype.Component;
55 * This class performs CNF Instantiation
57 * @author sagar.shetty@est.tech
58 * @author Waqas Ikram (waqas.ikram@est.tech)
59 * @author Raviteja Karumuri (raviteja.karumuri@est.tech)
62 public class CnfInstantiateTask {
63 private static final String CREATE_AS_REQUEST_OBJECT = "CreateAsRequestObject";
64 private static final String INSTANTIATE_AS_REQUEST_OBJECT = "InstantiateAsRequest";
65 private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
66 private static final String AS_INSTANCE_ID = "asInstanceid";
67 private static final Logger LOGGER = LoggerFactory.getLogger(CnfInstantiateTask.class);
68 private final ExceptionBuilder exceptionUtil;
69 private final CnfmHttpServiceProvider cnfmHttpServiceProvider;
72 public CnfInstantiateTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
73 final ExceptionBuilder exceptionUtil) {
74 this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
75 this.exceptionUtil = exceptionUtil;
78 public void createCreateAsRequest(final BuildingBlockExecution execution) {
80 LOGGER.debug("Executing createAsRequest task ...");
81 final ExecuteBuildingBlock executeBuildingBlock =
82 (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
84 final GeneralBuildingBlock generalBuildingBlock = execution.getGeneralBuildingBlock();
86 final RequestDetails requestDetails = executeBuildingBlock.getRequestDetails();
87 LOGGER.debug("RequestDetails: {}", requestDetails);
89 if (isNull(requestDetails) || isNull(requestDetails.getModelInfo())
90 || isNull(requestDetails.getRequestInfo())
91 || isNull(requestDetails.getCloudConfiguration()) && isNull(generalBuildingBlock)) {
92 LOGGER.error("Missing Mandatory attribute from RequestDetails: {} or GeneralBuildingBlock: {}",
93 requestDetails, generalBuildingBlock);
94 exceptionUtil.buildAndThrowWorkflowException(execution, 2000,
95 "Missing Mandatory attribute from RequestDetails or GeneralBuildingBlock", ONAPComponents.SO);
98 final ModelInfo modelInfo = requestDetails.getModelInfo();
99 final CloudConfiguration cloudConfiguration = requestDetails.getCloudConfiguration();
100 final ServiceInstance serviceInstance = generalBuildingBlock.getServiceInstance();
102 final CreateAsRequest createAsRequest = new CreateAsRequest().asdId(modelInfo.getModelVersionId())
103 .asInstanceName(requestDetails.getRequestInfo().getInstanceName())
104 .additionalParams(Maps.of(CLOUD_OWNER_PARAM_KEY, cloudConfiguration.getCloudOwner(),
105 CLOUD_REGION_PARAM_KEY, cloudConfiguration.getLcpCloudRegionId(), TENANT_ID_PARAM_KEY,
106 cloudConfiguration.getTenantId(), SERVICE_INSTANCE_ID_PARAM_KEY,
107 serviceInstance.getServiceInstanceId(), SERVICE_INSTANCE_NAME_PARAM_KEY,
108 serviceInstance.getServiceInstanceName()));
110 LOGGER.debug("Adding CreateAsRequest to execution {}", createAsRequest);
112 execution.setVariable(CREATE_AS_REQUEST_OBJECT, createAsRequest);
113 LOGGER.debug("Finished executing createAsRequest task ...");
115 } catch (final Exception exception) {
116 LOGGER.error("Unable to create CreateAsRequest", exception);
117 exceptionUtil.buildAndThrowWorkflowException(execution, 2001, exception);
121 public void invokeCnfmWithCreateAsRequest(final BuildingBlockExecution execution) {
123 final CreateAsRequest createAsRequest = execution.getVariable(CREATE_AS_REQUEST_OBJECT);
125 final Optional<AsInstance> optional = cnfmHttpServiceProvider.invokeCreateAsRequest(createAsRequest);
127 if (optional.isEmpty()) {
128 LOGGER.error("Unable to invoke CNFM for CreateAsRequest : {}", createAsRequest);
129 exceptionUtil.buildAndThrowWorkflowException(execution, 2003,
130 "Unable to invoke CNFM for CreateAsRequest", ONAPComponents.SO);
133 final AsInstance asInstance = optional.orElseThrow();
134 execution.setVariable(AS_INSTANCE_ID, asInstance.getAsInstanceid());
135 LOGGER.debug("Successfully invoked CNFM response: {}", asInstance);
137 } catch (final Exception exception) {
138 LOGGER.error("Unable to invoke CNFM AsCreateRequest", exception);
139 exceptionUtil.buildAndThrowWorkflowException(execution, 2004, exception);
143 public void createAsInstanceRequest(final BuildingBlockExecution execution) {
145 LOGGER.debug("Executing createAsInstanceRequest task ...");
146 final ExecuteBuildingBlock executeBuildingBlock =
147 (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
148 final RequestDetails requestDetails = executeBuildingBlock.getRequestDetails();
149 final InstantiateAsRequest instantiateAsRequest = new InstantiateAsRequest();
150 if (requestDetails != null && requestDetails.getRequestParameters() != null) {
151 List<Map<String, Object>> userParams = requestDetails.getRequestParameters().getUserParams();
152 if (userParams != null && !userParams.isEmpty()) {
153 List deploymentItems = new ArrayList<Object>();
154 List deploymentItemsReq = new ArrayList<AsInfoModificationRequestDeploymentItems>();
155 for (Map<String, Object> userParam : userParams) {
156 if (userParam.containsKey("deploymentItems")) {
157 deploymentItems = (ArrayList<Object>) userParam.get("deploymentItems");
161 for (Object deploymentItem : deploymentItems) {
162 Map<String, Object> deploymentItemMap = (Map<String, Object>) deploymentItem;
163 AsInfoModificationRequestDeploymentItems item = new AsInfoModificationRequestDeploymentItems();
164 item.setDeploymentItemsId(deploymentItemMap.get("deploymentItemsId").toString());
165 item.setLifecycleParameterKeyValues(deploymentItemMap.get("lifecycleParameterKeyValues"));
166 deploymentItemsReq.add(item);
168 instantiateAsRequest.setDeploymentItems(deploymentItemsReq);
171 LOGGER.debug("Adding InstantiateAsRequest to execution {}", instantiateAsRequest);
173 execution.setVariable(INSTANTIATE_AS_REQUEST_OBJECT, instantiateAsRequest);
174 LOGGER.debug("Finished executing createAsInstanceRequest task ...");
176 } catch (final Exception exception) {
177 LOGGER.error("Unable to create CreateAsInstanceRequest", exception);
178 exceptionUtil.buildAndThrowWorkflowException(execution, 2002, exception);
182 public void invokeCnfmWithInstantiateAsRequest(final BuildingBlockExecution execution) {
184 final InstantiateAsRequest instantiateAsRequest = execution.getVariable(INSTANTIATE_AS_REQUEST_OBJECT);
185 final String asInstanceId = execution.getVariable(AS_INSTANCE_ID);
186 Optional<URI> cnf_status_check_url =
187 cnfmHttpServiceProvider.invokeInstantiateAsRequest(instantiateAsRequest, asInstanceId);
188 execution.setVariable(CNFM_REQUEST_STATUS_CHECK_URL, cnf_status_check_url.get());
189 LOGGER.debug("Successfully invoked CNFM instantiate AS request: {}", asInstanceId);
190 } catch (final Exception exception) {
191 LOGGER.error("Unable to invoke CNFM InstantiateAsRequest", exception);
192 exceptionUtil.buildAndThrowWorkflowException(execution, 2005, exception);