2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.translator.services.heattotosca.mapping;
19 import static org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation.FunctionTranslator.getFunctionTranslateTo;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.Optional;
28 import org.apache.commons.collections4.MapUtils;
29 import org.onap.sdc.tosca.datatypes.model.*;
30 import org.onap.sdc.tosca.datatypes.model.heatextend.AnnotationDefinition;
31 import org.onap.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
32 import org.openecomp.core.utilities.file.FileUtils;
34 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
35 import org.openecomp.sdc.heat.datatypes.model.Output;
36 import org.openecomp.sdc.heat.datatypes.model.Parameter;
37 import org.openecomp.sdc.tosca.datatypes.extend.ToscaAnnotationType;
38 import org.openecomp.sdc.tosca.services.ToscaConstants;
39 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
40 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory;
41 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
42 import org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation.FunctionTranslator;
44 public class TranslatorHeatToToscaParameterConverter {
47 private static Map<String, String> parameterTypeMapping;
48 private static Map<String, String> parameterEntrySchemaTypeMapping;
49 private static final String RANGE = "range";
50 private static final String LENGTH = "length";
51 private static final String MIN = "min";
52 private static final String MAX = "max";
53 private static final String ALLOWED_VALUES = "allowed_values";
54 private static final String ALLOWED_PATTERN = "allowed_pattern";
58 parameterEntrySchemaTypeMapping = new HashMap<>();
59 parameterEntrySchemaTypeMapping.put("list", "string");
63 parameterTypeMapping = new HashMap<>();
64 parameterTypeMapping.put("string", "string");
65 parameterTypeMapping.put("number", "float");
66 parameterTypeMapping.put("comma_delimited_list", "list");
67 parameterTypeMapping.put("json", "json");
68 parameterTypeMapping.put("boolean", "boolean");
72 * Parameter converter map.
74 * @param parameters the parameters
75 * @param heatOrchestrationTemplate the heat orchestration template
76 * @param heatFileName the heat file name
77 * @param context the context
80 public static Map<String, ParameterDefinition> parameterConverter(ServiceTemplate serviceTemplate,
81 Map<String, Parameter> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
82 String heatFileName, String parentHeatFileName, TranslationContext context) {
83 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
84 for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
85 String heatParamName = entry.getKey();
86 parameterDefinitionMap.put(heatParamName,
87 getToscaParameter(serviceTemplate,heatParamName, entry.getValue(),
88 heatOrchestrationTemplate,
89 heatFileName, parentHeatFileName, context));
91 return parameterDefinitionMap;
95 * Parameter output converter map.
97 * @param parameters the parameters
98 * @param heatOrchestrationTemplate the heat orchestration template
99 * @param heatFileName the heat file name
100 * @param context the context
103 public static Map<String, ParameterDefinition> parameterOutputConverter(ServiceTemplate
105 Map<String, Output> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
106 String heatFileName, TranslationContext context) {
107 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
108 for (Map.Entry<String, Output> entry : parameters.entrySet()) {
109 parameterDefinitionMap.put(entry.getKey(),
110 getToscaOutputParameter(serviceTemplate,entry.getKey(),entry.getValue(),
111 heatOrchestrationTemplate,
115 return parameterDefinitionMap;
119 * Gets tosca parameter.
121 * @param heatParameter the heat parameter
122 * @param heatOrchestrationTemplate the heat orchestration template
123 * @param heatFileName the heat file name
124 * @param context the context
125 * @return the tosca parameter
127 private static ParameterDefinitionExt getToscaParameter(ServiceTemplate serviceTemplate,
128 String heatParameterName,
129 Parameter heatParameter,
130 HeatOrchestrationTemplate
131 heatOrchestrationTemplate,
133 String parentHeatFileName,
134 TranslationContext context) {
136 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
137 toscaParameter.setType(getToscaParameterType(heatParameter.getType()));
138 toscaParameter.setEntry_schema(getToscaParameterEntrySchema(toscaParameter.getType()));
139 toscaParameter.setLabel(heatParameter.getLabel());
140 toscaParameter.setDescription(heatParameter.getDescription());
141 toscaParameter.set_default(
142 getToscaParameterDefaultValue(serviceTemplate, heatParameterName, heatParameter.get_default(),
143 toscaParameter.getType(), heatFileName, heatOrchestrationTemplate, context));
144 toscaParameter.setHidden(heatParameter.isHidden());
145 toscaParameter.setImmutable(heatParameter.isImmutable());
146 toscaParameter.setConstraints(getToscaConstrains(heatParameter.getConstraints()));
147 Optional<Map<String, AnnotationDefinition>> annotations = getToscaAnnotations(context, heatFileName, parentHeatFileName, heatParameterName);
148 annotations.ifPresent(ant->toscaParameter.setAnnotations(annotations.get()));
151 return toscaParameter;
154 private static Optional<Map<String, AnnotationDefinition> > getToscaAnnotations (TranslationContext context, String heatFileName, String parentHeatFileName, String heatParameterName){
156 if(parentHeatFileName != null){
157 heatFileName = parentHeatFileName;
160 if(!isAnnotationRequired(context, heatFileName)){
161 return Optional.empty();
164 AnnotationDefinition annotationDefinition = new AnnotationDefinition();
165 annotationDefinition.setType(ToscaAnnotationType.SOURCE);
166 annotationDefinition.setProperties(new HashMap<>());
167 List<String> vfModuleList = new ArrayList<>();
168 vfModuleList.add( FileUtils.getFileWithoutExtention(heatFileName));
169 annotationDefinition.getProperties().put(ToscaConstants.VF_MODULE_LABEL_PROPERTY_NAME, vfModuleList);
170 annotationDefinition.getProperties().put(ToscaConstants.SOURCE_TYPE_PROPERTY_NAME, ToscaConstants.HEAT_SOURCE_TYPE);
171 annotationDefinition.getProperties().put(ToscaConstants.PARAM_NAME_PROPERTY_NAME, heatParameterName);
172 Map<String, AnnotationDefinition> annotationMap = new HashMap<>();
173 annotationMap.put(ToscaConstants.SOURCE_ANNOTATION_ID, annotationDefinition);
174 return Optional.of(annotationMap);
178 private static boolean isAnnotationRequired(TranslationContext context, String heatFileName){
179 return !isNestedServiceTemplate(context, heatFileName);
182 private static boolean isNestedServiceTemplate(TranslationContext context, String heatFileName) {
183 return HeatToToscaUtil.isHeatFileNested(context, heatFileName);
188 * Gets tosca output parameter.
190 * @param heatOutputParameter the heat output parameter
191 * @param heatOrchestrationTemplate the heat orchestration template
192 * @param heatFileName the heat file name
193 * @param context the context
194 * @return the tosca output parameter
196 private static ParameterDefinitionExt getToscaOutputParameter(ServiceTemplate serviceTemplate,
197 String parameterName,
198 Output heatOutputParameter,
199 HeatOrchestrationTemplate
200 heatOrchestrationTemplate,
202 TranslationContext context) {
204 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
205 toscaParameter.setDescription(heatOutputParameter.getDescription());
206 toscaParameter.setValue(
207 getToscaParameterDefaultValue(serviceTemplate,parameterName,heatOutputParameter.getValue(),
208 toscaParameter.getType(),
209 heatFileName, heatOrchestrationTemplate, context));
210 return toscaParameter;
214 * Gets tosca parameter default value.
216 * @param obj the a default
217 * @param type the type
218 * @param heatFileName the heat file name
219 * @param heatOrchestrationTemplate the heat orchestration template
220 * @param context the context
221 * @return the tosca parameter default value
223 public static Object getToscaParameterDefaultValue(ServiceTemplate serviceTemplate,
224 String parameterName,
225 Object obj, String type,
227 HeatOrchestrationTemplate
228 heatOrchestrationTemplate,
229 TranslationContext context) {
234 Object toscaDefaultValue = obj;
235 if ("list".equals(type)) {
236 if (obj instanceof String) {
237 return Arrays.asList(((String) obj).split(","));
239 return toscaDefaultValue;
243 return getToscaParameterValue(serviceTemplate,parameterName,toscaDefaultValue, heatFileName,
244 heatOrchestrationTemplate,
248 private static Object getToscaParameterValue(ServiceTemplate serviceTemplate,
249 String parameterName,
250 Object paramValue, String heatFileName,
251 HeatOrchestrationTemplate heatOrchestrationTemplate,
252 TranslationContext context) {
253 if (paramValue instanceof Map) {
254 if(MapUtils.isEmpty((Map) paramValue)){
255 return new HashMap<>();
257 Map.Entry<String, Object> functionMapEntry =
258 (Map.Entry<String, Object>) ((Map) paramValue).entrySet().iterator().next();
259 if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) {
261 FunctionTranslator functionTranslator = new FunctionTranslator(getFunctionTranslateTo(serviceTemplate, null,
262 heatFileName, heatOrchestrationTemplate, context), parameterName, functionMapEntry.getValue(), null);
263 return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get()
264 .translateFunction(functionTranslator);
271 private static List<Constraint> getToscaConstrains(List<Map<String, Object>> constraints) {
272 if (constraints == null) {
276 List<Constraint> constraintList = new ArrayList<>();
278 for (Map<String, Object> constraint : constraints) {
279 constraintList.addAll(getToscaParameterConstraint(constraint));
282 return constraintList;
285 private static List<Constraint> getToscaParameterConstraint(Map<String, Object> constraint) {
286 List<Constraint> convertedConstraintList = new ArrayList<>();
287 Constraint convertedConstraint;
289 if (constraint.containsKey(RANGE)) {
290 convertedConstraint = new Constraint();
291 convertedConstraintList.add(convertedConstraint);
292 Integer min = (Integer) ((Map) constraint.get(RANGE)).get(MIN);
293 Integer max = (Integer) ((Map) constraint.get(RANGE)).get(MAX);
294 convertedConstraint.setIn_range(new Integer[]{min, max});
296 } else if (constraint.containsKey(LENGTH)) {
297 Integer min = (Integer) ((Map) constraint.get(LENGTH)).get(MIN);
298 Integer max = (Integer) ((Map) constraint.get(LENGTH)).get(MAX);
300 convertedConstraint = new Constraint();
301 convertedConstraintList.add(convertedConstraint);
302 convertedConstraint.setMax_length(max);
305 convertedConstraint = new Constraint();
306 convertedConstraintList.add(convertedConstraint);
307 convertedConstraint.setMin_length(min);
309 } else if (constraint.containsKey(ALLOWED_VALUES)) {
310 convertedConstraint = new Constraint();
311 convertedConstraintList.add(convertedConstraint);
312 convertedConstraint.setValid_values((List) constraint.get(ALLOWED_VALUES));
313 } else if (constraint.containsKey(ALLOWED_PATTERN)) {
314 convertedConstraint = new Constraint();
315 convertedConstraintList.add(convertedConstraint);
316 convertedConstraint.setPattern(constraint.get(ALLOWED_PATTERN));
319 return convertedConstraintList;
322 private static EntrySchema getToscaParameterEntrySchema(String type) {
324 if (!parameterEntrySchemaTypeMapping.containsKey(type)) {
328 EntrySchema entrySchema = new EntrySchema();
329 entrySchema.setType(parameterEntrySchemaTypeMapping.get(type));
333 protected static String getToscaParameterType(String heatParameterType) {
334 return parameterTypeMapping.get(heatParameterType);