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