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