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 protected static final String REFER_PATTERN = "file:///";
43 protected Map <String, Object> yml;
44 protected Yaml yaml = new Yaml ();
46 public YamlEditor () {
50 public YamlEditor (byte[] body) {
54 public YamlEditor (Yaml yaml) {
58 @SuppressWarnings("unchecked")
59 protected synchronized void init (byte[] body) {
60 InputStream input = new ByteArrayInputStream (body);
61 yml = (Map <String, Object>) yaml.load (input);
64 public synchronized List <String> getYamlNestedFileResourceTypeList () {
65 List <String> typeList = new ArrayList<>();
67 @SuppressWarnings("unchecked")
68 Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("resources");
69 Iterator <Entry <String, Object>> it = resourceMap.entrySet ().iterator ();
70 while (it.hasNext ()) {
71 Map.Entry <String, Object> pair = it.next ();
72 @SuppressWarnings("unchecked")
73 Map <String, String> resourceEntry = (Map <String, String>) pair.getValue ();
74 String type = resourceEntry.get ("type");
76 if (type.contains (REFER_PATTERN)) {
79 it.remove (); // avoids a ConcurrentModificationException
84 public synchronized List <String> getYamlResourceTypeList () {
85 List <String> typeList = new ArrayList<>();
87 @SuppressWarnings("unchecked")
88 Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("resources");
89 for (Entry<String, Object> pair : resourceMap.entrySet()) {
90 @SuppressWarnings("unchecked")
91 Map<String, String> resourceEntry = (Map<String, String>) pair.getValue();
92 typeList.add(resourceEntry.get("type"));
97 // Generate the parameter list based on the Heat Template
98 // Based on the email from Ella Kvetny:
99 // Within Heat Template, under parameters catalog, it might indicate the default value of the parameter
100 // If default value exist, the parameter is not mandatory, otherwise its value should be set
101 public synchronized Set <HeatTemplateParam> getParameterList (String artifactUUID) {
102 Set <HeatTemplateParam> paramSet = new HashSet<>();
103 @SuppressWarnings("unchecked")
104 Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("parameters");
106 for (Entry<String, Object> stringObjectEntry : resourceMap.entrySet()) {
107 HeatTemplateParam param = new HeatTemplateParam();
108 Entry<String, Object> pair = stringObjectEntry;
109 @SuppressWarnings("unchecked")
110 Map<String, String> resourceEntry = (Map<String, String>) pair.getValue();
112 param.setParamName(pair.getKey());
113 // System.out.println(pair.getKey()+":"+type);
114 if (resourceEntry.containsKey("default")) {
115 param.setRequired(false);
117 param.setRequired(true);
120 String value = resourceEntry.get("type");
121 param.setParamType(value);
123 param.setHeatTemplateArtifactUuid(artifactUUID);
132 public synchronized void addParameterList (Set <HeatTemplateParam> heatSet) {
134 @SuppressWarnings("unchecked")
135 Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("parameters");
136 if (resourceMap == null) {
137 resourceMap = new LinkedHashMap<>();
138 this.yml.put ("parameters", resourceMap);
140 for (HeatTemplateParam heatParam : heatSet) {
141 Map <String, Object> paramInfo = new HashMap<>();
142 paramInfo.put ("type", heatParam.getParamType ());
144 resourceMap.put (heatParam.getParamName (), paramInfo);
147 // this.yml.put("parameters", resourceMap);
151 public boolean isParentTemplate (String templateBody) {
152 return templateBody.contains (REFER_PATTERN);
155 public boolean verifyTemplate () {
156 // Verify whether the heat template is for Vnf Resource
157 // We don't support other template installation yet
162 public String encode (Map <String, Object> content) {
163 return yaml.dump (content);
166 public synchronized String encode () {
167 return this.yaml.dump (this.yml);
171 * This method return the YAml file as a string.
175 public String toString () {