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;
29 import java.util.LinkedHashSet;
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;
38 public class DeployParameters {
40 private LinkedHashMap<String, JsonObject> deploymentParamMap = new LinkedHashMap<String, JsonObject>();
45 * @param blueprintArtifactList A list of blueprint artifacts
46 * @param loop The loop object
48 public DeployParameters(LinkedHashSet<BlueprintArtifact> blueprintArtifactList, Loop loop) {
49 this.init(blueprintArtifactList, loop);
55 * @param blueprintArtifact One blueprint artifact
56 * @param loop The loop object
58 public DeployParameters(BlueprintArtifact blueprintArtifact, Loop loop) {
59 LinkedHashSet<BlueprintArtifact> blueprintArtifactList = new LinkedHashSet<BlueprintArtifact>();
60 blueprintArtifactList.add(blueprintArtifact);
61 this.init(blueprintArtifactList, loop);
64 private void init(LinkedHashSet<BlueprintArtifact> blueprintArtifactList, Loop loop) {
65 String microServiceName = ((MicroServicePolicy) loop.getMicroServicePolicies().toArray()[0]).getName();
66 // Add index to the microservice name from the 2nd blueprint artifact for now.
67 // Update the microservice names, when able to link the microserivce <-> blueprint in the future
69 for (BlueprintArtifact blueprintArtifact: blueprintArtifactList) {
71 deploymentParamMap.put(microServiceName + index,
72 generateDeployParameter(blueprintArtifact, microServiceName));
74 deploymentParamMap.put(microServiceName, generateDeployParameter(blueprintArtifact, microServiceName));
80 private JsonObject generateDeployParameter(BlueprintArtifact blueprintArtifact, String microServiceName) {
81 JsonObject deployJsonBody = new JsonObject();
82 Yaml yaml = new Yaml();
83 Map<String, Object> inputsNodes = ((Map<String, Object>) ((Map<String, Object>) yaml
84 .load(blueprintArtifact.getDcaeBlueprint())).get("inputs"));
85 inputsNodes.entrySet().stream().filter(e -> !e.getKey().contains("policy_id")).forEach(elem -> {
86 Object defaultValue = ((Map<String, Object>) elem.getValue()).get("default");
87 if (defaultValue != null) {
88 addPropertyToNode(deployJsonBody, elem.getKey(), defaultValue);
90 deployJsonBody.addProperty(elem.getKey(), "");
93 // For Dublin only one micro service is expected
94 deployJsonBody.addProperty("policy_id", microServiceName);
95 return deployJsonBody;
98 private void addPropertyToNode(JsonObject node, String key, Object value) {
99 if (value instanceof String) {
100 node.addProperty(key, (String) value);
101 } else if (value instanceof Number) {
102 node.addProperty(key, (Number) value);
103 } else if (value instanceof Boolean) {
104 node.addProperty(key, (Boolean) value);
105 } else if (value instanceof Character) {
106 node.addProperty(key, (Character) value);
108 node.addProperty(key, JsonUtils.GSON.toJson(value));
113 * Convert the object in Json.
115 * @return The deploymentParameters in Json
117 public JsonObject getDeploymentParametersinJson() {
118 JsonObject globalProperties = new JsonObject();
119 JsonObject deployParamJson = new JsonObject();
120 for (Map.Entry<String, JsonObject> mapElement: deploymentParamMap.entrySet()) {
121 deployParamJson.add(mapElement.getKey(), mapElement.getValue());
123 globalProperties.add("dcaeDeployParameters", deployParamJson);
124 return globalProperties;