6f850fa8987a2c04b14702c18479d18a8608a7e6
[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.onap.so.client.cds.ExtractServiceFromUserParameters;
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) throws PayloadGenerationException {
51         try {
52             Service service = extractServiceFromUserParameters.getServiceFromRequestUserParams(userParamsFromRequest);
53
54             List<Map<String, String>> instanceParamsList =
55                     getInstanceParams(service, vnfCustomizationUuid, vfModuleCustomizationUuid);
56
57             instanceParamsList.stream().flatMap(instanceParamsMap -> instanceParamsMap.entrySet().stream())
58                     .forEachOrdered(entry -> jsonObject.addProperty(entry.getKey(), entry.getValue()));
59         } catch (Exception e) {
60             throw new PayloadGenerationException("Couldn't able to resolve instance parameters", e);
61         }
62     }
63
64     private List<Map<String, String>> getInstanceParams(Service service, String vnfCustomizationUuid,
65             String vfModuleCustomizationUuid) throws PayloadGenerationException {
66
67         Vnfs foundedVnf = service.getResources().getVnfs().stream()
68                 .filter(vnfs -> vnfs.getModelInfo().getModelCustomizationId().equals(vnfCustomizationUuid)).findFirst()
69                 .orElseThrow(() -> new PayloadGenerationException(String
70                         .format("Can not find vnf for genericVnfModelCustomizationUuid: %s", vnfCustomizationUuid)));
71
72         VfModules vfModule = foundedVnf.getVfModules().stream().filter(
73                 vfModules -> vfModules.getModelInfo().getModelCustomizationId().equals(vfModuleCustomizationUuid))
74                 .findFirst().orElseThrow(() -> new PayloadGenerationException(String
75                         .format("Can not find vnf for vfModuleCustomizationUuid: %s", vfModuleCustomizationUuid)));
76
77         return vfModule.getInstanceParams();
78     }
79 }