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 static org.onap.so.client.cds.ConfigureInstanceParamsUtil.applyParamsToObject;
24 import org.apache.commons.lang3.StringUtils;
25 import com.google.gson.JsonObject;
26 import org.onap.so.client.exception.PayloadGenerationException;
27 import org.onap.so.serviceinstancebeans.Service;
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;
33 import java.util.Optional;
36 public class ConfigureInstanceParamsForVnf {
39 private ExtractServiceFromUserParameters extractServiceFromUserParameters;
42 * Read instance parameters for VNF and put into JsonObject.
44 * @param jsonObject - JsonObject which will hold the payload to send to CDS.
45 * @param userParamsFromRequest - User parameters.
46 * @param modelCustomizationUuid - Unique ID for Vnf.
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 modelCustomizationUuid, String vnfInstanceName) throws PayloadGenerationException {
52 Optional<Service> service =
53 extractServiceFromUserParameters.getServiceFromRequestUserParams(userParamsFromRequest);
55 if (service.isPresent()) {
56 List<Map<String, String>> instanceParamsList;
57 if (StringUtils.isNotBlank(vnfInstanceName)) {
58 instanceParamsList = getInstanceParamByVnfInstanceName(service.get(), vnfInstanceName);
60 instanceParamsList = getInstanceParamForVnf(service.get(), modelCustomizationUuid);
62 applyParamsToObject(instanceParamsList, jsonObject);
64 } catch (Exception e) {
65 throw new PayloadGenerationException("Failed to resolve instance parameters", e);
69 private List<Map<String, String>> getInstanceParamByVnfInstanceName(Service service, String instanceName)
70 throws PayloadGenerationException {
71 return service.getResources().getVnfs().stream().filter(vnf -> instanceName.equals(vnf.getInstanceName()))
72 .findFirst().map(Vnfs::getInstanceParams).orElseThrow(
73 () -> new PayloadGenerationException("Could not find vnf with instanceName: " + instanceName));
76 private List<Map<String, String>> getInstanceParamForVnf(Service service, String genericVnfModelCustomizationUuid)
77 throws PayloadGenerationException {
78 Optional<Vnfs> foundedVnf = service.getResources().getVnfs().stream()
79 .filter(vnfs -> vnfs.getModelInfo().getModelCustomizationId().equals(genericVnfModelCustomizationUuid))
82 if (foundedVnf.isPresent()) {
83 return foundedVnf.get().getInstanceParams();
85 throw new PayloadGenerationException(String.format(
86 "Can not find vnf for genericVnfModelCustomizationUuid: %s", genericVnfModelCustomizationUuid));