From 3f48762a391733561bb1ed171ea0a15bf0ea50ee Mon Sep 17 00:00:00 2001 From: vasraz Date: Tue, 30 Aug 2022 18:17:21 +0100 Subject: Allow to select properties in the get_attribute function Signed-off-by: Vasyl Razinkov Change-Id: Ib35d5d1e3d83ed8e87ce45c20e9cc1a641c5bde2 Issue-ID: SDC-4149 --- .../validation/ToscaFunctionValidatorImpl.java | 71 +++++++++++++--------- 1 file changed, 41 insertions(+), 30 deletions(-) (limited to 'catalog-be') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/ToscaFunctionValidatorImpl.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/ToscaFunctionValidatorImpl.java index ed37347a45..083a03fb42 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/ToscaFunctionValidatorImpl.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/ToscaFunctionValidatorImpl.java @@ -24,6 +24,8 @@ package org.openecomp.sdc.be.components.impl.validation; import fj.data.Either; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.onap.sdc.tosca.datatypes.model.PropertyType; @@ -63,42 +65,51 @@ public class ToscaFunctionValidatorImpl implements ToscaFunctionValidator { private void validateToscaGetFunction(T property, Component parentComponent) { final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) property.getToscaFunction(); validateGetToscaFunctionAttributes(toscaGetFunction); - validateGetPropertySource(toscaGetFunction.getFunctionType(), toscaGetFunction.getPropertySource()); - if (toscaGetFunction.getFunctionType() == ToscaGetFunctionType.GET_INPUT) { - validateGetFunction(property, parentComponent.getInputs(), parentComponent.getModel()); - return; - } - if (toscaGetFunction.getFunctionType() == ToscaGetFunctionType.GET_PROPERTY) { - if (toscaGetFunction.getPropertySource() == PropertySource.SELF) { - validateGetFunction(property, parentComponent.getProperties(), parentComponent.getModel()); - } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE) { - final ComponentInstance componentInstance = - parentComponent.getComponentInstanceById(toscaGetFunction.getSourceUniqueId()) + final ToscaGetFunctionType functionType = toscaGetFunction.getFunctionType(); + validateGetPropertySource(functionType, toscaGetFunction.getPropertySource()); + final String model = parentComponent.getModel(); + switch (functionType) { + case GET_INPUT: + validateGetFunction(property, parentComponent.getInputs(), model); + break; + case GET_PROPERTY: + if (toscaGetFunction.getPropertySource() == PropertySource.SELF) { + validateGetFunction(property, parentComponent.getProperties(), model); + } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE) { + final ComponentInstance componentInstance = + parentComponent.getComponentInstanceById(toscaGetFunction.getSourceUniqueId()) + .orElseThrow(ToscaGetFunctionExceptionSupplier.instanceNotFound(toscaGetFunction.getSourceName())); + validateGetFunction(property, componentInstance.getProperties(), model); + } + break; + case GET_ATTRIBUTE: + if (toscaGetFunction.getPropertySource() == PropertySource.SELF) { + validateGetFunction(property, combine(parentComponent.getProperties(), parentComponent.getAttributes()), model); + } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE) { + final ComponentInstance componentInstance = parentComponent.getComponentInstanceById(toscaGetFunction.getSourceUniqueId()) .orElseThrow(ToscaGetFunctionExceptionSupplier.instanceNotFound(toscaGetFunction.getSourceName())); - validateGetFunction(property, componentInstance.getProperties(), parentComponent.getModel()); - } - - return; - } - if (toscaGetFunction.getFunctionType() == ToscaGetFunctionType.GET_ATTRIBUTE) { - if (toscaGetFunction.getPropertySource() == PropertySource.SELF) { - validateGetFunction(property, parentComponent.getAttributes(), parentComponent.getModel()); - } else if (toscaGetFunction.getPropertySource() == PropertySource.INSTANCE) { - final ComponentInstance componentInstance = - parentComponent.getComponentInstanceById(toscaGetFunction.getSourceUniqueId()) - .orElseThrow(ToscaGetFunctionExceptionSupplier.instanceNotFound(toscaGetFunction.getSourceName())); - validateGetFunction(property, componentInstance.getAttributes(), parentComponent.getModel()); - } - - return; + validateGetFunction(property, combine(componentInstance.getProperties(), componentInstance.getAttributes()), model); + } + break; + default: + throw ToscaGetFunctionExceptionSupplier.functionNotSupported(functionType).get(); } + } - throw ToscaGetFunctionExceptionSupplier.functionNotSupported(toscaGetFunction.getFunctionType()).get(); + private List combine(final List parentProperties, + final List parentAttributes) { + if (CollectionUtils.isNotEmpty(parentProperties) && CollectionUtils.isNotEmpty(parentAttributes)) { + return Stream.concat(parentProperties.stream(), parentAttributes.stream()).collect(Collectors.toList()); + } + if (CollectionUtils.isEmpty(parentProperties)) { + return parentAttributes; + } + return parentProperties; } private void validateGetFunction(final T property, - final List parentProperties, - final String model) { + final List parentProperties, + final String model) { final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) property.getToscaFunction(); if (CollectionUtils.isEmpty(parentProperties)) { throw ToscaGetFunctionExceptionSupplier -- cgit 1.2.3-korg