6d7b3e928780679b1ada4781ffd4d6fa182d0cd9
[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 import java.util.Optional;
29 import org.apache.groovy.util.Maps;
30 import org.onap.logging.filter.base.ONAPComponents;
31 import org.onap.so.bpmn.common.BuildingBlockExecution;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
33 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
34 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
35 import org.onap.so.client.exception.ExceptionBuilder;
36 import org.onap.so.cnfm.lcm.model.AsInstance;
37 import org.onap.so.cnfm.lcm.model.CreateAsRequest;
38 import org.onap.so.serviceinstancebeans.CloudConfiguration;
39 import org.onap.so.serviceinstancebeans.ModelInfo;
40 import org.onap.so.serviceinstancebeans.RequestDetails;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Component;
45
46
47 /**
48  * This class performs CNF Instantiation
49  * 
50  * @author sagar.shetty@est.tech
51  * @author Waqas Ikram (waqas.ikram@est.tech)
52  */
53 @Component
54 public class CnfInstantiateTask {
55     private static final String CREATE_AS_REQUEST_OBJECT = "CreateAsRequestObject";
56     private static final Logger LOGGER = LoggerFactory.getLogger(CnfInstantiateTask.class);
57     private final ExceptionBuilder exceptionUtil;
58     private final CnfmHttpServiceProvider cnfmHttpServiceProvider;
59
60     @Autowired
61     public CnfInstantiateTask(final CnfmHttpServiceProvider cnfmHttpServiceProvider,
62             final ExceptionBuilder exceptionUtil) {
63         this.cnfmHttpServiceProvider = cnfmHttpServiceProvider;
64         this.exceptionUtil = exceptionUtil;
65     }
66
67     public void createCreateAsRequest(final BuildingBlockExecution execution) {
68         try {
69             LOGGER.debug("Executing createAsRequest task  ...");
70             final ExecuteBuildingBlock executeBuildingBlock =
71                     (ExecuteBuildingBlock) execution.getVariable("buildingBlock");
72
73             final GeneralBuildingBlock generalBuildingBlock = execution.getGeneralBuildingBlock();
74
75             final RequestDetails requestDetails = executeBuildingBlock.getRequestDetails();
76             LOGGER.debug("RequestDetails: {}", requestDetails);
77
78             if (isNull(requestDetails) && isNull(requestDetails.getModelInfo())
79                     && isNull(requestDetails.getRequestInfo()) && isNull(requestDetails.getCloudConfiguration())
80                     && isNull(generalBuildingBlock)) {
81                 LOGGER.error("Missing Mandatory attribute from RequestDetails: {} or GeneralBuildingBlock: {}",
82                         requestDetails, generalBuildingBlock);
83                 exceptionUtil.buildAndThrowWorkflowException(execution, 2000,
84                         "Missing Mandatory attribute from RequestDetails or GeneralBuildingBlock", ONAPComponents.SO);
85             }
86
87             final ModelInfo modelInfo = requestDetails.getModelInfo();
88             final CloudConfiguration cloudConfiguration = requestDetails.getCloudConfiguration();
89             final ServiceInstance serviceInstance = generalBuildingBlock.getServiceInstance();
90
91             final CreateAsRequest createAsRequest = new CreateAsRequest().asdId(modelInfo.getModelVersionId())
92                     .asInstanceName(requestDetails.getRequestInfo().getInstanceName())
93                     .additionalParams(Maps.of(CLOUD_OWNER_PARAM_KEY, cloudConfiguration.getCloudOwner(),
94                             CLOUD_REGION_PARAM_KEY, cloudConfiguration.getLcpCloudRegionId(), TENANT_ID_PARAM_KEY,
95                             cloudConfiguration.getTenantId(), SERVICE_INSTANCE_ID_PARAM_KEY,
96                             serviceInstance.getServiceInstanceId(), SERVICE_INSTANCE_NAME_PARAM_KEY,
97                             serviceInstance.getServiceInstanceName()));
98
99             LOGGER.debug("Adding CreateAsRequest to execution {}", createAsRequest);
100
101             execution.setVariable(CREATE_AS_REQUEST_OBJECT, createAsRequest);
102             LOGGER.debug("Finished executing createAsRequest task ...");
103
104         } catch (final Exception exception) {
105             LOGGER.error("Unable to create CreateAsRequest", exception);
106             exceptionUtil.buildAndThrowWorkflowException(execution, 2001, exception);
107         }
108     }
109
110     public void invokeCnfmWithCreateAsRequest(final BuildingBlockExecution execution) {
111         try {
112             final CreateAsRequest createAsRequest = execution.getVariable(CREATE_AS_REQUEST_OBJECT);
113
114             final Optional<AsInstance> optional = cnfmHttpServiceProvider.invokeCreateAsRequest(createAsRequest);
115
116             if (optional.isEmpty()) {
117                 LOGGER.error("Unable to invoke CNFM for CreateAsRequest : {}", createAsRequest);
118                 exceptionUtil.buildAndThrowWorkflowException(execution, 2003,
119                         "Unable to invoke CNFM for CreateAsRequest", ONAPComponents.SO);
120             }
121
122             final AsInstance asInstance = optional.get();
123             LOGGER.debug("Successfully invoked CNFM response: {}", asInstance);
124
125         } catch (final Exception exception) {
126             LOGGER.error("Unable to invoke CNFM AsCreateRequest", exception);
127             exceptionUtil.buildAndThrowWorkflowException(execution, 2004, exception);
128         }
129     }
130
131 }