0fba2d382b037b2a54fc3e7a8f192252ddfe8e41
[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.SERVICE_INSTANCE_ID_PARAM_KEY;
26 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.SERVICE_INSTANCE_NAME_PARAM_KEY;
27 import static org.onap.so.cnfm.lcm.model.utils.AdditionalParamsConstants.TENANT_ID_PARAM_KEY;
28
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 import camundajar.impl.scala.collection.immutable.HashMap;
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  */
62 @Component
63 public class CnfInstantiateTask {
64     private static final String CREATE_AS_REQUEST_OBJECT = "CreateAsRequestObject";
65     private static final String INSTANTIATE_AS_REQUEST_OBJECT = "InstantiateAsRequest";
66     private static final String CNFM_REQUEST_STATUS_CHECK_URL = "CnfmStatusCheckUrl";
67     private static final String AS_INSTANCE_ID = "asInstanceid";
68     private static final Logger LOGGER = LoggerFactory.getLogger(CnfInstantiateTask.class);
69     private final ExceptionBuilder exceptionUtil;
70     private final CnfmHttpServiceProvider cnfmHttpServiceProvider;
71
72     @Autowired
73     public CnfInstantiateTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
74             final ExceptionBuilder exceptionUtil) {
75         this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
76         this.exceptionUtil = exceptionUtil;
77     }
78
79     public void createCreateAsRequest(final BuildingBlockExecution execution) {
80         try {
81             LOGGER.debug("Executing createAsRequest task  ...");
82             final ExecuteBuildingBlock executeBuildingBlock =
83                     (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
84
85             final GeneralBuildingBlock generalBuildingBlock = execution.getGeneralBuildingBlock();
86
87             final RequestDetails requestDetails = executeBuildingBlock.getRequestDetails();
88             LOGGER.debug("RequestDetails: {}", requestDetails);
89
90             if (isNull(requestDetails) && isNull(requestDetails.getModelInfo())
91                     && isNull(requestDetails.getRequestInfo()) && isNull(requestDetails.getCloudConfiguration())
92                     && isNull(generalBuildingBlock)) {
93                 LOGGER.error("Missing Mandatory attribute from RequestDetails: {} or GeneralBuildingBlock: {}",
94                         requestDetails, generalBuildingBlock);
95                 exceptionUtil.buildAndThrowWorkflowException(execution, 2000,
96                         "Missing Mandatory attribute from RequestDetails or GeneralBuildingBlock", ONAPComponents.SO);
97             }
98
99             final ModelInfo modelInfo = requestDetails.getModelInfo();
100             final CloudConfiguration cloudConfiguration = requestDetails.getCloudConfiguration();
101             final ServiceInstance serviceInstance = generalBuildingBlock.getServiceInstance();
102
103             final CreateAsRequest createAsRequest = new CreateAsRequest().asdId(modelInfo.getModelVersionId())
104                     .asInstanceName(requestDetails.getRequestInfo().getInstanceName())
105                     .additionalParams(Maps.of(CLOUD_OWNER_PARAM_KEY, cloudConfiguration.getCloudOwner(),
106                             CLOUD_REGION_PARAM_KEY, cloudConfiguration.getLcpCloudRegionId(), TENANT_ID_PARAM_KEY,
107                             cloudConfiguration.getTenantId(), SERVICE_INSTANCE_ID_PARAM_KEY,
108                             serviceInstance.getServiceInstanceId(), SERVICE_INSTANCE_NAME_PARAM_KEY,
109                             serviceInstance.getServiceInstanceName()));
110
111             LOGGER.debug("Adding CreateAsRequest to execution {}", createAsRequest);
112
113             execution.setVariable(CREATE_AS_REQUEST_OBJECT, createAsRequest);
114             LOGGER.debug("Finished executing createAsRequest task ...");
115
116         } catch (final Exception exception) {
117             LOGGER.error("Unable to create CreateAsRequest", exception);
118             exceptionUtil.buildAndThrowWorkflowException(execution, 2001, exception);
119         }
120     }
121
122     public void invokeCnfmWithCreateAsRequest(final BuildingBlockExecution execution) {
123         try {
124             final CreateAsRequest createAsRequest = execution.getVariable(CREATE_AS_REQUEST_OBJECT);
125
126             final Optional<AsInstance> optional = cnfmHttpServiceProvider.invokeCreateAsRequest(createAsRequest);
127
128             if (optional.isEmpty()) {
129                 LOGGER.error("Unable to invoke CNFM for CreateAsRequest : {}", createAsRequest);
130                 exceptionUtil.buildAndThrowWorkflowException(execution, 2003,
131                         "Unable to invoke CNFM for CreateAsRequest", ONAPComponents.SO);
132             }
133
134             final AsInstance asInstance = optional.get();
135             execution.setVariable(AS_INSTANCE_ID, asInstance.getAsInstanceid());
136             LOGGER.debug("Successfully invoked CNFM response: {}", asInstance);
137
138         } catch (final Exception exception) {
139             LOGGER.error("Unable to invoke CNFM AsCreateRequest", exception);
140             exceptionUtil.buildAndThrowWorkflowException(execution, 2004, exception);
141         }
142     }
143
144     public void createAsInstanceRequest(final BuildingBlockExecution execution) {
145         try {
146             LOGGER.debug("Executing createAsInstanceRequest task  ...");
147             final ExecuteBuildingBlock executeBuildingBlock =
148                     (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
149             final RequestDetails requestDetails = executeBuildingBlock.getRequestDetails();
150             final InstantiateAsRequest instantiateAsRequest = new InstantiateAsRequest();
151             if (requestDetails != null && requestDetails.getRequestParameters() != null) {
152                 List<Map<String, Object>> userParams = requestDetails.getRequestParameters().getUserParams();
153                 if (userParams != null && !userParams.isEmpty()) {
154                     List deploymentItems = new ArrayList<Object>();
155                     List deploymentItemsReq = new ArrayList<AsInfoModificationRequestDeploymentItems>();
156                     for (Map<String, Object> userParam : userParams) {
157                         if (userParam.containsKey("deploymentItems")) {
158                             deploymentItems = (ArrayList<Object>) userParam.get("deploymentItems");
159                             break;
160                         }
161                     }
162                     for (Object deploymentItem : deploymentItems) {
163                         Map<String, Object> deploymentItemMap = (Map<String, Object>) deploymentItem;
164                         AsInfoModificationRequestDeploymentItems item = new AsInfoModificationRequestDeploymentItems();
165                         item.setDeploymentItemsId(deploymentItemMap.get("deploymentItemsId").toString());
166                         item.setLifecycleParameterKeyValues(deploymentItemMap.get("lifecycleParameterKeyValues"));
167                         deploymentItemsReq.add(item);
168                     }
169                     instantiateAsRequest.setDeploymentItems(deploymentItemsReq);;
170                 }
171             }
172             LOGGER.debug("Adding InstantiateAsRequest to execution {}", instantiateAsRequest);
173
174             execution.setVariable(INSTANTIATE_AS_REQUEST_OBJECT, instantiateAsRequest);
175             LOGGER.debug("Finished executing createAsInstanceRequest task ...");
176
177         } catch (final Exception exception) {
178             LOGGER.error("Unable to create CreateAsInstanceRequest", exception);
179             exceptionUtil.buildAndThrowWorkflowException(execution, 2001, exception);
180         }
181     }
182
183     public void invokeCnfmWithInstantiateAsRequest(final BuildingBlockExecution execution) {
184         try {
185             final InstantiateAsRequest instantiateAsRequest = execution.getVariable(INSTANTIATE_AS_REQUEST_OBJECT);
186             final String asInstanceId = execution.getVariable(AS_INSTANCE_ID);
187             Optional<URI> cnf_status_check_url =
188                     cnfmHttpServiceProvider.invokeInstantiateAsRequest(instantiateAsRequest, asInstanceId);
189             execution.setVariable(CNFM_REQUEST_STATUS_CHECK_URL, cnf_status_check_url.get());
190             LOGGER.debug("Successfully invoked CNFM instantiate AS request: {}", asInstanceId);
191         } catch (final Exception exception) {
192             LOGGER.error("Unable to invoke CNFM InstantiateAsRequest", exception);
193             exceptionUtil.buildAndThrowWorkflowException(execution, 2004, exception);
194         }
195     }
196
197 }