Update based on comments
[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.LinkedHashSet;
30 import java.util.Map;
31
32 import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
33 import org.onap.clamp.clds.util.JsonUtils;
34 import org.onap.clamp.loop.Loop;
35 import org.onap.clamp.policy.microservice.MicroServicePolicy;
36 import org.yaml.snakeyaml.Yaml;
37
38 /**
39  * To decode the bluprint input parameters.
40  */
41 public class DcaeDeployParameters  {
42
43     private static LinkedHashMap<String, JsonObject> init(LinkedHashSet<BlueprintArtifact> blueprintArtifactList,
44             Loop loop) {
45         LinkedHashMap<String, JsonObject> deploymentParamMap = new LinkedHashMap<String, JsonObject>();
46         String microServiceName = ((MicroServicePolicy) loop.getMicroServicePolicies().toArray()[0]).getName();
47         // Add index to the microservice name from the 2nd blueprint artifact for now.
48         // Update the microservice names, when able to link the microserivce <-> blueprint in the future
49         int index = 0;
50         for (BlueprintArtifact blueprintArtifact: blueprintArtifactList) {
51             if (index > 0) {
52                 deploymentParamMap.put(microServiceName + index, 
53                         generateDcaeDeployParameter(blueprintArtifact, microServiceName));
54             } else {
55                 deploymentParamMap.put(microServiceName, 
56                         generateDcaeDeployParameter(blueprintArtifact, microServiceName));
57             }
58             index++;
59         }
60         return deploymentParamMap;
61     }
62
63     private static JsonObject generateDcaeDeployParameter(BlueprintArtifact blueprintArtifact,
64             String microServiceName) {
65         JsonObject deployJsonBody = new JsonObject();
66         Yaml yaml = new Yaml();
67         Map<String, Object> inputsNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
68             .load(blueprintArtifact.getDcaeBlueprint())).get("inputs"));
69         inputsNodes.entrySet().stream().filter(e -> !e.getKey().contains("policy_id")).forEach(elem -> {
70             Object defaultValue = ((Map<String, Object>) elem.getValue()).get("default");
71             if (defaultValue != null) {
72                 addPropertyToNode(deployJsonBody, elem.getKey(), defaultValue);
73             } else {
74                 deployJsonBody.addProperty(elem.getKey(), "");
75             }
76         });
77         // For Dublin only one micro service is expected
78         deployJsonBody.addProperty("policy_id", microServiceName);
79         return deployJsonBody;
80     }
81
82     private static void addPropertyToNode(JsonObject node, String key, Object value) {
83         if (value instanceof String) {
84             node.addProperty(key, (String) value);
85         } else if (value instanceof Number) {
86             node.addProperty(key, (Number) value);
87         } else if (value instanceof Boolean) {
88             node.addProperty(key, (Boolean) value);
89         } else if (value instanceof Character) {
90             node.addProperty(key, (Character) value);
91         } else {
92             node.addProperty(key, JsonUtils.GSON.toJson(value));
93         }
94     }
95
96     /**
97      * Convert the object in Json.
98      *
99      * @return The deploymentParameters in Json
100      */
101     public static JsonObject getDcaeDeploymentParametersInJson(LinkedHashSet<BlueprintArtifact> blueprintArtifactList,
102             Loop loop) {
103         LinkedHashMap<String, JsonObject> deploymentParamMap = init(blueprintArtifactList, loop);
104
105         JsonObject globalProperties = new JsonObject();
106         JsonObject deployParamJson = new JsonObject();
107         for (Map.Entry<String, JsonObject> mapElement: deploymentParamMap.entrySet()) {
108             deployParamJson.add(mapElement.getKey(), mapElement.getValue());
109         }
110         globalProperties.add("dcaeDeployParameters", deployParamJson);
111         return globalProperties;
112     }
113
114     /**
115      * Convert the object in Json.
116      *
117      * @return The deploymentParameters in Json
118      */
119     public static JsonObject getDcaeDeploymentParametersInJson(BlueprintArtifact blueprintArtifact, Loop loop) {
120         LinkedHashSet<BlueprintArtifact> blueprintArtifactList = new LinkedHashSet<BlueprintArtifact>();
121         blueprintArtifactList.add(blueprintArtifact);
122         return getDcaeDeploymentParametersInJson(blueprintArtifactList, loop);
123     }
124 }