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.onap.so.client.cds.ExtractServiceFromUserParameters;
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) throws PayloadGenerationException {
52 Service service = extractServiceFromUserParameters.getServiceFromRequestUserParams(userParamsFromRequest);
54 List<Map<String, String>> instanceParamsList =
55 getInstanceParams(service, vnfCustomizationUuid, vfModuleCustomizationUuid);
57 instanceParamsList.stream().flatMap(instanceParamsMap -> instanceParamsMap.entrySet().stream())
58 .forEachOrdered(entry -> jsonObject.addProperty(entry.getKey(), entry.getValue()));
59 } catch (Exception e) {
60 throw new PayloadGenerationException("Couldn't able to resolve instance parameters", e);
64 private List<Map<String, String>> getInstanceParams(Service service, String vnfCustomizationUuid,
65 String vfModuleCustomizationUuid) throws PayloadGenerationException {
67 Vnfs foundedVnf = service.getResources().getVnfs().stream()
68 .filter(vnfs -> vnfs.getModelInfo().getModelCustomizationId().equals(vnfCustomizationUuid)).findFirst()
69 .orElseThrow(() -> new PayloadGenerationException(String
70 .format("Can not find vnf for genericVnfModelCustomizationUuid: %s", vnfCustomizationUuid)));
72 VfModules vfModule = foundedVnf.getVfModules().stream().filter(
73 vfModules -> vfModules.getModelInfo().getModelCustomizationId().equals(vfModuleCustomizationUuid))
74 .findFirst().orElseThrow(() -> new PayloadGenerationException(String
75 .format("Can not find vnf for vfModuleCustomizationUuid: %s", vfModuleCustomizationUuid)));
77 return vfModule.getInstanceParams();