2 * Copyright © 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 org.apache.commons.collections4.MapUtils;
20 import org.openecomp.core.utilities.file.FileUtils;
21 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
22 import org.openecomp.sdc.heat.datatypes.model.Output;
23 import org.openecomp.sdc.heat.datatypes.model.Parameter;
24 import org.openecomp.sdc.tosca.datatypes.extend.ToscaAnnotationType;
25 import org.openecomp.sdc.tosca.datatypes.model.*;
26 import org.openecomp.sdc.tosca.datatypes.model.heatextend.AnnotationDefinition;
27 import org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
28 import org.openecomp.sdc.tosca.services.ToscaConstants;
29 import org.openecomp.sdc.tosca.services.ToscaUtil;
30 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
31 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory;
32 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
37 public class TranslatorHeatToToscaParameterConverter {
40 private static Map<String, String> parameterTypeMapping;
41 private static Map<String, String> parameterEntrySchemaTypeMapping;
44 parameterEntrySchemaTypeMapping = new HashMap<>();
45 parameterEntrySchemaTypeMapping.put("list", "string");
49 parameterTypeMapping = new HashMap<>();
50 parameterTypeMapping.put("string", "string");
51 parameterTypeMapping.put("number", "float");
52 parameterTypeMapping.put("comma_delimited_list", "list");
53 parameterTypeMapping.put("json", "json");
54 parameterTypeMapping.put("boolean", "boolean");
58 * Parameter converter map.
60 * @param parameters the parameters
61 * @param heatOrchestrationTemplate the heat orchestration template
62 * @param heatFileName the heat file name
63 * @param context the context
66 public static Map<String, ParameterDefinition> parameterConverter(ServiceTemplate serviceTemplate,
67 Map<String, Parameter> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
68 String heatFileName, TranslationContext context) {
69 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
70 for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
71 String heatParamName = entry.getKey();
72 parameterDefinitionMap.put(heatParamName,
73 getToscaParameter(serviceTemplate,heatParamName, entry.getValue(),
74 heatOrchestrationTemplate,
75 heatFileName, context));
77 return parameterDefinitionMap;
81 * Parameter output converter map.
83 * @param parameters the parameters
84 * @param heatOrchestrationTemplate the heat orchestration template
85 * @param heatFileName the heat file name
86 * @param context the context
89 public static Map<String, ParameterDefinition> parameterOutputConverter(ServiceTemplate
91 Map<String, Output> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
92 String heatFileName, TranslationContext context) {
93 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
94 for (Map.Entry<String, Output> entry : parameters.entrySet()) {
95 parameterDefinitionMap.put(entry.getKey(),
96 getToscaOutputParameter(serviceTemplate,entry.getKey(),entry.getValue(),
97 heatOrchestrationTemplate,
101 return parameterDefinitionMap;
105 * Gets tosca parameter.
107 * @param heatParameter the heat parameter
108 * @param heatOrchestrationTemplate the heat orchestration template
109 * @param heatFileName the heat file name
110 * @param context the context
111 * @return the tosca parameter
113 public static ParameterDefinitionExt getToscaParameter(ServiceTemplate serviceTemplate,
114 String heatParameterName,
115 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(serviceTemplate, heatParameterName, heatParameter.get_default(),
128 toscaParameter.getType(), heatFileName, heatOrchestrationTemplate, context));
129 toscaParameter.setHidden(heatParameter.isHidden());
130 toscaParameter.setImmutable(heatParameter.isImmutable());
131 toscaParameter.setConstraints(getToscaConstrains(heatParameter.getConstraints()));
132 Optional<Map<String, AnnotationDefinition>> annotations = getToscaAnnotations(context, serviceTemplate, heatFileName, heatParameterName);
133 annotations.ifPresent(ant->toscaParameter.setAnnotations(annotations.get()));
136 return toscaParameter;
139 private static Optional<Map<String, AnnotationDefinition> > getToscaAnnotations (TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName, String heatParameterName){
141 if(!isAnnotationRequired(context, serviceTemplate, heatFileName)){
142 return Optional.empty();
145 AnnotationDefinition annotationDefinition = new AnnotationDefinition();
146 annotationDefinition.setType(ToscaAnnotationType.SOURCE);
147 annotationDefinition.setProperties(new HashMap<>());
148 List<String> vfModuleList = new ArrayList<>();
149 vfModuleList.add( FileUtils.getFileWithoutExtention(heatFileName));
150 annotationDefinition.getProperties().put(ToscaConstants.VF_MODULE_LABEL_PROPERTY_NAME, vfModuleList);
151 annotationDefinition.getProperties().put(ToscaConstants.SOURCE_TYPE_PROPERTY_NAME, ToscaConstants.HEAT_SOURCE_TYPE);
152 annotationDefinition.getProperties().put(ToscaConstants.PARAM_NAME_PROPERTY_NAME, heatParameterName);
153 Map<String, AnnotationDefinition> annotationMap = new HashMap<>();
154 annotationMap.put(ToscaConstants.SOURCE_ANNOTATION_ID, annotationDefinition);
155 return Optional.of(annotationMap);
159 private static boolean isAnnotationRequired(TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName){
160 return HeatToToscaUtil.shouldAnnotationsToBeAdded() && !isNestedServiceTemplate(context, serviceTemplate, heatFileName);
163 private static boolean isNestedServiceTemplate(TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName) {
164 String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate.getMetadata());
165 return HeatToToscaUtil.isHeatFileNested(context, heatFileName) || context.getNestedHeatFileName().containsKey(serviceTemplateFileName);
170 * Gets tosca output parameter.
172 * @param heatOutputParameter the heat output parameter
173 * @param heatOrchestrationTemplate the heat orchestration template
174 * @param heatFileName the heat file name
175 * @param context the context
176 * @return the tosca output parameter
178 private static ParameterDefinitionExt getToscaOutputParameter(ServiceTemplate serviceTemplate,
179 String parameterName,
180 Output heatOutputParameter,
181 HeatOrchestrationTemplate
182 heatOrchestrationTemplate,
184 TranslationContext context) {
186 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
187 toscaParameter.setDescription(heatOutputParameter.getDescription());
188 toscaParameter.setValue(
189 getToscaParameterDefaultValue(serviceTemplate,parameterName,heatOutputParameter.getValue(),
190 toscaParameter.getType(),
191 heatFileName, heatOrchestrationTemplate, context));
192 return toscaParameter;
196 * Gets tosca parameter default value.
198 * @param obj the a default
199 * @param type the type
200 * @param heatFileName the heat file name
201 * @param heatOrchestrationTemplate the heat orchestration template
202 * @param context the context
203 * @return the tosca parameter default value
205 public static Object getToscaParameterDefaultValue(ServiceTemplate serviceTemplate,
206 String parameterName,
207 Object obj, String type,
209 HeatOrchestrationTemplate
210 heatOrchestrationTemplate,
211 TranslationContext context) {
216 Object toscaDefaultValue = obj;
217 if ("list".equals(type)) {
218 if (obj instanceof String) {
219 return Arrays.asList(((String) obj).split(","));
221 return toscaDefaultValue;
225 return getToscaParameterValue(serviceTemplate,parameterName,toscaDefaultValue, heatFileName,
226 heatOrchestrationTemplate,
230 private static Object getToscaParameterValue(ServiceTemplate serviceTemplate,
231 String parameterName,
232 Object paramValue, String heatFileName,
233 HeatOrchestrationTemplate heatOrchestrationTemplate,
234 TranslationContext context) {
235 if (paramValue instanceof Map) {
236 if(MapUtils.isEmpty((Map) paramValue)){
237 return new HashMap<>();
239 Map.Entry<String, Object> functionMapEntry =
240 (Map.Entry<String, Object>) ((Map) paramValue).entrySet().iterator().next();
241 if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) {
242 return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get()
243 .translateFunction(serviceTemplate, null, parameterName, functionMapEntry.getKey(),
244 functionMapEntry.getValue(),heatFileName,
245 heatOrchestrationTemplate, null, context);
252 private static List<Constraint> getToscaConstrains(List<Map<String, Object>> constraints) {
253 if (constraints == null) {
257 List<Constraint> constraintList = new ArrayList<>();
259 for (Map<String, Object> constraint : constraints) {
260 constraintList.addAll(getToscaParameterConstraint(constraint));
263 return constraintList;
266 private static List<Constraint> getToscaParameterConstraint(Map<String, Object> constraint) {
267 List<Constraint> convertedConstraintList = new ArrayList<>();
268 Constraint convertedConstraint;
270 if (constraint.containsKey("range")) {
271 convertedConstraint = new Constraint();
272 convertedConstraintList.add(convertedConstraint);
273 Integer min = (Integer) ((Map) constraint.get("range")).get("min");
274 Integer max = (Integer) ((Map) constraint.get("range")).get("max");
275 convertedConstraint.setIn_range(new Integer[]{min, max});
277 } else if (constraint.containsKey("length")) {
278 Integer min = (Integer) ((Map) constraint.get("length")).get("min");
279 Integer max = (Integer) ((Map) constraint.get("length")).get("max");
281 convertedConstraint = new Constraint();
282 convertedConstraintList.add(convertedConstraint);
283 convertedConstraint.setMax_length(max);
286 convertedConstraint = new Constraint();
287 convertedConstraintList.add(convertedConstraint);
288 convertedConstraint.setMin_length(min);
290 } else if (constraint.containsKey("allowed_values")) {
291 convertedConstraint = new Constraint();
292 convertedConstraintList.add(convertedConstraint);
293 convertedConstraint.setValid_values((List) constraint.get("allowed_values"));
294 } else if (constraint.containsKey("allowed_pattern")) {
295 convertedConstraint = new Constraint();
296 convertedConstraintList.add(convertedConstraint);
297 convertedConstraint.setPattern(constraint.get("allowed_pattern"));
300 return convertedConstraintList;
303 private static EntrySchema getToscaParameterEntrySchema(String type) {
305 if (!parameterEntrySchemaTypeMapping.containsKey(type)) {
309 EntrySchema entrySchema = new EntrySchema();
310 entrySchema.setType(parameterEntrySchemaTypeMapping.get(type));
314 protected static String getToscaParameterType(String heatParameterType) {
315 return parameterTypeMapping.get(heatParameterType);