Merge "Correct the rest URI of VFC"
[so.git] / adapters / mso-adapter-utils / src / main / java / org / openecomp / mso / openstack / utils / MsoYamlEditorWithEnvt.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.mso.openstack.utils;
23
24
25
26 import java.io.ByteArrayInputStream;
27 import java.io.InputStream;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.Map.Entry;
33 import org.openecomp.mso.db.catalog.beans.HeatTemplateParam;
34 import org.codehaus.jackson.map.ObjectMapper;
35 import java.util.LinkedHashMap;
36
37 import org.yaml.snakeyaml.Yaml;
38
39
40 public class MsoYamlEditorWithEnvt {
41
42     private Map <String, Object> yml;
43     private Yaml yaml = new Yaml ();
44     private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
45
46         public MsoYamlEditorWithEnvt() {
47                 super();
48         }
49         public MsoYamlEditorWithEnvt(byte[] b) {
50                 init(b);
51         }
52
53     @SuppressWarnings("unchecked")
54     private synchronized void init (byte[] body) {
55         InputStream input = new ByteArrayInputStream (body);
56         yml = (Map <String, Object>) yaml.load (input);
57     }   
58
59     @SuppressWarnings("unchecked")
60         public synchronized Set <MsoHeatEnvironmentParameter> getParameterListFromEnvt() {
61         // In an environment entry, the parameters section can only contain the name:value - 
62         // not other attributes.
63         Set <MsoHeatEnvironmentParameter> paramSet = new HashSet<MsoHeatEnvironmentParameter>();
64         Map<String, Object> resourceMap = null;
65         try {
66                 resourceMap = (Map<String,Object>) yml.get("parameters");
67         } catch (Exception e) {
68                 return paramSet;
69         }
70         if (resourceMap == null) {
71                 return paramSet;
72         }
73         Iterator <Entry <String, Object>> it = resourceMap.entrySet().iterator();
74         
75         while (it.hasNext()) {
76                 MsoHeatEnvironmentParameter hep = new MsoHeatEnvironmentParameter();
77                 Map.Entry <String, Object> pair = it.next();
78                 //Map<String, String> resourceEntry = (Map <String, String>) pair.getValue();
79                 //String value = null;
80                 String value = null;
81                 Object obj = pair.getValue();
82                 if (obj instanceof java.lang.String) {
83                         //value = (String) pair.getValue();
84                         // handle block scalar - literals and folded:
85                         value = yaml.dump(obj);
86                         // but this adds an extra '\n' at the end - which won't hurt - but we don't need it
87                         value = value.substring(0, value.length() - 1);
88                 } else if (obj instanceof java.util.LinkedHashMap) {
89                         //Handle that it's json
90                         try {
91                                 value = JSON_MAPPER.writeValueAsString(obj);
92                         } catch (Exception e) {
93                                 value = "_BAD_JSON_MAPPING";
94                         }
95                 } else {
96                         //this handles integers/longs/floats/etc.
97                         value = String.valueOf(obj);
98                 }
99                 hep.setName((String) pair.getKey());
100                 hep.setValue(value);
101                 paramSet.add(hep);
102         }
103         return paramSet;
104     }
105     public synchronized Set <MsoHeatEnvironmentResource> getResourceListFromEnvt() {
106         try {
107                 Set<MsoHeatEnvironmentResource> resourceList = new HashSet<MsoHeatEnvironmentResource>();
108                 @SuppressWarnings("unchecked")
109                 Map<String, Object> resourceMap = (Map<String,Object>) yml.get("resource_registry");
110                 Iterator<Entry <String,Object>> it = resourceMap.entrySet().iterator();
111         
112                 while (it.hasNext()) {
113                         MsoHeatEnvironmentResource her = new MsoHeatEnvironmentResource();
114                         Map.Entry<String, Object> pair = it.next();
115                         her.setName((String) pair.getKey());
116                         her.setValue((String) pair.getValue());
117                         resourceList.add(her);
118                 }
119                 return resourceList;
120         } catch (Exception e) {
121                 
122         }
123         return null;
124     }
125     public synchronized Set <HeatTemplateParam> getParameterList () {
126         Set <HeatTemplateParam> paramSet = new HashSet <HeatTemplateParam> ();
127         @SuppressWarnings("unchecked")
128         Map <String, Object> resourceMap = (Map <String, Object>) yml.get ("parameters");
129         Iterator <Entry <String, Object>> it = resourceMap.entrySet ().iterator ();
130
131         while (it.hasNext ()) {
132             HeatTemplateParam param = new HeatTemplateParam ();
133             Map.Entry <String, Object> pair = it.next ();
134             @SuppressWarnings("unchecked")
135             Map <String, String> resourceEntry = (Map <String, String>) pair.getValue ();
136             String value = null;
137             try {
138                 value = resourceEntry.get ("default");
139             } catch (java.lang.ClassCastException cce) {
140                 // This exception only - the value is an integer. For what we're doing
141                 // here - we don't care - so set value to something - and it will 
142                 // get marked as not being required - which is correct.
143                 //System.out.println("cce exception!");
144                 value = "300"; 
145                 // okay
146             }
147             param.setParamName ((String) pair.getKey ());
148             if (value != null) {
149                 param.setRequired (false);
150             } else {
151                 param.setRequired (true);
152             }
153             value = resourceEntry.get ("type");
154             param.setParamType (value);
155
156             paramSet.add (param);
157
158         }
159         return paramSet;
160
161     }
162
163
164 }