2 * ============LICENSE_START=======================================================
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
17 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
27 package org.onap.so.bpmn.infrastructure.workflow.tasks;
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;
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;
55 public class UserParamsServiceTraversal {
57 private static final Logger logger = LoggerFactory.getLogger(UserParamsServiceTraversal.class);
59 private final CatalogDbClient catalogDbClient;
60 private final ExceptionBuilder exceptionBuilder;
62 UserParamsServiceTraversal(CatalogDbClient catalogDbClient, ExceptionBuilder exceptionBuilder) {
63 this.catalogDbClient = catalogDbClient;
64 this.exceptionBuilder = exceptionBuilder;
67 protected List<Resource> getResourceListFromUserParams(DelegateExecution execution,
68 List<Map<String, Object>> userParams, String serviceModelVersionId, String requestAction)
70 List<Resource> resourceList = new ArrayList<>();
71 boolean foundVfModuleOrVG = false;
72 String vnfCustomizationUUID = "";
73 String vfModuleCustomizationUUID = "";
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);
81 new Resource(WorkflowType.SERVICE, validate.getModelInfo().getModelVersionId(), false));
82 if (validate.getResources().getVnfs() != null) {
83 for (Vnfs vnf : validate.getResources().getVnfs()) {
84 resourceList.add(new Resource(WorkflowType.VNF,
85 vnf.getModelInfo().getModelCustomizationId(), false));
86 if (vnf.getModelInfo() != null && vnf.getModelInfo().getModelCustomizationUuid() != null) {
87 vnfCustomizationUUID = vnf.getModelInfo().getModelCustomizationUuid();
89 if (vnf.getVfModules() != null) {
90 for (VfModules vfModule : vnf.getVfModules()) {
91 VfModuleCustomization vfModuleCustomization =
92 catalogDbClient.getVfModuleCustomizationByModelCuztomizationUUID(
93 vfModule.getModelInfo().getModelCustomizationUuid());
94 if (vfModuleCustomization != null) {
96 if (vfModuleCustomization.getVfModule() != null
97 && vfModuleCustomization.getVfModule().getVolumeHeatTemplate() != null
98 && vfModuleCustomization.getVolumeHeatEnv() != null) {
99 resourceList.add(new Resource(WorkflowType.VOLUMEGROUP,
100 vfModuleCustomization.getModelCustomizationUUID(), false));
101 foundVfModuleOrVG = true;
104 if ((vfModuleCustomization.getVfModule() != null)
105 && ((vfModuleCustomization.getVfModule().getModuleHeatTemplate() != null
106 && vfModuleCustomization.getHeatEnvironment() != null))
107 || (vfModuleCustomization.getVfModule().getModelName() != null
108 && vfModuleCustomization.getVfModule().getModelName()
109 .contains("helm"))) {
110 foundVfModuleOrVG = true;
111 Resource resource = new Resource(WorkflowType.VFMODULE,
112 vfModuleCustomization.getModelCustomizationUUID(), false);
113 resource.setBaseVfModule(
114 vfModuleCustomization.getVfModule().getIsBase() != null
115 && vfModuleCustomization.getVfModule().getIsBase());
116 resourceList.add(resource);
117 if (vfModule.getModelInfo() != null
118 && vfModule.getModelInfo().getModelCustomizationUuid() != null) {
119 vfModuleCustomizationUUID =
120 vfModule.getModelInfo().getModelCustomizationUuid();
122 if (!vnfCustomizationUUID.isEmpty()
123 && !vfModuleCustomizationUUID.isEmpty()) {
124 List<CvnfcConfigurationCustomization> configs =
125 traverseCatalogDbForConfiguration(
126 validate.getModelInfo().getModelVersionId(),
127 vnfCustomizationUUID, vfModuleCustomizationUUID);
128 for (CvnfcConfigurationCustomization config : configs) {
129 Resource configResource = new Resource(WorkflowType.CONFIGURATION,
130 config.getConfigurationResource().getModelUUID(), false);
131 resource.setVnfCustomizationId(
132 vnf.getModelInfo().getModelCustomizationId());
133 resource.setVfModuleCustomizationId(
134 vfModule.getModelInfo().getModelCustomizationId());
135 resourceList.add(configResource);
139 if (!foundVfModuleOrVG) {
140 buildAndThrowException(execution,
141 "Could not determine if vfModule was a vfModule or volume group. Heat template and Heat env are null");
148 if (validate.getResources().getPnfs() != null) {
149 for (Pnfs pnf : validate.getResources().getPnfs()) {
150 resourceList.add(new Resource(WorkflowType.PNF,
151 pnf.getModelInfo().getModelCustomizationId(), false));
154 if (validate.getResources().getNetworks() != null) {
155 for (Networks network : validate.getResources().getNetworks()) {
156 resourceList.add(new Resource(WorkflowType.NETWORK,
157 network.getModelInfo().getModelCustomizationId(), false));
159 if (requestAction.equals(CREATE_INSTANCE)) {
160 String networkColCustId =
161 queryCatalogDbForNetworkCollection(execution, serviceModelVersionId);
162 if (networkColCustId != null) {
163 resourceList.add(new Resource(WorkflowType.NETWORKCOLLECTION, networkColCustId, false));
175 private List<CvnfcConfigurationCustomization> traverseCatalogDbForConfiguration(String serviceModelUUID,
176 String vnfCustomizationUUID, String vfModuleCustomizationUUID) {
177 List<CvnfcConfigurationCustomization> configurations = new ArrayList<>();
179 List<CvnfcCustomization> cvnfcCustomizations = catalogDbClient.getCvnfcCustomization(serviceModelUUID,
180 vnfCustomizationUUID, vfModuleCustomizationUUID);
181 for (CvnfcCustomization cvnfc : cvnfcCustomizations) {
182 for (CvnfcConfigurationCustomization customization : cvnfc.getCvnfcConfigurationCustomization()) {
183 if (customization.getConfigurationResource().getToscaNodeType().contains(FABRIC_CONFIGURATION)) {
184 configurations.add(customization);
188 logger.debug("found {} fabric configuration(s)", configurations.size());
189 return configurations;
190 } catch (Exception ex) {
191 logger.error("Error in finding configurations", ex);
192 return configurations;
196 private String queryCatalogDbForNetworkCollection(DelegateExecution execution, String serviceModelVersionId) {
197 org.onap.so.db.catalog.beans.Service service = catalogDbClient.getServiceByID(serviceModelVersionId);
198 if (service != null) {
199 CollectionResourceCustomization networkCollection = this.findCatalogNetworkCollection(execution, service);
200 if (networkCollection != null) {
201 return networkCollection.getModelCustomizationUUID();
207 private CollectionResourceCustomization findCatalogNetworkCollection(DelegateExecution execution,
208 org.onap.so.db.catalog.beans.Service service) {
209 CollectionResourceCustomization networkCollection = null;
211 for (CollectionResourceCustomization collectionCustom : service.getCollectionResourceCustomizations()) {
212 if (catalogDbClient.getNetworkCollectionResourceCustomizationByID(
213 collectionCustom.getModelCustomizationUUID()) != null) {
214 networkCollection = collectionCustom;
220 } else if (count > 1) {
221 buildAndThrowException(execution,
222 "Found multiple Network Collections in the Service model, only one per Service is supported.");
224 return networkCollection;
227 private void buildAndThrowException(DelegateExecution execution, String msg) {
229 execution.setVariable(WORKFLOW_ACTION_ERROR_MESSAGE, msg);
230 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);