Enable ControllerExecutionBB for service scope
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / cds / ConfigureInstanceParamsForPnf.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 Nokia
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 org.onap.so.client.exception.PayloadGenerationException;
26 import org.onap.so.serviceinstancebeans.Service;
27 import org.onap.so.serviceinstancebeans.Pnfs;
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 ConfigureInstanceParamsForPnf {
36
37     private ExtractServiceFromUserParameters extractServiceFromUserParameters;
38
39     @Autowired
40     public ConfigureInstanceParamsForPnf(ExtractServiceFromUserParameters extractServiceFromUserParameters) {
41         this.extractServiceFromUserParameters = extractServiceFromUserParameters;
42     }
43
44     /**
45      * Read instance parameters for PNF and put into JsonObject.
46      *
47      * @param jsonObject - JsonObject which will hold the payload to send to CDS.
48      * @param userParamsFromRequest - User parameters.
49      * @param modelCustomizationUuid - Unique ID for Pnf.
50      * @throws PayloadGenerationException if it doesn't able to populate instance parameters from SO payload.
51      */
52     public void populateInstanceParams(JsonObject jsonObject, List<Map<String, Object>> userParamsFromRequest,
53             String modelCustomizationUuid) throws PayloadGenerationException {
54         try {
55             Optional<Service> service =
56                     extractServiceFromUserParameters.getServiceFromRequestUserParams(userParamsFromRequest);
57
58             if (service.isPresent()) {
59                 List<Map<String, String>> instanceParamsList =
60                         getInstanceParamForPnf(service.get(), modelCustomizationUuid);
61
62                 applyParamsToObject(instanceParamsList, jsonObject);
63             }
64         } catch (Exception exception) {
65             throw new PayloadGenerationException("Failed to resolve instance parameters", exception);
66         }
67     }
68
69     private List<Map<String, String>> getInstanceParamForPnf(Service service, String genericPnfModelCustomizationUuid)
70             throws PayloadGenerationException {
71         Optional<Pnfs> foundedPnfs = service.getResources().getPnfs().stream()
72                 .filter(pnfs -> pnfs.getModelInfo().getModelCustomizationId().equals(genericPnfModelCustomizationUuid))
73                 .findFirst();
74         if (foundedPnfs.isPresent()) {
75             return foundedPnfs.get().getInstanceParams();
76         } else {
77             throw new PayloadGenerationException(String.format(
78                     "Can not find pnf for genericPnfModelCustomizationUuid: %s", genericPnfModelCustomizationUuid));
79         }
80     }
81 }