2 * ============LICENSE_START=======================================================
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.so.openstack.utils;
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;
33 import java.util.Map.Entry;
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;
40 public class MsoYamlEditorWithEnvt {
42 private static final Logger logger = LoggerFactory.getLogger(MsoYamlEditorWithEnvt.class);
44 private Map<String, Object> yml;
45 private Yaml yaml = new Yaml();
46 private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
48 public MsoYamlEditorWithEnvt() {
52 public MsoYamlEditorWithEnvt(byte[] b) {
56 @SuppressWarnings("unchecked")
57 private synchronized void init(byte[] body) {
58 InputStream input = new ByteArrayInputStream(body);
59 yml = (Map<String, Object>) yaml.load(input);
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<>();
67 Map<String, Object> resourceMap = null;
69 resourceMap = (Map<String, Object>) yml.get("parameters");
70 } catch (Exception e) {
71 logger.debug("Exception:", e);
74 if (resourceMap == null) {
78 for (Entry<String, Object> stringObjectEntry : resourceMap.entrySet()) {
79 MsoHeatEnvironmentParameter hep = new MsoHeatEnvironmentParameter();
80 Entry<String, Object> pair = stringObjectEntry;
82 Object obj = pair.getValue();
83 if (obj instanceof String) {
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 LinkedHashMap) {
88 // Handle that it's json
90 value = JSON_MAPPER.writeValueAsString(obj);
91 } catch (Exception e) {
92 logger.debug("Exception:", e);
93 value = "_BAD_JSON_MAPPING";
96 // this handles integers/longs/floats/etc.
97 value = String.valueOf(obj);
99 hep.setName((String) pair.getKey());
106 public synchronized Set<MsoHeatEnvironmentResource> getResourceListFromEnvt() {
108 Set<MsoHeatEnvironmentResource> resourceList = new HashSet<>();
109 @SuppressWarnings("unchecked")
110 Map<String, Object> resourceMap = (Map<String, Object>) yml.get("resource_registry");
112 for (Entry<String, Object> stringObjectEntry : resourceMap.entrySet()) {
113 MsoHeatEnvironmentResource her = new MsoHeatEnvironmentResource();
114 Entry<String, Object> pair = stringObjectEntry;
115 her.setName((String) pair.getKey());
116 her.setValue((String) pair.getValue());
117 resourceList.add(her);
120 } catch (Exception e) {
121 logger.debug("Exception:", e);
126 public synchronized Set<HeatTemplateParam> getParameterList() {
127 Set<HeatTemplateParam> paramSet = new HashSet<>();
128 @SuppressWarnings("unchecked")
129 Map<String, Object> resourceMap = (Map<String, Object>) yml.get("parameters");
131 for (Entry<String, Object> stringObjectEntry : resourceMap.entrySet()) {
132 HeatTemplateParam param = new HeatTemplateParam();
133 Entry<String, Object> pair = stringObjectEntry;
134 @SuppressWarnings("unchecked")
135 Map<String, String> resourceEntry = (Map<String, String>) pair.getValue();
138 value = resourceEntry.get("default");
139 } catch (ClassCastException cce) {
140 logger.debug("Exception:", cce);
141 // This exception only - the value is an integer. For what we're doing
142 // here - we don't care - so set value to something - and it will
143 // get marked as not being required - which is correct.
144 // System.out.println("cce exception!");
148 param.setParamName((String) pair.getKey());
150 param.setRequired(false);
152 param.setRequired(true);
154 value = resourceEntry.get("type");
155 param.setParamType(value);