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