930a2dee586d2da268c7b08cf89304df8b6ae45a
[sdc.git] /
1 /*
2  * Copyright © 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, 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, 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   public static ParameterDefinitionExt getToscaParameter(ServiceTemplate serviceTemplate,
114                                                          String heatParameterName,
115                                                          Parameter heatParameter,
116                                                          HeatOrchestrationTemplate
117                                                              heatOrchestrationTemplate,
118                                                          String heatFileName,
119                                                          TranslationContext context) {
120
121     ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
122     toscaParameter.setType(getToscaParameterType(heatParameter.getType()));
123     toscaParameter.setEntry_schema(getToscaParameterEntrySchema(toscaParameter.getType()));
124     toscaParameter.setLabel(heatParameter.getLabel());
125     toscaParameter.setDescription(heatParameter.getDescription());
126     toscaParameter.set_default(
127         getToscaParameterDefaultValue(serviceTemplate, heatParameterName, heatParameter.get_default(),
128             toscaParameter.getType(), heatFileName, heatOrchestrationTemplate, context));
129     toscaParameter.setHidden(heatParameter.isHidden());
130     toscaParameter.setImmutable(heatParameter.isImmutable());
131     toscaParameter.setConstraints(getToscaConstrains(heatParameter.getConstraints()));
132     Optional<Map<String, AnnotationDefinition>>  annotations = getToscaAnnotations(context, serviceTemplate, heatFileName, heatParameterName);
133     annotations.ifPresent(ant->toscaParameter.setAnnotations(annotations.get()));
134
135
136     return toscaParameter;
137   }
138
139   private static Optional<Map<String, AnnotationDefinition> > getToscaAnnotations (TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName, String heatParameterName){
140
141     if(!isAnnotationRequired(context, serviceTemplate, heatFileName)){
142       return Optional.empty();
143     }
144
145     AnnotationDefinition annotationDefinition = new AnnotationDefinition();
146     annotationDefinition.setType(ToscaAnnotationType.SOURCE);
147     annotationDefinition.setProperties(new HashMap<>());
148     List<String> vfModuleList = new ArrayList<>();
149     vfModuleList.add( FileUtils.getFileWithoutExtention(heatFileName));
150     annotationDefinition.getProperties().put(ToscaConstants.VF_MODULE_LABEL_PROPERTY_NAME, vfModuleList);
151     annotationDefinition.getProperties().put(ToscaConstants.SOURCE_TYPE_PROPERTY_NAME, ToscaConstants.HEAT_SOURCE_TYPE);
152     annotationDefinition.getProperties().put(ToscaConstants.PARAM_NAME_PROPERTY_NAME, heatParameterName);
153     Map<String, AnnotationDefinition> annotationMap = new HashMap<>();
154     annotationMap.put(ToscaConstants.SOURCE_ANNOTATION_ID, annotationDefinition);
155     return Optional.of(annotationMap);
156
157   }
158
159   private static boolean isAnnotationRequired(TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName){
160     return HeatToToscaUtil.shouldAnnotationsToBeAdded() && !isNestedServiceTemplate(context, serviceTemplate, heatFileName);
161   }
162
163   private static boolean isNestedServiceTemplate(TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName) {
164     String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate.getMetadata());
165     return HeatToToscaUtil.isHeatFileNested(context, heatFileName) || context.getNestedHeatFileName().containsKey(serviceTemplateFileName);
166   }
167
168
169   /**
170    * Gets tosca output parameter.
171    *
172    * @param heatOutputParameter       the heat output parameter
173    * @param heatOrchestrationTemplate the heat orchestration template
174    * @param heatFileName              the heat file name
175    * @param context                   the context
176    * @return the tosca output parameter
177    */
178   private static ParameterDefinitionExt getToscaOutputParameter(ServiceTemplate serviceTemplate,
179                                                                String parameterName,
180                                                                Output heatOutputParameter,
181                                                                HeatOrchestrationTemplate
182                                                                    heatOrchestrationTemplate,
183                                                                String heatFileName,
184                                                                TranslationContext context) {
185
186     ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
187     toscaParameter.setDescription(heatOutputParameter.getDescription());
188     toscaParameter.setValue(
189         getToscaParameterDefaultValue(serviceTemplate,parameterName,heatOutputParameter.getValue(),
190             toscaParameter.getType(),
191             heatFileName, heatOrchestrationTemplate, context));
192     return toscaParameter;
193   }
194
195   /**
196    * Gets tosca parameter default value.
197    *
198    * @param obj                       the a default
199    * @param type                      the type
200    * @param heatFileName              the heat file name
201    * @param heatOrchestrationTemplate the heat orchestration template
202    * @param context                   the context
203    * @return the tosca parameter default value
204    */
205   public static Object getToscaParameterDefaultValue(ServiceTemplate serviceTemplate,
206                                                      String parameterName,
207                                                      Object obj, String type,
208                                                      String heatFileName,
209                                                      HeatOrchestrationTemplate
210                                                          heatOrchestrationTemplate,
211                                                      TranslationContext context) {
212
213     if (obj == null) {
214       return null;
215     }
216     Object toscaDefaultValue = obj;
217     if ("list".equals(type)) {
218       if (obj instanceof String) {
219         return Arrays.asList(((String) obj).split(","));
220       } else {
221         return toscaDefaultValue;
222       }
223     }
224
225     return getToscaParameterValue(serviceTemplate,parameterName,toscaDefaultValue, heatFileName,
226         heatOrchestrationTemplate,
227         context);
228   }
229
230   private static Object getToscaParameterValue(ServiceTemplate serviceTemplate,
231                                                String parameterName,
232                                                Object paramValue, String heatFileName,
233                                                HeatOrchestrationTemplate heatOrchestrationTemplate,
234                                                TranslationContext context) {
235     if (paramValue instanceof Map) {
236       if(MapUtils.isEmpty((Map) paramValue)){
237         return new HashMap<>();
238       }
239       Map.Entry<String, Object> functionMapEntry =
240           (Map.Entry<String, Object>) ((Map) paramValue).entrySet().iterator().next();
241       if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) {
242         return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get()
243             .translateFunction(serviceTemplate, null, parameterName, functionMapEntry.getKey(),
244                 functionMapEntry.getValue(),heatFileName,
245                 heatOrchestrationTemplate, null, context);
246       }
247     }
248
249     return paramValue;
250   }
251
252   private static List<Constraint> getToscaConstrains(List<Map<String, Object>> constraints) {
253     if (constraints == null) {
254       return null;
255     }
256
257     List<Constraint> constraintList = new ArrayList<>();
258
259     for (Map<String, Object> constraint : constraints) {
260       constraintList.addAll(getToscaParameterConstraint(constraint));
261     }
262
263     return constraintList;
264   }
265
266   private static List<Constraint> getToscaParameterConstraint(Map<String, Object> constraint) {
267     List<Constraint> convertedConstraintList = new ArrayList<>();
268     Constraint convertedConstraint;
269
270     if (constraint.containsKey("range")) {
271       convertedConstraint = new Constraint();
272       convertedConstraintList.add(convertedConstraint);
273       Integer min = (Integer) ((Map) constraint.get("range")).get("min");
274       Integer max = (Integer) ((Map) constraint.get("range")).get("max");
275       convertedConstraint.setIn_range(new Integer[]{min, max});
276
277     } else if (constraint.containsKey("length")) {
278       Integer min = (Integer) ((Map) constraint.get("length")).get("min");
279       Integer max = (Integer) ((Map) constraint.get("length")).get("max");
280       if (max != null) {
281         convertedConstraint = new Constraint();
282         convertedConstraintList.add(convertedConstraint);
283         convertedConstraint.setMax_length(max);
284       }
285       if (min != null) {
286         convertedConstraint = new Constraint();
287         convertedConstraintList.add(convertedConstraint);
288         convertedConstraint.setMin_length(min);
289       }
290     } else if (constraint.containsKey("allowed_values")) {
291       convertedConstraint = new Constraint();
292       convertedConstraintList.add(convertedConstraint);
293       convertedConstraint.setValid_values((List) constraint.get("allowed_values"));
294     } else if (constraint.containsKey("allowed_pattern")) {
295       convertedConstraint = new Constraint();
296       convertedConstraintList.add(convertedConstraint);
297       convertedConstraint.setPattern(constraint.get("allowed_pattern"));
298     }
299
300     return convertedConstraintList;
301   }
302
303   private static EntrySchema getToscaParameterEntrySchema(String type) {
304
305     if (!parameterEntrySchemaTypeMapping.containsKey(type)) {
306       return null;
307     }
308
309     EntrySchema entrySchema = new EntrySchema();
310     entrySchema.setType(parameterEntrySchemaTypeMapping.get(type));
311     return entrySchema;
312   }
313
314   protected static String getToscaParameterType(String heatParameterType) {
315     return parameterTypeMapping.get(heatParameterType);
316   }
317 }