Replaced all tabs with spaces in java and pom.xml
[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 import org.yaml.snakeyaml.Yaml;
36 import org.onap.so.db.catalog.beans.HeatTemplateParam;
37
38 public class YamlEditor {
39
40     protected static final String REFER_PATTERN = "file:///";
41     protected Map<String, Object> yml;
42     protected Yaml yaml = new Yaml();
43
44     public YamlEditor() {
45
46     }
47
48     public YamlEditor(byte[] body) {
49         init(body);
50     }
51
52     public YamlEditor(Yaml yaml) {
53         this.yaml = yaml;
54     }
55
56     @SuppressWarnings("unchecked")
57     protected synchronized void init(byte[] body) {
58         InputStream input = new ByteArrayInputStream(body);
59         yml = (Map<String, Object>) yaml.load(input);
60     }
61
62     public synchronized List<String> getYamlNestedFileResourceTypeList() {
63         List<String> typeList = new ArrayList<>();
64
65         @SuppressWarnings("unchecked")
66         Map<String, Object> resourceMap = (Map<String, Object>) yml.get("resources");
67         Iterator<Entry<String, Object>> it = resourceMap.entrySet().iterator();
68         while (it.hasNext()) {
69             Map.Entry<String, Object> pair = it.next();
70             @SuppressWarnings("unchecked")
71             Map<String, String> resourceEntry = (Map<String, String>) pair.getValue();
72             String type = resourceEntry.get("type");
73
74             if (type.contains(REFER_PATTERN)) {
75                 typeList.add(type);
76             }
77             it.remove(); // avoids a ConcurrentModificationException
78         }
79         return typeList;
80     }
81
82     public synchronized List<String> getYamlResourceTypeList() {
83         List<String> typeList = new ArrayList<>();
84
85         @SuppressWarnings("unchecked")
86         Map<String, Object> resourceMap = (Map<String, Object>) yml.get("resources");
87         for (Entry<String, Object> pair : resourceMap.entrySet()) {
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<>();
101         @SuppressWarnings("unchecked")
102         Map<String, Object> resourceMap = (Map<String, Object>) yml.get("parameters");
103
104         for (Entry<String, Object> stringObjectEntry : resourceMap.entrySet()) {
105             HeatTemplateParam param = new HeatTemplateParam();
106             Entry<String, Object> pair = stringObjectEntry;
107             @SuppressWarnings("unchecked")
108             Map<String, String> resourceEntry = (Map<String, String>) pair.getValue();
109
110             param.setParamName(pair.getKey());
111             // System.out.println(pair.getKey()+":"+type);
112             if (resourceEntry.containsKey("default")) {
113                 param.setRequired(false);
114             } else {
115                 param.setRequired(true);
116             }
117             // Now set the type
118             String value = resourceEntry.get("type");
119             param.setParamType(value);
120
121             param.setHeatTemplateArtifactUuid(artifactUUID);
122
123             paramSet.add(param);
124
125         }
126         return paramSet;
127
128     }
129
130     public synchronized void addParameterList(Set<HeatTemplateParam> heatSet) {
131
132         @SuppressWarnings("unchecked")
133         Map<String, Object> resourceMap = (Map<String, Object>) yml.get("parameters");
134         if (resourceMap == null) {
135             resourceMap = new LinkedHashMap<>();
136             this.yml.put("parameters", resourceMap);
137         }
138         for (HeatTemplateParam heatParam : heatSet) {
139             Map<String, Object> paramInfo = new HashMap<>();
140             paramInfo.put("type", heatParam.getParamType());
141
142             resourceMap.put(heatParam.getParamName(), paramInfo);
143         }
144
145         // this.yml.put("parameters", resourceMap);
146
147     }
148
149     public boolean isParentTemplate(String templateBody) {
150         return templateBody.contains(REFER_PATTERN);
151     }
152
153     public boolean verifyTemplate() {
154         // Verify whether the heat template is for Vnf Resource
155         // We don't support other template installation yet
156
157         return true;
158     }
159
160     public String encode(Map<String, Object> content) {
161         return yaml.dump(content);
162     }
163
164     public synchronized String encode() {
165         return this.yaml.dump(this.yml);
166     }
167
168     /**
169      * This method return the YAml file as a string.
170      * 
171      */
172     @Override
173     public String toString() {
174
175         return encode();
176     }
177
178 }