2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.translator.services.heattotosca.mapping;
23 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
24 import org.onap.sdc.tosca.datatypes.model.Template;
25 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
26 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
27 import org.openecomp.sdc.translator.services.heattotosca.ConfigConstants;
28 import org.openecomp.sdc.translator.services.heattotosca.Constants;
29 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory;
30 import org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation.FunctionTranslator;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
37 import static org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation.FunctionTranslator.getFunctionTranslateTo;
39 public class TranslatorHeatToToscaPropertyConverter {
42 * Gets tosca properties simple conversion.
44 * @param heatProperties the heat properties
45 * @param toscaProperties the tosca properties
46 * @param heatFileName the heat file name
47 * @param heatOrchestrationTemplate the heat orchestration template
48 * @param resourceType the resource type
49 * @param template the template
50 * @param context the context
51 * @return the tosca properties simple conversion
53 //Convert property assuming the property type in heat is same as the property type in tosca
54 public static Map<String, Object> getToscaPropertiesSimpleConversion(ServiceTemplate
56 String resourceId,Map<String, Object> heatProperties, Map<String, Object> toscaProperties,
57 String heatFileName, HeatOrchestrationTemplate heatOrchestrationTemplate, String
58 resourceType, Template template, TranslationContext context) {
60 toscaProperties = toscaProperties != null ? toscaProperties : new HashMap<>();
62 for (String heatPropertyName : context.getElementSet(resourceType, Constants.PROP)) {
64 setSimpleProperty(serviceTemplate, resourceId, heatProperties, heatFileName, resourceType,
65 heatOrchestrationTemplate,
66 context, toscaProperties, heatPropertyName, null, template);
68 return toscaProperties;
72 * Sets simple property.
74 * @param heatProperties the heat properties
75 * @param heatFileName the heat file name
76 * @param resourceType the resource type
77 * @param heatOrchestrationTemplate the heat orchestration template
78 * @param context the context
79 * @param toscaProperties the tosca properties
80 * @param heatPropertyName the heat property name
81 * @param toscaPropertyName the tosca property name
82 * @param template the template
84 public static void setSimpleProperty(ServiceTemplate serviceTemplate,String resourceId,
85 Map<String, Object> heatProperties,
88 HeatOrchestrationTemplate heatOrchestrationTemplate,
89 TranslationContext context,
90 Map<String, Object> toscaProperties, String heatPropertyName,
91 String toscaPropertyName, Template template) {
92 Object propertyValue = null;
93 if (heatProperties != null) {
94 propertyValue = heatProperties.get(heatPropertyName);
96 if (propertyValue == null) {
100 if (toscaPropertyName == null) {
101 toscaPropertyName = resourceType == null ? heatPropertyName
102 : context.getElementMapping(resourceType, Constants.PROP, heatPropertyName);
103 if (toscaPropertyName == null) {
107 toscaProperties.put(toscaPropertyName,
108 getToscaPropertyValue(serviceTemplate, resourceId,heatPropertyName, propertyValue,
110 heatFileName, heatOrchestrationTemplate, template, context));
115 * Gets tosca property value.
117 * @param propertyName the property name
118 * @param propertyValue the property value
119 * @param resourceType the resource type
120 * @param heatFileName the heat file name
121 * @param heatOrchestrationTemplate the heat orchestration template
122 * @param template the template
123 * @param context the context
124 * @return the tosca property value
126 public static Object getToscaPropertyValue(ServiceTemplate serviceTemplate, String resourceId,
127 String propertyName, Object propertyValue,
128 String resourceType, String heatFileName,
129 HeatOrchestrationTemplate heatOrchestrationTemplate,
130 Template template, TranslationContext context) {
131 if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
132 Map.Entry<String, Object> functionMapEntry =
133 (Map.Entry<String, Object>) ((Map) propertyValue).entrySet().iterator().next();
134 if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) {
135 FunctionTranslator functionTranslator = new FunctionTranslator(getFunctionTranslateTo(serviceTemplate,
136 resourceId, heatFileName, heatOrchestrationTemplate, context), propertyName,
137 functionMapEntry.getValue(), template);
138 return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get()
139 .translateFunction(functionTranslator);
141 Map<String, Object> propertyValueMap = new HashMap<>();
142 for (Map.Entry<String, Object> entry : ((Map<String, Object>) propertyValue).entrySet()) {
143 String toscaPropertyName = resourceType == null ? null : context
144 .getElementMapping(resourceType, Constants.PROP, propertyName
145 + ConfigConstants.TRANS_MAPPING_DELIMITER_CHAR + entry.getKey());
146 toscaPropertyName = toscaPropertyName != null ? toscaPropertyName : entry.getKey();
147 propertyValueMap.put(toscaPropertyName,
148 getToscaPropertyValue(serviceTemplate, resourceId,
149 propertyName + ConfigConstants.TRANS_MAPPING_DELIMITER_CHAR
150 + entry.getKey(), entry.getValue(), resourceType, heatFileName,
151 heatOrchestrationTemplate, template, context));
153 return propertyValueMap;
154 } else if (propertyValue instanceof List && !((List) propertyValue).isEmpty()) {
155 List propertyValueArray = new ArrayList<>();
156 for (int i = 0; i < ((List) propertyValue).size(); i++) {
157 propertyValueArray.add(
158 getToscaPropertyValue(serviceTemplate, resourceId, propertyName, ((List)
159 propertyValue).get(i),
160 resourceType, heatFileName, heatOrchestrationTemplate, template, context));
162 return propertyValueArray;
164 return propertyValue;