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.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, String parentHeatFileName, 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, parentHeatFileName, 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 private static ParameterDefinitionExt getToscaParameter(ServiceTemplate serviceTemplate,
114 String heatParameterName,
115 Parameter heatParameter,
116 HeatOrchestrationTemplate
117 heatOrchestrationTemplate,
119 String parentHeatFileName,
120 TranslationContext context) {
122 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
123 toscaParameter.setType(getToscaParameterType(heatParameter.getType()));
124 toscaParameter.setEntry_schema(getToscaParameterEntrySchema(toscaParameter.getType()));
125 toscaParameter.setLabel(heatParameter.getLabel());
126 toscaParameter.setDescription(heatParameter.getDescription());
127 toscaParameter.set_default(
128 getToscaParameterDefaultValue(serviceTemplate, heatParameterName, heatParameter.get_default(),
129 toscaParameter.getType(), heatFileName, heatOrchestrationTemplate, context));
130 toscaParameter.setHidden(heatParameter.isHidden());
131 toscaParameter.setImmutable(heatParameter.isImmutable());
132 toscaParameter.setConstraints(getToscaConstrains(heatParameter.getConstraints()));
133 Optional<Map<String, AnnotationDefinition>> annotations = getToscaAnnotations(context, serviceTemplate, heatFileName, parentHeatFileName, heatParameterName);
134 annotations.ifPresent(ant->toscaParameter.setAnnotations(annotations.get()));
137 return toscaParameter;
140 private static Optional<Map<String, AnnotationDefinition> > getToscaAnnotations (TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName, String parentHeatFileName, String heatParameterName){
142 if(parentHeatFileName != null){
143 heatFileName = parentHeatFileName;
146 if(!isAnnotationRequired(context, serviceTemplate, heatFileName)){
147 return Optional.empty();
150 AnnotationDefinition annotationDefinition = new AnnotationDefinition();
151 annotationDefinition.setType(ToscaAnnotationType.SOURCE);
152 annotationDefinition.setProperties(new HashMap<>());
153 List<String> vfModuleList = new ArrayList<>();
154 vfModuleList.add( FileUtils.getFileWithoutExtention(heatFileName));
155 annotationDefinition.getProperties().put(ToscaConstants.VF_MODULE_LABEL_PROPERTY_NAME, vfModuleList);
156 annotationDefinition.getProperties().put(ToscaConstants.SOURCE_TYPE_PROPERTY_NAME, ToscaConstants.HEAT_SOURCE_TYPE);
157 annotationDefinition.getProperties().put(ToscaConstants.PARAM_NAME_PROPERTY_NAME, heatParameterName);
158 Map<String, AnnotationDefinition> annotationMap = new HashMap<>();
159 annotationMap.put(ToscaConstants.SOURCE_ANNOTATION_ID, annotationDefinition);
160 return Optional.of(annotationMap);
164 private static boolean isAnnotationRequired(TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName){
165 return HeatToToscaUtil.shouldAnnotationsToBeAdded() && !isNestedServiceTemplate(context, serviceTemplate, heatFileName);
168 private static boolean isNestedServiceTemplate(TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName) {
169 String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate.getMetadata());
170 return HeatToToscaUtil.isHeatFileNested(context, heatFileName);
175 * Gets tosca output parameter.
177 * @param heatOutputParameter the heat output parameter
178 * @param heatOrchestrationTemplate the heat orchestration template
179 * @param heatFileName the heat file name
180 * @param context the context
181 * @return the tosca output parameter
183 private static ParameterDefinitionExt getToscaOutputParameter(ServiceTemplate serviceTemplate,
184 String parameterName,
185 Output heatOutputParameter,
186 HeatOrchestrationTemplate
187 heatOrchestrationTemplate,
189 TranslationContext context) {
191 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
192 toscaParameter.setDescription(heatOutputParameter.getDescription());
193 toscaParameter.setValue(
194 getToscaParameterDefaultValue(serviceTemplate,parameterName,heatOutputParameter.getValue(),
195 toscaParameter.getType(),
196 heatFileName, heatOrchestrationTemplate, context));
197 return toscaParameter;
201 * Gets tosca parameter default value.
203 * @param obj the a default
204 * @param type the type
205 * @param heatFileName the heat file name
206 * @param heatOrchestrationTemplate the heat orchestration template
207 * @param context the context
208 * @return the tosca parameter default value
210 public static Object getToscaParameterDefaultValue(ServiceTemplate serviceTemplate,
211 String parameterName,
212 Object obj, String type,
214 HeatOrchestrationTemplate
215 heatOrchestrationTemplate,
216 TranslationContext context) {
221 Object toscaDefaultValue = obj;
222 if ("list".equals(type)) {
223 if (obj instanceof String) {
224 return Arrays.asList(((String) obj).split(","));
226 return toscaDefaultValue;
230 return getToscaParameterValue(serviceTemplate,parameterName,toscaDefaultValue, heatFileName,
231 heatOrchestrationTemplate,
235 private static Object getToscaParameterValue(ServiceTemplate serviceTemplate,
236 String parameterName,
237 Object paramValue, String heatFileName,
238 HeatOrchestrationTemplate heatOrchestrationTemplate,
239 TranslationContext context) {
240 if (paramValue instanceof Map) {
241 if(MapUtils.isEmpty((Map) paramValue)){
242 return new HashMap<>();
244 Map.Entry<String, Object> functionMapEntry =
245 (Map.Entry<String, Object>) ((Map) paramValue).entrySet().iterator().next();
246 if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) {
247 return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get()
248 .translateFunction(serviceTemplate, null, parameterName, functionMapEntry.getKey(),
249 functionMapEntry.getValue(),heatFileName,
250 heatOrchestrationTemplate, null, context);
257 private static List<Constraint> getToscaConstrains(List<Map<String, Object>> constraints) {
258 if (constraints == null) {
262 List<Constraint> constraintList = new ArrayList<>();
264 for (Map<String, Object> constraint : constraints) {
265 constraintList.addAll(getToscaParameterConstraint(constraint));
268 return constraintList;
271 private static List<Constraint> getToscaParameterConstraint(Map<String, Object> constraint) {
272 List<Constraint> convertedConstraintList = new ArrayList<>();
273 Constraint convertedConstraint;
275 if (constraint.containsKey("range")) {
276 convertedConstraint = new Constraint();
277 convertedConstraintList.add(convertedConstraint);
278 Integer min = (Integer) ((Map) constraint.get("range")).get("min");
279 Integer max = (Integer) ((Map) constraint.get("range")).get("max");
280 convertedConstraint.setIn_range(new Integer[]{min, max});
282 } else if (constraint.containsKey("length")) {
283 Integer min = (Integer) ((Map) constraint.get("length")).get("min");
284 Integer max = (Integer) ((Map) constraint.get("length")).get("max");
286 convertedConstraint = new Constraint();
287 convertedConstraintList.add(convertedConstraint);
288 convertedConstraint.setMax_length(max);
291 convertedConstraint = new Constraint();
292 convertedConstraintList.add(convertedConstraint);
293 convertedConstraint.setMin_length(min);
295 } else if (constraint.containsKey("allowed_values")) {
296 convertedConstraint = new Constraint();
297 convertedConstraintList.add(convertedConstraint);
298 convertedConstraint.setValid_values((List) constraint.get("allowed_values"));
299 } else if (constraint.containsKey("allowed_pattern")) {
300 convertedConstraint = new Constraint();
301 convertedConstraintList.add(convertedConstraint);
302 convertedConstraint.setPattern(constraint.get("allowed_pattern"));
305 return convertedConstraintList;
308 private static EntrySchema getToscaParameterEntrySchema(String type) {
310 if (!parameterEntrySchemaTypeMapping.containsKey(type)) {
314 EntrySchema entrySchema = new EntrySchema();
315 entrySchema.setType(parameterEntrySchemaTypeMapping.get(type));
319 protected static String getToscaParameterType(String heatParameterType) {
320 return parameterTypeMapping.get(heatParameterType);