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