9ed95bf084632df07680fd01a8ea7e5c4c7559e1
[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.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;
37
38 import java.util.*;
39
40
41 public class TranslatorHeatToToscaParameterConverter {
42
43
44   private static Map<String, String> parameterTypeMapping;
45   private static Map<String, String> parameterEntrySchemaTypeMapping;
46
47   static {
48     parameterEntrySchemaTypeMapping = new HashMap<>();
49     parameterEntrySchemaTypeMapping.put("list", "string");
50   }
51
52   static {
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");
59   }
60
61   /**
62    * Parameter converter map.
63    *
64    * @param parameters                the parameters
65    * @param heatOrchestrationTemplate the heat orchestration template
66    * @param heatFileName              the heat file name
67    * @param context                   the context
68    * @return the map
69    */
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));
80     }
81     return parameterDefinitionMap;
82   }
83
84   /**
85    * Parameter output converter map.
86    *
87    * @param parameters                the parameters
88    * @param heatOrchestrationTemplate the heat orchestration template
89    * @param heatFileName              the heat file name
90    * @param context                   the context
91    * @return the map
92    */
93   public static Map<String, ParameterDefinition> parameterOutputConverter(ServiceTemplate
94                                                                               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,
102               heatFileName,
103               context));
104     }
105     return parameterDefinitionMap;
106   }
107
108   /**
109    * Gets tosca parameter.
110    *
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
116    */
117   private static ParameterDefinitionExt getToscaParameter(ServiceTemplate serviceTemplate,
118                                                          String heatParameterName,
119                                                          Parameter heatParameter,
120                                                          HeatOrchestrationTemplate
121                                                              heatOrchestrationTemplate,
122                                                          String heatFileName,
123                                                          String parentHeatFileName,
124                                                          TranslationContext context) {
125
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()));
139
140
141     return toscaParameter;
142   }
143
144   private static Optional<Map<String, AnnotationDefinition> > getToscaAnnotations (TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName, String parentHeatFileName, String heatParameterName){
145
146     if(parentHeatFileName != null){
147       heatFileName = parentHeatFileName;
148     }
149
150     if(!isAnnotationRequired(context, serviceTemplate, heatFileName)){
151       return Optional.empty();
152     }
153
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);
165
166   }
167
168   private static boolean isAnnotationRequired(TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName){
169     return HeatToToscaUtil.shouldAnnotationsToBeAdded() && !isNestedServiceTemplate(context, serviceTemplate, heatFileName);
170   }
171
172   private static boolean isNestedServiceTemplate(TranslationContext context, ServiceTemplate serviceTemplate, String heatFileName) {
173     String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate.getMetadata());
174     return HeatToToscaUtil.isHeatFileNested(context, heatFileName);
175   }
176
177
178   /**
179    * Gets tosca output parameter.
180    *
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
186    */
187   private static ParameterDefinitionExt getToscaOutputParameter(ServiceTemplate serviceTemplate,
188                                                                String parameterName,
189                                                                Output heatOutputParameter,
190                                                                HeatOrchestrationTemplate
191                                                                    heatOrchestrationTemplate,
192                                                                String heatFileName,
193                                                                TranslationContext context) {
194
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;
202   }
203
204   /**
205    * Gets tosca parameter default value.
206    *
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
213    */
214   public static Object getToscaParameterDefaultValue(ServiceTemplate serviceTemplate,
215                                                      String parameterName,
216                                                      Object obj, String type,
217                                                      String heatFileName,
218                                                      HeatOrchestrationTemplate
219                                                          heatOrchestrationTemplate,
220                                                      TranslationContext context) {
221
222     if (obj == null) {
223       return null;
224     }
225     Object toscaDefaultValue = obj;
226     if ("list".equals(type)) {
227       if (obj instanceof String) {
228         return Arrays.asList(((String) obj).split(","));
229       } else {
230         return toscaDefaultValue;
231       }
232     }
233
234     return getToscaParameterValue(serviceTemplate,parameterName,toscaDefaultValue, heatFileName,
235         heatOrchestrationTemplate,
236         context);
237   }
238
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<>();
247       }
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);
255       }
256     }
257
258     return paramValue;
259   }
260
261   private static List<Constraint> getToscaConstrains(List<Map<String, Object>> constraints) {
262     if (constraints == null) {
263       return null;
264     }
265
266     List<Constraint> constraintList = new ArrayList<>();
267
268     for (Map<String, Object> constraint : constraints) {
269       constraintList.addAll(getToscaParameterConstraint(constraint));
270     }
271
272     return constraintList;
273   }
274
275   private static List<Constraint> getToscaParameterConstraint(Map<String, Object> constraint) {
276     List<Constraint> convertedConstraintList = new ArrayList<>();
277     Constraint convertedConstraint;
278
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});
285
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");
289       if (max != null) {
290         convertedConstraint = new Constraint();
291         convertedConstraintList.add(convertedConstraint);
292         convertedConstraint.setMax_length(max);
293       }
294       if (min != null) {
295         convertedConstraint = new Constraint();
296         convertedConstraintList.add(convertedConstraint);
297         convertedConstraint.setMin_length(min);
298       }
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"));
307     }
308
309     return convertedConstraintList;
310   }
311
312   private static EntrySchema getToscaParameterEntrySchema(String type) {
313
314     if (!parameterEntrySchemaTypeMapping.containsKey(type)) {
315       return null;
316     }
317
318     EntrySchema entrySchema = new EntrySchema();
319     entrySchema.setType(parameterEntrySchemaTypeMapping.get(type));
320     return entrySchema;
321   }
322
323   protected static String getToscaParameterType(String heatParameterType) {
324     return parameterTypeMapping.get(heatParameterType);
325   }
326 }