d1246580b2d160bd6a398a9936bb00ffc9290f56
[so.git] / adapters / mso-adapter-utils / src / main / java / org / openecomp / mso / openstack / utils / MsoYamlEditorWithEnvt.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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.openstack.utils;
22
23
24
25 import java.io.ByteArrayInputStream;
26 import java.io.InputStream;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.Map;
30 import java.util.Set;
31 import java.util.Map.Entry;
32 import org.openecomp.mso.db.catalog.beans.HeatTemplateParam;
33 import org.codehaus.jackson.map.ObjectMapper;
34 import java.util.LinkedHashMap;
35
36 import org.yaml.snakeyaml.Yaml;
37
38
39 public class MsoYamlEditorWithEnvt {
40
41     private Map <String, Object> yml;
42     private Yaml yaml = new Yaml ();
43     private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
44
45         public MsoYamlEditorWithEnvt() {
46                 super();
47         }
48         public MsoYamlEditorWithEnvt(byte[] b) {
49                 init(b);
50         }
51
52     @SuppressWarnings("unchecked")
53     private synchronized void init (byte[] body) {
54         InputStream input = new ByteArrayInputStream (body);
55         yml = (Map <String, Object>) yaml.load (input);
56     }   
57
58     @SuppressWarnings("unchecked")
59         public synchronized Set <MsoHeatEnvironmentParameter> getParameterListFromEnvt() {
60         // In an environment entry, the parameters section can only contain the name:value - 
61         // not other attributes.
62         Set <MsoHeatEnvironmentParameter> paramSet = new HashSet<MsoHeatEnvironmentParameter>();
63         Map<String, Object> resourceMap = null;
64         try {
65                 resourceMap = (Map<String,Object>) yml.get("parameters");
66         } catch (Exception e) {
67                 return paramSet;
68         }
69         if (resourceMap == null) {
70                 return paramSet;
71         }
72         Iterator <Entry <String, Object>> it = resourceMap.entrySet().iterator();
73         
74         while (it.hasNext()) {
75                 MsoHeatEnvironmentParameter hep = new MsoHeatEnvironmentParameter();
76                 Map.Entry <String, Object> pair = it.next();
77                 //Map<String, String> resourceEntry = (Map <String, String>) pair.getValue();
78                 //String value = null;
79                 String value = null;
80                 Object obj = pair.getValue();
81                 if (obj instanceof java.lang.String) {
82                         //value = (String) pair.getValue();
83                         // handle block scalar - literals and folded:
84                         value = yaml.dump(obj);
85                         // but this adds an extra '\n' at the end - which won't hurt - but we don't need it
86                         value = value.substring(0, value.length() - 1);
87                 } else if (obj instanceof java.util.LinkedHashMap) {
88                         //Handle that it's json
89                         try {
90                                 value = JSON_MAPPER.writeValueAsString(obj);
91                         } catch (Exception e) {
92                                 value = "_BAD_JSON_MAPPING";
93                         }
94                 } else {
95                         //this handles integers/longs/floats/etc.
96                         value = String.valueOf(obj);
97                 }
98                 hep.setName((String) pair.getKey());
99                 hep.setValue(value);
100                 paramSet.add(hep);
101         }
102         return paramSet;
103     }
104     public synchronized Set <MsoHeatEnvironmentResource> getResourceListFromEnvt() {
105         try {
106                 Set<MsoHeatEnvironmentResource> resourceList = new HashSet<MsoHeatEnvironmentResource>();
107                 @SuppressWarnings("unchecked")
108                 Map<String, Object> resourceMap = (Map<String,Object>) yml.get("resource_registry");
109                 Iterator<Entry <String,Object>> it = resourceMap.entrySet().iterator();
110         
111                 while (it.hasNext()) {
112                         MsoHeatEnvironmentResource her = new MsoHeatEnvironmentResource();
113                         Map.Entry<String, Object> pair = it.next();
114                         her.setName((String) pair.getKey());
115                         her.setValue((String) pair.getValue());
116                         resourceList.add(her);
117                 }
118                 return resourceList;
119         } catch (Exception e) {
120                 
121         }
122         return null;
123     }
124     public synchronized Set <HeatTemplateParam> getParameterList () {
125         Set <HeatTemplateParam> paramSet = new HashSet <HeatTemplateParam> ();
126         @SuppressWarnings("unchecked")
127         Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("parameters");
128         Iterator <Entry <String, Object>> it = resourceMap.entrySet ().iterator ();
129
130         while (it.hasNext ()) {
131             HeatTemplateParam param = new HeatTemplateParam ();
132             Map.Entry <String, Object> pair = it.next ();
133             @SuppressWarnings("unchecked")
134             Map <String, String> resourceEntry = (Map <String, String>) pair.getValue ();
135             String value = null;
136             try {
137                 value = resourceEntry.get ("default");
138             } catch (java.lang.ClassCastException cce) {
139                 // This exception only - the value is an integer. For what we're doing
140                 // here - we don't care - so set value to something - and it will 
141                 // get marked as not being required - which is correct.
142                 //System.out.println("cce exception!");
143                 value = "300"; 
144                 // okay
145             }
146             param.setParamName ((String) pair.getKey ());
147             if (value != null) {
148                 param.setRequired (false);
149             } else {
150                 param.setRequired (true);
151             }
152             value = resourceEntry.get ("type");
153             param.setParamType (value);
154
155             paramSet.add (param);
156
157         }
158         return paramSet;
159
160     }
161
162
163 }