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() != null
108 && vfModuleCustomization.getVfModule().getModelName() != null
109 && vfModuleCustomization.getVfModule().getModelName()
110 .contains("helm"))) {
111 foundVfModuleOrVG = true;
112 Resource resource = new Resource(WorkflowType.VFMODULE,
113 vfModuleCustomization.getModelCustomizationUUID(), false);
114 resource.setBaseVfModule(
115 vfModuleCustomization.getVfModule().getIsBase() != null
116 && vfModuleCustomization.getVfModule().getIsBase());
117 resourceList.add(resource);
118 if (vfModule.getModelInfo() != null
119 && vfModule.getModelInfo().getModelCustomizationUuid() != null) {
120 vfModuleCustomizationUUID =
121 vfModule.getModelInfo().getModelCustomizationUuid();
123 if (!vnfCustomizationUUID.isEmpty()
124 && !vfModuleCustomizationUUID.isEmpty()) {
125 List<CvnfcConfigurationCustomization> configs =
126 traverseCatalogDbForConfiguration(
127 validate.getModelInfo().getModelVersionId(),
128 vnfCustomizationUUID, vfModuleCustomizationUUID);
129 for (CvnfcConfigurationCustomization config : configs) {
130 Resource configResource = new Resource(WorkflowType.CONFIGURATION,
131 config.getConfigurationResource().getModelUUID(), false);
132 resource.setVnfCustomizationId(
133 vnf.getModelInfo().getModelCustomizationId());
134 resource.setVfModuleCustomizationId(
135 vfModule.getModelInfo().getModelCustomizationId());
136 resourceList.add(configResource);
140 if (!foundVfModuleOrVG) {
141 buildAndThrowException(execution,
142 "Could not determine if vfModule was a vfModule or volume group. Heat template and Heat env are null");
149 if (validate.getResources().getPnfs() != null) {
150 for (Pnfs pnf : validate.getResources().getPnfs()) {
151 resourceList.add(new Resource(WorkflowType.PNF,
152 pnf.getModelInfo().getModelCustomizationId(), false));
155 if (validate.getResources().getNetworks() != null) {
156 for (Networks network : validate.getResources().getNetworks()) {
157 resourceList.add(new Resource(WorkflowType.NETWORK,
158 network.getModelInfo().getModelCustomizationId(), false));
160 if (requestAction.equals(CREATE_INSTANCE)) {
161 String networkColCustId =
162 queryCatalogDbForNetworkCollection(execution, serviceModelVersionId);
163 if (networkColCustId != null) {
164 resourceList.add(new Resource(WorkflowType.NETWORKCOLLECTION, networkColCustId, false));
176 private List<CvnfcConfigurationCustomization> traverseCatalogDbForConfiguration(String serviceModelUUID,
177 String vnfCustomizationUUID, String vfModuleCustomizationUUID) {
178 List<CvnfcConfigurationCustomization> configurations = new ArrayList<>();
180 List<CvnfcCustomization> cvnfcCustomizations = catalogDbClient.getCvnfcCustomization(serviceModelUUID,
181 vnfCustomizationUUID, vfModuleCustomizationUUID);
182 for (CvnfcCustomization cvnfc : cvnfcCustomizations) {
183 for (CvnfcConfigurationCustomization customization : cvnfc.getCvnfcConfigurationCustomization()) {
184 if (customization.getConfigurationResource().getToscaNodeType().contains(FABRIC_CONFIGURATION)) {
185 configurations.add(customization);
189 logger.debug("found {} fabric configuration(s)", configurations.size());
190 return configurations;
191 } catch (Exception ex) {
192 logger.error("Error in finding configurations", ex);
193 return configurations;
197 private String queryCatalogDbForNetworkCollection(DelegateExecution execution, String serviceModelVersionId) {
198 org.onap.so.db.catalog.beans.Service service = catalogDbClient.getServiceByID(serviceModelVersionId);
199 if (service != null) {
200 CollectionResourceCustomization networkCollection = this.findCatalogNetworkCollection(execution, service);
201 if (networkCollection != null) {
202 return networkCollection.getModelCustomizationUUID();
208 private CollectionResourceCustomization findCatalogNetworkCollection(DelegateExecution execution,
209 org.onap.so.db.catalog.beans.Service service) {
210 CollectionResourceCustomization networkCollection = null;
212 for (CollectionResourceCustomization collectionCustom : service.getCollectionResourceCustomizations()) {
213 if (catalogDbClient.getNetworkCollectionResourceCustomizationByID(
214 collectionCustom.getModelCustomizationUUID()) != null) {
215 networkCollection = collectionCustom;
221 } else if (count > 1) {
222 buildAndThrowException(execution,
223 "Found multiple Network Collections in the Service model, only one per Service is supported.");
225 return networkCollection;
228 private void buildAndThrowException(DelegateExecution execution, String msg) {
230 execution.setVariable(WORKFLOW_ACTION_ERROR_MESSAGE, msg);
231 exceptionBuilder.buildAndThrowWorkflowException(execution, 7000, msg);