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