eeaecb926460da03b13ad615d0ec0feea86c6b5c
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / cds / ConfigureInstanceParamsForVfModule.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.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;
32 import java.util.Map;
33
34 @Component
35 public class ConfigureInstanceParamsForVfModule {
36
37     @Autowired
38     private ExtractServiceFromUserParameters extractServiceFromUserParameters;
39
40     /**
41      * Read instance parameters for VF-Module and put into JsonObject.
42      *
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.
48      */
49     public void populateInstanceParams(JsonObject jsonObject, List<Map<String, Object>> userParamsFromRequest,
50             String vnfCustomizationUuid, String vfModuleCustomizationUuid, String vfModuleInstanceName)
51             throws PayloadGenerationException {
52         try {
53             Service service = extractServiceFromUserParameters.getServiceFromRequestUserParams(userParamsFromRequest);
54
55             List<Map<String, String>> instanceParamsList;
56             if (StringUtils.isNotBlank(vfModuleInstanceName)) {
57                 instanceParamsList = getInstanceParamsByInstanceNames(service, vfModuleInstanceName);
58             } else {
59                 instanceParamsList = getInstanceParams(service, vnfCustomizationUuid, vfModuleCustomizationUuid);
60             }
61
62             instanceParamsList.stream().flatMap(instanceParamsMap -> instanceParamsMap.entrySet().stream())
63                     .forEachOrdered(entry -> jsonObject.addProperty(entry.getKey(), entry.getValue()));
64         } catch (Exception e) {
65             throw new PayloadGenerationException("Couldn't able to resolve instance parameters", e);
66         }
67     }
68
69     private List<Map<String, String>> getInstanceParamsByInstanceNames(Service service, String vfModuleInstanceName)
70             throws PayloadGenerationException {
71         return service.getResources().getVnfs().stream().map(Vnfs::getVfModules).flatMap(List::stream)
72                 .filter(vfm -> vfModuleInstanceName.equals(vfm.getInstanceName())).findFirst()
73                 .map(VfModules::getInstanceParams).orElseThrow(() -> new PayloadGenerationException(
74                         "Could not find vfModule with instanceName: " + vfModuleInstanceName));
75     }
76
77     private List<Map<String, String>> getInstanceParams(Service service, String vnfCustomizationUuid,
78             String vfModuleCustomizationUuid) throws PayloadGenerationException {
79
80         Vnfs foundedVnf = service.getResources().getVnfs().stream()
81                 .filter(vnfs -> vnfs.getModelInfo().getModelCustomizationId().equals(vnfCustomizationUuid)).findFirst()
82                 .orElseThrow(() -> new PayloadGenerationException(String
83                         .format("Can not find vnf for genericVnfModelCustomizationUuid: %s", vnfCustomizationUuid)));
84
85         VfModules vfModule = foundedVnf.getVfModules().stream().filter(
86                 vfModules -> vfModules.getModelInfo().getModelCustomizationId().equals(vfModuleCustomizationUuid))
87                 .findFirst().orElseThrow(() -> new PayloadGenerationException(String
88                         .format("Can not find vnf for vfModuleCustomizationUuid: %s", vfModuleCustomizationUuid)));
89
90         return vfModule.getInstanceParams();
91     }
92 }