0d79dbb375ac857c4b9b230839c4f47511708155
[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 com.google.gson.JsonObject;
24 import org.onap.so.client.exception.PayloadGenerationException;
25 import org.onap.so.serviceinstancebeans.Service;
26 import org.onap.so.serviceinstancebeans.Pnfs;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Component;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Optional;
32
33 @Component
34 public class ConfigureInstanceParamsForPnf {
35
36     private ExtractServiceFromUserParameters extractServiceFromUserParameters;
37
38     @Autowired
39     public ConfigureInstanceParamsForPnf(ExtractServiceFromUserParameters extractServiceFromUserParameters) {
40         this.extractServiceFromUserParameters = extractServiceFromUserParameters;
41     }
42
43     /**
44      * Read instance parameters for PNF and put into JsonObject.
45      *
46      * @param jsonObject - JsonObject which will hold the payload to send to CDS.
47      * @param userParamsFromRequest - User parameters.
48      * @param modelCustomizationUuid - Unique ID for Pnf.
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 modelCustomizationUuid) throws PayloadGenerationException {
53         try {
54             Service service = extractServiceFromUserParameters.getServiceFromRequestUserParams(userParamsFromRequest);
55             List<Map<String, String>> instanceParamsList = getInstanceParamForPnf(service, modelCustomizationUuid);
56
57             instanceParamsList.stream().flatMap(instanceParamsMap -> instanceParamsMap.entrySet().stream())
58                     .forEachOrdered(entry -> jsonObject.addProperty(entry.getKey(), entry.getValue()));
59         } catch (Exception exception) {
60             throw new PayloadGenerationException("Couldn't able to resolve instance parameters", exception);
61         }
62     }
63
64     private List<Map<String, String>> getInstanceParamForPnf(Service service, String genericPnfModelCustomizationUuid)
65             throws PayloadGenerationException {
66         Optional<Pnfs> foundedPnfs = service.getResources().getPnfs().stream()
67                 .filter(pnfs -> pnfs.getModelInfo().getModelCustomizationId().equals(genericPnfModelCustomizationUuid))
68                 .findFirst();
69         if (foundedPnfs.isPresent()) {
70             return foundedPnfs.get().getInstanceParams();
71         } else {
72             throw new PayloadGenerationException(String.format(
73                     "Can not find pnf for genericPnfModelCustomizationUuid: %s", genericPnfModelCustomizationUuid));
74         }
75     }
76 }