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;
27 import java.util.LinkedHashMap;
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;
37 * To decode the bluprint input parameters.
39 public class DcaeDeployParameters {
41 private static LinkedHashMap<String, JsonObject> init(Loop loop) {
42 LinkedHashMap<String, JsonObject> deploymentParamMap = new LinkedHashMap<>();
43 Set<MicroServicePolicy> microServiceList = loop.getMicroServicePolicies();
45 for (MicroServicePolicy microService : microServiceList) {
46 deploymentParamMap.put(microService.getName(),
47 generateDcaeDeployParameter(microService));
49 return deploymentParamMap;
52 private static JsonObject generateDcaeDeployParameter(MicroServicePolicy microService) {
53 return generateDcaeDeployParameter(microService.getLoopElementModel().getBlueprint(),
54 microService.getName());
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);
68 deployJsonBody.addProperty(elem.getKey(), "");
71 deployJsonBody.addProperty("policy_id", policyId);
72 return deployJsonBody;
75 private static void addPropertyToNode(JsonObject node, String key, Object value) {
76 if (value instanceof String) {
77 node.addProperty(key, (String) value);
79 else if (value instanceof Number) {
80 node.addProperty(key, (Number) value);
82 else if (value instanceof Boolean) {
83 node.addProperty(key, (Boolean) value);
85 else if (value instanceof Character) {
86 node.addProperty(key, (Character) value);
89 node.addProperty(key, JsonUtils.GSON.toJson(value));
94 * Convert the object in Json.
96 * @return The deploymentParameters in Json
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()));
110 LinkedHashMap<String, JsonObject> deploymentParamMap = init(loop);
111 for (Map.Entry<String, JsonObject> mapElement : deploymentParamMap.entrySet()) {
112 deployParamJson.add(mapElement.getKey(), mapElement.getValue());
115 globalProperties.add("dcaeDeployParameters", deployParamJson);
116 return globalProperties;