43283ac98ba1c7c801bb200f96780226b8ecfa76
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / cds / ConfigureInstanceParamsForVnf.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.so.client.cds;
22
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;
31 import java.util.Map;
32 import java.util.Optional;
33
34 @Component
35 public class ConfigureInstanceParamsForVnf {
36
37     @Autowired
38     private ExtractServiceFromUserParameters extractServiceFromUserParameters;
39
40     /**
41      * Read instance parameters for VNF and put into JsonObject.
42      *
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.
47      */
48     public void populateInstanceParams(JsonObject jsonObject, List<Map<String, Object>> userParamsFromRequest,
49             String modelCustomizationUuid, String vnfInstanceName) throws PayloadGenerationException {
50         try {
51             Service service = extractServiceFromUserParameters.getServiceFromRequestUserParams(userParamsFromRequest);
52
53             List<Map<String, String>> instanceParamsList;
54             if (StringUtils.isNotBlank(vnfInstanceName)) {
55                 instanceParamsList = getInstanceParamByVnfInstanceName(service, vnfInstanceName);
56             } else {
57                 instanceParamsList = getInstanceParamForVnf(service, modelCustomizationUuid);
58             }
59
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);
64         }
65     }
66
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));
72     }
73
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))
78                 .findFirst();
79
80         if (foundedVnf.isPresent()) {
81             return foundedVnf.get().getInstanceParams();
82         } else {
83             throw new PayloadGenerationException(String.format(
84                     "Can not find vnf for genericVnfModelCustomizationUuid: %s", genericVnfModelCustomizationUuid));
85         }
86     }
87 }