3418ee32951d61ca1e42c5a21c4c6d0a08121dd4
[so.git] / asdc-controller / src / main / java / org / onap / so / 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.onap.so.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.onap.so.db.catalog.beans.HeatTemplateParam;
39
40 public class YamlEditor {
41
42     protected static final String REFER_PATTERN = "file:///";
43     protected Map <String, Object> yml;
44     protected Yaml yaml = new Yaml ();
45
46     public YamlEditor () {
47
48     }
49
50     public YamlEditor (byte[] body) {
51         init (body);
52     }
53     
54     public YamlEditor (Yaml yaml) {
55         this.yaml = yaml;
56     }
57
58     @SuppressWarnings("unchecked")
59     protected synchronized void init (byte[] body) {
60         InputStream input = new ByteArrayInputStream (body);
61         yml = (Map <String, Object>) yaml.load (input);
62     }
63
64     public synchronized List <String> getYamlNestedFileResourceTypeList () {
65         List <String> typeList = new ArrayList<>();
66
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");
75
76             if (type.contains (REFER_PATTERN)) {
77                 typeList.add (type);
78             }
79             it.remove (); // avoids a ConcurrentModificationException
80         }
81         return typeList;
82     }
83     
84     public synchronized List <String> getYamlResourceTypeList () {
85         List <String> typeList = new ArrayList<>();
86
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"));
93         }
94         return typeList;
95     }    
96
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");
105
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();
111
112             param.setParamName(pair.getKey());
113             // System.out.println(pair.getKey()+":"+type);
114             if (resourceEntry.containsKey("default")) {
115                 param.setRequired(false);
116             } else {
117                 param.setRequired(true);
118             }
119             // Now set the type
120             String value = resourceEntry.get("type");
121             param.setParamType(value);
122
123             param.setHeatTemplateArtifactUuid(artifactUUID);
124
125             paramSet.add(param);
126
127         }
128         return paramSet;
129
130     }
131
132     public synchronized void addParameterList (Set <HeatTemplateParam> heatSet) {
133
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);
139         }
140         for (HeatTemplateParam heatParam : heatSet) {
141             Map <String, Object> paramInfo = new HashMap<>();
142             paramInfo.put ("type", heatParam.getParamType ());
143
144             resourceMap.put (heatParam.getParamName (), paramInfo);
145         }
146
147         // this.yml.put("parameters", resourceMap);
148
149     }
150
151     public boolean isParentTemplate (String templateBody) {
152         return templateBody.contains (REFER_PATTERN);
153     }
154
155     public boolean verifyTemplate () {
156         // Verify whether the heat template is for Vnf Resource
157         // We don't support other template installation yet
158
159         return true;
160     }
161
162     public String encode (Map <String, Object> content) {
163         return yaml.dump (content);
164     }
165
166     public synchronized String encode () {
167         return this.yaml.dump (this.yml);
168     }
169
170     /**
171      * This method return the YAml file as a string.
172      * 
173      */
174     @Override
175     public String toString () {
176
177         return encode ();
178     }
179
180 }