re base code
[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 static org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation.FunctionTranslator.getFunctionTranslateTo;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27
28 import org.apache.commons.collections4.MapUtils;
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.core.utilities.file.FileUtils;
33
34 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
35 import org.openecomp.sdc.heat.datatypes.model.Output;
36 import org.openecomp.sdc.heat.datatypes.model.Parameter;
37 import org.openecomp.sdc.tosca.datatypes.extend.ToscaAnnotationType;
38 import org.openecomp.sdc.tosca.services.ToscaConstants;
39 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
40 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory;
41 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
42 import org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation.FunctionTranslator;
43
44 public class TranslatorHeatToToscaParameterConverter {
45
46
47   private static Map<String, String> parameterTypeMapping;
48   private static Map<String, String> parameterEntrySchemaTypeMapping;
49   private static final String RANGE = "range";
50   private static final String LENGTH = "length";
51   private static final String MIN = "min";
52   private static final String MAX = "max";
53   private static final String ALLOWED_VALUES = "allowed_values";
54   private static final String ALLOWED_PATTERN = "allowed_pattern";
55
56
57   static {
58     parameterEntrySchemaTypeMapping = new HashMap<>();
59     parameterEntrySchemaTypeMapping.put("list", "string");
60   }
61
62   static {
63     parameterTypeMapping = new HashMap<>();
64     parameterTypeMapping.put("string", "string");
65     parameterTypeMapping.put("number", "float");
66     parameterTypeMapping.put("comma_delimited_list", "list");
67     parameterTypeMapping.put("json", "json");
68     parameterTypeMapping.put("boolean", "boolean");
69   }
70
71   /**
72    * Parameter converter map.
73    *
74    * @param parameters                the parameters
75    * @param heatOrchestrationTemplate the heat orchestration template
76    * @param heatFileName              the heat file name
77    * @param context                   the context
78    * @return the map
79    */
80   public static Map<String, ParameterDefinition> parameterConverter(ServiceTemplate serviceTemplate,
81       Map<String, Parameter> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
82       String heatFileName, String parentHeatFileName, TranslationContext context) {
83     Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
84     for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
85       String heatParamName = entry.getKey();
86       parameterDefinitionMap.put(heatParamName,
87           getToscaParameter(serviceTemplate,heatParamName, entry.getValue(),
88               heatOrchestrationTemplate,
89               heatFileName, parentHeatFileName, context));
90     }
91     return parameterDefinitionMap;
92   }
93
94   /**
95    * Parameter output converter map.
96    *
97    * @param parameters                the parameters
98    * @param heatOrchestrationTemplate the heat orchestration template
99    * @param heatFileName              the heat file name
100    * @param context                   the context
101    * @return the map
102    */
103   public static Map<String, ParameterDefinition> parameterOutputConverter(ServiceTemplate
104                                                                               serviceTemplate,
105       Map<String, Output> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
106       String heatFileName, TranslationContext context) {
107     Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
108     for (Map.Entry<String, Output> entry : parameters.entrySet()) {
109       parameterDefinitionMap.put(entry.getKey(),
110           getToscaOutputParameter(serviceTemplate,entry.getKey(),entry.getValue(),
111               heatOrchestrationTemplate,
112               heatFileName,
113               context));
114     }
115     return parameterDefinitionMap;
116   }
117
118   /**
119    * Gets tosca parameter.
120    *
121    * @param heatParameter             the heat parameter
122    * @param heatOrchestrationTemplate the heat orchestration template
123    * @param heatFileName              the heat file name
124    * @param context                   the context
125    * @return the tosca parameter
126    */
127   private static ParameterDefinitionExt getToscaParameter(ServiceTemplate serviceTemplate,
128                                                          String heatParameterName,
129                                                          Parameter heatParameter,
130                                                          HeatOrchestrationTemplate
131                                                              heatOrchestrationTemplate,
132                                                          String heatFileName,
133                                                          String parentHeatFileName,
134                                                          TranslationContext context) {
135
136     ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
137     toscaParameter.setType(getToscaParameterType(heatParameter.getType()));
138     toscaParameter.setEntry_schema(getToscaParameterEntrySchema(toscaParameter.getType()));
139     toscaParameter.setLabel(heatParameter.getLabel());
140     toscaParameter.setDescription(heatParameter.getDescription());
141     toscaParameter.set_default(
142         getToscaParameterDefaultValue(serviceTemplate, heatParameterName, heatParameter.get_default(),
143             toscaParameter.getType(), heatFileName, heatOrchestrationTemplate, context));
144     toscaParameter.setHidden(heatParameter.isHidden());
145     toscaParameter.setImmutable(heatParameter.isImmutable());
146     toscaParameter.setConstraints(getToscaConstrains(heatParameter.getConstraints()));
147     Optional<Map<String, AnnotationDefinition>>  annotations = getToscaAnnotations(context, heatFileName, parentHeatFileName, heatParameterName);
148     annotations.ifPresent(ant->toscaParameter.setAnnotations(annotations.get()));
149
150
151     return toscaParameter;
152   }
153
154   private static Optional<Map<String, AnnotationDefinition> > getToscaAnnotations (TranslationContext context, String heatFileName, String parentHeatFileName, String heatParameterName){
155
156     if(parentHeatFileName != null){
157       heatFileName = parentHeatFileName;
158     }
159
160     if(!isAnnotationRequired(context, heatFileName)){
161       return Optional.empty();
162     }
163
164     AnnotationDefinition annotationDefinition = new AnnotationDefinition();
165     annotationDefinition.setType(ToscaAnnotationType.SOURCE);
166     annotationDefinition.setProperties(new HashMap<>());
167     List<String> vfModuleList = new ArrayList<>();
168     vfModuleList.add( FileUtils.getFileWithoutExtention(heatFileName));
169     annotationDefinition.getProperties().put(ToscaConstants.VF_MODULE_LABEL_PROPERTY_NAME, vfModuleList);
170     annotationDefinition.getProperties().put(ToscaConstants.SOURCE_TYPE_PROPERTY_NAME, ToscaConstants.HEAT_SOURCE_TYPE);
171     annotationDefinition.getProperties().put(ToscaConstants.PARAM_NAME_PROPERTY_NAME, heatParameterName);
172     Map<String, AnnotationDefinition> annotationMap = new HashMap<>();
173     annotationMap.put(ToscaConstants.SOURCE_ANNOTATION_ID, annotationDefinition);
174     return Optional.of(annotationMap);
175
176   }
177
178   private static boolean isAnnotationRequired(TranslationContext context, String heatFileName){
179     return !isNestedServiceTemplate(context, heatFileName);
180   }
181
182   private static boolean isNestedServiceTemplate(TranslationContext context, String heatFileName) {
183     return HeatToToscaUtil.isHeatFileNested(context, heatFileName);
184   }
185
186
187   /**
188    * Gets tosca output parameter.
189    *
190    * @param heatOutputParameter       the heat output parameter
191    * @param heatOrchestrationTemplate the heat orchestration template
192    * @param heatFileName              the heat file name
193    * @param context                   the context
194    * @return the tosca output parameter
195    */
196   private static ParameterDefinitionExt getToscaOutputParameter(ServiceTemplate serviceTemplate,
197                                                                String parameterName,
198                                                                Output heatOutputParameter,
199                                                                HeatOrchestrationTemplate
200                                                                    heatOrchestrationTemplate,
201                                                                String heatFileName,
202                                                                TranslationContext context) {
203
204     ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
205     toscaParameter.setDescription(heatOutputParameter.getDescription());
206     toscaParameter.setValue(
207         getToscaParameterDefaultValue(serviceTemplate,parameterName,heatOutputParameter.getValue(),
208             toscaParameter.getType(),
209             heatFileName, heatOrchestrationTemplate, context));
210     return toscaParameter;
211   }
212
213   /**
214    * Gets tosca parameter default value.
215    *
216    * @param obj                       the a default
217    * @param type                      the type
218    * @param heatFileName              the heat file name
219    * @param heatOrchestrationTemplate the heat orchestration template
220    * @param context                   the context
221    * @return the tosca parameter default value
222    */
223   public static Object getToscaParameterDefaultValue(ServiceTemplate serviceTemplate,
224                                                      String parameterName,
225                                                      Object obj, String type,
226                                                      String heatFileName,
227                                                      HeatOrchestrationTemplate
228                                                          heatOrchestrationTemplate,
229                                                      TranslationContext context) {
230
231     if (obj == null) {
232       return null;
233     }
234     Object toscaDefaultValue = obj;
235     if ("list".equals(type)) {
236       if (obj instanceof String) {
237         return Arrays.asList(((String) obj).split(","));
238       } else {
239         return toscaDefaultValue;
240       }
241     }
242
243     return getToscaParameterValue(serviceTemplate,parameterName,toscaDefaultValue, heatFileName,
244         heatOrchestrationTemplate,
245         context);
246   }
247
248   private static Object getToscaParameterValue(ServiceTemplate serviceTemplate,
249                                                String parameterName,
250                                                Object paramValue, String heatFileName,
251                                                HeatOrchestrationTemplate heatOrchestrationTemplate,
252                                                TranslationContext context) {
253     if (paramValue instanceof Map) {
254       if(MapUtils.isEmpty((Map) paramValue)){
255         return new HashMap<>();
256       }
257       Map.Entry<String, Object> functionMapEntry =
258           (Map.Entry<String, Object>) ((Map) paramValue).entrySet().iterator().next();
259       if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) {
260
261         FunctionTranslator functionTranslator = new FunctionTranslator(getFunctionTranslateTo(serviceTemplate, null,
262                 heatFileName, heatOrchestrationTemplate, context), parameterName,  functionMapEntry.getValue(), null);
263         return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get()
264             .translateFunction(functionTranslator);
265       }
266     }
267
268     return paramValue;
269   }
270
271   private static List<Constraint> getToscaConstrains(List<Map<String, Object>> constraints) {
272     if (constraints == null) {
273       return null;
274     }
275
276     List<Constraint> constraintList = new ArrayList<>();
277
278     for (Map<String, Object> constraint : constraints) {
279       constraintList.addAll(getToscaParameterConstraint(constraint));
280     }
281
282     return constraintList;
283   }
284
285   private static List<Constraint> getToscaParameterConstraint(Map<String, Object> constraint) {
286     List<Constraint> convertedConstraintList = new ArrayList<>();
287     Constraint convertedConstraint;
288
289     if (constraint.containsKey(RANGE)) {
290       convertedConstraint = new Constraint();
291       convertedConstraintList.add(convertedConstraint);
292       Integer min = (Integer) ((Map) constraint.get(RANGE)).get(MIN);
293       Integer max = (Integer) ((Map) constraint.get(RANGE)).get(MAX);
294       convertedConstraint.setIn_range(new Integer[]{min, max});
295
296     } else if (constraint.containsKey(LENGTH)) {
297       Integer min = (Integer) ((Map) constraint.get(LENGTH)).get(MIN);
298       Integer max = (Integer) ((Map) constraint.get(LENGTH)).get(MAX);
299       if (max != null) {
300         convertedConstraint = new Constraint();
301         convertedConstraintList.add(convertedConstraint);
302         convertedConstraint.setMax_length(max);
303       }
304       if (min != null) {
305         convertedConstraint = new Constraint();
306         convertedConstraintList.add(convertedConstraint);
307         convertedConstraint.setMin_length(min);
308       }
309     } else if (constraint.containsKey(ALLOWED_VALUES)) {
310       convertedConstraint = new Constraint();
311       convertedConstraintList.add(convertedConstraint);
312       convertedConstraint.setValid_values((List) constraint.get(ALLOWED_VALUES));
313     } else if (constraint.containsKey(ALLOWED_PATTERN)) {
314       convertedConstraint = new Constraint();
315       convertedConstraintList.add(convertedConstraint);
316       convertedConstraint.setPattern(constraint.get(ALLOWED_PATTERN));
317     }
318
319     return convertedConstraintList;
320   }
321
322   private static EntrySchema getToscaParameterEntrySchema(String type) {
323
324     if (!parameterEntrySchemaTypeMapping.containsKey(type)) {
325       return null;
326     }
327
328     EntrySchema entrySchema = new EntrySchema();
329     entrySchema.setType(parameterEntrySchemaTypeMapping.get(type));
330     return entrySchema;
331   }
332
333   protected static String getToscaParameterType(String heatParameterType) {
334     return parameterTypeMapping.get(heatParameterType);
335   }
336 }