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.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
 
  24 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
 
  25 import org.onap.sdc.tosca.datatypes.model.Template;
 
  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;
 
  31 import java.util.ArrayList;
 
  32 import java.util.HashMap;
 
  33 import java.util.List;
 
  36 public class TranslatorHeatToToscaPropertyConverter {
 
  39    * Gets tosca properties simple conversion.
 
  41    * @param heatProperties            the heat properties
 
  42    * @param toscaProperties           the tosca properties
 
  43    * @param heatFileName              the heat file name
 
  44    * @param heatOrchestrationTemplate the heat orchestration template
 
  45    * @param resourceType              the resource type
 
  46    * @param template                  the template
 
  47    * @param context                   the context
 
  48    * @return the tosca properties simple conversion
 
  50   //Convert property assuming the property type in heat is same as the property type in tosca
 
  51   public static Map<String, Object> getToscaPropertiesSimpleConversion(ServiceTemplate
 
  53       String resourceId,Map<String, Object> heatProperties, Map<String, Object> toscaProperties,
 
  54       String heatFileName, HeatOrchestrationTemplate heatOrchestrationTemplate, String
 
  55       resourceType, Template template, TranslationContext context) {
 
  57     toscaProperties = toscaProperties != null ? toscaProperties : new HashMap<>();
 
  59     for (String heatPropertyName : context.getElementSet(resourceType, Constants.PROP)) {
 
  61       setSimpleProperty(serviceTemplate, resourceId, heatProperties, heatFileName, resourceType,
 
  62           heatOrchestrationTemplate,
 
  63           context, toscaProperties, heatPropertyName, null, template);
 
  65     return toscaProperties;
 
  69    * Sets simple property.
 
  71    * @param heatProperties            the heat properties
 
  72    * @param heatFileName              the heat file name
 
  73    * @param resourceType              the resource type
 
  74    * @param heatOrchestrationTemplate the heat orchestration template
 
  75    * @param context                   the context
 
  76    * @param toscaProperties           the tosca properties
 
  77    * @param heatPropertyName          the heat property name
 
  78    * @param toscaPropertyName         the tosca property name
 
  79    * @param template                  the template
 
  81   public static void setSimpleProperty(ServiceTemplate serviceTemplate,String resourceId,
 
  82                                        Map<String, Object> heatProperties,
 
  85                                        HeatOrchestrationTemplate heatOrchestrationTemplate,
 
  86                                        TranslationContext context,
 
  87                                        Map<String, Object> toscaProperties, String heatPropertyName,
 
  88                                        String toscaPropertyName, Template template) {
 
  89     Object propertyValue = null;
 
  90     if (heatProperties != null) {
 
  91       propertyValue = heatProperties.get(heatPropertyName);
 
  93     if (propertyValue == null) {
 
  97     if (toscaPropertyName == null) {
 
  98       toscaPropertyName = resourceType == null ? heatPropertyName
 
  99           : context.getElementMapping(resourceType, Constants.PROP, heatPropertyName);
 
 100       if (toscaPropertyName == null) {
 
 104     toscaProperties.put(toscaPropertyName,
 
 105         getToscaPropertyValue(serviceTemplate, resourceId,heatPropertyName, propertyValue,
 
 107             heatFileName, heatOrchestrationTemplate, template, context));
 
 112    * Gets tosca property value.
 
 114    * @param propertyName              the property name
 
 115    * @param propertyValue             the property value
 
 116    * @param resourceType              the resource type
 
 117    * @param heatFileName              the heat file name
 
 118    * @param heatOrchestrationTemplate the heat orchestration template
 
 119    * @param template                  the template
 
 120    * @param context                   the context
 
 121    * @return the tosca property value
 
 123   public static Object getToscaPropertyValue(ServiceTemplate serviceTemplate, String resourceId,
 
 124                                              String propertyName, Object propertyValue,
 
 125                                              String  resourceType, String heatFileName,
 
 126                                              HeatOrchestrationTemplate heatOrchestrationTemplate,
 
 127                                              Template template, TranslationContext context) {
 
 128     if (propertyValue instanceof Map && !((Map) propertyValue).isEmpty()) {
 
 129       Map.Entry<String, Object> functionMapEntry =
 
 130           (Map.Entry<String, Object>) ((Map) propertyValue).entrySet().iterator().next();
 
 131       if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) {
 
 132         return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get()
 
 133             .translateFunction(serviceTemplate, resourceId, propertyName ,functionMapEntry
 
 134                 .getKey(), functionMapEntry
 
 135                 .getValue(),  heatFileName, heatOrchestrationTemplate, template, context);
 
 137       Map<String, Object> propertyValueMap = new HashMap<>();
 
 138       for (Map.Entry<String, Object> entry : ((Map<String, Object>) propertyValue).entrySet()) {
 
 139         String toscaPropertyName = resourceType == null ? null : context
 
 140             .getElementMapping(resourceType, Constants.PROP, propertyName
 
 141                 + ConfigConstants.TRANS_MAPPING_DELIMITER_CHAR + entry.getKey());
 
 142         toscaPropertyName = toscaPropertyName != null ? toscaPropertyName : entry.getKey();
 
 143         propertyValueMap.put(toscaPropertyName,
 
 144             getToscaPropertyValue(serviceTemplate, resourceId,
 
 145                 propertyName + ConfigConstants.TRANS_MAPPING_DELIMITER_CHAR
 
 146                     + entry.getKey(), entry.getValue(), resourceType, heatFileName,
 
 147                 heatOrchestrationTemplate, template, context));
 
 149       return propertyValueMap;
 
 150     } else if (propertyValue instanceof List && !((List) propertyValue).isEmpty()) {
 
 151       List propertyValueArray = new ArrayList<>();
 
 152       for (int i = 0; i < ((List) propertyValue).size(); i++) {
 
 153         propertyValueArray.add(
 
 154             getToscaPropertyValue(serviceTemplate, resourceId, propertyName, ((List)
 
 155                 propertyValue).get(i),
 
 156                 resourceType, heatFileName, heatOrchestrationTemplate, template, context));
 
 158       return propertyValueArray;
 
 160     return propertyValue;