3b9497644cd0b048f445d8a2bb776b27c0277c1d
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / mapping / TranslatorHeatToToscaParameterConverter.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.translator.services.heattotosca.mapping;
18
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;
33
34 import java.util.*;
35
36
37 public class TranslatorHeatToToscaParameterConverter {
38
39
40   private static Map<String, String> parameterTypeMapping;
41   private static Map<String, String> parameterEntrySchemaTypeMapping;
42
43   static {
44     parameterEntrySchemaTypeMapping = new HashMap<>();
45     parameterEntrySchemaTypeMapping.put("list", "string");
46   }
47
48   static {
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");
55   }
56
57   /**
58    * Parameter converter map.
59    *
60    * @param parameters                the parameters
61    * @param heatOrchestrationTemplate the heat orchestration template
62    * @param heatFileName              the heat file name
63    * @param context                   the context
64    * @return the map
65    */
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));
76     }
77     return parameterDefinitionMap;
78   }
79
80   /**
81    * Parameter output converter map.
82    *
83    * @param parameters                the parameters
84    * @param heatOrchestrationTemplate the heat orchestration template
85    * @param heatFileName              the heat file name
86    * @param context                   the context
87    * @return the map
88    */
89   public static Map<String, ParameterDefinition> parameterOutputConverter(ServiceTemplate
90                                                                               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,
98               heatFileName,
99               context));
100     }
101     return parameterDefinitionMap;
102   }
103
104   /**
105    * Gets tosca parameter.
106    *
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
112    */
113   private static ParameterDefinitionExt getToscaParameter(ServiceTemplate serviceTemplate,
114                                                          String heatParameterName,
115                                                          Parameter heatParameter,
116                                                          HeatOrchestrationTemplate
117                                                              heatOrchestrationTemplate,
118                                                          String heatFileName,
119                                                          String parentHeatFileName,
120                                                          TranslationContext context) {
121
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()));
135
136
137     return toscaParameter;
138   }
139
140   private static Optional<Map<String, AnnotationDefinition> > getToscaAnnotations (TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName, String parentHeatFileName, String heatParameterName){
141
142     if(parentHeatFileName != null){
143       heatFileName = parentHeatFileName;
144     }
145
146     if(!isAnnotationRequired(context, serviceTemplate, heatFileName)){
147       return Optional.empty();
148     }
149
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);
161
162   }
163
164   private static boolean isAnnotationRequired(TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName){
165     return HeatToToscaUtil.shouldAnnotationsToBeAdded() && !isNestedServiceTemplate(context, serviceTemplate, heatFileName);
166   }
167
168   private static boolean isNestedServiceTemplate(TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName) {
169     String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate.getMetadata());
170     return HeatToToscaUtil.isHeatFileNested(context, heatFileName);
171   }
172
173
174   /**
175    * Gets tosca output parameter.
176    *
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
182    */
183   private static ParameterDefinitionExt getToscaOutputParameter(ServiceTemplate serviceTemplate,
184                                                                String parameterName,
185                                                                Output heatOutputParameter,
186                                                                HeatOrchestrationTemplate
187                                                                    heatOrchestrationTemplate,
188                                                                String heatFileName,
189                                                                TranslationContext context) {
190
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;
198   }
199
200   /**
201    * Gets tosca parameter default value.
202    *
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
209    */
210   public static Object getToscaParameterDefaultValue(ServiceTemplate serviceTemplate,
211                                                      String parameterName,
212                                                      Object obj, String type,
213                                                      String heatFileName,
214                                                      HeatOrchestrationTemplate
215                                                          heatOrchestrationTemplate,
216                                                      TranslationContext context) {
217
218     if (obj == null) {
219       return null;
220     }
221     Object toscaDefaultValue = obj;
222     if ("list".equals(type)) {
223       if (obj instanceof String) {
224         return Arrays.asList(((String) obj).split(","));
225       } else {
226         return toscaDefaultValue;
227       }
228     }
229
230     return getToscaParameterValue(serviceTemplate,parameterName,toscaDefaultValue, heatFileName,
231         heatOrchestrationTemplate,
232         context);
233   }
234
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<>();
243       }
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);
251       }
252     }
253
254     return paramValue;
255   }
256
257   private static List<Constraint> getToscaConstrains(List<Map<String, Object>> constraints) {
258     if (constraints == null) {
259       return null;
260     }
261
262     List<Constraint> constraintList = new ArrayList<>();
263
264     for (Map<String, Object> constraint : constraints) {
265       constraintList.addAll(getToscaParameterConstraint(constraint));
266     }
267
268     return constraintList;
269   }
270
271   private static List<Constraint> getToscaParameterConstraint(Map<String, Object> constraint) {
272     List<Constraint> convertedConstraintList = new ArrayList<>();
273     Constraint convertedConstraint;
274
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});
281
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");
285       if (max != null) {
286         convertedConstraint = new Constraint();
287         convertedConstraintList.add(convertedConstraint);
288         convertedConstraint.setMax_length(max);
289       }
290       if (min != null) {
291         convertedConstraint = new Constraint();
292         convertedConstraintList.add(convertedConstraint);
293         convertedConstraint.setMin_length(min);
294       }
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"));
303     }
304
305     return convertedConstraintList;
306   }
307
308   private static EntrySchema getToscaParameterEntrySchema(String type) {
309
310     if (!parameterEntrySchemaTypeMapping.containsKey(type)) {
311       return null;
312     }
313
314     EntrySchema entrySchema = new EntrySchema();
315     entrySchema.setType(parameterEntrySchemaTypeMapping.get(type));
316     return entrySchema;
317   }
318
319   protected static String getToscaParameterType(String heatParameterType) {
320     return parameterTypeMapping.get(heatParameterType);
321   }
322 }