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.Vnfs;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.stereotype.Component;
30 import java.util.List;
32 import java.util.Optional;
35 public class ConfigureInstanceParamsForVnf {
38 private ExtractServiceFromUserParameters extractServiceFromUserParameters;
41 * Read instance parameters for VNF and put into JsonObject.
43 * @param jsonObject - JsonObject which will hold the payload to send to CDS.
44 * @param userParamsFromRequest - User parameters.
45 * @param modelCustomizationUuid - Unique ID for Vnf.
46 * @throws PayloadGenerationException if it doesn't able to populate instance parameters from SO payload.
48 public void populateInstanceParams(JsonObject jsonObject, List<Map<String, Object>> userParamsFromRequest,
49 String modelCustomizationUuid, String vnfInstanceName) throws PayloadGenerationException {
51 Service service = extractServiceFromUserParameters.getServiceFromRequestUserParams(userParamsFromRequest);
53 List<Map<String, String>> instanceParamsList;
54 if (StringUtils.isNotBlank(vnfInstanceName)) {
55 instanceParamsList = getInstanceParamByVnfInstanceName(service, vnfInstanceName);
57 instanceParamsList = getInstanceParamForVnf(service, modelCustomizationUuid);
60 instanceParamsList.stream().flatMap(instanceParamsMap -> instanceParamsMap.entrySet().stream())
61 .forEachOrdered(entry -> jsonObject.addProperty(entry.getKey(), entry.getValue()));
62 } catch (Exception e) {
63 throw new PayloadGenerationException("Couldn't able to resolve instance parameters", e);
67 private List<Map<String, String>> getInstanceParamByVnfInstanceName(Service service, String instanceName)
68 throws PayloadGenerationException {
69 return service.getResources().getVnfs().stream().filter(vnf -> instanceName.equals(vnf.getInstanceName()))
70 .findFirst().map(Vnfs::getInstanceParams).orElseThrow(
71 () -> new PayloadGenerationException("Could not find vnf with instanceName: " + instanceName));
74 private List<Map<String, String>> getInstanceParamForVnf(Service service, String genericVnfModelCustomizationUuid)
75 throws PayloadGenerationException {
76 Optional<Vnfs> foundedVnf = service.getResources().getVnfs().stream()
77 .filter(vnfs -> vnfs.getModelInfo().getModelCustomizationId().equals(genericVnfModelCustomizationUuid))
80 if (foundedVnf.isPresent()) {
81 return foundedVnf.get().getInstanceParams();
83 throw new PayloadGenerationException(String.format(
84 "Can not find vnf for genericVnfModelCustomizationUuid: %s", genericVnfModelCustomizationUuid));