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