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