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 org.apache.commons.collections4.MapUtils;
20 import org.onap.sdc.tosca.datatypes.model.Constraint;
21 import org.onap.sdc.tosca.datatypes.model.EntrySchema;
22 import org.onap.sdc.tosca.datatypes.model.ParameterDefinition;
23 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
24 import org.openecomp.core.utilities.file.FileUtils;
25 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
26 import org.openecomp.sdc.heat.datatypes.model.Output;
27 import org.openecomp.sdc.heat.datatypes.model.Parameter;
28 import org.openecomp.sdc.tosca.datatypes.extend.ToscaAnnotationType;
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.sdc.tosca.services.ToscaConstants;
33 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
34 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory;
35 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
39 public class TranslatorHeatToToscaParameterConverter {
42 private static Map<String, String> parameterTypeMapping;
43 private static Map<String, String> parameterEntrySchemaTypeMapping;
44 private static final String RANGE = "range";
45 private static final String LENGTH = "length";
46 private static final String MIN = "min";
47 private static final String MAX = "max";
48 private static final String ALLOWED_VALUES = "allowed_values";
49 private static final String ALLOWED_PATTERN = "allowed_pattern";
53 parameterEntrySchemaTypeMapping = new HashMap<>();
54 parameterEntrySchemaTypeMapping.put("list", "string");
58 parameterTypeMapping = new HashMap<>();
59 parameterTypeMapping.put("string", "string");
60 parameterTypeMapping.put("number", "float");
61 parameterTypeMapping.put("comma_delimited_list", "list");
62 parameterTypeMapping.put("json", "json");
63 parameterTypeMapping.put("boolean", "boolean");
67 * Parameter converter map.
69 * @param parameters the parameters
70 * @param heatOrchestrationTemplate the heat orchestration template
71 * @param heatFileName the heat file name
72 * @param context the context
75 public static Map<String, ParameterDefinition> parameterConverter(ServiceTemplate serviceTemplate,
76 Map<String, Parameter> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
77 String heatFileName, String parentHeatFileName, TranslationContext context) {
78 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
79 for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
80 String heatParamName = entry.getKey();
81 parameterDefinitionMap.put(heatParamName,
82 getToscaParameter(serviceTemplate,heatParamName, entry.getValue(),
83 heatOrchestrationTemplate,
84 heatFileName, parentHeatFileName, context));
86 return parameterDefinitionMap;
90 * Parameter output converter map.
92 * @param parameters the parameters
93 * @param heatOrchestrationTemplate the heat orchestration template
94 * @param heatFileName the heat file name
95 * @param context the context
98 public static Map<String, ParameterDefinition> parameterOutputConverter(ServiceTemplate
100 Map<String, Output> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
101 String heatFileName, TranslationContext context) {
102 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
103 for (Map.Entry<String, Output> entry : parameters.entrySet()) {
104 parameterDefinitionMap.put(entry.getKey(),
105 getToscaOutputParameter(serviceTemplate,entry.getKey(),entry.getValue(),
106 heatOrchestrationTemplate,
110 return parameterDefinitionMap;
114 * Gets tosca parameter.
116 * @param heatParameter the heat parameter
117 * @param heatOrchestrationTemplate the heat orchestration template
118 * @param heatFileName the heat file name
119 * @param context the context
120 * @return the tosca parameter
122 private static ParameterDefinitionExt getToscaParameter(ServiceTemplate serviceTemplate,
123 String heatParameterName,
124 Parameter heatParameter,
125 HeatOrchestrationTemplate
126 heatOrchestrationTemplate,
128 String parentHeatFileName,
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, heatParameterName, 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 Optional<Map<String, AnnotationDefinition>> annotations = getToscaAnnotations(context, heatFileName, parentHeatFileName, heatParameterName);
143 annotations.ifPresent(ant->toscaParameter.setAnnotations(annotations.get()));
146 return toscaParameter;
149 private static Optional<Map<String, AnnotationDefinition> > getToscaAnnotations (TranslationContext context, String heatFileName, String parentHeatFileName, String heatParameterName){
151 if(parentHeatFileName != null){
152 heatFileName = parentHeatFileName;
155 if(!isAnnotationRequired(context, heatFileName)){
156 return Optional.empty();
159 AnnotationDefinition annotationDefinition = new AnnotationDefinition();
160 annotationDefinition.setType(ToscaAnnotationType.SOURCE);
161 annotationDefinition.setProperties(new HashMap<>());
162 List<String> vfModuleList = new ArrayList<>();
163 vfModuleList.add( FileUtils.getFileWithoutExtention(heatFileName));
164 annotationDefinition.getProperties().put(ToscaConstants.VF_MODULE_LABEL_PROPERTY_NAME, vfModuleList);
165 annotationDefinition.getProperties().put(ToscaConstants.SOURCE_TYPE_PROPERTY_NAME, ToscaConstants.HEAT_SOURCE_TYPE);
166 annotationDefinition.getProperties().put(ToscaConstants.PARAM_NAME_PROPERTY_NAME, heatParameterName);
167 Map<String, AnnotationDefinition> annotationMap = new HashMap<>();
168 annotationMap.put(ToscaConstants.SOURCE_ANNOTATION_ID, annotationDefinition);
169 return Optional.of(annotationMap);
173 private static boolean isAnnotationRequired(TranslationContext context, String heatFileName){
174 return HeatToToscaUtil.shouldAnnotationsToBeAdded() && !isNestedServiceTemplate(context, heatFileName);
177 private static boolean isNestedServiceTemplate(TranslationContext context, String heatFileName) {
178 return HeatToToscaUtil.isHeatFileNested(context, heatFileName);
183 * Gets tosca output parameter.
185 * @param heatOutputParameter the heat output parameter
186 * @param heatOrchestrationTemplate the heat orchestration template
187 * @param heatFileName the heat file name
188 * @param context the context
189 * @return the tosca output parameter
191 private static ParameterDefinitionExt getToscaOutputParameter(ServiceTemplate serviceTemplate,
192 String parameterName,
193 Output heatOutputParameter,
194 HeatOrchestrationTemplate
195 heatOrchestrationTemplate,
197 TranslationContext context) {
199 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
200 toscaParameter.setDescription(heatOutputParameter.getDescription());
201 toscaParameter.setValue(
202 getToscaParameterDefaultValue(serviceTemplate,parameterName,heatOutputParameter.getValue(),
203 toscaParameter.getType(),
204 heatFileName, heatOrchestrationTemplate, context));
205 return toscaParameter;
209 * Gets tosca parameter default value.
211 * @param obj the a default
212 * @param type the type
213 * @param heatFileName the heat file name
214 * @param heatOrchestrationTemplate the heat orchestration template
215 * @param context the context
216 * @return the tosca parameter default value
218 public static Object getToscaParameterDefaultValue(ServiceTemplate serviceTemplate,
219 String parameterName,
220 Object obj, String type,
222 HeatOrchestrationTemplate
223 heatOrchestrationTemplate,
224 TranslationContext context) {
229 Object toscaDefaultValue = obj;
230 if ("list".equals(type)) {
231 if (obj instanceof String) {
232 return Arrays.asList(((String) obj).split(","));
234 return toscaDefaultValue;
238 return getToscaParameterValue(serviceTemplate,parameterName,toscaDefaultValue, heatFileName,
239 heatOrchestrationTemplate,
243 private static Object getToscaParameterValue(ServiceTemplate serviceTemplate,
244 String parameterName,
245 Object paramValue, String heatFileName,
246 HeatOrchestrationTemplate heatOrchestrationTemplate,
247 TranslationContext context) {
248 if (paramValue instanceof Map) {
249 if(MapUtils.isEmpty((Map) paramValue)){
250 return new HashMap<>();
252 Map.Entry<String, Object> functionMapEntry =
253 (Map.Entry<String, Object>) ((Map) paramValue).entrySet().iterator().next();
254 if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) {
255 return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get()
256 .translateFunction(serviceTemplate, null, parameterName, functionMapEntry.getKey(),
257 functionMapEntry.getValue(),heatFileName,
258 heatOrchestrationTemplate, null, context);
265 private static List<Constraint> getToscaConstrains(List<Map<String, Object>> constraints) {
266 if (constraints == null) {
270 List<Constraint> constraintList = new ArrayList<>();
272 for (Map<String, Object> constraint : constraints) {
273 constraintList.addAll(getToscaParameterConstraint(constraint));
276 return constraintList;
279 private static List<Constraint> getToscaParameterConstraint(Map<String, Object> constraint) {
280 List<Constraint> convertedConstraintList = new ArrayList<>();
281 Constraint convertedConstraint;
283 if (constraint.containsKey(RANGE)) {
284 convertedConstraint = new Constraint();
285 convertedConstraintList.add(convertedConstraint);
286 Integer min = (Integer) ((Map) constraint.get(RANGE)).get(MIN);
287 Integer max = (Integer) ((Map) constraint.get(RANGE)).get(MAX);
288 convertedConstraint.setIn_range(new Integer[]{min, max});
290 } else if (constraint.containsKey(LENGTH)) {
291 Integer min = (Integer) ((Map) constraint.get(LENGTH)).get(MIN);
292 Integer max = (Integer) ((Map) constraint.get(LENGTH)).get(MAX);
294 convertedConstraint = new Constraint();
295 convertedConstraintList.add(convertedConstraint);
296 convertedConstraint.setMax_length(max);
299 convertedConstraint = new Constraint();
300 convertedConstraintList.add(convertedConstraint);
301 convertedConstraint.setMin_length(min);
303 } else if (constraint.containsKey(ALLOWED_VALUES)) {
304 convertedConstraint = new Constraint();
305 convertedConstraintList.add(convertedConstraint);
306 convertedConstraint.setValid_values((List) constraint.get(ALLOWED_VALUES));
307 } else if (constraint.containsKey(ALLOWED_PATTERN)) {
308 convertedConstraint = new Constraint();
309 convertedConstraintList.add(convertedConstraint);
310 convertedConstraint.setPattern(constraint.get(ALLOWED_PATTERN));
313 return convertedConstraintList;
316 private static EntrySchema getToscaParameterEntrySchema(String type) {
318 if (!parameterEntrySchemaTypeMapping.containsKey(type)) {
322 EntrySchema entrySchema = new EntrySchema();
323 entrySchema.setType(parameterEntrySchemaTypeMapping.get(type));
327 protected static String getToscaParameterType(String heatParameterType) {
328 return parameterTypeMapping.get(heatParameterType);