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