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