diff options
author | 2018-05-29 17:58:56 +0530 | |
---|---|---|
committer | 2018-05-31 14:01:02 +0000 | |
commit | a8d58165f25ff2cde93ed623f0ebbf78fcd82057 (patch) | |
tree | 4cbefda35217f2c75dbe16e1041ee8b815bdd4ad /openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/impl/functiontranslation/FunctionTranslationGetAttrImpl.java | |
parent | ba61c887dbcb1b81e31e15b18a44f68ff344b2fb (diff) |
Refactor Onboarding Function Translation code
1. Fixed sonar violations and static
analysis issues for function translation
2. Refactored methods for code smells
Note: The below files are modified to keep
successful compilation and will be handled
separately in a different commit
(i) HeatToToscaUtil
(ii) TranslatorHeatToToscaParameterConverter
(iii) TranslatorHeatToToscaPropertyConverter
Change-Id: I75472c2848d1eb812468e7d6457c8da96d6c0e4e
Issue-ID: SDC-1331
Signed-off-by: ojasdubey <ojas.dubey@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/impl/functiontranslation/FunctionTranslationGetAttrImpl.java')
1 files changed, 320 insertions, 422 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/impl/functiontranslation/FunctionTranslationGetAttrImpl.java b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/impl/functiontranslation/FunctionTranslationGetAttrImpl.java index 38dcc54f73..f533353d18 100644 --- a/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/impl/functiontranslation/FunctionTranslationGetAttrImpl.java +++ b/openecomp-be/lib/openecomp-sdc-translator-lib/openecomp-sdc-translator-core/src/main/java/org/openecomp/sdc/translator/services/heattotosca/impl/functiontranslation/FunctionTranslationGetAttrImpl.java @@ -18,15 +18,22 @@ package org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslati import static org.openecomp.sdc.translator.services.heattotosca.ConfigConstants.TRANS_MAPPING_DELIMITER_CHAR; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + import org.apache.commons.lang3.StringUtils; +import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; +import org.onap.sdc.tosca.services.YamlUtil; import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate; import org.openecomp.sdc.heat.datatypes.model.Resource; import org.openecomp.sdc.heat.services.HeatConstants; import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; -import org.onap.sdc.tosca.datatypes.model.NodeTemplate; -import org.onap.sdc.tosca.datatypes.model.ServiceTemplate; -import org.onap.sdc.tosca.datatypes.model.Template; -import org.onap.sdc.tosca.services.YamlUtil; import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext; import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo; import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.EntityConsolidationData; @@ -37,461 +44,352 @@ import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslation; import org.openecomp.sdc.translator.services.heattotosca.FunctionTranslationFactory; import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil; import org.openecomp.sdc.translator.services.heattotosca.NameExtractor; -import org.openecomp.sdc.translator.services.heattotosca.helper.FunctionTranslationHelper; import org.openecomp.sdc.translator.services.heattotosca.impl.resourcetranslation.ResourceTranslationBase; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - public class FunctionTranslationGetAttrImpl implements FunctionTranslation { + private static List<Object> translateGetAttributeFunctionExpression(FunctionTranslator functionTranslator) { - @Override - public Object translateFunction(ServiceTemplate serviceTemplate, - String resourceId, String propertyName, String functionKey, - Object functionValue, String heatFileName, - HeatOrchestrationTemplate heatOrchestrationTemplate, - Template toscaTemplate, TranslationContext context) { - Object returnValue = new HashMap<>(); - List<Object> attributeFunctionExpression = - translateGetAttributeFunctionExpression(serviceTemplate, resourceId, functionValue, - propertyName, heatFileName, heatOrchestrationTemplate, (NodeTemplate) toscaTemplate, - context); - if (FunctionTranslationHelper.isResourceSupported(attributeFunctionExpression.get(0).toString()) - && FunctionTranslationHelper.isAttributeSupported(attributeFunctionExpression.get(0) - .toString())) { - ((Map) returnValue) - .put(ToscaFunctions.GET_ATTRIBUTE.getDisplayName(), attributeFunctionExpression); - } else { - returnValue = attributeFunctionExpression; - } + List<Object> attributeParamList = (List) functionTranslator.getFunctionValue(); + List<Object> toscaAttributeParamList = new ArrayList<>(); - return returnValue; - } - - private static List<Object> translateGetAttributeFunctionExpression( - ServiceTemplate serviceTemplate, - String resourceId, - Object functionValue, - String propertyName, - String heatFileName, - HeatOrchestrationTemplate heatOrchestrationTemplate, - NodeTemplate nodeTemplate, - TranslationContext context) { - - List<Object> attributeParamList = (List) functionValue; - List<Object> toscaAttributeParamList = new ArrayList<>(); - - Optional<String> targetResourceTranslatedId = Optional.empty(); - String targetResourceId = null; - if( attributeParamList.get(0) instanceof String) { - targetResourceId = (String) attributeParamList.get(0); - targetResourceTranslatedId = - handleResourceName(targetResourceId, heatFileName, heatOrchestrationTemplate, - context); - } - if (!targetResourceTranslatedId.isPresent()) { - //unsupported resource - toscaAttributeParamList - .add( - FunctionTranslationHelper.getUnsupportedResourcePrefix() + attributeParamList.get(0)); - return toscaAttributeParamList; - } else { - toscaAttributeParamList.add(targetResourceTranslatedId.get()); + Optional<String> targetResourceTranslatedId = Optional.empty(); + String targetResourceId = null; + if (attributeParamList.get(0) instanceof String) { + targetResourceId = (String) attributeParamList.get(0); + targetResourceTranslatedId = handleResourceName(targetResourceId, functionTranslator.getHeatFileName(), + functionTranslator.getHeatOrchestrationTemplate(), functionTranslator.getContext()); + } + if (!targetResourceTranslatedId.isPresent()) { + //unsupported resource + toscaAttributeParamList.add(functionTranslator.getUnsupportedResourcePrefix() + attributeParamList.get(0)); + return toscaAttributeParamList; + } + toscaAttributeParamList.add(targetResourceTranslatedId.get()); + Optional<List<Object>> toscaAttList = handleAttributeName(attributeParamList, functionTranslator); + if (!toscaAttList.isPresent()) { + //Unsupported attribute + toscaAttributeParamList.clear(); + toscaAttributeParamList.add(functionTranslator.getUnsupportedAttributePrefix() + + attributeParamList.get(0) + "." + attributeParamList.get(1)); + return toscaAttributeParamList; + } + toscaAttributeParamList.addAll(toscaAttList.get()); + handleGetAttrConsolidationData(functionTranslator, targetResourceId, targetResourceTranslatedId.get(), + toscaAttList.get()); + + String resourceType = HeatToToscaUtil.getResourceType((String) attributeParamList.get(0), functionTranslator + .getHeatOrchestrationTemplate(), functionTranslator.getHeatFileName()); + Optional<List<Object>> toscaIndexOrKey = handleAttributeIndexOrKey(functionTranslator, resourceType, + attributeParamList); + toscaIndexOrKey.ifPresent(toscaAttributeParamList::addAll); + return toscaAttributeParamList; } - Optional<List<Object>> toscaAttList = - handleAttributeName(attributeParamList, heatOrchestrationTemplate, propertyName, - heatFileName, serviceTemplate, - context); - if (!toscaAttList.isPresent()) { - //Unsupported attribute - toscaAttributeParamList.clear(); - toscaAttributeParamList - .add(FunctionTranslationHelper.getUnsupportedAttributePrefix() + attributeParamList.get(0) - + "." + attributeParamList.get(1)); - return toscaAttributeParamList; - } else { - toscaAttributeParamList.addAll(toscaAttList.get()); - - handleGetAttrConsolidationData(serviceTemplate, resourceId, propertyName, heatFileName, - heatOrchestrationTemplate, context, targetResourceId, - targetResourceTranslatedId, - toscaAttList.get()); + private static void handleGetAttrConsolidationData(FunctionTranslator functionTranslator, + String targetResourceId, + String targetResourceTranslatedId, + List<Object> toscaAttList) { + + Optional<String> resourceTranslatedId; + String resourceId = functionTranslator.getResourceId(); + String resourceTranslatedIdValue = null; + if (resourceId != null) { + resourceTranslatedId = handleResourceName(resourceId, functionTranslator.getHeatFileName(), + functionTranslator.getHeatOrchestrationTemplate(), functionTranslator.getContext()); + if (resourceTranslatedId.isPresent()) { + resourceTranslatedIdValue = resourceTranslatedId.get(); + handleGetAttrOutConsolidationData(functionTranslator, targetResourceTranslatedId, + resourceTranslatedIdValue, toscaAttList); + } + } + handleGetAttrInConsolidationData(functionTranslator, resourceTranslatedIdValue, targetResourceId, + targetResourceTranslatedId, toscaAttList); } - Optional<List<Object>> toscaIndexOrKey = handleAttributeIndexOrKey(serviceTemplate, - resourceId, propertyName, HeatToToscaUtil - .getResourceType((String) attributeParamList.get(0), heatOrchestrationTemplate, - heatFileName), attributeParamList, context, heatFileName, - heatOrchestrationTemplate); - toscaIndexOrKey.ifPresent(toscaAttributeParamList::addAll); - - return toscaAttributeParamList; - } - - private static void handleGetAttrConsolidationData( - ServiceTemplate serviceTemplate, - String resourceId, String propertyName, - String heatFileName, - HeatOrchestrationTemplate heatOrchestrationTemplate, - TranslationContext context, - String targetResourceId, - Optional<String> targetResourceTranslatedId, - List<Object> toscaAttList) { - - Optional<String> resourceTranslatedId = Optional.empty(); - if (resourceId != null) { - resourceTranslatedId = - handleResourceName(resourceId, heatFileName, heatOrchestrationTemplate, - context); - resourceTranslatedId - .ifPresent(resourceTranslatedIdValue -> handleGetAttrOutConsolidationData(serviceTemplate, - propertyName, - heatOrchestrationTemplate, context, resourceId, targetResourceTranslatedId.get(), - resourceTranslatedIdValue, toscaAttList, heatFileName)); + private static void handleGetAttrOutConsolidationData(FunctionTranslator functionTranslator, + String targetTranslatedResourceId, + String resourceTranslatedId, + List<Object> toscaAttList) { + if (functionTranslator.getServiceTemplate() == null) { + return; + } + Optional<EntityConsolidationData> entityConsolidationData = getEntityConsolidationData(functionTranslator, + functionTranslator.getResourceId(), resourceTranslatedId); + if (entityConsolidationData.isPresent()) { + String attName = (String) toscaAttList.get(0); + handleNodeGetAttrOut(targetTranslatedResourceId, functionTranslator, entityConsolidationData.get(), + attName); + } } - if (targetResourceTranslatedId.isPresent()) { - handleGetAttrInConsolidationData(serviceTemplate, resourceId, resourceTranslatedId, - propertyName, heatOrchestrationTemplate, context, targetResourceId, - targetResourceTranslatedId.get(), toscaAttList, heatFileName); - } - } - - private static void handleGetAttrOutConsolidationData( - ServiceTemplate serviceTemplate, - String propertyName, - HeatOrchestrationTemplate heatOrchestrationTemplate, - TranslationContext context, - String resourceId, - String targetTranslatedResourceId, - String resourceTranslatedId, - List<Object> toscaAttList, - String heatFileName) { - if (serviceTemplate != null) { - Optional<EntityConsolidationData> entityConsolidationData = - getEntityConsolidationData(serviceTemplate, heatOrchestrationTemplate, context, - resourceId, resourceTranslatedId, heatFileName); - if (entityConsolidationData.isPresent()) { - String attName = (String) toscaAttList.get(0); - handleNodeGetAttrOut(targetTranslatedResourceId, propertyName, heatOrchestrationTemplate, - context, resourceId, entityConsolidationData.get(), attName); - } - } - } - - private static void handleGetAttrInConsolidationData( - ServiceTemplate serviceTemplate, - String resourceId, - Optional<String> resourceTranslatedId, - String propertyName, - HeatOrchestrationTemplate heatOrchestrationTemplate, - TranslationContext context, - String targetResourceId, - String targetResourceTranslatedId, - List<Object> toscaAttList, - String heatFileName) { - - if (serviceTemplate != null) { - Optional<EntityConsolidationData> entityConsolidationData = - getEntityConsolidationData(serviceTemplate, heatOrchestrationTemplate, context, - targetResourceId, targetResourceTranslatedId, heatFileName); - if (entityConsolidationData.isPresent()) { + private static void handleGetAttrInConsolidationData(FunctionTranslator functionTranslator, + String resourceTranslatedId, + String targetResourceId, + String targetResourceTranslatedId, + List<Object> toscaAttList) { + if (functionTranslator.getServiceTemplate() == null) { + return; + } + Optional<EntityConsolidationData> entityConsolidationData = getEntityConsolidationData(functionTranslator, + targetResourceId, targetResourceTranslatedId); + if (!entityConsolidationData.isPresent()) { + return; + } String attName = (String) toscaAttList.get(0); - if (resourceTranslatedId.isPresent()) { - handleNodeGetAttrIn(resourceTranslatedId.get(), propertyName, heatOrchestrationTemplate, - context, - resourceId, entityConsolidationData.get(), attName); + if (Objects.nonNull(resourceTranslatedId)) { + handleNodeGetAttrIn(resourceTranslatedId, functionTranslator, entityConsolidationData.get(), attName); } else { - ConsolidationDataUtil - .updateOutputGetAttributeInConsolidationData(entityConsolidationData.get(), - propertyName, attName); + ConsolidationDataUtil.updateOutputGetAttributeInConsolidationData(entityConsolidationData.get(), + functionTranslator.getPropertyName(), attName); } - } - } - } - - private static void handleNodeGetAttrOut(String nodeTemplateId, String propertyName, - HeatOrchestrationTemplate heatOrchestrationTemplate, - TranslationContext context, String resourceId, - EntityConsolidationData entityConsolidationData, - String attName) { - Resource resource = heatOrchestrationTemplate.getResources().get(resourceId); - boolean isNestedResource = HeatToToscaUtil.isNestedResource(resource); - String toscaPropertyName = propertyName; - if (!isNestedResource) { - toscaPropertyName = HeatToToscaUtil.getToscaPropertyName(context, resource - .getType(), propertyName); - } - ConsolidationDataUtil - .updateNodeGetAttributeOut(entityConsolidationData, - nodeTemplateId, toscaPropertyName, attName); - } - - private static void handleNodeGetAttrIn(String nodeTemplateId, String propertyName, - HeatOrchestrationTemplate heatOrchestrationTemplate, - TranslationContext context, String resourceId, - EntityConsolidationData entityConsolidationData, - String attName) { - Resource resource = heatOrchestrationTemplate.getResources().get(resourceId); - boolean isNestedResource = HeatToToscaUtil.isNestedResource(resource); - String heatPropertyName = propertyName; - String toscaPropertyName = propertyName; - //For handling get_attr in inner levels for complex properties - if (propertyName.contains(TRANS_MAPPING_DELIMITER_CHAR)) { - heatPropertyName = propertyName.substring(0, - propertyName.indexOf(TRANS_MAPPING_DELIMITER_CHAR)); - } - if (!isNestedResource) { - toscaPropertyName = HeatToToscaUtil.getToscaPropertyName(context, resource - .getType(), heatPropertyName); } - ConsolidationDataUtil - .updateNodeGetAttributeIn(entityConsolidationData, - nodeTemplateId, toscaPropertyName, attName); - } - - private static Optional<EntityConsolidationData> getEntityConsolidationData( - ServiceTemplate serviceTemplate, - HeatOrchestrationTemplate heatOrchestrationTemplate, - TranslationContext context, - String resourceId, - String resourceTranslatedId, - String heatFileName) { - Resource resource = heatOrchestrationTemplate.getResources().get(resourceId); - if (ConsolidationDataUtil.isComputeResource(heatOrchestrationTemplate, resourceId)) { - String resourceType = heatOrchestrationTemplate.getResources().get(resourceId).getType(); - NameExtractor nodeTypeNameExtractor = - context.getNameExtractorImpl(resourceType); - String computeType = - nodeTypeNameExtractor.extractNodeTypeName( - resource, resourceId, context.getTranslatedIds().get(heatFileName).get(resourceId)); - - return Optional.of( - ConsolidationDataUtil.getComputeTemplateConsolidationData(context, serviceTemplate, - computeType, resourceTranslatedId)); - } else if (ConsolidationDataUtil.isPortResource(heatOrchestrationTemplate, resourceId)) { - return Optional.of(ConsolidationDataUtil - .getPortTemplateConsolidationData(context, serviceTemplate, resourceId, resource.getType(), - resourceTranslatedId)); - } else if (HeatToToscaUtil.isSubInterfaceResource(resource, context)) { - TranslateTo subInterfaceTo = new TranslateTo(heatFileName, serviceTemplate, heatOrchestrationTemplate, - resource, resourceId, resourceTranslatedId, context); - Optional<SubInterfaceTemplateConsolidationData> subInterfaceTemplateConsolidationData = - ConsolidationDataUtil.getSubInterfaceTemplateConsolidationData(subInterfaceTo, resourceTranslatedId); - if (subInterfaceTemplateConsolidationData.isPresent()) { - return Optional.of(subInterfaceTemplateConsolidationData.get()); - } - } else if (HeatToToscaUtil.isNestedResource(resource)) { - return Optional.ofNullable(ConsolidationDataUtil - .getNestedTemplateConsolidationData(context, serviceTemplate, heatFileName, resourceTranslatedId)); + + private static void handleNodeGetAttrOut(String nodeTemplateId, FunctionTranslator functionTranslator, + EntityConsolidationData entityConsolidationData, + String attName) { + Resource resource = functionTranslator.getHeatOrchestrationTemplate().getResources().get(functionTranslator + .getResourceId()); + boolean isNestedResource = HeatToToscaUtil.isNestedResource(resource); + String toscaPropertyName = functionTranslator.getPropertyName(); + if (!isNestedResource) { + toscaPropertyName = HeatToToscaUtil.getToscaPropertyName(functionTranslator.getContext(), resource + .getType(), functionTranslator.getPropertyName()); + } + ConsolidationDataUtil.updateNodeGetAttributeOut(entityConsolidationData, nodeTemplateId, toscaPropertyName, + attName); } - return Optional.empty(); - } - - private static Optional<List<Object>> handleAttributeIndexOrKey( - ServiceTemplate serviceTemplate, - String resourceId, String propertyName, - String resourceType, - List<Object> attributeParamList, - TranslationContext context, - String heatFileName, - HeatOrchestrationTemplate heatOrchestrationTemplate) { - - List<Object> attributeIndexOrKey = new ArrayList<>(); - if (attributeParamList.size() < 3) { - return Optional.empty(); + + private static void handleNodeGetAttrIn(String nodeTemplateId, FunctionTranslator functionTranslator, + EntityConsolidationData entityConsolidationData, + String attName) { + Resource resource = functionTranslator.getHeatOrchestrationTemplate().getResources().get(functionTranslator + .getResourceId()); + boolean isNestedResource = HeatToToscaUtil.isNestedResource(resource); + String propertyName = functionTranslator.getPropertyName(); + String heatPropertyName = propertyName; + String toscaPropertyName = propertyName; + //For handling get_attr in inner levels for complex properties + if (propertyName.contains(TRANS_MAPPING_DELIMITER_CHAR)) { + heatPropertyName = propertyName.substring(0, propertyName.indexOf(TRANS_MAPPING_DELIMITER_CHAR)); + } + if (!isNestedResource) { + toscaPropertyName = HeatToToscaUtil.getToscaPropertyName(functionTranslator.getContext(), resource + .getType(), heatPropertyName); + } + ConsolidationDataUtil.updateNodeGetAttributeIn(entityConsolidationData, nodeTemplateId, toscaPropertyName, + attName); } - Object attributeName = attributeParamList.get(1); - for (int i = 2; i < attributeParamList.size(); i++) { - - if (isInteger(attributeParamList.get(i))) { - attributeIndexOrKey.add(attributeParamList.get(i)); - } else if (attributeParamList.get(i) instanceof Map) { - attributeIndexOrKey.add(getToscaAttributeValue(serviceTemplate, resourceId, - propertyName, attributeParamList.get(i), resourceType, heatFileName, - heatOrchestrationTemplate, null, context)); - - } else { - Object toscaAttributeName = resourceType == null ? null : context - .getElementMapping(resourceType, Constants.ATTR, - getAttributeFullPath(attributeParamList, i)); - if (toscaAttributeName == null) { - toscaAttributeName = attributeParamList.get(i); + private static Optional<EntityConsolidationData> getEntityConsolidationData(FunctionTranslator functionTranslator, + String resourceId, + String resourceTranslatedId) { + HeatOrchestrationTemplate heatOrchestrationTemplate = functionTranslator.getHeatOrchestrationTemplate(); + TranslationContext context = functionTranslator.getContext(); + ServiceTemplate serviceTemplate = functionTranslator.getServiceTemplate(); + String heatFileName = functionTranslator.getHeatFileName(); + + Resource resource = heatOrchestrationTemplate.getResources().get(resourceId); + if (ConsolidationDataUtil.isComputeResource(heatOrchestrationTemplate, resourceId)) { + String resourceType = heatOrchestrationTemplate.getResources().get(resourceId).getType(); + NameExtractor nodeTypeNameExtractor = TranslationContext.getNameExtractorImpl(resourceType); + String computeType = nodeTypeNameExtractor.extractNodeTypeName(resource, resourceId, context + .getTranslatedIds().get(heatFileName).get(resourceId)); + + return Optional.of(ConsolidationDataUtil.getComputeTemplateConsolidationData(context, serviceTemplate, + computeType, resourceTranslatedId)); + } else if (ConsolidationDataUtil.isPortResource(heatOrchestrationTemplate, resourceId)) { + return Optional.of(ConsolidationDataUtil + .getPortTemplateConsolidationData(context, serviceTemplate, resourceId, resource.getType(), + resourceTranslatedId)); + } else if (HeatToToscaUtil.isSubInterfaceResource(resource, context)) { + TranslateTo subInterfaceTo = new TranslateTo(heatFileName, serviceTemplate, heatOrchestrationTemplate, + resource, resourceId, resourceTranslatedId, context); + Optional<SubInterfaceTemplateConsolidationData> subInterfaceTemplateConsolidationData = + ConsolidationDataUtil.getSubInterfaceTemplateConsolidationData(subInterfaceTo, + resourceTranslatedId); + if (subInterfaceTemplateConsolidationData.isPresent()) { + return Optional.of(subInterfaceTemplateConsolidationData.get()); + } + } else if (HeatToToscaUtil.isNestedResource(resource)) { + return Optional.ofNullable(ConsolidationDataUtil + .getNestedTemplateConsolidationData(context, serviceTemplate, heatFileName, resourceTranslatedId)); } - attributeIndexOrKey.add(toscaAttributeName); - } + return Optional.empty(); } - return Optional.of(attributeIndexOrKey); - } + private static Optional<List<Object>> handleAttributeIndexOrKey(FunctionTranslator functionTranslator, + String resourceType, + List<Object> attributeParamList) { - private static String getAttributeFullPath(List<Object> attributeParamList, int attributeIndex) { - if (attributeParamList.size() < 3) { - return null; - } - StringBuilder attributeFullPath = new StringBuilder(); - attributeFullPath.append(attributeParamList.get(1)); - for (int j = 2; j <= attributeIndex; j++) { - if (isInteger(attributeParamList.get(j))) { - continue; - } - attributeFullPath.append(TRANS_MAPPING_DELIMITER_CHAR); - attributeFullPath.append(attributeParamList.get(j)); + List<Object> attributeIndexOrKey = new ArrayList<>(); + if (attributeParamList.size() < 3) { + return Optional.empty(); + } + + for (int i = 2; i < attributeParamList.size(); i++) { + if (isInteger(attributeParamList.get(i))) { + attributeIndexOrKey.add(attributeParamList.get(i)); + } else if (attributeParamList.get(i) instanceof Map) { + attributeIndexOrKey.add(getToscaAttributeValue(functionTranslator, attributeParamList.get(i))); + } else { + Object toscaAttributeName = resourceType == null ? null : functionTranslator.getContext() + .getElementMapping(resourceType, Constants.ATTR, getAttributeFullPath(attributeParamList, i)); + if (toscaAttributeName == null) { + toscaAttributeName = attributeParamList.get(i); + } + attributeIndexOrKey.add(toscaAttributeName); + } + } + + return Optional.of(attributeIndexOrKey); } - return attributeFullPath.toString(); - } - private static boolean isInteger(Object inputNumber) { - if (inputNumber == null) { - return false; + private static String getAttributeFullPath(List<Object> attributeParamList, int attributeIndex) { + if (attributeParamList.size() < 3) { + return null; + } + StringBuilder attributeFullPath = new StringBuilder(); + attributeFullPath.append(attributeParamList.get(1)); + for (int j = 2; j <= attributeIndex; j++) { + if (isInteger(attributeParamList.get(j))) { + continue; + } + attributeFullPath.append(TRANS_MAPPING_DELIMITER_CHAR); + attributeFullPath.append(attributeParamList.get(j)); + } + return attributeFullPath.toString(); } - if (StringUtils.isNumeric(String.valueOf(inputNumber))){ - return true; - } else { - return false; + private static boolean isInteger(Object inputNumber) { + if (inputNumber == null) { + return false; + } + return StringUtils.isNumeric(String.valueOf(inputNumber)); } - } - - private static Optional<String> handleResourceName(String resourceId, String heatFileName, - HeatOrchestrationTemplate - heatOrchestrationTemplate, - TranslationContext context) { - return ResourceTranslationBase - .getResourceTranslatedId(heatFileName, heatOrchestrationTemplate, resourceId, context); - } - - private static Optional<List<Object>> handleAttributeName(List<Object> attributeParamList, - HeatOrchestrationTemplate - heatOrchestrationTemplate, - String propertyName, - String heatFileName, - ServiceTemplate serviceTemplate, - TranslationContext context) { - String resourceId = (String) attributeParamList.get(0); - Resource resource = - HeatToToscaUtil.getResource(heatOrchestrationTemplate, resourceId, heatFileName); - - if (attributeParamList.size() == 1) { - return getResourceTranslatedAttributesList(resource, context); + + private static Optional<String> handleResourceName(String resourceId, String heatFileName, + HeatOrchestrationTemplate heatOrchestrationTemplate, + TranslationContext context) { + return ResourceTranslationBase + .getResourceTranslatedId(heatFileName, heatOrchestrationTemplate, resourceId, context); } - if (!(attributeParamList.get(1) instanceof String)) { - return Optional.empty(); + private static Optional<List<Object>> handleAttributeName(List<Object> attributeParamList, + FunctionTranslator functionTranslator) { + String resourceId = (String) attributeParamList.get(0); + Resource resource = HeatToToscaUtil.getResource(functionTranslator.getHeatOrchestrationTemplate(), + resourceId, functionTranslator.getHeatFileName()); + if (attributeParamList.size() == 1) { + return getResourceTranslatedAttributesList(resource, functionTranslator.getContext()); + } + if (!(attributeParamList.get(1) instanceof String)) { + return Optional.empty(); + } + if (HeatToToscaUtil.isNestedResource(resource)) { + return getNestedResourceTranslatedAttribute((String) attributeParamList.get(1)); + } else { + return getResourceTranslatedAttribute(resource, (String) attributeParamList.get(1), functionTranslator + .getContext()); + } } - if (HeatToToscaUtil.isNestedResource(resource)) { - return getNestedResourceTranslatedAttribute((String) attributeParamList.get(1)); - } else { - return getResourceTranslatedAttribute(resource, (String) attributeParamList.get(1), context); + private static Optional<List<Object>> getNestedResourceTranslatedAttribute(String attributeName) { + List<Object> translatedAttributesList = new ArrayList<>(); + if (attributeName.startsWith(HeatConstants.GET_ATTR_FROM_RESOURCE_GROUP_PREFIX)) { + String[] attributeSplit = attributeName.split("\\."); + if (attributeSplit.length == 2) { + translatedAttributesList.add(attributeSplit[1]); + } else if (attributeSplit.length == 3) { + translatedAttributesList.add(attributeSplit[2]); + translatedAttributesList.add(Integer.valueOf(attributeSplit[1])); + } else { + return Optional.empty(); + } + } else { + translatedAttributesList.add(attributeName); + } + return Optional.of(translatedAttributesList); } - } - - private static Optional<List<Object>> getNestedResourceTranslatedAttribute(String attributeName) { - List<Object> translatedAttributesList = new ArrayList<>(); - - if (attributeName.startsWith(HeatConstants.GET_ATTR_FROM_RESOURCE_GROUP_PREFIX)) { - String[] attributeSplit = attributeName.split("\\."); - if (attributeSplit.length == 2) { - translatedAttributesList.add(attributeSplit[1]); - } else if (attributeSplit.length == 3) { - translatedAttributesList.add(attributeSplit[2]); - translatedAttributesList.add(Integer.valueOf(attributeSplit[1])); - } else { - return Optional.empty(); - } - } else { - translatedAttributesList.add(attributeName); + + private static Optional<List<Object>> getResourceTranslatedAttributesList(Resource resource, + TranslationContext context) { + List<Object> translatedAttributes = new ArrayList<>(); + if (HeatToToscaUtil.isNestedResource(resource)) { + Optional<String> nestedFile = HeatToToscaUtil.getNestedFile(resource); + if (!nestedFile.isPresent()) { + return Optional.empty(); + } + HeatOrchestrationTemplate nestedHeatOrchestrationTemplate = new YamlUtil() + .yamlToObject(context.getFiles().getFileContent(nestedFile.get()), HeatOrchestrationTemplate.class); + translatedAttributes.addAll(nestedHeatOrchestrationTemplate.getOutputs().keySet()); + return Optional.of(translatedAttributes); + + } else { + Map<String, String> resourceMappingAttributes = + context.getElementMapping(resource.getType(), Constants.ATTR); + if (resourceMappingAttributes == null) { + return Optional.empty(); + } + Set<String> mappingAttributes = new HashSet<>(new ArrayList<>(resourceMappingAttributes.values())); + translatedAttributes.addAll(mappingAttributes); + return Optional.of(translatedAttributes); + } } - return Optional.of(translatedAttributesList); - } - - private static Optional<List<Object>> getResourceTranslatedAttributesList(Resource resource, - TranslationContext - context) { - List<Object> translatedAttributes = new ArrayList<>(); - if (HeatToToscaUtil.isNestedResource(resource)) { - Optional<String> nestedFile = HeatToToscaUtil.getNestedFile(resource); - if (!nestedFile.isPresent()) { - return Optional.empty(); - } - HeatOrchestrationTemplate nestedHeatOrchestrationTemplate = new YamlUtil() - .yamlToObject(context.getFiles().getFileContent(nestedFile.get()), - HeatOrchestrationTemplate.class); - translatedAttributes.addAll(nestedHeatOrchestrationTemplate.getOutputs().keySet()); - return Optional.of(translatedAttributes); - - } else { - Map<String, String> resourceMappingAttributes = - context.getElementMapping(resource.getType(), Constants.ATTR); - if (resourceMappingAttributes == null) { - return Optional.empty(); - } - Set<String> mappingAttributes = new HashSet<>(); - mappingAttributes - .addAll(resourceMappingAttributes.values().stream().collect(Collectors.toList())); - translatedAttributes.addAll(mappingAttributes); - return Optional.of(translatedAttributes); + + private static Optional<List<Object>> getResourceTranslatedAttribute(Resource resource, + String attributeName, + TranslationContext context) { + List<Object> translatedAttributesList = new ArrayList<>(); + String translatedAttribute = context.getElementMapping(resource.getType(), Constants.ATTR, attributeName); + if (translatedAttribute != null) { + translatedAttributesList.add(translatedAttribute); + return Optional.of(translatedAttributesList); + } else { //unsupported attribute + return Optional.empty(); + } } - } - - private static Optional<List<Object>> getResourceTranslatedAttribute(Resource resource, - String attributeName, - TranslationContext context) { - List<Object> translatedAttributesList = new ArrayList<>(); - String translatedAttribute = - context.getElementMapping(resource.getType(), Constants.ATTR, attributeName); - if (translatedAttribute != null) { - translatedAttributesList.add(translatedAttribute); - return Optional.of(translatedAttributesList); - } else { //unsupported attribute - return Optional.empty(); + + private static Object getToscaAttributeValue(FunctionTranslator functionTranslator, + Object attributeVal) { + if (attributeVal instanceof Map && !((Map) attributeVal).isEmpty()) { + Map.Entry<String, Object> functionMapEntry = + (Map.Entry<String, Object>) ((Map) attributeVal).entrySet().iterator().next(); + Optional<FunctionTranslation> functionTranslationInstance = + FunctionTranslationFactory.getInstance(functionMapEntry.getKey()); + if (functionTranslationInstance.isPresent()) { + functionTranslator.setFunctionValue(functionMapEntry.getValue()); + return functionTranslationInstance.get().translateFunction(functionTranslator); + } + Map<String, Object> attrValueMap = new HashMap<>(); + for (Map.Entry<String, Object> entry : ((Map<String, Object>) attributeVal).entrySet()) { + attrValueMap.put(entry.getKey(), getToscaAttributeValue(functionTranslator, entry.getValue())); + } + return attrValueMap; + } else if (attributeVal instanceof List && !((List) attributeVal).isEmpty()) { + List<Object> propertyValueArray = new ArrayList<>(); + for (int i = 0; i < ((List) attributeVal).size(); i++) { + propertyValueArray.add(getToscaAttributeValue(functionTranslator, ((List) attributeVal).get(i))); + } + return propertyValueArray; + } + return attributeVal; } - } - - private static Object getToscaAttributeValue(ServiceTemplate serviceTemplate, - String resourceId, String propertyName, - Object attributeVal, String resourceType, - String heatFileName, - HeatOrchestrationTemplate heatOrchestrationTemplate, - Template template, TranslationContext context) { - if (attributeVal instanceof Map && !((Map) attributeVal).isEmpty()) { - Map.Entry<String, Object> functionMapEntry = - (Map.Entry<String, Object>) ((Map) attributeVal).entrySet().iterator().next(); - if (FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).isPresent()) { - return FunctionTranslationFactory.getInstance(functionMapEntry.getKey()).get() - .translateFunction(serviceTemplate, resourceId, propertyName, - functionMapEntry.getKey(), functionMapEntry - .getValue(), heatFileName, heatOrchestrationTemplate, template, context); - } - Map<String, Object> attrValueMap = new HashMap<>(); - for (Map.Entry<String, Object> entry : ((Map<String, Object>) attributeVal).entrySet()) { - attrValueMap.put(entry.getKey(), - getToscaAttributeValue(serviceTemplate, resourceId, propertyName, entry.getValue(), - resourceType, heatFileName, heatOrchestrationTemplate, template, context)); - } - return attrValueMap; - } else if (attributeVal instanceof List && !((List) attributeVal).isEmpty()) { - List propertyValueArray = new ArrayList<>(); - for (int i = 0; i < ((List) attributeVal).size(); i++) { - propertyValueArray.add( - getToscaAttributeValue(serviceTemplate, resourceId, propertyName, - ((List) attributeVal).get(i), resourceType, heatFileName, - heatOrchestrationTemplate, template, context)); - } - return propertyValueArray; + + @Override + public Object translateFunction(FunctionTranslator functionTranslator) { + Object returnValue; + List<Object> attributeFunctionExpression = translateGetAttributeFunctionExpression(functionTranslator); + if (functionTranslator.isResourceSupported(attributeFunctionExpression.get(0).toString()) + && functionTranslator.isAttributeSupported(attributeFunctionExpression.get(0).toString())) { + Map<String, Object> getAttrValue = new HashMap<>(); + getAttrValue.put(ToscaFunctions.GET_ATTRIBUTE.getDisplayName(), attributeFunctionExpression); + returnValue = getAttrValue; + } else { + returnValue = attributeFunctionExpression; + } + return returnValue; } - return attributeVal; - } } |