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