b56bd905a62532550c9c161d16bd29e1aac175c0
[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().getServices() != null) {
104             setResourceListForChildServices(execution, resourceList, serviceResource, validate);
105         }
106         if (validate.getResources().getVnfs() != null) {
107             setResourceListForVnfs(execution, resourceList, serviceResource, validate);
108         }
109         if (validate.getResources().getPnfs() != null) {
110             setResourceListForPnfs(resourceList, serviceResource, validate);
111         }
112         if (validate.getResources().getNetworks() != null) {
113             setResourceListForNetworks(execution, serviceModelVersionId, requestAction, resourceList, serviceResource,
114                     validate);
115         }
116         return resourceList;
117     }
118
119     private void setResourceListForChildServices(DelegateExecution execution, List<Resource> resourceList,
120             Resource serviceResource, Service validate) {
121         for (Service childService : validate.getResources().getServices()) {
122             Resource childServiceResource = new Resource(WorkflowType.SERVICE,
123                     childService.getModelInfo().getModelVersionId(), false, serviceResource);
124             childServiceResource.setProcessingPriority(childService.getProcessingPriority());
125             childServiceResource.setInstanceName(childService.getInstanceName());
126             resourceList.add(childServiceResource);
127         }
128     }
129
130     private void setResourceListForVnfs(DelegateExecution execution, List<Resource> resourceList,
131             Resource serviceResource, Service validate) {
132         for (Vnfs vnf : validate.getResources().getVnfs()) {
133             setVnfCustomizationUUID(vnf);
134             Resource vnfResource = new Resource(WorkflowType.VNF, vnf.getModelInfo().getModelCustomizationId(), false,
135                     serviceResource);
136             vnfResource.setProcessingPriority(vnf.getProcessingPriority());
137             vnfResource.setInstanceName(vnf.getInstanceName());
138             ModelInfo modelInfo = vnf.getModelInfo();
139             if (modelInfo != null) {
140                 vnfResource.setModelCustomizationId(modelInfo.getModelCustomizationUuid());
141                 vnfResource.setModelVersionId(modelInfo.getModelVersionId());
142             }
143             resourceList.add(vnfResource);
144             setResourceListForVfModules(execution, resourceList, vnfResource, validate, vnf);
145         }
146     }
147
148     private void setResourceListForVfModules(DelegateExecution execution, List<Resource> resourceList,
149             Resource vnfResource, Service validate, Vnfs vnf) {
150         if (vnf.getVfModules() != null) {
151             for (VfModules vfModule : vnf.getVfModules()) {
152                 setVfModuleCustomizationUUID(vfModule);
153                 VfModuleCustomization vfModuleCustomization =
154                         catalogDbClient.getVfModuleCustomizationByModelCuztomizationUUID(vfModuleCustomizationUUID);
155                 if (vfModuleCustomization != null && vfModuleCustomization.getVfModule() != null) {
156                     setVolumeGroupWorkFlowTypeToResourceList(resourceList, vnfResource, vfModuleCustomization);
157                     setVfModuleAndConfigurationWorkFlowTypeToResourceList(resourceList, vnfResource, validate, vnf,
158                             vfModule, vfModuleCustomization);
159                     if (!foundVfModuleOrVG) {
160                         buildAndThrowException(execution,
161                                 "Could not determine if vfModule was a vfModule or volume group. Heat template and Heat env are null");
162                     }
163                 }
164             }
165         }
166     }
167
168     private void setVolumeGroupWorkFlowTypeToResourceList(List<Resource> resourceList, Resource vnfResource,
169             VfModuleCustomization vfModuleCustomization) {
170         if (vfModuleCustomization.getVfModule().getVolumeHeatTemplate() != null
171                 && vfModuleCustomization.getVolumeHeatEnv() != null) {
172             foundVfModuleOrVG = true;
173             resourceList.add(new Resource(WorkflowType.VOLUMEGROUP, vfModuleCustomization.getModelCustomizationUUID(),
174                     false, vnfResource));
175         }
176     }
177
178     private void setVfModuleAndConfigurationWorkFlowTypeToResourceList(List<Resource> resourceList,
179             Resource vnfResource, Service validate, Vnfs vnf, VfModules vfModule,
180             VfModuleCustomization vfModuleCustomization) {
181         if ((vfModuleCustomization.getVfModule().getModuleHeatTemplate() != null
182                 && vfModuleCustomization.getHeatEnvironment() != null)
183                 || (vfModuleCustomization.getVfModule().getModelName() != null
184                         && vfModuleCustomization.getVfModule().getModelName().contains("helm"))) {
185             foundVfModuleOrVG = true;
186             Resource resource =
187                     setVfModuleWorkFlowTypeToResourceList(resourceList, vnfResource, vfModuleCustomization, vfModule);
188             if (vnf.getModelInfo() != null) {
189                 resource.setModelVersionId(vnf.getModelInfo().getModelVersionId());
190             }
191             resource.setVfModuleCustomizationId(vfModuleCustomization.getModelCustomizationUUID());
192             resource.setModelCustomizationId(vfModuleCustomization.getModelCustomizationUUID());
193             setConfigurationWorkFlowTypeToResourceList(resourceList, vnfResource, validate, vnf, vfModule, resource);
194         }
195     }
196
197     private Resource setVfModuleWorkFlowTypeToResourceList(List<Resource> resourceList, Resource vnfResource,
198             VfModuleCustomization vfModuleCustomization, VfModules vfModule) {
199         Resource resource = new Resource(WorkflowType.VFMODULE, vfModuleCustomization.getModelCustomizationUUID(),
200                 false, vnfResource);
201         resource.setProcessingPriority(vfModule.getProcessingPriority());
202         resource.setBaseVfModule(vfModuleCustomization.getVfModule().getIsBase() != null
203                 && vfModuleCustomization.getVfModule().getIsBase());
204         resource.setInstanceName(vfModule.getInstanceName());
205         resourceList.add(resource);
206         return resource;
207     }
208
209     private void setConfigurationWorkFlowTypeToResourceList(List<Resource> resourceList, Resource vnfResource,
210             Service validate, Vnfs vnf, VfModules vfModule, Resource resource) {
211         if (!vnfCustomizationUUID.isEmpty() && !vfModuleCustomizationUUID.isEmpty()) {
212             List<CvnfcConfigurationCustomization> configs =
213                     traverseCatalogDbForConfiguration(validate.getModelInfo().getModelVersionId());
214             for (CvnfcConfigurationCustomization config : configs) {
215                 Resource configResource = new Resource(WorkflowType.CONFIGURATION,
216                         config.getConfigurationResource().getModelUUID(), false, vnfResource);
217                 resource.setVnfCustomizationId(vnf.getModelInfo().getModelCustomizationId());
218                 resource.setVfModuleCustomizationId(vfModule.getModelInfo().getModelCustomizationId());
219                 resourceList.add(configResource);
220             }
221         }
222     }
223
224     private void setVfModuleCustomizationUUID(VfModules vfModule) {
225         if (vfModule.getModelInfo() != null && vfModule.getModelInfo().getModelCustomizationUuid() != null) {
226             vfModuleCustomizationUUID = vfModule.getModelInfo().getModelCustomizationUuid();
227         } else {
228             vfModuleCustomizationUUID = "";
229         }
230     }
231
232     private void setVnfCustomizationUUID(Vnfs vnf) {
233         if (vnf.getModelInfo() != null && vnf.getModelInfo().getModelCustomizationUuid() != null) {
234             vnfCustomizationUUID = vnf.getModelInfo().getModelCustomizationUuid();
235         } else {
236             vnfCustomizationUUID = "";
237         }
238     }
239
240     private void setResourceListForPnfs(List<Resource> resourceList, Resource serviceResource, Service validate) {
241         for (Pnfs pnf : validate.getResources().getPnfs()) {
242             Resource pnfResource = new Resource(WorkflowType.PNF, pnf.getModelInfo().getModelCustomizationId(), false,
243                     serviceResource);
244             ModelInfo modelInfo = pnf.getModelInfo();
245             if (modelInfo != null) {
246                 pnfResource.setModelCustomizationId(modelInfo.getModelCustomizationUuid());
247                 pnfResource.setModelVersionId(modelInfo.getModelVersionId());
248             }
249             pnfResource.setProcessingPriority(pnf.getProcessingPriority());
250             resourceList.add(pnfResource);
251         }
252     }
253
254     private void setResourceListForNetworks(DelegateExecution execution, String serviceModelVersionId,
255             String requestAction, List<Resource> resourceList, Resource serviceResource, Service validate) {
256         for (Networks network : validate.getResources().getNetworks()) {
257             Resource networkResource = new Resource(WorkflowType.NETWORK,
258                     network.getModelInfo().getModelCustomizationId(), false, serviceResource);
259             networkResource.setProcessingPriority(network.getProcessingPriority());
260             ModelInfo modelInfo = network.getModelInfo();
261             if (modelInfo != null) {
262                 networkResource.setModelCustomizationId(modelInfo.getModelCustomizationUuid());
263                 networkResource.setModelVersionId(modelInfo.getModelVersionId());
264             }
265             resourceList.add(networkResource);
266         }
267         if (requestAction.equals(CREATE_INSTANCE)) {
268             String networkColCustId = queryCatalogDbForNetworkCollection(execution, serviceModelVersionId);
269             if (networkColCustId != null) {
270                 resourceList
271                         .add(new Resource(WorkflowType.NETWORKCOLLECTION, networkColCustId, false, serviceResource));
272             }
273         }
274     }
275
276
277     private List<CvnfcConfigurationCustomization> traverseCatalogDbForConfiguration(String serviceModelUUID) {
278         try {
279             List<CvnfcCustomization> cvnfcCustomizations = catalogDbClient.getCvnfcCustomization(serviceModelUUID,
280                     vnfCustomizationUUID, vfModuleCustomizationUUID);
281             return getCvnfcConfigurationCustomizations(cvnfcCustomizations);
282         } catch (Exception ex) {
283             logger.error("Error in finding configurations", ex);
284             return Collections.emptyList();
285         }
286     }
287
288     private List<CvnfcConfigurationCustomization> getCvnfcConfigurationCustomizations(
289             List<CvnfcCustomization> cvnfcCustomizations) {
290         List<CvnfcConfigurationCustomization> configurations = new ArrayList<>();
291         for (CvnfcCustomization cvnfc : cvnfcCustomizations) {
292             for (CvnfcConfigurationCustomization customization : cvnfc.getCvnfcConfigurationCustomization()) {
293                 if (customization.getConfigurationResource().getToscaNodeType().contains(FABRIC_CONFIGURATION)) {
294                     configurations.add(customization);
295                 }
296             }
297         }
298         logger.debug("found {} fabric configuration(s)", configurations.size());
299         return configurations;
300     }
301
302     private String queryCatalogDbForNetworkCollection(DelegateExecution execution, String serviceModelVersionId) {
303         org.onap.so.db.catalog.beans.Service service = catalogDbClient.getServiceByID(serviceModelVersionId);
304         if (service != null) {
305             CollectionResourceCustomization networkCollection = this.findCatalogNetworkCollection(execution, service);
306             if (networkCollection != null) {
307                 return networkCollection.getModelCustomizationUUID();
308             }
309         }
310         return null;
311     }
312
313     private CollectionResourceCustomization findCatalogNetworkCollection(DelegateExecution execution,
314             org.onap.so.db.catalog.beans.Service service) {
315         CollectionResourceCustomization networkCollection = null;
316         int count = 0;
317         for (CollectionResourceCustomization collectionCustom : service.getCollectionResourceCustomizations()) {
318             if (catalogDbClient.getNetworkCollectionResourceCustomizationByID(
319                     collectionCustom.getModelCustomizationUUID()) != null) {
320                 networkCollection = collectionCustom;
321                 count++;
322             }
323         }
324         if (count > 1) {
325             buildAndThrowException(execution,
326                     "Found multiple Network Collections in the Service model, only one per Service is supported.");
327         }
328         return networkCollection;
329     }
330
331     private void buildAndThrowException(DelegateExecution execution, String msg) {
332         logger.error(msg);
333         execution.setVariable(WORKFLOW_ACTION_ERROR_MESSAGE, msg);
334         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
335     }
336 }