2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.loop.deploy;
26 import com.google.gson.JsonObject;
28 import java.util.LinkedHashMap;
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;
38 * To decode the bluprint input parameters.
40 public class DcaeDeployParameters {
42 private static LinkedHashMap<String, JsonObject> init(Loop loop) {
43 LinkedHashMap<String, JsonObject> deploymentParamMap = new LinkedHashMap<>();
44 Set<MicroServicePolicy> microServiceList = loop.getMicroServicePolicies();
46 for (MicroServicePolicy microService : microServiceList) {
47 deploymentParamMap.put(microService.getName(),
48 generateDcaeDeployParameter(microService));
50 return deploymentParamMap;
53 private static JsonObject generateDcaeDeployParameter(MicroServicePolicy microService) {
54 return generateDcaeDeployParameter(microService.getLoopElementModel().getBlueprint(),
55 microService.getName());
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);
68 deployJsonBody.addProperty(elem.getKey(), "");
71 // For Dublin only one micro service is expected
72 deployJsonBody.addProperty("policy_id", tabName);
73 return deployJsonBody;
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);
86 node.addProperty(key, JsonUtils.GSON.toJson(value));
91 * Convert the object in Json.
93 * @return The deploymentParameters in Json
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));
102 LinkedHashMap<String, JsonObject> deploymentParamMap = init(loop);
103 for (Map.Entry<String, JsonObject> mapElement : deploymentParamMap.entrySet()) {
104 deployParamJson.add(mapElement.getKey(), mapElement.getValue());
107 globalProperties.add("dcaeDeployParameters", deployParamJson);
108 return globalProperties;