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.NodeTemplate;
30 import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition;
31 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
32 import org.openecomp.sdc.tosca.datatypes.model.Template;
33 import org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
34 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
35 import org.openecomp.sdc.translator.services.heattotosca.ConsolidationDataUtil;
36 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.HashMap;
41 import java.util.List;
45 public class TranslatorHeatToToscaParameterConverter {
48 private static Map<String, String> parameterTypeMapping;
49 private static Map<String, String> parameterEntrySchemaTypeMapping;
52 parameterEntrySchemaTypeMapping = new HashMap<>();
53 parameterEntrySchemaTypeMapping.put("list", "string");
57 parameterTypeMapping = new HashMap<>();
58 parameterTypeMapping.put("string", "string");
59 parameterTypeMapping.put("number", "float");
60 parameterTypeMapping.put("comma_delimited_list", "list");
61 parameterTypeMapping.put("json", "json");
62 parameterTypeMapping.put("boolean", "boolean");
66 * Parameter converter map.
68 * @param parameters the parameters
69 * @param heatOrchestrationTemplate the heat orchestration template
70 * @param heatFileName the heat file name
71 * @param context the context
74 public static Map<String, ParameterDefinition> parameterConverter(ServiceTemplate serviceTemplate,
75 Map<String, Parameter> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
76 String heatFileName, TranslationContext context) {
77 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
78 for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
79 //parameterDefinitionMap.put(entry.getKey()+"_"+ FileUtils.getFileWithoutExtention
80 // (heatFileName),getToscaParameter(entry.getValue(), heatOrchestrationTemplate,
81 // heatFileName, context));
82 parameterDefinitionMap.put(entry.getKey(),
83 getToscaParameter(serviceTemplate,entry.getKey(), entry.getValue(),
84 heatOrchestrationTemplate,
85 heatFileName, context));
87 return parameterDefinitionMap;
91 * Parameter output converter map.
93 * @param parameters the parameters
94 * @param heatOrchestrationTemplate the heat orchestration template
95 * @param heatFileName the heat file name
96 * @param context the context
99 public static Map<String, ParameterDefinition> parameterOutputConverter(ServiceTemplate
101 Map<String, Output> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
102 String heatFileName, TranslationContext context) {
103 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
104 for (Map.Entry<String, Output> entry : parameters.entrySet()) {
105 parameterDefinitionMap.put(entry.getKey(),
106 getToscaOutputParameter(serviceTemplate,entry.getKey(),entry.getValue(),
107 heatOrchestrationTemplate,
111 return parameterDefinitionMap;
115 * Gets tosca parameter.
117 * @param heatParameter the heat parameter
118 * @param heatOrchestrationTemplate the heat orchestration template
119 * @param heatFileName the heat file name
120 * @param context the context
121 * @return the tosca parameter
123 public static ParameterDefinitionExt getToscaParameter(ServiceTemplate serviceTemplate,
124 String parameterName,
125 Parameter heatParameter,
126 HeatOrchestrationTemplate
127 heatOrchestrationTemplate,
129 TranslationContext context) {
131 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
132 toscaParameter.setType(getToscaParameterType(heatParameter.getType()));
133 toscaParameter.setEntry_schema(getToscaParameterEntrySchema(toscaParameter.getType()));
134 toscaParameter.setLabel(heatParameter.getLabel());
135 toscaParameter.setDescription(heatParameter.getDescription());
136 toscaParameter.set_default(
137 getToscaParameterDefaultValue(serviceTemplate, parameterName, heatParameter.get_default(),
138 toscaParameter.getType(), heatFileName, heatOrchestrationTemplate, context));
139 toscaParameter.setHidden(heatParameter.isHidden());
140 toscaParameter.setImmutable(heatParameter.isImmutable());
141 toscaParameter.setConstraints(getToscaConstrains(heatParameter.getConstraints()));
142 return toscaParameter;
146 * Gets tosca output parameter.
148 * @param heatOutputParameter the heat output parameter
149 * @param heatOrchestrationTemplate the heat orchestration template
150 * @param heatFileName the heat file name
151 * @param context the context
152 * @return the tosca output parameter
154 public static ParameterDefinitionExt getToscaOutputParameter(ServiceTemplate serviceTemplate,
155 String parameterName,
156 Output heatOutputParameter,
157 HeatOrchestrationTemplate
158 heatOrchestrationTemplate,
160 TranslationContext context) {
162 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
163 toscaParameter.setDescription(heatOutputParameter.getDescription());
164 toscaParameter.setValue(
165 getToscaParameterDefaultValue(serviceTemplate,parameterName,heatOutputParameter.getValue(),
166 toscaParameter.getType(),
167 heatFileName, heatOrchestrationTemplate, context));
168 return toscaParameter;
172 * Gets tosca parameter default value.
174 * @param obj the a default
175 * @param type the type
176 * @param heatFileName the heat file name
177 * @param heatOrchestrationTemplate the heat orchestration template
178 * @param context the context
179 * @return the tosca parameter default value
181 public static Object getToscaParameterDefaultValue(ServiceTemplate serviceTemplate,
182 String parameterName,
183 Object obj, String type,
185 HeatOrchestrationTemplate
186 heatOrchestrationTemplate,
187 TranslationContext context) {
192 Object toscaDefaultValue = obj;
193 if ("list".equals(type)) {
194 if (obj instanceof String) {
195 return Arrays.asList(((String) obj).split(","));
197 return toscaDefaultValue;
201 return getToscaParameterValue(serviceTemplate,parameterName,toscaDefaultValue, heatFileName,
202 heatOrchestrationTemplate,
206 private static Object getToscaParameterValue(ServiceTemplate serviceTemplate,
207 String parameterName,
208 Object paramValue, String heatFileName,
209 HeatOrchestrationTemplate heatOrchestrationTemplate,
210 TranslationContext context) {
211 if (paramValue instanceof Map) {
212 if(MapUtils.isEmpty((Map) paramValue)){
213 return new HashMap<>();
215 Map.Entry<String, Object> functionMapEntry =
216 (Map.Entry<String, Object>) ((Map) paramValue).entrySet().iterator().next();
217 if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) {
218 return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get()
219 .translateFunction(serviceTemplate, null, parameterName, functionMapEntry.getKey(),
220 functionMapEntry.getValue(),heatFileName,
221 heatOrchestrationTemplate, null, context);
228 private static List<Constraint> getToscaConstrains(List<Map<String, Object>> constraints) {
229 if (constraints == null) {
233 List<Constraint> constraintList = new ArrayList<>();
235 for (Map<String, Object> constraint : constraints) {
236 constraintList.addAll(getToscaParameterConstraint(constraint));
239 return constraintList;
242 private static List<Constraint> getToscaParameterConstraint(Map<String, Object> constraint) {
243 List<Constraint> convertedConstraintList = new ArrayList<>();
244 Constraint convertedConstraint;
246 if (constraint.containsKey("range")) {
247 convertedConstraint = new Constraint();
248 convertedConstraintList.add(convertedConstraint);
249 Integer min = (Integer) ((Map) constraint.get("range")).get("min");
250 Integer max = (Integer) ((Map) constraint.get("range")).get("max");
251 convertedConstraint.setIn_range(new Integer[]{min, max});
253 } else if (constraint.containsKey("length")) {
254 Integer min = (Integer) ((Map) constraint.get("length")).get("min");
255 Integer max = (Integer) ((Map) constraint.get("length")).get("max");
257 convertedConstraint = new Constraint();
258 convertedConstraintList.add(convertedConstraint);
259 convertedConstraint.setMax_length(max);
262 convertedConstraint = new Constraint();
263 convertedConstraintList.add(convertedConstraint);
264 convertedConstraint.setMin_length(min);
266 } else if (constraint.containsKey("allowed_values")) {
267 convertedConstraint = new Constraint();
268 convertedConstraintList.add(convertedConstraint);
269 convertedConstraint.setValid_values((List) constraint.get("allowed_values"));
270 } else if (constraint.containsKey("allowed_pattern")) {
271 convertedConstraint = new Constraint();
272 convertedConstraintList.add(convertedConstraint);
273 convertedConstraint.setPattern(constraint.get("allowed_pattern"));
276 return convertedConstraintList;
279 private static EntrySchema getToscaParameterEntrySchema(String type) {
281 if (!parameterEntrySchemaTypeMapping.containsKey(type)) {
285 EntrySchema entrySchema = new EntrySchema();
286 entrySchema.setType(parameterEntrySchemaTypeMapping.get(type));
290 protected static String getToscaParameterType(String heatParameterType) {
291 return parameterTypeMapping.get(heatParameterType);