Change the header to SO
[so.git] / asdc-controller / src / main / java / org / openecomp / mso / asdc / util / YamlEditor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.mso.asdc.util;
22
23
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;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Set;
35
36 import org.yaml.snakeyaml.Yaml;
37
38 import org.openecomp.mso.db.catalog.beans.HeatTemplateParam;
39
40 public class YamlEditor {
41
42     private static final String REFER_PATTERN = "file:///";
43     private Map <String, Object> yml;
44     private Yaml yaml = new Yaml ();
45
46     public YamlEditor () {
47
48     }
49
50     public YamlEditor (byte[] body) {
51         init (body);
52     }
53
54     @SuppressWarnings("unchecked")
55     private synchronized void init (byte[] body) {
56         InputStream input = new ByteArrayInputStream (body);
57         yml = (Map <String, Object>) yaml.load (input);
58     }
59
60     public synchronized List <String> getYamlNestedFileResourceTypeList () {
61         List <String> typeList = new ArrayList <String> ();
62
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");
71
72             if (type.contains (REFER_PATTERN)) {
73                 typeList.add (type);
74             }
75             it.remove (); // avoids a ConcurrentModificationException
76         }
77         return typeList;
78     }
79     
80     public synchronized List <String> getYamlResourceTypeList () {
81         List <String> typeList = new ArrayList <String> ();
82
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"));
91         }
92         return typeList;
93     }    
94
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 ();
104
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 ();
110             
111             param.setParamName (pair.getKey ());
112             // System.out.println(pair.getKey()+":"+type);
113             if (resourceEntry.containsKey("default")) {
114                 param.setRequired (false);
115             } else {
116                 param.setRequired (true);
117             }
118             // Now set the type
119             String value = resourceEntry.get ("type");
120             param.setParamType (value);
121             
122             param.setHeatTemplateArtifactUuid(artifactUUID);
123
124             paramSet.add (param);
125
126         }
127         return paramSet;
128
129     }
130
131     public synchronized void addParameterList (Set <HeatTemplateParam> heatSet) {
132
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);
138         }
139         for (HeatTemplateParam heatParam : heatSet) {
140             Map <String, Object> paramInfo = new HashMap <String, Object> ();
141             paramInfo.put ("type", heatParam.getParamType ());
142
143             resourceMap.put (heatParam.getParamName (), paramInfo);
144         }
145
146         // this.yml.put("parameters", resourceMap);
147
148     }
149
150     public boolean isParentTemplate (String templateBody) {
151         return templateBody.contains (REFER_PATTERN);
152     }
153
154     public boolean verifyTemplate () {
155         // Verify whether the heat template is for Vnf Resource
156         // We don't support other template installation yet
157
158         return true;
159     }
160
161     public String encode (Map <String, Object> content) {
162         return yaml.dump (content);
163     }
164
165     public synchronized String encode () {
166         return this.yaml.dump (this.yml);
167     }
168
169     /**
170      * This method return the YAml file as a string.
171      * 
172      */
173     @Override
174     public String toString () {
175
176         return encode ();
177     }
178
179 }