push addional 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  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.translator.services.heattotosca.mapping;
22
23 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
24 import org.openecomp.sdc.heat.datatypes.model.Output;
25 import org.openecomp.sdc.heat.datatypes.model.Parameter;
26 import org.openecomp.sdc.tosca.datatypes.model.Constraint;
27 import org.openecomp.sdc.tosca.datatypes.model.EntrySchema;
28 import org.openecomp.sdc.tosca.datatypes.model.ParameterDefinition;
29 import org.openecomp.sdc.tosca.datatypes.model.heatextend.ParameterDefinitionExt;
30 import org.openecomp.sdc.translator.services.heattotosca.TranslationContext;
31
32
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38
39 public class TranslatorHeatToToscaParameterConverter {
40
41
42   private static Map<String, String> parameterTypeMapping;
43   private static Map<String, String> parameterEntrySchemaTypeMapping;
44
45   static {
46     parameterEntrySchemaTypeMapping = new HashMap<>();
47     parameterEntrySchemaTypeMapping.put("list", "string");
48   }
49
50   static {
51     parameterTypeMapping = new HashMap<>();
52     parameterTypeMapping.put("string", "string");
53     parameterTypeMapping.put("number", "float");
54     parameterTypeMapping.put("comma_delimited_list", "list");
55     parameterTypeMapping.put("json", "json");
56     parameterTypeMapping.put("boolean", "boolean");
57   }
58
59   /**
60    * Parameter converter map.
61    *
62    * @param parameters                the parameters
63    * @param heatOrchestrationTemplate the heat orchestration template
64    * @param heatFileName              the heat file name
65    * @param context                   the context
66    * @return the map
67    */
68   public static Map<String, ParameterDefinition> parameterConverter(
69       Map<String, Parameter> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
70       String heatFileName, TranslationContext context) {
71     Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
72     for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
73       //parameterDefinitionMap.put(entry.getKey()+"_"+ FileUtils
74       // .getFileWithoutExtention(heatFileName),getToscaParameter(entry.getValue(),
75       // heatOrchestrationTemplate, heatFileName, context));
76       parameterDefinitionMap.put(entry.getKey(),
77           getToscaParameter(entry.getValue(), heatOrchestrationTemplate, heatFileName, context));
78     }
79     return parameterDefinitionMap;
80   }
81
82   /**
83    * Parameter output converter map.
84    *
85    * @param parameters                the parameters
86    * @param heatOrchestrationTemplate the heat orchestration template
87    * @param heatFileName              the heat file name
88    * @param context                   the context
89    * @return the map
90    */
91   public static Map<String, ParameterDefinition> parameterOutputConverter(
92       Map<String, Output> parameters, HeatOrchestrationTemplate heatOrchestrationTemplate,
93       String heatFileName, TranslationContext context) {
94     Map<String, ParameterDefinition> parameterDefinitionMap = new HashMap<>();
95     for (Map.Entry<String, Output> entry : parameters.entrySet()) {
96       //parameterDefinitionMap.put(entry.getKey()+"_"+FileUtils
97       // .getFileWithoutExtention(heatFileName),getToscaOutputParameter(entry.getValue(),
98       // heatOrchestrationTemplate, heatFileName, context));
99       parameterDefinitionMap.put(entry.getKey(),
100           getToscaOutputParameter(entry.getValue(), heatOrchestrationTemplate, heatFileName,
101               context));
102     }
103     return parameterDefinitionMap;
104   }
105
106   /**
107    * Gets tosca parameter.
108    *
109    * @param heatParameter             the heat parameter
110    * @param heatOrchestrationTemplate the heat orchestration template
111    * @param heatFileName              the heat file name
112    * @param context                   the context
113    * @return the tosca parameter
114    */
115   public static ParameterDefinitionExt getToscaParameter(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(heatParameter.get_default(), toscaParameter.getType(),
128             heatFileName, heatOrchestrationTemplate, context));
129     toscaParameter.setHidden(heatParameter.isHidden());
130     toscaParameter.setImmutable(heatParameter.isImmutable());
131     toscaParameter.setConstraints(getToscaConstrains(heatParameter.getConstraints()));
132     return toscaParameter;
133   }
134
135   /**
136    * Gets tosca output parameter.
137    *
138    * @param heatOutputParameter       the heat output parameter
139    * @param heatOrchestrationTemplate the heat orchestration template
140    * @param heatFileName              the heat file name
141    * @param context                   the context
142    * @return the tosca output parameter
143    */
144   public static ParameterDefinitionExt getToscaOutputParameter(Output heatOutputParameter,
145                                                                HeatOrchestrationTemplate
146                                                                        heatOrchestrationTemplate,
147                                                                String heatFileName,
148                                                                TranslationContext context) {
149
150     ParameterDefinitionExt toscaParameter = new ParameterDefinitionExt();
151     toscaParameter.setDescription(heatOutputParameter.getDescription());
152     toscaParameter.setValue(
153         getToscaParameterDefaultValue(heatOutputParameter.getValue(), toscaParameter.getType(),
154             heatFileName, heatOrchestrationTemplate, context));
155     return toscaParameter;
156   }
157
158   /**
159    * Gets tosca parameter default value.
160    *
161    * @param defaultObj                  the a default
162    * @param type                      the type
163    * @param heatFileName              the heat file name
164    * @param heatOrchestrationTemplate the heat orchestration template
165    * @param context                   the context
166    * @return the tosca parameter default value
167    */
168   public static Object getToscaParameterDefaultValue(Object defaultObj, String type,
169                                                      String heatFileName,
170                                                      HeatOrchestrationTemplate
171                                                              heatOrchestrationTemplate,
172                                                      TranslationContext context) {
173
174     if (defaultObj == null) {
175       return null;
176     }
177     if ("list".equals(type)) {
178       if (defaultObj instanceof String) {
179         return Arrays.asList(((String) defaultObj).split(","));
180       } else {
181         return defaultObj;
182       }
183     }
184
185     return getToscaParameterValue(defaultObj, heatFileName, heatOrchestrationTemplate,
186         context);
187   }
188
189   private static Object getToscaParameterValue(Object paramValue, String heatFileName,
190                                                HeatOrchestrationTemplate heatOrchestrationTemplate,
191                                                TranslationContext context) {
192     if (paramValue instanceof Map) {
193       Map.Entry<String, Object> functionMapEntry =
194           (Map.Entry<String, Object>) ((Map) paramValue).entrySet().iterator().next();
195       if (TranslatorHeatToToscaFunctionConverter.functionNameSet
196           .contains(functionMapEntry.getKey())) {
197         return TranslatorHeatToToscaFunctionConverter
198             .getToscaFunction(functionMapEntry.getKey(), functionMapEntry.getValue(), heatFileName,
199                 heatOrchestrationTemplate, null, context);
200       }
201     }
202
203     return paramValue;
204   }
205
206   private static List<Constraint> getToscaConstrains(List<Map<String, Object>> constraints) {
207     if (constraints == null) {
208       return null;
209     }
210
211     List<Constraint> constraintList = new ArrayList<>();
212
213     for (Map<String, Object> constraint : constraints) {
214       constraintList.addAll(getToscaParameterConstraint(constraint));
215     }
216
217     return constraintList;
218   }
219
220   private static List<Constraint> getToscaParameterConstraint(Map<String, Object> constraint) {
221     List<Constraint> convertedConstraintList = new ArrayList<>();
222     Constraint convertedConstraint;
223
224     if (constraint.containsKey("range")) {
225       convertedConstraint = new Constraint();
226       convertedConstraintList.add(convertedConstraint);
227       Integer min = (Integer) ((Map) constraint.get("range")).get("min");
228       Integer max = (Integer) ((Map) constraint.get("range")).get("max");
229       convertedConstraint.setIn_range(new Integer[]{min, max});
230
231     } else if (constraint.containsKey("length")) {
232       Integer min = (Integer) ((Map) constraint.get("length")).get("min");
233       Integer max = (Integer) ((Map) constraint.get("length")).get("max");
234       if (max != null) {
235         convertedConstraint = new Constraint();
236         convertedConstraintList.add(convertedConstraint);
237         convertedConstraint.setMax_length(max);
238       }
239       if (min != null) {
240         convertedConstraint = new Constraint();
241         convertedConstraintList.add(convertedConstraint);
242         convertedConstraint.setMin_length(min);
243       }
244     } else if (constraint.containsKey("allowed_values")) {
245       convertedConstraint = new Constraint();
246       convertedConstraintList.add(convertedConstraint);
247       convertedConstraint.setValid_values((List) constraint.get("allowed_values"));
248     } else if (constraint.containsKey("allowed_pattern")) {
249       convertedConstraint = new Constraint();
250       convertedConstraintList.add(convertedConstraint);
251       convertedConstraint.setPattern(constraint.get("allowed_pattern"));
252     }
253
254     return convertedConstraintList;
255   }
256
257   private static EntrySchema getToscaParameterEntrySchema(String type) {
258
259     if (!parameterEntrySchemaTypeMapping.containsKey(type)) {
260       return null;
261     }
262
263     EntrySchema entrySchema = new EntrySchema();
264     entrySchema.setType(parameterEntrySchemaTypeMapping.get(type));
265     return entrySchema;
266   }
267
268   protected static String getToscaParameterType(String heatParameterType) {
269     return parameterTypeMapping.get(heatParameterType);
270   }
271 }