Enable ControllerExecutionBB for service scope
[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 static org.onap.so.client.cds.ConfigureInstanceParamsUtil.applyParamsToObject;
24 import com.google.gson.JsonObject;
25 import java.util.Optional;
26 import org.apache.commons.lang3.StringUtils;
27 import org.onap.so.client.exception.PayloadGenerationException;
28 import org.onap.so.serviceinstancebeans.Service;
29 import org.onap.so.serviceinstancebeans.VfModules;
30 import org.onap.so.serviceinstancebeans.Vnfs;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Component;
33 import java.util.List;
34 import java.util.Map;
35
36 @Component
37 public class ConfigureInstanceParamsForVfModule {
38
39     @Autowired
40     private ExtractServiceFromUserParameters extractServiceFromUserParameters;
41
42     /**
43      * Read instance parameters for VF-Module and put into JsonObject.
44      *
45      * @param jsonObject- JsonObject which will hold the payload to send to CDS.
46      * @param userParamsFromRequest - User parameters for a vf-module
47      * @param vnfCustomizationUuid - Unique ID for vnf.
48      * @param vfModuleCustomizationUuid - Unique ID for vf-module.
49      * @throws PayloadGenerationException- If it doesn't able to populate instance parameters from SO payload.
50      */
51     public void populateInstanceParams(JsonObject jsonObject, List<Map<String, Object>> userParamsFromRequest,
52             String vnfCustomizationUuid, String vfModuleCustomizationUuid, String vfModuleInstanceName)
53             throws PayloadGenerationException {
54         try {
55             Optional<Service> service =
56                     extractServiceFromUserParameters.getServiceFromRequestUserParams(userParamsFromRequest);
57
58             if (service.isPresent()) {
59                 List<Map<String, String>> instanceParamsList;
60                 if (StringUtils.isNotBlank(vfModuleInstanceName)) {
61                     instanceParamsList = getInstanceParamsByInstanceNames(service.get(), vfModuleInstanceName);
62                 } else {
63                     instanceParamsList =
64                             getInstanceParams(service.get(), vnfCustomizationUuid, vfModuleCustomizationUuid);
65                 }
66
67                 applyParamsToObject(instanceParamsList, jsonObject);
68             }
69         } catch (Exception e) {
70             throw new PayloadGenerationException("Failed to resolve instance parameters", e);
71         }
72     }
73
74     private List<Map<String, String>> getInstanceParamsByInstanceNames(Service service, String vfModuleInstanceName)
75             throws PayloadGenerationException {
76         return service.getResources().getVnfs().stream().map(Vnfs::getVfModules).flatMap(List::stream)
77                 .filter(vfm -> vfModuleInstanceName.equals(vfm.getInstanceName())).findFirst()
78                 .map(VfModules::getInstanceParams).orElseThrow(() -> new PayloadGenerationException(
79                         "Could not find vfModule with instanceName: " + vfModuleInstanceName));
80     }
81
82     private List<Map<String, String>> getInstanceParams(Service service, String vnfCustomizationUuid,
83             String vfModuleCustomizationUuid) throws PayloadGenerationException {
84
85         Vnfs foundedVnf = service.getResources().getVnfs().stream()
86                 .filter(vnfs -> vnfs.getModelInfo().getModelCustomizationId().equals(vnfCustomizationUuid)).findFirst()
87                 .orElseThrow(() -> new PayloadGenerationException(String
88                         .format("Can not find vnf for genericVnfModelCustomizationUuid: %s", vnfCustomizationUuid)));
89
90         VfModules vfModule = foundedVnf.getVfModules().stream().filter(
91                 vfModules -> vfModules.getModelInfo().getModelCustomizationId().equals(vfModuleCustomizationUuid))
92                 .findFirst().orElseThrow(() -> new PayloadGenerationException(String
93                         .format("Can not find vnf for vfModuleCustomizationUuid: %s", vfModuleCustomizationUuid)));
94
95         return vfModule.getInstanceParams();
96     }
97 }