2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.translator.services.heattotosca.mapping;
23 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
24 import org.openecomp.sdc.heat.datatypes.model.Output;
25 import org.openecomp.sdc.heat.datatypes.model.Parameter;
26 import org.openecomp.sdc.tosca.datatypes.model.Constraint;
27 import org.openecomp.sdc.tosca.datatypes.model.EntrySchema;
28 import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition;
29 import org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
30 import org.openecomp.sdc.translator.services.heattotosca.TranslationContext;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.HashMap;
36 import java.util.List;
39 public class TranslatorHeatToToscaParameterConverter {
42 private static Map<String, String> parameterTypeMapping;
43 private static Map<String, String> parameterEntrySchemaTypeMapping;
46 parameterEntrySchemaTypeMapping = new HashMap<>();
47 parameterEntrySchemaTypeMapping.put("list", "string");
51 parameterTypeMapping = new HashMap<>();
52 parameterTypeMapping.put("string", "string");
53 parameterTypeMapping.put("number", "float");
54 parameterTypeMapping.put("comma_delimited_list", "list");
55 parameterTypeMapping.put("json", "json");
56 parameterTypeMapping.put("boolean", "boolean");
60 * Parameter converter map.
62 * @param parameters the parameters
63 * @param heatOrchestrationTemplate the heat orchestration template
64 * @param heatFileName the heat file name
65 * @param context the context
68 public static Map<String, ParameterDefinition> parameterConverter(
69 Map<String, Parameter> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
70 String heatFileName, TranslationContext context) {
71 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
72 for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
73 //parameterDefinitionMap.put(entry.getKey()+"_"+ FileUtils
74 // .getFileWithoutExtention(heatFileName),getToscaParameter(entry.getValue(),
75 // heatOrchestrationTemplate, heatFileName, context));
76 parameterDefinitionMap.put(entry.getKey(),
77 getToscaParameter(entry.getValue(), heatOrchestrationTemplate, heatFileName, context));
79 return parameterDefinitionMap;
83 * Parameter output converter map.
85 * @param parameters the parameters
86 * @param heatOrchestrationTemplate the heat orchestration template
87 * @param heatFileName the heat file name
88 * @param context the context
91 public static Map<String, ParameterDefinition> parameterOutputConverter(
92 Map<String, Output> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
93 String heatFileName, TranslationContext context) {
94 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
95 for (Map.Entry<String, Output> entry : parameters.entrySet()) {
96 //parameterDefinitionMap.put(entry.getKey()+"_"+FileUtils
97 // .getFileWithoutExtention(heatFileName),getToscaOutputParameter(entry.getValue(),
98 // heatOrchestrationTemplate, heatFileName, context));
99 parameterDefinitionMap.put(entry.getKey(),
100 getToscaOutputParameter(entry.getValue(), heatOrchestrationTemplate, heatFileName,
103 return parameterDefinitionMap;
107 * Gets tosca parameter.
109 * @param heatParameter the heat parameter
110 * @param heatOrchestrationTemplate the heat orchestration template
111 * @param heatFileName the heat file name
112 * @param context the context
113 * @return the tosca parameter
115 public static ParameterDefinitionExt getToscaParameter(Parameter heatParameter,
116 HeatOrchestrationTemplate
117 heatOrchestrationTemplate,
119 TranslationContext context) {
121 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
122 toscaParameter.setType(getToscaParameterType(heatParameter.getType()));
123 toscaParameter.setEntry_schema(getToscaParameterEntrySchema(toscaParameter.getType()));
124 toscaParameter.setLabel(heatParameter.getLabel());
125 toscaParameter.setDescription(heatParameter.getDescription());
126 toscaParameter.set_default(
127 getToscaParameterDefaultValue(heatParameter.get_default(), toscaParameter.getType(),
128 heatFileName, heatOrchestrationTemplate, context));
129 toscaParameter.setHidden(heatParameter.isHidden());
130 toscaParameter.setImmutable(heatParameter.isImmutable());
131 toscaParameter.setConstraints(getToscaConstrains(heatParameter.getConstraints()));
132 return toscaParameter;
136 * Gets tosca output parameter.
138 * @param heatOutputParameter the heat output parameter
139 * @param heatOrchestrationTemplate the heat orchestration template
140 * @param heatFileName the heat file name
141 * @param context the context
142 * @return the tosca output parameter
144 public static ParameterDefinitionExt getToscaOutputParameter(Output heatOutputParameter,
145 HeatOrchestrationTemplate
146 heatOrchestrationTemplate,
148 TranslationContext context) {
150 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
151 toscaParameter.setDescription(heatOutputParameter.getDescription());
152 toscaParameter.setValue(
153 getToscaParameterDefaultValue(heatOutputParameter.getValue(), toscaParameter.getType(),
154 heatFileName, heatOrchestrationTemplate, context));
155 return toscaParameter;
159 * Gets tosca parameter default value.
161 * @param defaultObj the a default
162 * @param type the type
163 * @param heatFileName the heat file name
164 * @param heatOrchestrationTemplate the heat orchestration template
165 * @param context the context
166 * @return the tosca parameter default value
168 public static Object getToscaParameterDefaultValue(Object defaultObj, String type,
170 HeatOrchestrationTemplate
171 heatOrchestrationTemplate,
172 TranslationContext context) {
174 if (defaultObj == null) {
177 if ("list".equals(type)) {
178 if (defaultObj instanceof String) {
179 return Arrays.asList(((String) defaultObj).split(","));
185 return getToscaParameterValue(defaultObj, heatFileName, heatOrchestrationTemplate,
189 private static Object getToscaParameterValue(Object paramValue, String heatFileName,
190 HeatOrchestrationTemplate heatOrchestrationTemplate,
191 TranslationContext context) {
192 if (paramValue instanceof Map) {
193 Map.Entry<String, Object> functionMapEntry =
194 (Map.Entry<String, Object>) ((Map) paramValue).entrySet().iterator().next();
195 if (TranslatorHeatToToscaFunctionConverter.functionNameSet
196 .contains(functionMapEntry.getKey())) {
197 return TranslatorHeatToToscaFunctionConverter
198 .getToscaFunction(functionMapEntry.getKey(), functionMapEntry.getValue(), heatFileName,
199 heatOrchestrationTemplate, null, context);
206 private static List<Constraint> getToscaConstrains(List<Map<String, Object>> constraints) {
207 if (constraints == null) {
211 List<Constraint> constraintList = new ArrayList<>();
213 for (Map<String, Object> constraint : constraints) {
214 constraintList.addAll(getToscaParameterConstraint(constraint));
217 return constraintList;
220 private static List<Constraint> getToscaParameterConstraint(Map<String, Object> constraint) {
221 List<Constraint> convertedConstraintList = new ArrayList<>();
222 Constraint convertedConstraint;
224 if (constraint.containsKey("range")) {
225 convertedConstraint = new Constraint();
226 convertedConstraintList.add(convertedConstraint);
227 Integer min = (Integer) ((Map) constraint.get("range")).get("min");
228 Integer max = (Integer) ((Map) constraint.get("range")).get("max");
229 convertedConstraint.setIn_range(new Integer[]{min, max});
231 } else if (constraint.containsKey("length")) {
232 Integer min = (Integer) ((Map) constraint.get("length")).get("min");
233 Integer max = (Integer) ((Map) constraint.get("length")).get("max");
235 convertedConstraint = new Constraint();
236 convertedConstraintList.add(convertedConstraint);
237 convertedConstraint.setMax_length(max);
240 convertedConstraint = new Constraint();
241 convertedConstraintList.add(convertedConstraint);
242 convertedConstraint.setMin_length(min);
244 } else if (constraint.containsKey("allowed_values")) {
245 convertedConstraint = new Constraint();
246 convertedConstraintList.add(convertedConstraint);
247 convertedConstraint.setValid_values((List) constraint.get("allowed_values"));
248 } else if (constraint.containsKey("allowed_pattern")) {
249 convertedConstraint = new Constraint();
250 convertedConstraintList.add(convertedConstraint);
251 convertedConstraint.setPattern(constraint.get("allowed_pattern"));
254 return convertedConstraintList;
257 private static EntrySchema getToscaParameterEntrySchema(String type) {
259 if (!parameterEntrySchemaTypeMapping.containsKey(type)) {
263 EntrySchema entrySchema = new EntrySchema();
264 entrySchema.setType(parameterEntrySchemaTypeMapping.get(type));
268 protected static String getToscaParameterType(String heatParameterType) {
269 return parameterTypeMapping.get(heatParameterType);