2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 Nokia
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.exception.PayloadGenerationException;
25 import org.onap.so.serviceinstancebeans.Service;
26 import org.onap.so.serviceinstancebeans.Pnfs;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Component;
29 import java.util.List;
31 import java.util.Optional;
34 public class ConfigureInstanceParamsForPnf {
36 private ExtractServiceFromUserParameters extractServiceFromUserParameters;
39 public ConfigureInstanceParamsForPnf(ExtractServiceFromUserParameters extractServiceFromUserParameters) {
40 this.extractServiceFromUserParameters = extractServiceFromUserParameters;
44 * Read instance parameters for PNF and put into JsonObject.
46 * @param jsonObject - JsonObject which will hold the payload to send to CDS.
47 * @param userParamsFromRequest - User parameters.
48 * @param modelCustomizationUuid - Unique ID for Pnf.
49 * @throws PayloadGenerationException if it doesn't able to populate instance parameters from SO payload.
51 public void populateInstanceParams(JsonObject jsonObject, List<Map<String, Object>> userParamsFromRequest,
52 String modelCustomizationUuid) throws PayloadGenerationException {
54 Service service = extractServiceFromUserParameters.getServiceFromRequestUserParams(userParamsFromRequest);
55 List<Map<String, String>> instanceParamsList = getInstanceParamForPnf(service, modelCustomizationUuid);
57 instanceParamsList.stream().flatMap(instanceParamsMap -> instanceParamsMap.entrySet().stream())
58 .forEachOrdered(entry -> jsonObject.addProperty(entry.getKey(), entry.getValue()));
59 } catch (Exception exception) {
60 throw new PayloadGenerationException("Couldn't able to resolve instance parameters", exception);
64 private List<Map<String, String>> getInstanceParamForPnf(Service service, String genericPnfModelCustomizationUuid)
65 throws PayloadGenerationException {
66 Optional<Pnfs> foundedPnfs = service.getResources().getPnfs().stream()
67 .filter(pnfs -> pnfs.getModelInfo().getModelCustomizationId().equals(genericPnfModelCustomizationUuid))
69 if (foundedPnfs.isPresent()) {
70 return foundedPnfs.get().getInstanceParams();
72 throw new PayloadGenerationException(String.format(
73 "Can not find pnf for genericPnfModelCustomizationUuid: %s", genericPnfModelCustomizationUuid));