2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.asdc.util;
24 import java.io.ByteArrayInputStream;
25 import java.io.InputStream;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.LinkedHashMap;
31 import java.util.List;
33 import java.util.Map.Entry;
35 import org.yaml.snakeyaml.Yaml;
36 import org.onap.so.db.catalog.beans.HeatTemplateParam;
38 public class YamlEditor {
40 protected static final String REFER_PATTERN = "file:///";
41 protected Map<String, Object> yml;
42 protected Yaml yaml = new Yaml();
48 public YamlEditor(byte[] body) {
52 public YamlEditor(Yaml yaml) {
56 @SuppressWarnings("unchecked")
57 protected synchronized void init(byte[] body) {
58 InputStream input = new ByteArrayInputStream(body);
59 yml = (Map<String, Object>) yaml.load(input);
62 public synchronized List<String> getYamlNestedFileResourceTypeList() {
63 List<String> typeList = new ArrayList<>();
65 @SuppressWarnings("unchecked")
66 Map<String, Object> resourceMap = (Map<String, Object>) yml.get("resources");
67 Iterator<Entry<String, Object>> it = resourceMap.entrySet().iterator();
68 while (it.hasNext()) {
69 Map.Entry<String, Object> pair = it.next();
70 @SuppressWarnings("unchecked")
71 Map<String, String> resourceEntry = (Map<String, String>) pair.getValue();
72 String type = resourceEntry.get("type");
74 if (type.contains(REFER_PATTERN)) {
77 it.remove(); // avoids a ConcurrentModificationException
82 public synchronized List<String> getYamlResourceTypeList() {
83 List<String> typeList = new ArrayList<>();
85 @SuppressWarnings("unchecked")
86 Map<String, Object> resourceMap = (Map<String, Object>) yml.get("resources");
87 for (Entry<String, Object> pair : resourceMap.entrySet()) {
88 @SuppressWarnings("unchecked")
89 Map<String, String> resourceEntry = (Map<String, String>) pair.getValue();
90 typeList.add(resourceEntry.get("type"));
95 // Generate the parameter list based on the Heat Template
96 // Based on the email from Ella Kvetny:
97 // Within Heat Template, under parameters catalog, it might indicate the default value of the parameter
98 // If default value exist, the parameter is not mandatory, otherwise its value should be set
99 public synchronized Set<HeatTemplateParam> getParameterList(String artifactUUID) {
100 Set<HeatTemplateParam> paramSet = new HashSet<>();
101 @SuppressWarnings("unchecked")
102 Map<String, Object> resourceMap = (Map<String, Object>) yml.get("parameters");
104 for (Entry<String, Object> stringObjectEntry : resourceMap.entrySet()) {
105 HeatTemplateParam param = new HeatTemplateParam();
106 Entry<String, Object> pair = stringObjectEntry;
107 @SuppressWarnings("unchecked")
108 Map<String, String> resourceEntry = (Map<String, String>) pair.getValue();
110 param.setParamName(pair.getKey());
111 // System.out.println(pair.getKey()+":"+type);
112 if (resourceEntry.containsKey("default")) {
113 param.setRequired(false);
115 param.setRequired(true);
118 String value = resourceEntry.get("type");
119 param.setParamType(value);
121 param.setHeatTemplateArtifactUuid(artifactUUID);
130 public synchronized void addParameterList(Set<HeatTemplateParam> heatSet) {
132 @SuppressWarnings("unchecked")
133 Map<String, Object> resourceMap = (Map<String, Object>) yml.get("parameters");
134 if (resourceMap == null) {
135 resourceMap = new LinkedHashMap<>();
136 this.yml.put("parameters", resourceMap);
138 for (HeatTemplateParam heatParam : heatSet) {
139 Map<String, Object> paramInfo = new HashMap<>();
140 paramInfo.put("type", heatParam.getParamType());
142 resourceMap.put(heatParam.getParamName(), paramInfo);
145 // this.yml.put("parameters", resourceMap);
149 public boolean isParentTemplate(String templateBody) {
150 return templateBody.contains(REFER_PATTERN);
153 public boolean verifyTemplate() {
154 // Verify whether the heat template is for Vnf Resource
155 // We don't support other template installation yet
160 public String encode(Map<String, Object> content) {
161 return yaml.dump(content);
164 public synchronized String encode() {
165 return this.yaml.dump(this.yml);
169 * This method return the YAml file as a string.
173 public String toString() {