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;
36 import org.yaml.snakeyaml.Yaml;
38 import org.onap.so.db.catalog.beans.HeatTemplateParam;
40 public class YamlEditor {
42 private static final String REFER_PATTERN = "file:///";
43 private Map <String, Object> yml;
44 private Yaml yaml = new Yaml ();
46 public YamlEditor () {
50 public YamlEditor (byte[] body) {
54 @SuppressWarnings("unchecked")
55 private synchronized void init (byte[] body) {
56 InputStream input = new ByteArrayInputStream (body);
57 yml = (Map <String, Object>) yaml.load (input);
60 public synchronized List <String> getYamlNestedFileResourceTypeList () {
61 List <String> typeList = new ArrayList<>();
63 @SuppressWarnings("unchecked")
64 Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("resources");
65 Iterator <Entry <String, Object>> it = resourceMap.entrySet ().iterator ();
66 while (it.hasNext ()) {
67 Map.Entry <String, Object> pair = it.next ();
68 @SuppressWarnings("unchecked")
69 Map <String, String> resourceEntry = (Map <String, String>) pair.getValue ();
70 String type = resourceEntry.get ("type");
72 if (type.contains (REFER_PATTERN)) {
75 it.remove (); // avoids a ConcurrentModificationException
80 public synchronized List <String> getYamlResourceTypeList () {
81 List <String> typeList = new ArrayList<>();
83 @SuppressWarnings("unchecked")
84 Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("resources");
85 for (Entry<String, Object> pair : resourceMap.entrySet()) {
86 @SuppressWarnings("unchecked")
87 Map<String, String> resourceEntry = (Map<String, String>) pair.getValue();
88 typeList.add(resourceEntry.get("type"));
93 // Generate the parameter list based on the Heat Template
94 // Based on the email from Ella Kvetny:
95 // Within Heat Template, under parameters catalog, it might indicate the default value of the parameter
96 // If default value exist, the parameter is not mandatory, otherwise its value should be set
97 public synchronized Set <HeatTemplateParam> getParameterList (String artifactUUID) {
98 Set <HeatTemplateParam> paramSet = new HashSet<>();
99 @SuppressWarnings("unchecked")
100 Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("parameters");
102 for (Entry<String, Object> stringObjectEntry : resourceMap.entrySet()) {
103 HeatTemplateParam param = new HeatTemplateParam();
104 Entry<String, Object> pair = stringObjectEntry;
105 @SuppressWarnings("unchecked")
106 Map<String, String> resourceEntry = (Map<String, String>) pair.getValue();
108 param.setParamName(pair.getKey());
109 // System.out.println(pair.getKey()+":"+type);
110 if (resourceEntry.containsKey("default")) {
111 param.setRequired(false);
113 param.setRequired(true);
116 String value = resourceEntry.get("type");
117 param.setParamType(value);
119 param.setHeatTemplateArtifactUuid(artifactUUID);
128 public synchronized void addParameterList (Set <HeatTemplateParam> heatSet) {
130 @SuppressWarnings("unchecked")
131 Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("parameters");
132 if (resourceMap == null) {
133 resourceMap = new LinkedHashMap<>();
134 this.yml.put ("parameters", resourceMap);
136 for (HeatTemplateParam heatParam : heatSet) {
137 Map <String, Object> paramInfo = new HashMap<>();
138 paramInfo.put ("type", heatParam.getParamType ());
140 resourceMap.put (heatParam.getParamName (), paramInfo);
143 // this.yml.put("parameters", resourceMap);
147 public boolean isParentTemplate (String templateBody) {
148 return templateBody.contains (REFER_PATTERN);
151 public boolean verifyTemplate () {
152 // Verify whether the heat template is for Vnf Resource
153 // We don't support other template installation yet
158 public String encode (Map <String, Object> content) {
159 return yaml.dump (content);
162 public synchronized String encode () {
163 return this.yaml.dump (this.yml);
167 * This method return the YAml file as a string.
171 public String toString () {