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