Bump the version for the SO core to Kohn
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / ebb / loader / UserParamsServiceTraversal.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Modifications Copyright (c) 2020 Nokia
10  * ================================================================================
11  * Modifications Copyright (c) 2020 Tech Mahindra
12  * ================================================================================
13  * Modifications Copyright (c) 2021 Orange
14  * ================================================================================
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  *      http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  * ============LICENSE_END=========================================================
27  */
28
29 package org.onap.so.bpmn.infrastructure.workflow.tasks.ebb.loader;
30
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import org.camunda.bpm.engine.delegate.DelegateExecution;
33 import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
34 import org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowType;
35 import org.onap.so.client.exception.ExceptionBuilder;
36 import org.onap.so.db.catalog.beans.CollectionResourceCustomization;
37 import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
38 import org.onap.so.db.catalog.beans.CvnfcCustomization;
39 import org.onap.so.db.catalog.beans.VfModuleCustomization;
40 import org.onap.so.db.catalog.client.CatalogDbClient;
41 import org.onap.so.serviceinstancebeans.ModelInfo;
42 import org.onap.so.serviceinstancebeans.Networks;
43 import org.onap.so.serviceinstancebeans.Pnfs;
44 import org.onap.so.serviceinstancebeans.Service;
45 import org.onap.so.serviceinstancebeans.VfModules;
46 import org.onap.so.serviceinstancebeans.Vnfs;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.stereotype.Component;
50 import java.io.IOException;
51 import java.util.ArrayList;
52 import java.util.Collections;
53 import java.util.List;
54 import java.util.Map;
55 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.CREATE_INSTANCE;
56 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.FABRIC_CONFIGURATION;
57 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.USER_PARAM_SERVICE;
58 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.WORKFLOW_ACTION_ERROR_MESSAGE;
59
60 @Component
61 public class UserParamsServiceTraversal {
62
63     private static final Logger logger = LoggerFactory.getLogger(UserParamsServiceTraversal.class);
64
65     private final CatalogDbClient catalogDbClient;
66     private final ExceptionBuilder exceptionBuilder;
67     private boolean foundVfModuleOrVG;
68     private String vnfCustomizationUUID;
69     private String vfModuleCustomizationUUID;
70
71     UserParamsServiceTraversal(CatalogDbClient catalogDbClient, ExceptionBuilder exceptionBuilder) {
72         this.catalogDbClient = catalogDbClient;
73         this.exceptionBuilder = exceptionBuilder;
74     }
75
76     public List<Resource> getResourceListFromUserParams(DelegateExecution execution,
77             List<Map<String, Object>> userParams, String serviceModelVersionId, String requestAction)
78             throws IOException {
79         if (userParams != null) {
80             for (Map<String, Object> params : userParams) {
81                 if (params.containsKey(USER_PARAM_SERVICE)) {
82                     ObjectMapper obj = new ObjectMapper();
83                     String input = obj.writeValueAsString(params.get(USER_PARAM_SERVICE));
84                     Service validate = obj.readValue(input, Service.class);
85                     return getResourceList(execution, serviceModelVersionId, requestAction, validate);
86                 }
87             }
88         }
89         return Collections.emptyList();
90     }
91
92     private List<Resource> getResourceList(DelegateExecution execution, String serviceModelVersionId,
93             String requestAction, Service validate) {
94         List<Resource> resourceList = new ArrayList<>();
95         Resource serviceResource =
96                 new Resource(WorkflowType.SERVICE, validate.getModelInfo().getModelVersionId(), false, null);
97         ModelInfo modelInfo = validate.getModelInfo();
98         if (modelInfo != null) {
99             serviceResource.setModelVersionId(modelInfo.getModelVersionId());
100             serviceResource.setModelInvariantId(modelInfo.getModelInvariantUuid());
101         }
102         resourceList.add(serviceResource);
103         if (validate.getResources().getVnfs() != null) {
104             setResourceListForVnfs(execution, resourceList, serviceResource, validate);
105         }
106         if (validate.getResources().getPnfs() != null) {
107             setResourceListForPnfs(resourceList, serviceResource, validate);
108         }
109         if (validate.getResources().getNetworks() != null) {
110             setResourceListForNetworks(execution, serviceModelVersionId, requestAction, resourceList, serviceResource,
111                     validate);
112         }
113         return resourceList;
114     }
115
116     private void setResourceListForVnfs(DelegateExecution execution, List<Resource> resourceList,
117             Resource serviceResource, Service validate) {
118         for (Vnfs vnf : validate.getResources().getVnfs()) {
119             setVnfCustomizationUUID(vnf);
120             Resource vnfResource = new Resource(WorkflowType.VNF, vnf.getModelInfo().getModelCustomizationId(), false,
121                     serviceResource);
122             vnfResource.setProcessingPriority(vnf.getProcessingPriority());
123             vnfResource.setInstanceName(vnf.getInstanceName());
124             ModelInfo modelInfo = vnf.getModelInfo();
125             if (modelInfo != null) {
126                 vnfResource.setModelCustomizationId(modelInfo.getModelCustomizationUuid());
127                 vnfResource.setModelVersionId(modelInfo.getModelVersionId());
128             }
129             resourceList.add(vnfResource);
130             setResourceListForVfModules(execution, resourceList, vnfResource, validate, vnf);
131         }
132     }
133
134     private void setResourceListForVfModules(DelegateExecution execution, List<Resource> resourceList,
135             Resource vnfResource, Service validate, Vnfs vnf) {
136         if (vnf.getVfModules() != null) {
137             for (VfModules vfModule : vnf.getVfModules()) {
138                 setVfModuleCustomizationUUID(vfModule);
139                 VfModuleCustomization vfModuleCustomization =
140                         catalogDbClient.getVfModuleCustomizationByModelCuztomizationUUID(vfModuleCustomizationUUID);
141                 if (vfModuleCustomization != null && vfModuleCustomization.getVfModule() != null) {
142                     setVolumeGroupWorkFlowTypeToResourceList(resourceList, vnfResource, vfModuleCustomization);
143                     setVfModuleAndConfigurationWorkFlowTypeToResourceList(resourceList, vnfResource, validate, vnf,
144                             vfModule, vfModuleCustomization);
145                     if (!foundVfModuleOrVG) {
146                         buildAndThrowException(execution,
147                                 "Could not determine if vfModule was a vfModule or volume group. Heat template and Heat env are null");
148                     }
149                 }
150             }
151         }
152     }
153
154     private void setVolumeGroupWorkFlowTypeToResourceList(List<Resource> resourceList, Resource vnfResource,
155             VfModuleCustomization vfModuleCustomization) {
156         if (vfModuleCustomization.getVfModule().getVolumeHeatTemplate() != null
157                 && vfModuleCustomization.getVolumeHeatEnv() != null) {
158             foundVfModuleOrVG = true;
159             resourceList.add(new Resource(WorkflowType.VOLUMEGROUP, vfModuleCustomization.getModelCustomizationUUID(),
160                     false, vnfResource));
161         }
162     }
163
164     private void setVfModuleAndConfigurationWorkFlowTypeToResourceList(List<Resource> resourceList,
165             Resource vnfResource, Service validate, Vnfs vnf, VfModules vfModule,
166             VfModuleCustomization vfModuleCustomization) {
167         if ((vfModuleCustomization.getVfModule().getModuleHeatTemplate() != null
168                 && vfModuleCustomization.getHeatEnvironment() != null)
169                 || (vfModuleCustomization.getVfModule().getModelName() != null
170                         && vfModuleCustomization.getVfModule().getModelName().contains("helm"))) {
171             foundVfModuleOrVG = true;
172             Resource resource =
173                     setVfModuleWorkFlowTypeToResourceList(resourceList, vnfResource, vfModuleCustomization, vfModule);
174             if (vnf.getModelInfo() != null) {
175                 resource.setModelVersionId(vnf.getModelInfo().getModelVersionId());
176             }
177             resource.setVfModuleCustomizationId(vfModuleCustomization.getModelCustomizationUUID());
178             resource.setModelCustomizationId(vfModuleCustomization.getModelCustomizationUUID());
179             setConfigurationWorkFlowTypeToResourceList(resourceList, vnfResource, validate, vnf, vfModule, resource);
180         }
181     }
182
183     private Resource setVfModuleWorkFlowTypeToResourceList(List<Resource> resourceList, Resource vnfResource,
184             VfModuleCustomization vfModuleCustomization, VfModules vfModule) {
185         Resource resource = new Resource(WorkflowType.VFMODULE, vfModuleCustomization.getModelCustomizationUUID(),
186                 false, vnfResource);
187         resource.setProcessingPriority(vfModule.getProcessingPriority());
188         resource.setBaseVfModule(vfModuleCustomization.getVfModule().getIsBase() != null
189                 && vfModuleCustomization.getVfModule().getIsBase());
190         resource.setInstanceName(vfModule.getInstanceName());
191         resourceList.add(resource);
192         return resource;
193     }
194
195     private void setConfigurationWorkFlowTypeToResourceList(List<Resource> resourceList, Resource vnfResource,
196             Service validate, Vnfs vnf, VfModules vfModule, Resource resource) {
197         if (!vnfCustomizationUUID.isEmpty() && !vfModuleCustomizationUUID.isEmpty()) {
198             List<CvnfcConfigurationCustomization> configs =
199                     traverseCatalogDbForConfiguration(validate.getModelInfo().getModelVersionId());
200             for (CvnfcConfigurationCustomization config : configs) {
201                 Resource configResource = new Resource(WorkflowType.CONFIGURATION,
202                         config.getConfigurationResource().getModelUUID(), false, vnfResource);
203                 resource.setVnfCustomizationId(vnf.getModelInfo().getModelCustomizationId());
204                 resource.setVfModuleCustomizationId(vfModule.getModelInfo().getModelCustomizationId());
205                 resourceList.add(configResource);
206             }
207         }
208     }
209
210     private void setVfModuleCustomizationUUID(VfModules vfModule) {
211         if (vfModule.getModelInfo() != null && vfModule.getModelInfo().getModelCustomizationUuid() != null) {
212             vfModuleCustomizationUUID = vfModule.getModelInfo().getModelCustomizationUuid();
213         } else {
214             vfModuleCustomizationUUID = "";
215         }
216     }
217
218     private void setVnfCustomizationUUID(Vnfs vnf) {
219         if (vnf.getModelInfo() != null && vnf.getModelInfo().getModelCustomizationUuid() != null) {
220             vnfCustomizationUUID = vnf.getModelInfo().getModelCustomizationUuid();
221         } else {
222             vnfCustomizationUUID = "";
223         }
224     }
225
226     private void setResourceListForPnfs(List<Resource> resourceList, Resource serviceResource, Service validate) {
227         for (Pnfs pnf : validate.getResources().getPnfs()) {
228             Resource pnfResource = new Resource(WorkflowType.PNF, pnf.getModelInfo().getModelCustomizationId(), false,
229                     serviceResource);
230             ModelInfo modelInfo = pnf.getModelInfo();
231             if (modelInfo != null) {
232                 pnfResource.setModelCustomizationId(modelInfo.getModelCustomizationUuid());
233                 pnfResource.setModelVersionId(modelInfo.getModelVersionId());
234             }
235             pnfResource.setProcessingPriority(pnf.getProcessingPriority());
236             resourceList.add(pnfResource);
237         }
238     }
239
240     private void setResourceListForNetworks(DelegateExecution execution, String serviceModelVersionId,
241             String requestAction, List<Resource> resourceList, Resource serviceResource, Service validate) {
242         for (Networks network : validate.getResources().getNetworks()) {
243             Resource networkResource = new Resource(WorkflowType.NETWORK,
244                     network.getModelInfo().getModelCustomizationId(), false, serviceResource);
245             networkResource.setProcessingPriority(network.getProcessingPriority());
246             ModelInfo modelInfo = network.getModelInfo();
247             if (modelInfo != null) {
248                 networkResource.setModelCustomizationId(modelInfo.getModelCustomizationUuid());
249                 networkResource.setModelVersionId(modelInfo.getModelVersionId());
250             }
251             resourceList.add(networkResource);
252         }
253         if (requestAction.equals(CREATE_INSTANCE)) {
254             String networkColCustId = queryCatalogDbForNetworkCollection(execution, serviceModelVersionId);
255             if (networkColCustId != null) {
256                 resourceList
257                         .add(new Resource(WorkflowType.NETWORKCOLLECTION, networkColCustId, false, serviceResource));
258             }
259         }
260     }
261
262
263     private List<CvnfcConfigurationCustomization> traverseCatalogDbForConfiguration(String serviceModelUUID) {
264         try {
265             List<CvnfcCustomization> cvnfcCustomizations = catalogDbClient.getCvnfcCustomization(serviceModelUUID,
266                     vnfCustomizationUUID, vfModuleCustomizationUUID);
267             return getCvnfcConfigurationCustomizations(cvnfcCustomizations);
268         } catch (Exception ex) {
269             logger.error("Error in finding configurations", ex);
270             return Collections.emptyList();
271         }
272     }
273
274     private List<CvnfcConfigurationCustomization> getCvnfcConfigurationCustomizations(
275             List<CvnfcCustomization> cvnfcCustomizations) {
276         List<CvnfcConfigurationCustomization> configurations = new ArrayList<>();
277         for (CvnfcCustomization cvnfc : cvnfcCustomizations) {
278             for (CvnfcConfigurationCustomization customization : cvnfc.getCvnfcConfigurationCustomization()) {
279                 if (customization.getConfigurationResource().getToscaNodeType().contains(FABRIC_CONFIGURATION)) {
280                     configurations.add(customization);
281                 }
282             }
283         }
284         logger.debug("found {} fabric configuration(s)", configurations.size());
285         return configurations;
286     }
287
288     private String queryCatalogDbForNetworkCollection(DelegateExecution execution, String serviceModelVersionId) {
289         org.onap.so.db.catalog.beans.Service service = catalogDbClient.getServiceByID(serviceModelVersionId);
290         if (service != null) {
291             CollectionResourceCustomization networkCollection = this.findCatalogNetworkCollection(execution, service);
292             if (networkCollection != null) {
293                 return networkCollection.getModelCustomizationUUID();
294             }
295         }
296         return null;
297     }
298
299     private CollectionResourceCustomization findCatalogNetworkCollection(DelegateExecution execution,
300             org.onap.so.db.catalog.beans.Service service) {
301         CollectionResourceCustomization networkCollection = null;
302         int count = 0;
303         for (CollectionResourceCustomization collectionCustom : service.getCollectionResourceCustomizations()) {
304             if (catalogDbClient.getNetworkCollectionResourceCustomizationByID(
305                     collectionCustom.getModelCustomizationUUID()) != null) {
306                 networkCollection = collectionCustom;
307                 count++;
308             }
309         }
310         if (count > 1) {
311             buildAndThrowException(execution,
312                     "Found multiple Network Collections in the Service model, only one per Service is supported.");
313         }
314         return networkCollection;
315     }
316
317     private void buildAndThrowException(DelegateExecution execution, String msg) {
318         logger.error(msg);
319         execution.setVariable(WORKFLOW_ACTION_ERROR_MESSAGE, msg);
320         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
321     }
322 }