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