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=========================================================
20 package org.openecomp.sdc.translator.services.heattotosca.mapping;
22 import static org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation.FunctionTranslator.getFunctionTranslateTo;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
29 import org.onap.sdc.tosca.datatypes.model.Template;
30 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
31 import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
32 import org.openecomp.sdc.translator.services.heattotosca.ConfigConstants;
33 import org.openecomp.sdc.translator.services.heattotosca.Constants;
34 import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory;
35 import org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation.FunctionTranslator;
37 public class TranslatorHeatToToscaPropertyConverter {
40 * Gets tosca properties simple conversion.
42 * @param heatProperties the heat properties
43 * @param toscaProperties the tosca properties
44 * @param heatFileName the heat file name
45 * @param heatOrchestrationTemplate the heat orchestration template
46 * @param resourceType the resource type
47 * @param template the template
48 * @param context the context
49 * @return the tosca properties simple conversion
52 //Convert property assuming the property type in heat is same as the property type in tosca
53 public static Map<String, Object> getToscaPropertiesSimpleConversion(ServiceTemplate serviceTemplate, String resourceId,
54 Map<String, Object> heatProperties, Map<String, Object> toscaProperties,
55 String heatFileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
56 String resourceType, Template template, TranslationContext context) {
57 toscaProperties = toscaProperties != null ? toscaProperties : new HashMap<>();
58 for (String heatPropertyName : context.getElementSet(resourceType, Constants.PROP)) {
59 setSimpleProperty(serviceTemplate, resourceId, heatProperties, heatFileName, resourceType, heatOrchestrationTemplate, context,
60 toscaProperties, heatPropertyName, null, template);
62 return toscaProperties;
66 * Sets simple property.
68 * @param heatProperties the heat properties
69 * @param heatFileName the heat file name
70 * @param resourceType the resource type
71 * @param heatOrchestrationTemplate the heat orchestration template
72 * @param context the context
73 * @param toscaProperties the tosca properties
74 * @param heatPropertyName the heat property name
75 * @param toscaPropertyName the tosca property name
76 * @param template the template
78 public static void setSimpleProperty(ServiceTemplate serviceTemplate, String resourceId, Map<String, Object> heatProperties, String heatFileName,
79 String resourceType, HeatOrchestrationTemplate heatOrchestrationTemplate, TranslationContext context,
80 Map<String, Object> toscaProperties, String heatPropertyName, String toscaPropertyName, Template template) {
81 Object propertyValue = null;
82 if (heatProperties != null) {
83 propertyValue = heatProperties.get(heatPropertyName);
85 if (propertyValue == null) {
88 if (toscaPropertyName == null) {
89 toscaPropertyName = resourceType == null ? heatPropertyName : context.getElementMapping(resourceType, Constants.PROP, heatPropertyName);
90 if (toscaPropertyName == null) {
94 toscaProperties.put(toscaPropertyName,
95 getToscaPropertyValue(serviceTemplate, resourceId, heatPropertyName, propertyValue, resourceType, heatFileName, heatOrchestrationTemplate,
100 * Gets tosca property value.
102 * @param propertyName the property name
103 * @param propertyValue the property value
104 * @param resourceType the resource type
105 * @param heatFileName the heat file name
106 * @param heatOrchestrationTemplate the heat orchestration template
107 * @param template the template
108 * @param context the context
109 * @return the tosca property value
111 public static Object getToscaPropertyValue(ServiceTemplate serviceTemplate, String resourceId, String propertyName, Object propertyValue,
112 String resourceType, String heatFileName, HeatOrchestrationTemplate heatOrchestrationTemplate,
113 Template template, TranslationContext context) {
114 if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
115 Map.Entry<String, Object> functionMapEntry = (Map.Entry<String, Object>) ((Map) propertyValue).entrySet().iterator().next();
116 if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) {
117 FunctionTranslator functionTranslator = new FunctionTranslator(
118 getFunctionTranslateTo(serviceTemplate, resourceId, heatFileName, heatOrchestrationTemplate, context), propertyName,
119 functionMapEntry.getValue(), template);
120 return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get().translateFunction(functionTranslator);
122 Map<String, Object> propertyValueMap = new HashMap<>();
123 for (Map.Entry<String, Object> entry : ((Map<String, Object>) propertyValue).entrySet()) {
124 String toscaPropertyName = resourceType == null ? null : context
125 .getElementMapping(resourceType, Constants.PROP, propertyName + ConfigConstants.TRANS_MAPPING_DELIMITER_CHAR + entry.getKey());
126 toscaPropertyName = toscaPropertyName != null ? toscaPropertyName : entry.getKey();
127 propertyValueMap.put(toscaPropertyName,
128 getToscaPropertyValue(serviceTemplate, resourceId, propertyName + ConfigConstants.TRANS_MAPPING_DELIMITER_CHAR + entry.getKey(),
129 entry.getValue(), resourceType, heatFileName, heatOrchestrationTemplate, template, context));
131 return propertyValueMap;
132 } else if (propertyValue instanceof List && !((List) propertyValue).isEmpty()) {
133 List propertyValueArray = new ArrayList<>();
134 for (int i = 0; i < ((List) propertyValue).size(); i++) {
135 propertyValueArray.add(
136 getToscaPropertyValue(serviceTemplate, resourceId, propertyName, ((List) propertyValue).get(i), resourceType, heatFileName,
137 heatOrchestrationTemplate, template, context));
139 return propertyValueArray;
141 return propertyValue;