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 / TranslatorHeatToToscaPropertyConverter.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.tosca.datatypes.model.Template;
25 import org.openecomp.sdc.translator.services.heattotosca.Constants;
26 import org.openecomp.sdc.translator.services.heattotosca.TranslationContext;
27
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32
33 public class TranslatorHeatToToscaPropertyConverter {
34
35   /**
36    * Gets tosca properties simple conversion.
37    *
38    * @param heatProperties            the heat properties
39    * @param toscaProperties           the tosca properties
40    * @param heatFileName              the heat file name
41    * @param heatOrchestrationTemplate the heat orchestration template
42    * @param resourceType              the resource type
43    * @param template                  the template
44    * @param context                   the context
45    * @return the tosca properties simple conversion
46    */
47   //Convert property assuming the property type in heat is same as the property type in tosca
48   public static Map<String, Object> getToscaPropertiesSimpleConversion(
49       Map<String, Object> heatProperties, Map<String, Object> toscaProperties, String heatFileName,
50       HeatOrchestrationTemplate heatOrchestrationTemplate, String resourceType, Template template,
51       TranslationContext context) {
52
53     toscaProperties = toscaProperties != null ? toscaProperties : new HashMap<>();
54
55     for (String heatPropertyName : context.getElementSet(resourceType, Constants.PROP)) {
56
57       setSimpleProperty(heatProperties, heatFileName, resourceType, heatOrchestrationTemplate,
58           context, toscaProperties, heatPropertyName, null, template);
59     }
60     return toscaProperties;
61   }
62
63   /**
64    * Sets simple property.
65    *
66    * @param heatProperties            the heat properties
67    * @param heatFileName              the heat file name
68    * @param resourceType              the resource type
69    * @param heatOrchestrationTemplate the heat orchestration template
70    * @param context                   the context
71    * @param toscaProperties           the tosca properties
72    * @param heatPropertyName          the heat property name
73    * @param toscaPropertyName         the tosca property name
74    * @param template                  the template
75    */
76   public static void setSimpleProperty(Map<String, Object> heatProperties, String heatFileName,
77                                        String resourceType,
78                                        HeatOrchestrationTemplate heatOrchestrationTemplate,
79                                        TranslationContext context,
80                                        Map<String, Object> toscaProperties, String heatPropertyName,
81                                        String toscaPropertyName, Template template) {
82     Object propertyValue = null;
83     if (heatProperties != null) {
84       propertyValue = heatProperties.get(heatPropertyName);
85     }
86     if (propertyValue == null) {
87       return;
88     }
89
90     if (toscaPropertyName == null) {
91       toscaPropertyName = resourceType == null ? heatPropertyName
92           : context.getElementMapping(resourceType, Constants.PROP, heatPropertyName);
93     }
94     toscaProperties.put(toscaPropertyName,
95         getToscaPropertyValue(heatPropertyName, propertyValue, resourceType, heatFileName,
96             heatOrchestrationTemplate, template, context));
97   }
98
99
100   /**
101    * Gets tosca property value.
102    *
103    * @param propertyName              the property name
104    * @param propertyValue             the property value
105    * @param resourceType              the resource type
106    * @param heatFileName              the heat file name
107    * @param heatOrchestrationTemplate the heat orchestration template
108    * @param template                  the template
109    * @param context                   the context
110    * @return the tosca property value
111    */
112   public static Object getToscaPropertyValue(String propertyName, Object propertyValue,
113                                              String resourceType, String heatFileName,
114                                              HeatOrchestrationTemplate heatOrchestrationTemplate,
115                                              Template template, TranslationContext context) {
116     if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
117       Map.Entry<String, Object> functionMapEntry =
118           (Map.Entry<String, Object>) ((Map) propertyValue).entrySet().iterator().next();
119       if (TranslatorHeatToToscaFunctionConverter.functionNameSet
120           .contains(functionMapEntry.getKey())) {
121         return TranslatorHeatToToscaFunctionConverter
122             .getToscaFunction(functionMapEntry.getKey(), functionMapEntry.getValue(), heatFileName,
123                 heatOrchestrationTemplate, template, context);
124       }
125       Map<String, Object> propertyValueMap = new HashMap<>();
126       for (Map.Entry<String, Object> entry : ((Map<String, Object>) propertyValue).entrySet()) {
127         String toscaPropertyName = resourceType == null ? null : context
128             .getElementMapping(resourceType, Constants.PROP, propertyName + "." + entry.getKey());
129         toscaPropertyName = toscaPropertyName != null ? toscaPropertyName : entry.getKey();
130         propertyValueMap.put(toscaPropertyName,
131             getToscaPropertyValue(propertyName, entry.getValue(), resourceType, heatFileName,
132                 heatOrchestrationTemplate, template, context));
133       }
134       return propertyValueMap;
135     } else if (propertyValue instanceof List && !((List) propertyValue).isEmpty()) {
136       List propertyValueArray = new ArrayList<>();
137       for (int i = 0; i < ((List) propertyValue).size(); i++) {
138         propertyValueArray.add(
139             getToscaPropertyValue(propertyName, ((List) propertyValue).get(i), resourceType,
140                 heatFileName, heatOrchestrationTemplate, template, context));
141       }
142       return propertyValueArray;
143     }
144     return propertyValue;
145   }
146 }