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