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.tosca.services.ToscaUtil;
34 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
35 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory;
36 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
41 public class TranslatorHeatToToscaParameterConverter {
44 private static Map<String, String> parameterTypeMapping;
45 private static Map<String, String> parameterEntrySchemaTypeMapping;
48 parameterEntrySchemaTypeMapping = new HashMap<>();
49 parameterEntrySchemaTypeMapping.put("list", "string");
53 parameterTypeMapping = new HashMap<>();
54 parameterTypeMapping.put("string", "string");
55 parameterTypeMapping.put("number", "float");
56 parameterTypeMapping.put("comma_delimited_list", "list");
57 parameterTypeMapping.put("json", "json");
58 parameterTypeMapping.put("boolean", "boolean");
62 * Parameter converter map.
64 * @param parameters the parameters
65 * @param heatOrchestrationTemplate the heat orchestration template
66 * @param heatFileName the heat file name
67 * @param context the context
70 public static Map<String, ParameterDefinition> parameterConverter(ServiceTemplate serviceTemplate,
71 Map<String, Parameter> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
72 String heatFileName, String parentHeatFileName, TranslationContext context) {
73 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
74 for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
75 String heatParamName = entry.getKey();
76 parameterDefinitionMap.put(heatParamName,
77 getToscaParameter(serviceTemplate,heatParamName, entry.getValue(),
78 heatOrchestrationTemplate,
79 heatFileName, parentHeatFileName, context));
81 return parameterDefinitionMap;
85 * Parameter output converter map.
87 * @param parameters the parameters
88 * @param heatOrchestrationTemplate the heat orchestration template
89 * @param heatFileName the heat file name
90 * @param context the context
93 public static Map<String, ParameterDefinition> parameterOutputConverter(ServiceTemplate
95 Map<String, Output> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
96 String heatFileName, TranslationContext context) {
97 Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
98 for (Map.Entry<String, Output> entry : parameters.entrySet()) {
99 parameterDefinitionMap.put(entry.getKey(),
100 getToscaOutputParameter(serviceTemplate,entry.getKey(),entry.getValue(),
101 heatOrchestrationTemplate,
105 return parameterDefinitionMap;
109 * Gets tosca parameter.
111 * @param heatParameter the heat parameter
112 * @param heatOrchestrationTemplate the heat orchestration template
113 * @param heatFileName the heat file name
114 * @param context the context
115 * @return the tosca parameter
117 private static ParameterDefinitionExt getToscaParameter(ServiceTemplate serviceTemplate,
118 String heatParameterName,
119 Parameter heatParameter,
120 HeatOrchestrationTemplate
121 heatOrchestrationTemplate,
123 String parentHeatFileName,
124 TranslationContext context) {
126 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
127 toscaParameter.setType(getToscaParameterType(heatParameter.getType()));
128 toscaParameter.setEntry_schema(getToscaParameterEntrySchema(toscaParameter.getType()));
129 toscaParameter.setLabel(heatParameter.getLabel());
130 toscaParameter.setDescription(heatParameter.getDescription());
131 toscaParameter.set_default(
132 getToscaParameterDefaultValue(serviceTemplate, heatParameterName, heatParameter.get_default(),
133 toscaParameter.getType(), heatFileName, heatOrchestrationTemplate, context));
134 toscaParameter.setHidden(heatParameter.isHidden());
135 toscaParameter.setImmutable(heatParameter.isImmutable());
136 toscaParameter.setConstraints(getToscaConstrains(heatParameter.getConstraints()));
137 Optional<Map<String, AnnotationDefinition>> annotations = getToscaAnnotations(context, serviceTemplate, heatFileName, parentHeatFileName, heatParameterName);
138 annotations.ifPresent(ant->toscaParameter.setAnnotations(annotations.get()));
141 return toscaParameter;
144 private static Optional<Map<String, AnnotationDefinition> > getToscaAnnotations (TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName, String parentHeatFileName, String heatParameterName){
146 if(parentHeatFileName != null){
147 heatFileName = parentHeatFileName;
150 if(!isAnnotationRequired(context, serviceTemplate, heatFileName)){
151 return Optional.empty();
154 AnnotationDefinition annotationDefinition = new AnnotationDefinition();
155 annotationDefinition.setType(ToscaAnnotationType.SOURCE);
156 annotationDefinition.setProperties(new HashMap<>());
157 List<String> vfModuleList = new ArrayList<>();
158 vfModuleList.add( FileUtils.getFileWithoutExtention(heatFileName));
159 annotationDefinition.getProperties().put(ToscaConstants.VF_MODULE_LABEL_PROPERTY_NAME, vfModuleList);
160 annotationDefinition.getProperties().put(ToscaConstants.SOURCE_TYPE_PROPERTY_NAME, ToscaConstants.HEAT_SOURCE_TYPE);
161 annotationDefinition.getProperties().put(ToscaConstants.PARAM_NAME_PROPERTY_NAME, heatParameterName);
162 Map<String, AnnotationDefinition> annotationMap = new HashMap<>();
163 annotationMap.put(ToscaConstants.SOURCE_ANNOTATION_ID, annotationDefinition);
164 return Optional.of(annotationMap);
168 private static boolean isAnnotationRequired(TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName){
169 return HeatToToscaUtil.shouldAnnotationsToBeAdded() && !isNestedServiceTemplate(context, serviceTemplate, heatFileName);
172 private static boolean isNestedServiceTemplate(TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName) {
173 String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate.getMetadata());
174 return HeatToToscaUtil.isHeatFileNested(context, heatFileName);
179 * Gets tosca output parameter.
181 * @param heatOutputParameter the heat output parameter
182 * @param heatOrchestrationTemplate the heat orchestration template
183 * @param heatFileName the heat file name
184 * @param context the context
185 * @return the tosca output parameter
187 private static ParameterDefinitionExt getToscaOutputParameter(ServiceTemplate serviceTemplate,
188 String parameterName,
189 Output heatOutputParameter,
190 HeatOrchestrationTemplate
191 heatOrchestrationTemplate,
193 TranslationContext context) {
195 ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
196 toscaParameter.setDescription(heatOutputParameter.getDescription());
197 toscaParameter.setValue(
198 getToscaParameterDefaultValue(serviceTemplate,parameterName,heatOutputParameter.getValue(),
199 toscaParameter.getType(),
200 heatFileName, heatOrchestrationTemplate, context));
201 return toscaParameter;
205 * Gets tosca parameter default value.
207 * @param obj the a default
208 * @param type the type
209 * @param heatFileName the heat file name
210 * @param heatOrchestrationTemplate the heat orchestration template
211 * @param context the context
212 * @return the tosca parameter default value
214 public static Object getToscaParameterDefaultValue(ServiceTemplate serviceTemplate,
215 String parameterName,
216 Object obj, String type,
218 HeatOrchestrationTemplate
219 heatOrchestrationTemplate,
220 TranslationContext context) {
225 Object toscaDefaultValue = obj;
226 if ("list".equals(type)) {
227 if (obj instanceof String) {
228 return Arrays.asList(((String) obj).split(","));
230 return toscaDefaultValue;
234 return getToscaParameterValue(serviceTemplate,parameterName,toscaDefaultValue, heatFileName,
235 heatOrchestrationTemplate,
239 private static Object getToscaParameterValue(ServiceTemplate serviceTemplate,
240 String parameterName,
241 Object paramValue, String heatFileName,
242 HeatOrchestrationTemplate heatOrchestrationTemplate,
243 TranslationContext context) {
244 if (paramValue instanceof Map) {
245 if(MapUtils.isEmpty((Map) paramValue)){
246 return new HashMap<>();
248 Map.Entry<String, Object> functionMapEntry =
249 (Map.Entry<String, Object>) ((Map) paramValue).entrySet().iterator().next();
250 if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) {
251 return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get()
252 .translateFunction(serviceTemplate, null, parameterName, functionMapEntry.getKey(),
253 functionMapEntry.getValue(),heatFileName,
254 heatOrchestrationTemplate, null, context);
261 private static List<Constraint> getToscaConstrains(List<Map<String, Object>> constraints) {
262 if (constraints == null) {
266 List<Constraint> constraintList = new ArrayList<>();
268 for (Map<String, Object> constraint : constraints) {
269 constraintList.addAll(getToscaParameterConstraint(constraint));
272 return constraintList;
275 private static List<Constraint> getToscaParameterConstraint(Map<String, Object> constraint) {
276 List<Constraint> convertedConstraintList = new ArrayList<>();
277 Constraint convertedConstraint;
279 if (constraint.containsKey("range")) {
280 convertedConstraint = new Constraint();
281 convertedConstraintList.add(convertedConstraint);
282 Integer min = (Integer) ((Map) constraint.get("range")).get("min");
283 Integer max = (Integer) ((Map) constraint.get("range")).get("max");
284 convertedConstraint.setIn_range(new Integer[]{min, max});
286 } else if (constraint.containsKey("length")) {
287 Integer min = (Integer) ((Map) constraint.get("length")).get("min");
288 Integer max = (Integer) ((Map) constraint.get("length")).get("max");
290 convertedConstraint = new Constraint();
291 convertedConstraintList.add(convertedConstraint);
292 convertedConstraint.setMax_length(max);
295 convertedConstraint = new Constraint();
296 convertedConstraintList.add(convertedConstraint);
297 convertedConstraint.setMin_length(min);
299 } else if (constraint.containsKey("allowed_values")) {
300 convertedConstraint = new Constraint();
301 convertedConstraintList.add(convertedConstraint);
302 convertedConstraint.setValid_values((List) constraint.get("allowed_values"));
303 } else if (constraint.containsKey("allowed_pattern")) {
304 convertedConstraint = new Constraint();
305 convertedConstraintList.add(convertedConstraint);
306 convertedConstraint.setPattern(constraint.get("allowed_pattern"));
309 return convertedConstraintList;
312 private static EntrySchema getToscaParameterEntrySchema(String type) {
314 if (!parameterEntrySchemaTypeMapping.containsKey(type)) {
318 EntrySchema entrySchema = new EntrySchema();
319 entrySchema.setType(parameterEntrySchemaTypeMapping.get(type));
323 protected static String getToscaParameterType(String heatParameterType) {
324 return parameterTypeMapping.get(heatParameterType);