[SO] Pending Create changes for SO-API and BPMN-INFRA to support CNF's through ASD
[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.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;
29 import java.net.URI;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Map;
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;
52
53
54 /**
55  * This class performs CNF Instantiation
56  *
57  * @author sagar.shetty@est.tech
58  * @author Waqas Ikram (waqas.ikram@est.tech)
59  * @author Raviteja Karumuri (raviteja.karumuri@est.tech)
60  */
61 @Component
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;
70
71     @Autowired
72     public CnfInstantiateTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
73             final ExceptionBuilder exceptionUtil) {
74         this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
75         this.exceptionUtil = exceptionUtil;
76     }
77
78     public void createCreateAsRequest(final BuildingBlockExecution execution) {
79         try {
80             LOGGER.debug("Executing createAsRequest task  ...");
81             final ExecuteBuildingBlock executeBuildingBlock =
82                     (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
83
84             final GeneralBuildingBlock generalBuildingBlock = execution.getGeneralBuildingBlock();
85
86             final RequestDetails requestDetails = executeBuildingBlock.getRequestDetails();
87             LOGGER.debug("RequestDetails: {}", requestDetails);
88
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);
96             }
97
98             final ModelInfo modelInfo = requestDetails.getModelInfo();
99             final CloudConfiguration cloudConfiguration = requestDetails.getCloudConfiguration();
100             final ServiceInstance serviceInstance = generalBuildingBlock.getServiceInstance();
101
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()));
109
110             LOGGER.debug("Adding CreateAsRequest to execution {}", createAsRequest);
111
112             execution.setVariable(CREATE_AS_REQUEST_OBJECT, createAsRequest);
113             LOGGER.debug("Finished executing createAsRequest task ...");
114
115         } catch (final Exception exception) {
116             LOGGER.error("Unable to create CreateAsRequest", exception);
117             exceptionUtil.buildAndThrowWorkflowException(execution, 2001, exception);
118         }
119     }
120
121     public void invokeCnfmWithCreateAsRequest(final BuildingBlockExecution execution) {
122         try {
123             final CreateAsRequest createAsRequest = execution.getVariable(CREATE_AS_REQUEST_OBJECT);
124
125             final Optional<AsInstance> optional = cnfmHttpServiceProvider.invokeCreateAsRequest(createAsRequest);
126
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);
131             }
132
133             final AsInstance asInstance = optional.orElseThrow();
134             execution.setVariable(AS_INSTANCE_ID, asInstance.getAsInstanceid());
135             LOGGER.debug("Successfully invoked CNFM response: {}", asInstance);
136
137         } catch (final Exception exception) {
138             LOGGER.error("Unable to invoke CNFM AsCreateRequest", exception);
139             exceptionUtil.buildAndThrowWorkflowException(execution, 2004, exception);
140         }
141     }
142
143     public void createAsInstanceRequest(final BuildingBlockExecution execution) {
144         try {
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");
158                             break;
159                         }
160                     }
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);
167                     }
168                     instantiateAsRequest.setDeploymentItems(deploymentItemsReq);
169                 }
170             }
171             LOGGER.debug("Adding InstantiateAsRequest to execution {}", instantiateAsRequest);
172
173             execution.setVariable(INSTANTIATE_AS_REQUEST_OBJECT, instantiateAsRequest);
174             LOGGER.debug("Finished executing createAsInstanceRequest task ...");
175
176         } catch (final Exception exception) {
177             LOGGER.error("Unable to create CreateAsInstanceRequest", exception);
178             exceptionUtil.buildAndThrowWorkflowException(execution, 2002, exception);
179         }
180     }
181
182     public void invokeCnfmWithInstantiateAsRequest(final BuildingBlockExecution execution) {
183         try {
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);
193         }
194     }
195
196 }