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.apache.commons.collections4.MapUtils;
24 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
25 import org.openecomp.sdc.heat.datatypes.model.Output;
26 import org.openecomp.sdc.heat.datatypes.model.Parameter;
27 import org.openecomp.sdc.tosca.datatypes.model.Constraint;
28 import org.openecomp.sdc.tosca.datatypes.model.EntrySchema;
29 import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition;
30 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
31 import org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
32 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
33 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.HashMap;
38 import java.util.List;
42 public class TranslatorHeatToToscaParameterConverter {
45 private static Map<String, String> parameterTypeMapping;
46 private static Map<String, String> parameterEntrySchemaTypeMapping;
49 parameterEntrySchemaTypeMapping = new HashMap<>();
50 parameterEntrySchemaTypeMapping.put("list", "string");
54 parameterTypeMapping = new HashMap<>();
55 parameterTypeMapping.put("string", "string");
56 parameterTypeMapping.put("number", "float");
57 parameterTypeMapping.put("comma_delimited_list", "list");
58 parameterTypeMapping.put("json", "json");
59 parameterTypeMapping.put("boolean", "boolean");
63 * Parameter converter map.
65 * @param parameters the parameters
66 * @param heatOrchestrationTemplate the heat orchestration template
67 * @param heatFileName the heat file name
68 * @param context the context
71 public static Map<String, ParameterDefinition> parameterConverter(ServiceTemplate serviceTemplate,
72 Map<String, Parameter> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
73 String heatFileName, TranslationContext context) {
74 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
75 for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
76 //parameterDefinitionMap.put(entry.getKey()+"_"+ FileUtils.getFileWithoutExtention
77 // (heatFileName),getToscaParameter(entry.getValue(), heatOrchestrationTemplate,
78 // heatFileName, context));
79 parameterDefinitionMap.put(entry.getKey(),
80 getToscaParameter(serviceTemplate,entry.getKey(), entry.getValue(),
81 heatOrchestrationTemplate,
82 heatFileName, context));
84 return parameterDefinitionMap;
88 * Parameter output converter map.
90 * @param parameters the parameters
91 * @param heatOrchestrationTemplate the heat orchestration template
92 * @param heatFileName the heat file name
93 * @param context the context
96 public static Map<String, ParameterDefinition> parameterOutputConverter(ServiceTemplate
98 Map<String, Output> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
99 String heatFileName, TranslationContext context) {
100 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
101 for (Map.Entry<String, Output> entry : parameters.entrySet()) {
102 parameterDefinitionMap.put(entry.getKey(),
103 getToscaOutputParameter(serviceTemplate,entry.getKey(),entry.getValue(),
104 heatOrchestrationTemplate,
108 return parameterDefinitionMap;
112 * Gets tosca parameter.
114 * @param heatParameter the heat parameter
115 * @param heatOrchestrationTemplate the heat orchestration template
116 * @param heatFileName the heat file name
117 * @param context the context
118 * @return the tosca parameter
120 public static ParameterDefinitionExt getToscaParameter(ServiceTemplate serviceTemplate,
121 String parameterName,
122 Parameter heatParameter,
123 HeatOrchestrationTemplate
124 heatOrchestrationTemplate,
126 TranslationContext context) {
128 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
129 toscaParameter.setType(getToscaParameterType(heatParameter.getType()));
130 toscaParameter.setEntry_schema(getToscaParameterEntrySchema(toscaParameter.getType()));
131 toscaParameter.setLabel(heatParameter.getLabel());
132 toscaParameter.setDescription(heatParameter.getDescription());
133 toscaParameter.set_default(
134 getToscaParameterDefaultValue(serviceTemplate, parameterName, heatParameter.get_default(),
135 toscaParameter.getType(), heatFileName, heatOrchestrationTemplate, context));
136 toscaParameter.setHidden(heatParameter.isHidden());
137 toscaParameter.setImmutable(heatParameter.isImmutable());
138 toscaParameter.setConstraints(getToscaConstrains(heatParameter.getConstraints()));
139 return toscaParameter;
143 * Gets tosca output parameter.
145 * @param heatOutputParameter the heat output parameter
146 * @param heatOrchestrationTemplate the heat orchestration template
147 * @param heatFileName the heat file name
148 * @param context the context
149 * @return the tosca output parameter
151 public static ParameterDefinitionExt getToscaOutputParameter(ServiceTemplate serviceTemplate,
152 String parameterName,
153 Output heatOutputParameter,
154 HeatOrchestrationTemplate
155 heatOrchestrationTemplate,
157 TranslationContext context) {
159 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
160 toscaParameter.setDescription(heatOutputParameter.getDescription());
161 toscaParameter.setValue(
162 getToscaParameterDefaultValue(serviceTemplate,parameterName,heatOutputParameter.getValue(),
163 toscaParameter.getType(),
164 heatFileName, heatOrchestrationTemplate, context));
165 return toscaParameter;
169 * Gets tosca parameter default value.
171 * @param obj the a default
172 * @param type the type
173 * @param heatFileName the heat file name
174 * @param heatOrchestrationTemplate the heat orchestration template
175 * @param context the context
176 * @return the tosca parameter default value
178 public static Object getToscaParameterDefaultValue(ServiceTemplate serviceTemplate,
179 String parameterName,
180 Object obj, String type,
182 HeatOrchestrationTemplate
183 heatOrchestrationTemplate,
184 TranslationContext context) {
189 Object toscaDefaultValue = obj;
190 if ("list".equals(type)) {
191 if (obj instanceof String) {
192 return Arrays.asList(((String) obj).split(","));
194 return toscaDefaultValue;
198 return getToscaParameterValue(serviceTemplate,parameterName,toscaDefaultValue, heatFileName,
199 heatOrchestrationTemplate,
203 private static Object getToscaParameterValue(ServiceTemplate serviceTemplate,
204 String parameterName,
205 Object paramValue, String heatFileName,
206 HeatOrchestrationTemplate heatOrchestrationTemplate,
207 TranslationContext context) {
208 if (paramValue instanceof Map) {
209 if(MapUtils.isEmpty((Map) paramValue)){
210 return new HashMap<>();
212 Map.Entry<String, Object> functionMapEntry =
213 (Map.Entry<String, Object>) ((Map) paramValue).entrySet().iterator().next();
214 if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) {
215 return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get()
216 .translateFunction(serviceTemplate, null, parameterName, functionMapEntry.getKey(),
217 functionMapEntry.getValue(),heatFileName,
218 heatOrchestrationTemplate, null, context);
225 private static List<Constraint> getToscaConstrains(List<Map<String, Object>> constraints) {
226 if (constraints == null) {
230 List<Constraint> constraintList = new ArrayList<>();
232 for (Map<String, Object> constraint : constraints) {
233 constraintList.addAll(getToscaParameterConstraint(constraint));
236 return constraintList;
239 private static List<Constraint> getToscaParameterConstraint(Map<String, Object> constraint) {
240 List<Constraint> convertedConstraintList = new ArrayList<>();
241 Constraint convertedConstraint;
243 if (constraint.containsKey("range")) {
244 convertedConstraint = new Constraint();
245 convertedConstraintList.add(convertedConstraint);
246 Integer min = (Integer) ((Map) constraint.get("range")).get("min");
247 Integer max = (Integer) ((Map) constraint.get("range")).get("max");
248 convertedConstraint.setIn_range(new Integer[]{min, max});
250 } else if (constraint.containsKey("length")) {
251 Integer min = (Integer) ((Map) constraint.get("length")).get("min");
252 Integer max = (Integer) ((Map) constraint.get("length")).get("max");
254 convertedConstraint = new Constraint();
255 convertedConstraintList.add(convertedConstraint);
256 convertedConstraint.setMax_length(max);
259 convertedConstraint = new Constraint();
260 convertedConstraintList.add(convertedConstraint);
261 convertedConstraint.setMin_length(min);
263 } else if (constraint.containsKey("allowed_values")) {
264 convertedConstraint = new Constraint();
265 convertedConstraintList.add(convertedConstraint);
266 convertedConstraint.setValid_values((List) constraint.get("allowed_values"));
267 } else if (constraint.containsKey("allowed_pattern")) {
268 convertedConstraint = new Constraint();
269 convertedConstraintList.add(convertedConstraint);
270 convertedConstraint.setPattern(constraint.get("allowed_pattern"));
273 return convertedConstraintList;
276 private static EntrySchema getToscaParameterEntrySchema(String type) {
278 if (!parameterEntrySchemaTypeMapping.containsKey(type)) {
282 EntrySchema entrySchema = new EntrySchema();
283 entrySchema.setType(parameterEntrySchemaTypeMapping.get(type));
287 protected static String getToscaParameterType(String heatParameterType) {
288 return parameterTypeMapping.get(heatParameterType);