Fix the policy_id not set
[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 import java.util.LinkedHashMap;
28 import java.util.Map;
29 import java.util.Set;
30 import org.onap.clamp.clds.util.JsonUtils;
31 import org.onap.clamp.loop.Loop;
32 import org.onap.clamp.loop.components.external.DcaeComponent;
33 import org.onap.clamp.policy.microservice.MicroServicePolicy;
34 import org.yaml.snakeyaml.Yaml;
35
36 /**
37  * To decode the bluprint input parameters.
38  */
39 public class DcaeDeployParameters {
40
41     private static LinkedHashMap<String, JsonObject> init(Loop loop) {
42         LinkedHashMap<String, JsonObject> deploymentParamMap = new LinkedHashMap<>();
43         Set<MicroServicePolicy> microServiceList = loop.getMicroServicePolicies();
44
45         for (MicroServicePolicy microService : microServiceList) {
46             deploymentParamMap.put(microService.getName(),
47                     generateDcaeDeployParameter(microService));
48         }
49         return deploymentParamMap;
50     }
51
52     private static JsonObject generateDcaeDeployParameter(MicroServicePolicy microService) {
53         return generateDcaeDeployParameter(microService.getLoopElementModel().getBlueprint(),
54                 microService.getName());
55     }
56
57     private static JsonObject generateDcaeDeployParameter(String blueprint, String policyId) {
58         JsonObject deployJsonBody = new JsonObject();
59         Yaml yaml = new Yaml();
60         Map<String, Object> inputsNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
61                 .load(blueprint)).get("inputs"));
62         inputsNodes.entrySet().stream().filter(e -> !e.getKey().contains("policy_id")).forEach(elem -> {
63             Object defaultValue = ((Map<String, Object>) elem.getValue()).get("default");
64             if (defaultValue != null) {
65                 addPropertyToNode(deployJsonBody, elem.getKey(), defaultValue);
66             }
67             else {
68                 deployJsonBody.addProperty(elem.getKey(), "");
69             }
70         });
71         deployJsonBody.addProperty("policy_id", policyId);
72         return deployJsonBody;
73     }
74
75     private static void addPropertyToNode(JsonObject node, String key, Object value) {
76         if (value instanceof String) {
77             node.addProperty(key, (String) value);
78         }
79         else if (value instanceof Number) {
80             node.addProperty(key, (Number) value);
81         }
82         else if (value instanceof Boolean) {
83             node.addProperty(key, (Boolean) value);
84         }
85         else if (value instanceof Character) {
86             node.addProperty(key, (Character) value);
87         }
88         else {
89             node.addProperty(key, JsonUtils.GSON.toJson(value));
90         }
91     }
92
93     /**
94      * Convert the object in Json.
95      *
96      * @return The deploymentParameters in Json
97      */
98     public static JsonObject getDcaeDeploymentParametersInJson(Loop loop) {
99         JsonObject globalProperties = new JsonObject();
100         JsonObject deployParamJson = new JsonObject();
101         if (loop.getLoopTemplate().getUniqueBlueprint()) {
102             // Normally the unique blueprint could contain multiple microservices but then we can't guess
103             // the policy id params that will be used, so here we expect only one by default.
104             deployParamJson.add(DcaeComponent.UNIQUE_BLUEPRINT_PARAMETERS,
105                     generateDcaeDeployParameter(loop.getLoopTemplate().getBlueprint(),
106                             ((MicroServicePolicy) loop.getMicroServicePolicies().toArray()[0]).getName()));
107
108         }
109         else {
110             LinkedHashMap<String, JsonObject> deploymentParamMap = init(loop);
111             for (Map.Entry<String, JsonObject> mapElement : deploymentParamMap.entrySet()) {
112                 deployParamJson.add(mapElement.getKey(), mapElement.getValue());
113             }
114         }
115         globalProperties.add("dcaeDeployParameters", deployParamJson);
116         return globalProperties;
117     }
118
119 }