X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=catalog-be%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fsdc%2Fbe%2Fcomponents%2Fimpl%2FToscaFunctionService.java;h=3a89e85a4f3d25fbc12862861d72b37edde49ff8;hb=9699b67917b34c1a10536d353cef09d8904354a6;hp=28843af54ade97c79c7b7b6568ef28176a12ef9a;hpb=92b18f188105d5ba4b2c469cdfaedc7d2953d593;p=sdc.git diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ToscaFunctionService.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ToscaFunctionService.java index 28843af54a..3a89e85a4f 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ToscaFunctionService.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ToscaFunctionService.java @@ -23,6 +23,9 @@ package org.openecomp.sdc.be.components.impl; import java.util.List; import java.util.Map; +import java.util.Optional; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType; @@ -115,43 +118,75 @@ public class ToscaFunctionService { if (toscaGetFunction.getPropertySource() == PropertySource.SELF) { toscaGetFunction.setSourceUniqueId(selfComponent.getUniqueId()); toscaGetFunction.setSourceName(selfComponent.getName()); - if (toscaGetFunction.getType() == ToscaFunctionType.GET_PROPERTY) { - selfComponent.getProperties().stream() - .filter(property -> property.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0))) - .findAny() - .ifPresent(property -> - toscaGetFunction.setPropertyUniqueId(property.getUniqueId()) - ); - } else { - selfComponent.getAttributes().stream() - .filter(attribute -> attribute.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0))) - .findAny() - .ifPresent(attribute -> - toscaGetFunction.setPropertyUniqueId(attribute.getUniqueId()) - ); + if (isGetAttributeAndComponentHasAttributes(toscaGetFunction, selfComponent)) { + setPropertyIdFromAttribute(selfComponent, toscaGetFunction); } - } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE) { - selfComponent.getComponentInstances().stream() - .filter(componentInstance -> toscaGetFunction.getSourceName().equals(componentInstance.getName())) - .findAny() - .ifPresent(componentInstance -> toscaGetFunction.setSourceUniqueId(componentInstance.getUniqueId())); - if (toscaGetFunction.getType() == ToscaFunctionType.GET_PROPERTY) { - final List instanceProperties = instancePropertyMap.get(toscaGetFunction.getSourceUniqueId()); - instanceProperties.stream() - .filter(property -> property.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0))) - .findAny() - .ifPresent(property -> - toscaGetFunction.setPropertyUniqueId(property.getUniqueId()) - ); - } else { - final List instanceAttributes = instanceAttributeMap.get(toscaGetFunction.getSourceUniqueId()); - instanceAttributes.stream() - .filter(attribute -> attribute.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0))) - .findAny() - .ifPresent(attribute -> - toscaGetFunction.setPropertyUniqueId(attribute.getUniqueId()) - ); + if (isGetPropertyOrPropertyIdNotSetAndComponentHasProperties(toscaGetFunction, selfComponent)) { + setPropertyIdFromProperty(selfComponent, toscaGetFunction); + } + } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE && CollectionUtils.isNotEmpty(selfComponent.getComponentInstances())) { + setSourceIdFromInstance(selfComponent, toscaGetFunction); + if (toscaGetFunction.getType() == ToscaFunctionType.GET_ATTRIBUTE) { + setPropertyIdFromInstanceAttribute(instanceAttributeMap, toscaGetFunction); } + if (toscaGetFunction.getType() == ToscaFunctionType.GET_PROPERTY || StringUtils.isEmpty(toscaGetFunction.getPropertyUniqueId())) { + setPropertyIdFromInstanceProperty(instancePropertyMap, toscaGetFunction); + } + } + } + + private boolean isGetAttributeAndComponentHasAttributes(final ToscaGetFunctionDataDefinition toscaGetFunction, final Component component) { + return toscaGetFunction.getType() == ToscaFunctionType.GET_ATTRIBUTE && CollectionUtils.isNotEmpty(component.getAttributes()); + } + + private boolean isGetPropertyOrPropertyIdNotSetAndComponentHasProperties(final ToscaGetFunctionDataDefinition toscaGetFunction, final Component component) { + return (toscaGetFunction.getType() == ToscaFunctionType.GET_PROPERTY || StringUtils.isEmpty(toscaGetFunction.getPropertyUniqueId())) && CollectionUtils.isNotEmpty(component.getProperties()); + } + + private void setPropertyIdFromAttribute(final Component component, final ToscaGetFunctionDataDefinition toscaGetFunction) { + component.getAttributes().stream() + .filter(attribute -> attribute.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0))) + .findAny() + .ifPresent(attribute -> toscaGetFunction.setPropertyUniqueId(attribute.getUniqueId())); + } + + private void setPropertyIdFromProperty(final Component component, final ToscaGetFunctionDataDefinition toscaGetFunction) { + component.getProperties().stream() + .filter(property -> property.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0))) + .findAny() + .ifPresent(property -> + toscaGetFunction.setPropertyUniqueId(property.getUniqueId()) + ); + } + + private void setSourceIdFromInstance(final Component component, final ToscaGetFunctionDataDefinition toscaGetFunction) { + component.getComponentInstances().stream() + .filter(componentInstance -> toscaGetFunction.getSourceName().equals(componentInstance.getName())) + .findAny() + .ifPresent(componentInstance -> toscaGetFunction.setSourceUniqueId(componentInstance.getUniqueId())); + } + + private void setPropertyIdFromInstanceAttribute(final Map> instanceAttributeMap, final ToscaGetFunctionDataDefinition toscaGetFunction) { + final List instanceAttributes = instanceAttributeMap.get(toscaGetFunction.getSourceUniqueId()); + if (CollectionUtils.isNotEmpty(instanceAttributes)) { + instanceAttributes.stream() + .filter(attribute -> attribute.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0))) + .findAny() + .ifPresent(attribute -> + toscaGetFunction.setPropertyUniqueId(attribute.getUniqueId()) + ); + } + } + + private void setPropertyIdFromInstanceProperty(final Map> instancePropertyMap, final ToscaGetFunctionDataDefinition toscaGetFunction) { + final List instanceProperties = instancePropertyMap.get(toscaGetFunction.getSourceUniqueId()); + if (CollectionUtils.isNotEmpty(instanceProperties)) { + instanceProperties.stream() + .filter(property -> property.getName().equals(toscaGetFunction.getPropertyPathFromSource().get(0))) + .findAny() + .ifPresent(property -> + toscaGetFunction.setPropertyUniqueId(property.getUniqueId()) + ); } }