2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Bell Canada
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client.cds;
23 import com.google.gson.JsonObject;
24 import org.apache.commons.lang3.StringUtils;
25 import org.onap.so.client.exception.PayloadGenerationException;
26 import org.onap.so.serviceinstancebeans.Service;
27 import org.onap.so.serviceinstancebeans.VfModules;
28 import org.onap.so.serviceinstancebeans.Vnfs;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Component;
31 import java.util.List;
35 public class ConfigureInstanceParamsForVfModule {
38 private ExtractServiceFromUserParameters extractServiceFromUserParameters;
41 * Read instance parameters for VF-Module and put into JsonObject.
43 * @param jsonObject- JsonObject which will hold the payload to send to CDS.
44 * @param userParamsFromRequest - User parameters for a vf-module
45 * @param vnfCustomizationUuid - Unique ID for vnf.
46 * @param vfModuleCustomizationUuid - Unique ID for vf-module.
47 * @throws PayloadGenerationException- If it doesn't able to populate instance parameters from SO payload.
49 public void populateInstanceParams(JsonObject jsonObject, List<Map<String, Object>> userParamsFromRequest,
50 String vnfCustomizationUuid, String vfModuleCustomizationUuid, String vfModuleInstanceName)
51 throws PayloadGenerationException {
53 Service service = extractServiceFromUserParameters.getServiceFromRequestUserParams(userParamsFromRequest);
55 List<Map<String, String>> instanceParamsList;
56 if (StringUtils.isNotBlank(vfModuleInstanceName)) {
57 instanceParamsList = getInstanceParamsByInstanceNames(service, vfModuleInstanceName);
59 instanceParamsList = getInstanceParams(service, vnfCustomizationUuid, vfModuleCustomizationUuid);
62 instanceParamsList.stream().flatMap(instanceParamsMap -> instanceParamsMap.entrySet().stream())
63 .forEachOrdered(entry -> jsonObject.addProperty(entry.getKey(), entry.getValue()));
64 } catch (Exception e) {
65 throw new PayloadGenerationException("Couldn't able to resolve instance parameters", e);
69 private List<Map<String, String>> getInstanceParamsByInstanceNames(Service service, String vfModuleInstanceName)
70 throws PayloadGenerationException {
71 return service.getResources().getVnfs().stream().map(Vnfs::getVfModules).flatMap(List::stream)
72 .filter(vfm -> vfModuleInstanceName.equals(vfm.getInstanceName())).findFirst()
73 .map(VfModules::getInstanceParams).orElseThrow(() -> new PayloadGenerationException(
74 "Could not find vfModule with instanceName: " + vfModuleInstanceName));
77 private List<Map<String, String>> getInstanceParams(Service service, String vnfCustomizationUuid,
78 String vfModuleCustomizationUuid) throws PayloadGenerationException {
80 Vnfs foundedVnf = service.getResources().getVnfs().stream()
81 .filter(vnfs -> vnfs.getModelInfo().getModelCustomizationId().equals(vnfCustomizationUuid)).findFirst()
82 .orElseThrow(() -> new PayloadGenerationException(String
83 .format("Can not find vnf for genericVnfModelCustomizationUuid: %s", vnfCustomizationUuid)));
85 VfModules vfModule = foundedVnf.getVfModules().stream().filter(
86 vfModules -> vfModules.getModelInfo().getModelCustomizationId().equals(vfModuleCustomizationUuid))
87 .findFirst().orElseThrow(() -> new PayloadGenerationException(String
88 .format("Can not find vnf for vfModuleCustomizationUuid: %s", vfModuleCustomizationUuid)));
90 return vfModule.getInstanceParams();