Populate deployment parameters
[clamp.git] / src / main / java / org / onap / clamp / loop / deploy / DcaeDeployParameters.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop.deploy;
25
26 import com.google.gson.JsonObject;
27
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.Set;
31
32 import org.onap.clamp.clds.util.JsonUtils;
33 import org.onap.clamp.loop.Loop;
34 import org.onap.clamp.policy.microservice.MicroServicePolicy;
35 import org.yaml.snakeyaml.Yaml;
36
37 /**
38  * To decode the bluprint input parameters.
39  */
40 public class DcaeDeployParameters {
41
42     private static LinkedHashMap<String, JsonObject> init(Loop loop) {
43         LinkedHashMap<String, JsonObject> deploymentParamMap = new LinkedHashMap<>();
44         Set<MicroServicePolicy> microServiceList = loop.getMicroServicePolicies();
45
46         for (MicroServicePolicy microService : microServiceList) {
47                 deploymentParamMap.put(microService.getName(),
48                         generateDcaeDeployParameter(microService));
49         }
50         return deploymentParamMap;
51     }
52
53     private static JsonObject generateDcaeDeployParameter(MicroServicePolicy microService) {
54         return generateDcaeDeployParameter(microService.getLoopElementModel().getBlueprint(),
55                 microService.getName());
56     }
57
58     private static JsonObject generateDcaeDeployParameter(String blueprint, String tabName) {
59         JsonObject deployJsonBody = new JsonObject();
60         Yaml yaml = new Yaml();
61         Map<String, Object> inputsNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
62                 .load(blueprint)).get("inputs"));
63         inputsNodes.entrySet().stream().filter(e -> !e.getKey().contains("policy_id")).forEach(elem -> {
64             Object defaultValue = ((Map<String, Object>) elem.getValue()).get("default");
65             if (defaultValue != null) {
66                 addPropertyToNode(deployJsonBody, elem.getKey(), defaultValue);
67             } else {
68                 deployJsonBody.addProperty(elem.getKey(), "");
69             }
70         });
71         // For Dublin only one micro service is expected
72         deployJsonBody.addProperty("policy_id", tabName);
73         return deployJsonBody;
74     }
75
76     private static void addPropertyToNode(JsonObject node, String key, Object value) {
77         if (value instanceof String) {
78             node.addProperty(key, (String) value);
79         } else if (value instanceof Number) {
80             node.addProperty(key, (Number) value);
81         } else if (value instanceof Boolean) {
82             node.addProperty(key, (Boolean) value);
83         } else if (value instanceof Character) {
84             node.addProperty(key, (Character) value);
85         } else {
86             node.addProperty(key, JsonUtils.GSON.toJson(value));
87         }
88     }
89
90     /**
91      * Convert the object in Json.
92      *
93      * @return The deploymentParameters in Json
94      */
95     public static JsonObject getDcaeDeploymentParametersInJson(Loop loop) {
96         JsonObject globalProperties = new JsonObject();
97         JsonObject deployParamJson = new JsonObject();
98         if (loop.getLoopTemplate().getUniqueBlueprint()) {
99             String tabName = "loop template blueprint";
100             deployParamJson.add(tabName, generateDcaeDeployParameter(loop.getLoopTemplate().getBlueprint(), tabName));
101         } else {
102             LinkedHashMap<String, JsonObject> deploymentParamMap = init(loop);
103             for (Map.Entry<String, JsonObject> mapElement : deploymentParamMap.entrySet()) {
104                 deployParamJson.add(mapElement.getKey(), mapElement.getValue());
105             }
106         }
107         globalProperties.add("dcaeDeployParameters", deployParamJson);
108         return globalProperties;
109     }
110
111 }