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.openecomp.mso.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.openecomp.mso.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 <String> ();
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 <String> ();
83 @SuppressWarnings("unchecked")
84 Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("resources");
85 Iterator <Entry <String, Object>> it = resourceMap.entrySet ().iterator ();
86 while (it.hasNext ()) {
87 Map.Entry <String, Object> pair = it.next ();
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 <HeatTemplateParam> ();
101 @SuppressWarnings("unchecked")
102 Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("parameters");
103 Iterator <Entry <String, Object>> it = resourceMap.entrySet ().iterator ();
105 while (it.hasNext ()) {
106 HeatTemplateParam param = new HeatTemplateParam ();
107 Map.Entry <String, Object> pair = it.next ();
108 @SuppressWarnings("unchecked")
109 Map <String, String> resourceEntry = (Map <String, String>) pair.getValue ();
111 param.setParamName (pair.getKey ());
112 // System.out.println(pair.getKey()+":"+type);
113 if (resourceEntry.containsKey("default")) {
114 param.setRequired (false);
116 param.setRequired (true);
119 String value = resourceEntry.get ("type");
120 param.setParamType (value);
122 param.setHeatTemplateArtifactUuid(artifactUUID);
124 paramSet.add (param);
131 public synchronized void addParameterList (Set <HeatTemplateParam> heatSet) {
133 @SuppressWarnings("unchecked")
134 Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("parameters");
135 if (resourceMap == null) {
136 resourceMap = new LinkedHashMap <String, Object> ();
137 this.yml.put ("parameters", resourceMap);
139 for (HeatTemplateParam heatParam : heatSet) {
140 Map <String, Object> paramInfo = new HashMap <String, Object> ();
141 paramInfo.put ("type", heatParam.getParamType ());
143 resourceMap.put (heatParam.getParamName (), paramInfo);
146 // this.yml.put("parameters", resourceMap);
150 public boolean isParentTemplate (String templateBody) {
151 return templateBody.contains (REFER_PATTERN);
154 public boolean verifyTemplate () {
155 // Verify whether the heat template is for Vnf Resource
156 // We don't support other template installation yet
161 public String encode (Map <String, Object> content) {
162 return yaml.dump (content);
165 public synchronized String encode () {
166 return this.yaml.dump (this.yml);
170 * This method return the YAml file as a string.
174 public String toString () {