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 org.apache.commons.collections4.MapUtils;
22 import org.onap.sdc.tosca.datatypes.model.Constraint;
23 import org.onap.sdc.tosca.datatypes.model.EntrySchema;
24 import org.onap.sdc.tosca.datatypes.model.ParameterDefinition;
25 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
26 import org.openecomp.core.utilities.file.FileUtils;
27 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
28 import org.openecomp.sdc.heat.datatypes.model.Output;
29 import org.openecomp.sdc.heat.datatypes.model.Parameter;
30 import org.openecomp.sdc.tosca.datatypes.extend.ToscaAnnotationType;
31 import org.onap.sdc.tosca.datatypes.model.*;
32 import org.onap.sdc.tosca.datatypes.model.heatextend.AnnotationDefinition;
33 import org.onap.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
34 import org.openecomp.sdc.tosca.services.ToscaConstants;
35 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
36 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory;
37 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
38 import org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation.FunctionTranslator;
42 public class TranslatorHeatToToscaParameterConverter {
45 private static Map<String, String> parameterTypeMapping;
46 private static Map<String, String> parameterEntrySchemaTypeMapping;
47 private static final String RANGE = "range";
48 private static final String LENGTH = "length";
49 private static final String MIN = "min";
50 private static final String MAX = "max";
51 private static final String ALLOWED_VALUES = "allowed_values";
52 private static final String ALLOWED_PATTERN = "allowed_pattern";
56 parameterEntrySchemaTypeMapping = new HashMap<>();
57 parameterEntrySchemaTypeMapping.put("list", "string");
61 parameterTypeMapping = new HashMap<>();
62 parameterTypeMapping.put("string", "string");
63 parameterTypeMapping.put("number", "float");
64 parameterTypeMapping.put("comma_delimited_list", "list");
65 parameterTypeMapping.put("json", "json");
66 parameterTypeMapping.put("boolean", "boolean");
70 * Parameter converter map.
72 * @param parameters the parameters
73 * @param heatOrchestrationTemplate the heat orchestration template
74 * @param heatFileName the heat file name
75 * @param context the context
78 public static Map<String, ParameterDefinition> parameterConverter(ServiceTemplate serviceTemplate,
79 Map<String, Parameter> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
80 String heatFileName, String parentHeatFileName, TranslationContext context) {
81 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
82 for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
83 String heatParamName = entry.getKey();
84 parameterDefinitionMap.put(heatParamName,
85 getToscaParameter(serviceTemplate,heatParamName, entry.getValue(),
86 heatOrchestrationTemplate,
87 heatFileName, parentHeatFileName, context));
89 return parameterDefinitionMap;
93 * Parameter output converter map.
95 * @param parameters the parameters
96 * @param heatOrchestrationTemplate the heat orchestration template
97 * @param heatFileName the heat file name
98 * @param context the context
101 public static Map<String, ParameterDefinition> parameterOutputConverter(ServiceTemplate
103 Map<String, Output> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
104 String heatFileName, TranslationContext context) {
105 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
106 for (Map.Entry<String, Output> entry : parameters.entrySet()) {
107 parameterDefinitionMap.put(entry.getKey(),
108 getToscaOutputParameter(serviceTemplate,entry.getKey(),entry.getValue(),
109 heatOrchestrationTemplate,
113 return parameterDefinitionMap;
117 * Gets tosca parameter.
119 * @param heatParameter the heat parameter
120 * @param heatOrchestrationTemplate the heat orchestration template
121 * @param heatFileName the heat file name
122 * @param context the context
123 * @return the tosca parameter
125 private static ParameterDefinitionExt getToscaParameter(ServiceTemplate serviceTemplate,
126 String heatParameterName,
127 Parameter heatParameter,
128 HeatOrchestrationTemplate
129 heatOrchestrationTemplate,
131 String parentHeatFileName,
132 TranslationContext context) {
134 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
135 toscaParameter.setType(getToscaParameterType(heatParameter.getType()));
136 toscaParameter.setEntry_schema(getToscaParameterEntrySchema(toscaParameter.getType()));
137 toscaParameter.setLabel(heatParameter.getLabel());
138 toscaParameter.setDescription(heatParameter.getDescription());
139 toscaParameter.set_default(
140 getToscaParameterDefaultValue(serviceTemplate, heatParameterName, heatParameter.get_default(),
141 toscaParameter.getType(), heatFileName, heatOrchestrationTemplate, context));
142 toscaParameter.setHidden(heatParameter.isHidden());
143 toscaParameter.setImmutable(heatParameter.isImmutable());
144 toscaParameter.setConstraints(getToscaConstrains(heatParameter.getConstraints()));
145 Optional<Map<String, AnnotationDefinition>> annotations = getToscaAnnotations(context, heatFileName, parentHeatFileName, heatParameterName);
146 annotations.ifPresent(ant->toscaParameter.setAnnotations(annotations.get()));
149 return toscaParameter;
152 private static Optional<Map<String, AnnotationDefinition> > getToscaAnnotations (TranslationContext context, String heatFileName, String parentHeatFileName, String heatParameterName){
154 if(parentHeatFileName != null){
155 heatFileName = parentHeatFileName;
158 if(!isAnnotationRequired(context, heatFileName)){
159 return Optional.empty();
162 AnnotationDefinition annotationDefinition = new AnnotationDefinition();
163 annotationDefinition.setType(ToscaAnnotationType.SOURCE);
164 annotationDefinition.setProperties(new HashMap<>());
165 List<String> vfModuleList = new ArrayList<>();
166 vfModuleList.add( FileUtils.getFileWithoutExtention(heatFileName));
167 annotationDefinition.getProperties().put(ToscaConstants.VF_MODULE_LABEL_PROPERTY_NAME, vfModuleList);
168 annotationDefinition.getProperties().put(ToscaConstants.SOURCE_TYPE_PROPERTY_NAME, ToscaConstants.HEAT_SOURCE_TYPE);
169 annotationDefinition.getProperties().put(ToscaConstants.PARAM_NAME_PROPERTY_NAME, heatParameterName);
170 Map<String, AnnotationDefinition> annotationMap = new HashMap<>();
171 annotationMap.put(ToscaConstants.SOURCE_ANNOTATION_ID, annotationDefinition);
172 return Optional.of(annotationMap);
176 private static boolean isAnnotationRequired(TranslationContext context, String heatFileName){
177 return HeatToToscaUtil.shouldAnnotationsToBeAdded() && !isNestedServiceTemplate(context, heatFileName);
180 private static boolean isNestedServiceTemplate(TranslationContext context, String heatFileName) {
181 return HeatToToscaUtil.isHeatFileNested(context, heatFileName);
186 * Gets tosca output parameter.
188 * @param heatOutputParameter the heat output parameter
189 * @param heatOrchestrationTemplate the heat orchestration template
190 * @param heatFileName the heat file name
191 * @param context the context
192 * @return the tosca output parameter
194 private static ParameterDefinitionExt getToscaOutputParameter(ServiceTemplate serviceTemplate,
195 String parameterName,
196 Output heatOutputParameter,
197 HeatOrchestrationTemplate
198 heatOrchestrationTemplate,
200 TranslationContext context) {
202 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
203 toscaParameter.setDescription(heatOutputParameter.getDescription());
204 toscaParameter.setValue(
205 getToscaParameterDefaultValue(serviceTemplate,parameterName,heatOutputParameter.getValue(),
206 toscaParameter.getType(),
207 heatFileName, heatOrchestrationTemplate, context));
208 return toscaParameter;
212 * Gets tosca parameter default value.
214 * @param obj the a default
215 * @param type the type
216 * @param heatFileName the heat file name
217 * @param heatOrchestrationTemplate the heat orchestration template
218 * @param context the context
219 * @return the tosca parameter default value
221 public static Object getToscaParameterDefaultValue(ServiceTemplate serviceTemplate,
222 String parameterName,
223 Object obj, String type,
225 HeatOrchestrationTemplate
226 heatOrchestrationTemplate,
227 TranslationContext context) {
232 Object toscaDefaultValue = obj;
233 if ("list".equals(type)) {
234 if (obj instanceof String) {
235 return Arrays.asList(((String) obj).split(","));
237 return toscaDefaultValue;
241 return getToscaParameterValue(serviceTemplate,parameterName,toscaDefaultValue, heatFileName,
242 heatOrchestrationTemplate,
246 private static Object getToscaParameterValue(ServiceTemplate serviceTemplate,
247 String parameterName,
248 Object paramValue, String heatFileName,
249 HeatOrchestrationTemplate heatOrchestrationTemplate,
250 TranslationContext context) {
251 if (paramValue instanceof Map) {
252 if(MapUtils.isEmpty((Map) paramValue)){
253 return new HashMap<>();
255 Map.Entry<String, Object> functionMapEntry =
256 (Map.Entry<String, Object>) ((Map) paramValue).entrySet().iterator().next();
257 if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) {
259 FunctionTranslator functionTranslator = new FunctionTranslator(getFunctionTranslateTo(serviceTemplate, null,
260 heatFileName, heatOrchestrationTemplate, context), parameterName, functionMapEntry.getValue(), null);
261 return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get()
262 .translateFunction(functionTranslator);
269 private static List<Constraint> getToscaConstrains(List<Map<String, Object>> constraints) {
270 if (constraints == null) {
274 List<Constraint> constraintList = new ArrayList<>();
276 for (Map<String, Object> constraint : constraints) {
277 constraintList.addAll(getToscaParameterConstraint(constraint));
280 return constraintList;
283 private static List<Constraint> getToscaParameterConstraint(Map<String, Object> constraint) {
284 List<Constraint> convertedConstraintList = new ArrayList<>();
285 Constraint convertedConstraint;
287 if (constraint.containsKey(RANGE)) {
288 convertedConstraint = new Constraint();
289 convertedConstraintList.add(convertedConstraint);
290 Integer min = (Integer) ((Map) constraint.get(RANGE)).get(MIN);
291 Integer max = (Integer) ((Map) constraint.get(RANGE)).get(MAX);
292 convertedConstraint.setIn_range(new Integer[]{min, max});
294 } else if (constraint.containsKey(LENGTH)) {
295 Integer min = (Integer) ((Map) constraint.get(LENGTH)).get(MIN);
296 Integer max = (Integer) ((Map) constraint.get(LENGTH)).get(MAX);
298 convertedConstraint = new Constraint();
299 convertedConstraintList.add(convertedConstraint);
300 convertedConstraint.setMax_length(max);
303 convertedConstraint = new Constraint();
304 convertedConstraintList.add(convertedConstraint);
305 convertedConstraint.setMin_length(min);
307 } else if (constraint.containsKey(ALLOWED_VALUES)) {
308 convertedConstraint = new Constraint();
309 convertedConstraintList.add(convertedConstraint);
310 convertedConstraint.setValid_values((List) constraint.get(ALLOWED_VALUES));
311 } else if (constraint.containsKey(ALLOWED_PATTERN)) {
312 convertedConstraint = new Constraint();
313 convertedConstraintList.add(convertedConstraint);
314 convertedConstraint.setPattern(constraint.get(ALLOWED_PATTERN));
317 return convertedConstraintList;
320 private static EntrySchema getToscaParameterEntrySchema(String type) {
322 if (!parameterEntrySchemaTypeMapping.containsKey(type)) {
326 EntrySchema entrySchema = new EntrySchema();
327 entrySchema.setType(parameterEntrySchemaTypeMapping.get(type));
331 protected static String getToscaParameterType(String heatParameterType) {
332 return parameterTypeMapping.get(heatParameterType);