Enable ControllerExecutionBB for service scope
[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 static org.onap.so.client.cds.ConfigureInstanceParamsUtil.applyParamsToObject;
24 import org.apache.commons.lang3.StringUtils;
25 import com.google.gson.JsonObject;
26 import org.onap.so.client.exception.PayloadGenerationException;
27 import org.onap.so.serviceinstancebeans.Service;
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 import java.util.Optional;
34
35 @Component
36 public class ConfigureInstanceParamsForVnf {
37
38     @Autowired
39     private ExtractServiceFromUserParameters extractServiceFromUserParameters;
40
41     /**
42      * Read instance parameters for VNF and put into JsonObject.
43      *
44      * @param jsonObject - JsonObject which will hold the payload to send to CDS.
45      * @param userParamsFromRequest - User parameters.
46      * @param modelCustomizationUuid - Unique ID for Vnf.
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 modelCustomizationUuid, String vnfInstanceName) throws PayloadGenerationException {
51         try {
52             Optional<Service> service =
53                     extractServiceFromUserParameters.getServiceFromRequestUserParams(userParamsFromRequest);
54
55             if (service.isPresent()) {
56                 List<Map<String, String>> instanceParamsList;
57                 if (StringUtils.isNotBlank(vnfInstanceName)) {
58                     instanceParamsList = getInstanceParamByVnfInstanceName(service.get(), vnfInstanceName);
59                 } else {
60                     instanceParamsList = getInstanceParamForVnf(service.get(), modelCustomizationUuid);
61                 }
62                 applyParamsToObject(instanceParamsList, jsonObject);
63             }
64         } catch (Exception e) {
65             throw new PayloadGenerationException("Failed to resolve instance parameters", e);
66         }
67     }
68
69     private List<Map<String, String>> getInstanceParamByVnfInstanceName(Service service, String instanceName)
70             throws PayloadGenerationException {
71         return service.getResources().getVnfs().stream().filter(vnf -> instanceName.equals(vnf.getInstanceName()))
72                 .findFirst().map(Vnfs::getInstanceParams).orElseThrow(
73                         () -> new PayloadGenerationException("Could not find vnf with instanceName: " + instanceName));
74     }
75
76     private List<Map<String, String>> getInstanceParamForVnf(Service service, String genericVnfModelCustomizationUuid)
77             throws PayloadGenerationException {
78         Optional<Vnfs> foundedVnf = service.getResources().getVnfs().stream()
79                 .filter(vnfs -> vnfs.getModelInfo().getModelCustomizationId().equals(genericVnfModelCustomizationUuid))
80                 .findFirst();
81
82         if (foundedVnf.isPresent()) {
83             return foundedVnf.get().getInstanceParams();
84         } else {
85             throw new PayloadGenerationException(String.format(
86                     "Can not find vnf for genericVnfModelCustomizationUuid: %s", genericVnfModelCustomizationUuid));
87         }
88     }
89 }