Containerization feature of SO
[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     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<>();
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<>();
82
83         @SuppressWarnings("unchecked")
84         Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("resources");
85         for (Entry<String, Object> pair : resourceMap.entrySet()) {
86             @SuppressWarnings("unchecked")
87             Map<String, String> resourceEntry = (Map<String, String>) pair.getValue();
88             typeList.add(resourceEntry.get("type"));
89         }
90         return typeList;
91     }    
92
93     // Generate the parameter list based on the Heat Template
94     // Based on the email from Ella Kvetny:
95     // Within Heat Template, under parameters catalog, it might indicate the default value of the parameter
96     // If default value exist, the parameter is not mandatory, otherwise its value should be set
97     public synchronized Set <HeatTemplateParam> getParameterList (String artifactUUID) {
98         Set <HeatTemplateParam> paramSet = new HashSet<>();
99         @SuppressWarnings("unchecked")
100         Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("parameters");
101
102         for (Entry<String, Object> stringObjectEntry : resourceMap.entrySet()) {
103             HeatTemplateParam param = new HeatTemplateParam();
104             Entry<String, Object> pair = stringObjectEntry;
105             @SuppressWarnings("unchecked")
106             Map<String, String> resourceEntry = (Map<String, String>) pair.getValue();
107
108             param.setParamName(pair.getKey());
109             // System.out.println(pair.getKey()+":"+type);
110             if (resourceEntry.containsKey("default")) {
111                 param.setRequired(false);
112             } else {
113                 param.setRequired(true);
114             }
115             // Now set the type
116             String value = resourceEntry.get("type");
117             param.setParamType(value);
118
119             param.setHeatTemplateArtifactUuid(artifactUUID);
120
121             paramSet.add(param);
122
123         }
124         return paramSet;
125
126     }
127
128     public synchronized void addParameterList (Set <HeatTemplateParam> heatSet) {
129
130         @SuppressWarnings("unchecked")
131         Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("parameters");
132         if (resourceMap == null) {
133             resourceMap = new LinkedHashMap<>();
134             this.yml.put ("parameters", resourceMap);
135         }
136         for (HeatTemplateParam heatParam : heatSet) {
137             Map <String, Object> paramInfo = new HashMap<>();
138             paramInfo.put ("type", heatParam.getParamType ());
139
140             resourceMap.put (heatParam.getParamName (), paramInfo);
141         }
142
143         // this.yml.put("parameters", resourceMap);
144
145     }
146
147     public boolean isParentTemplate (String templateBody) {
148         return templateBody.contains (REFER_PATTERN);
149     }
150
151     public boolean verifyTemplate () {
152         // Verify whether the heat template is for Vnf Resource
153         // We don't support other template installation yet
154
155         return true;
156     }
157
158     public String encode (Map <String, Object> content) {
159         return yaml.dump (content);
160     }
161
162     public synchronized String encode () {
163         return this.yaml.dump (this.yml);
164     }
165
166     /**
167      * This method return the YAml file as a string.
168      * 
169      */
170     @Override
171     public String toString () {
172
173         return encode ();
174     }
175
176 }