aa1108cbb905ace40876589c253fd87d6f00a796
[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.List;
48 import java.util.Map;
49 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.CREATE_INSTANCE;
50 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.FABRIC_CONFIGURATION;
51 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.USER_PARAM_SERVICE;
52 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.WORKFLOW_ACTION_ERROR_MESSAGE;
53
54 @Component
55 public class UserParamsServiceTraversal {
56
57     private static final Logger logger = LoggerFactory.getLogger(UserParamsServiceTraversal.class);
58
59     private final CatalogDbClient catalogDbClient;
60     private final ExceptionBuilder exceptionBuilder;
61     private boolean foundVfModuleOrVG;
62
63     UserParamsServiceTraversal(CatalogDbClient catalogDbClient, ExceptionBuilder exceptionBuilder) {
64         this.catalogDbClient = catalogDbClient;
65         this.exceptionBuilder = exceptionBuilder;
66     }
67
68     protected List<Resource> getResourceListFromUserParams(DelegateExecution execution,
69             List<Map<String, Object>> userParams, String serviceModelVersionId, String requestAction)
70             throws IOException {
71         List<Resource> resourceList = new ArrayList<>();
72         if (userParams != null) {
73             for (Map<String, Object> params : userParams) {
74                 if (params.containsKey(USER_PARAM_SERVICE)) {
75                     ObjectMapper obj = new ObjectMapper();
76                     String input = obj.writeValueAsString(params.get(USER_PARAM_SERVICE));
77                     Service validate = obj.readValue(input, Service.class);
78                     setResourceList(execution, serviceModelVersionId, requestAction, resourceList, validate);
79                     break;
80                 }
81             }
82         }
83         return resourceList;
84     }
85
86     private void setResourceList(DelegateExecution execution, String serviceModelVersionId, String requestAction,
87             List<Resource> resourceList, Service validate) {
88         resourceList.add(new Resource(WorkflowType.SERVICE, validate.getModelInfo().getModelVersionId(), false));
89         if (validate.getResources().getVnfs() != null) {
90             setResourceListForVnfs(execution, resourceList, validate);
91         }
92         if (validate.getResources().getPnfs() != null) {
93             setResourceListForPnfs(resourceList, validate);
94         }
95         if (validate.getResources().getNetworks() != null) {
96             setResourceListForNetworks(execution, serviceModelVersionId, requestAction, resourceList, validate);
97         }
98     }
99
100     private void setResourceListForVnfs(DelegateExecution execution, List<Resource> resourceList, Service validate) {
101         foundVfModuleOrVG = false;
102         for (Vnfs vnf : validate.getResources().getVnfs()) {
103             resourceList.add(new Resource(WorkflowType.VNF, vnf.getModelInfo().getModelCustomizationId(), false));
104             setResourceListForVfModules(execution, resourceList, validate, vnf);
105         }
106     }
107
108     private void setResourceListForVfModules(DelegateExecution execution, List<Resource> resourceList, Service validate,
109             Vnfs vnf) {
110         if (vnf.getVfModules() != null) {
111             for (VfModules vfModule : vnf.getVfModules()) {
112                 VfModuleCustomization vfModuleCustomization =
113                         catalogDbClient.getVfModuleCustomizationByModelCuztomizationUUID(
114                                 vfModule.getModelInfo().getModelCustomizationUuid());
115                 if (vfModuleCustomization != null) {
116                     setVolumeGroupWorkFlowTypeToResourceList(resourceList, vfModuleCustomization);
117                     setVfModuleAndConfigurationWorkFlowTypeToResourceList(resourceList, validate, vnf, vfModule,
118                             vfModuleCustomization);
119                     if (!foundVfModuleOrVG) {
120                         buildAndThrowException(execution,
121                                 "Could not determine if vfModule was a vfModule or volume group. Heat template and Heat env are null");
122                     }
123                 }
124             }
125         }
126     }
127
128     private void setVolumeGroupWorkFlowTypeToResourceList(List<Resource> resourceList,
129             VfModuleCustomization vfModuleCustomization) {
130         if (vfModuleCustomization.getVfModule() != null
131                 && vfModuleCustomization.getVfModule().getVolumeHeatTemplate() != null
132                 && vfModuleCustomization.getVolumeHeatEnv() != null) {
133             foundVfModuleOrVG = true;
134             resourceList.add(
135                     new Resource(WorkflowType.VOLUMEGROUP, vfModuleCustomization.getModelCustomizationUUID(), false));
136         }
137     }
138
139     private void setVfModuleAndConfigurationWorkFlowTypeToResourceList(List<Resource> resourceList, Service validate,
140             Vnfs vnf, VfModules vfModule, VfModuleCustomization vfModuleCustomization) {
141         if ((vfModuleCustomization.getVfModule() != null)
142                 && ((vfModuleCustomization.getVfModule().getModuleHeatTemplate() != null
143                         && vfModuleCustomization.getHeatEnvironment() != null))
144                 || (vfModuleCustomization.getVfModule() != 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         String vfModuleCustomizationUUID = getVfModuleCustomizationUUID(vfModule);
166         String vnfCustomizationUUID = getVnfCustomizationUUID(vnf);
167         if (!vnfCustomizationUUID.isEmpty() && !vfModuleCustomizationUUID.isEmpty()) {
168             List<CvnfcConfigurationCustomization> configs = traverseCatalogDbForConfiguration(
169                     validate.getModelInfo().getModelVersionId(), vnfCustomizationUUID, vfModuleCustomizationUUID);
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 String getVfModuleCustomizationUUID(VfModules vfModule) {
181         String vfModuleCustomizationUUID;
182         if (vfModule.getModelInfo() != null && vfModule.getModelInfo().getModelCustomizationUuid() != null) {
183             vfModuleCustomizationUUID = vfModule.getModelInfo().getModelCustomizationUuid();
184         } else {
185             vfModuleCustomizationUUID = "";
186         }
187         return vfModuleCustomizationUUID;
188     }
189
190     private String getVnfCustomizationUUID(Vnfs vnf) {
191         String vnfCustomizationUUID;
192         if (vnf.getModelInfo() != null && vnf.getModelInfo().getModelCustomizationUuid() != null) {
193             vnfCustomizationUUID = vnf.getModelInfo().getModelCustomizationUuid();
194         } else {
195             vnfCustomizationUUID = "";
196         }
197         return vnfCustomizationUUID;
198     }
199
200     private void setResourceListForPnfs(List<Resource> resourceList, Service validate) {
201         for (Pnfs pnf : validate.getResources().getPnfs()) {
202             resourceList.add(new Resource(WorkflowType.PNF, pnf.getModelInfo().getModelCustomizationId(), false));
203         }
204     }
205
206     private void setResourceListForNetworks(DelegateExecution execution, String serviceModelVersionId,
207             String requestAction, List<Resource> resourceList, Service validate) {
208         for (Networks network : validate.getResources().getNetworks()) {
209             resourceList
210                     .add(new Resource(WorkflowType.NETWORK, network.getModelInfo().getModelCustomizationId(), false));
211         }
212         if (requestAction.equals(CREATE_INSTANCE)) {
213             String networkColCustId = queryCatalogDbForNetworkCollection(execution, serviceModelVersionId);
214             if (networkColCustId != null) {
215                 resourceList.add(new Resource(WorkflowType.NETWORKCOLLECTION, networkColCustId, false));
216             }
217         }
218     }
219
220
221     private List<CvnfcConfigurationCustomization> traverseCatalogDbForConfiguration(String serviceModelUUID,
222             String vnfCustomizationUUID, String vfModuleCustomizationUUID) {
223         List<CvnfcConfigurationCustomization> configurations = new ArrayList<>();
224         try {
225             List<CvnfcCustomization> cvnfcCustomizations = catalogDbClient.getCvnfcCustomization(serviceModelUUID,
226                     vnfCustomizationUUID, vfModuleCustomizationUUID);
227             for (CvnfcCustomization cvnfc : cvnfcCustomizations) {
228                 for (CvnfcConfigurationCustomization customization : cvnfc.getCvnfcConfigurationCustomization()) {
229                     if (customization.getConfigurationResource().getToscaNodeType().contains(FABRIC_CONFIGURATION)) {
230                         configurations.add(customization);
231                     }
232                 }
233             }
234             logger.debug("found {} fabric configuration(s)", configurations.size());
235             return configurations;
236         } catch (Exception ex) {
237             logger.error("Error in finding configurations", ex);
238             return configurations;
239         }
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 == 0) {
265             return null;
266         } else if (count > 1) {
267             buildAndThrowException(execution,
268                     "Found multiple Network Collections in the Service model, only one per Service is supported.");
269         }
270         return networkCollection;
271     }
272
273     private void buildAndThrowException(DelegateExecution execution, String msg) {
274         logger.error(msg);
275         execution.setVariable(WORKFLOW_ACTION_ERROR_MESSAGE, msg);
276         exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);
277     }
278 }