diff options
Diffstat (limited to 'catalog-be')
2 files changed, 198 insertions, 88 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/NodeFilterValidator.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/NodeFilterValidator.java index a0716fe4ce..f75abf73a1 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/NodeFilterValidator.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/NodeFilterValidator.java @@ -17,6 +17,7 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.openecomp.sdc.be.components.validation; import com.google.gson.Gson; @@ -30,16 +31,19 @@ import java.util.Set; import java.util.stream.Collectors; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; +import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; +import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType; import org.openecomp.sdc.be.datatypes.enums.PropertySource; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.model.CapabilityDefinition; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.ComponentInstanceInput; import org.openecomp.sdc.be.model.ComponentInstanceProperty; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.InputDefinition; @@ -84,6 +88,28 @@ public class NodeFilterValidator { this.filterConstraintValidator = filterConstraintValidator; } + private static List<? extends ToscaPropertyData> getSelfPropertyFromGetFunction(final Component component, + final ToscaGetFunctionDataDefinition toscaGetFunction) { + switch (toscaGetFunction.getFunctionType()) { + case GET_INPUT: + if (component.getInputs() != null) { + return component.getInputs(); + } + break; + case GET_PROPERTY: + if (component.getProperties() != null) { + return component.getProperties(); + } + break; + case GET_ATTRIBUTE: + if (component.getAttributes() != null) { + return component.getAttributes(); + } + break; + } + return List.of(); + } + public Either<Boolean, ResponseFormat> validateComponentInstanceExist(final Component component, final String componentInstanceId) { if (component == null || StringUtils.isEmpty(componentInstanceId)) { LOGGER.error("Expecting a component and a component instance id, given was '{}' and '{}'", component, componentInstanceId); @@ -148,23 +174,20 @@ public class NodeFilterValidator { final List<ToscaGetFunctionDataDefinition> toscaGetFunctionList = filterConstraint.getAsListToscaGetFunction().orElse(null); if (toscaGetFunctionList == null || toscaGetFunctionList.isEmpty() || !(filterConstraint.getValue() instanceof List)) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.TOSCA_FUNCTION_EXPECTED_ERROR)); - } - else { + } else { toscaGetFunctionDataDefinitionList = toscaGetFunctionList; } - } - else{ + } else { toscaGetFunctionDataDefinitionList.add(toscaGetFunction); } Boolean allGood = true; - for (ToscaGetFunctionDataDefinition _toscaGetFunction: toscaGetFunctionDataDefinitionList) { + for (ToscaGetFunctionDataDefinition _toscaGetFunction : toscaGetFunctionDataDefinitionList) { final Optional<? extends ToscaPropertyData> sourceSelectedProperty = findPropertyFromGetFunction(parentComponent, _toscaGetFunction); if (sourceSelectedProperty.isPresent()) { Optional<? extends PropertyDefinition> targetComponentInstanceProperty = - getInstanceProperties(parentComponent, componentInstanceId, capabilityName, - filterConstraint.getPropertyName()); + getInstanceProperties(parentComponent, componentInstanceId, capabilityName, filterConstraint); source = targetComponentInstanceProperty.isEmpty() ? TARGET : SOURCE; if (targetComponentInstanceProperty.isPresent()) { @@ -176,8 +199,7 @@ public class NodeFilterValidator { break; } - } - else { + } else { allGood = false; final String missingProperty = SOURCE.equals(source) ? filterConstraint.getValue().toString() : filterConstraint.getPropertyName(); @@ -185,8 +207,7 @@ public class NodeFilterValidator { componentsUtils.getResponseFormat(ActionStatus.FILTER_PROPERTY_NOT_FOUND, source, missingProperty); break; } - } - else { + } else { allGood = false; final String missingProperty = SOURCE.equals(source) ? filterConstraint.getValue().toString() : filterConstraint.getPropertyName(); @@ -249,28 +270,6 @@ public class NodeFilterValidator { return instanceProperties; } - private static List<? extends ToscaPropertyData> getSelfPropertyFromGetFunction(final Component component, - final ToscaGetFunctionDataDefinition toscaGetFunction) { - switch (toscaGetFunction.getFunctionType()) { - case GET_INPUT: - if (component.getInputs() != null) { - return component.getInputs(); - } - break; - case GET_PROPERTY: - if (component.getProperties() != null) { - return component.getProperties(); - } - break; - case GET_ATTRIBUTE: - if (component.getAttributes() != null) { - return component.getAttributes(); - } - break; - } - return List.of(); - } - private Optional<PropertyDefinition> findSubProperty(final List<String> propertyPath, final String parentPropertyType, final Map<String, DataTypeDefinition> modelDataTypes) { final DataTypeDefinition dataTypeDefinition = modelDataTypes.get(parentPropertyType); @@ -288,11 +287,24 @@ public class NodeFilterValidator { return findSubProperty(propertyPath.subList(1, propertyPath.size()), propertyDefinition.getType(), modelDataTypes); } - private Optional<ComponentInstanceProperty> getInstanceProperties(final Component parentComponent, final String componentInstanceId, - final String capabilityName, final String propertyName) { + private Optional<? extends PropertyDefinition> getInstanceProperties(final Component parentComponent, final String componentInstanceId, + final String capabilityName, final FilterConstraintDto filterConstraint) { if (StringUtils.isEmpty(capabilityName)) { + OriginTypeEnum componentInstanceType = getComponentInstanceOriginType(parentComponent, componentInstanceId); + if (componentInstanceType == null) { + return Optional.empty(); + } + PropertyDefinition componentInstanceProperty = + getComponentInstanceProperty(componentInstanceType, parentComponent, componentInstanceId, filterConstraint); + if (componentInstanceProperty == null) { + throw new ByActionStatusComponentException(ActionStatus.SELECTED_PROPERTY_NOT_PRESENT, filterConstraint.getPropertyName()); + } + if (componentInstanceProperty instanceof ComponentInstanceInput) { + return parentComponent.getComponentInstancesInputs().get(componentInstanceId).stream() + .filter(property -> filterConstraint.getPropertyName().equals(property.getName())).findFirst(); + } return parentComponent.getComponentInstancesProperties().get(componentInstanceId).stream() - .filter(property -> propertyName.equals(property.getName())).findFirst(); + .filter(property -> filterConstraint.getPropertyName().equals(property.getName())).findFirst(); } else { final Optional<ComponentInstance> componentInstanceOptional = parentComponent.getComponentInstances().stream() .filter(componentInstance -> componentInstance.getUniqueId().equals(componentInstanceId)).findAny(); @@ -300,7 +312,8 @@ public class NodeFilterValidator { for (final List<CapabilityDefinition> listOfCaps : componentInstanceOptional.get().getCapabilities().values()) { final Optional<CapabilityDefinition> capDef = listOfCaps.stream().filter(cap -> cap.getName().equals(capabilityName)).findAny(); if (capDef.isPresent()) { - return capDef.get().getProperties().stream().filter(property -> propertyName.equals(property.getName())).findFirst(); + return capDef.get().getProperties().stream().filter(property -> filterConstraint.getPropertyName().equals(property.getName())) + .findFirst(); } } } @@ -322,8 +335,7 @@ public class NodeFilterValidator { if (optValid.isPresent()) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.TOSCA_FUNCTION_EXPECTED_ERROR)); } - } - else { + } else { return Either.right(componentsUtils.getResponseFormat(ActionStatus.TOSCA_FUNCTION_EXPECTED_ERROR)); } } @@ -414,9 +426,8 @@ public class NodeFilterValidator { } } return null; - } - else { - if (null != ((PropertyDefinition) sourcePropDefinition).getSchemaProperty()){ + } else { + if (null != ((PropertyDefinition) sourcePropDefinition).getSchemaProperty()) { if (((PropertyDefinition) sourcePropDefinition).getSchemaProperty().getType().equals(targetType)) { if (TYPES_WITH_SCHEMA.contains(((PropertyDefinition) sourcePropDefinition).getSchemaProperty().getType())) { final String sourceSchemaType = sourcePropDefinition.getSchemaType(); @@ -431,8 +442,7 @@ public class NodeFilterValidator { } } } - } - else { + } else { if (sourceType.equalsIgnoreCase("integer")) { if (TYPES_WITH_SCHEMA.contains(sourceType)) { final String sourceSchemaType = sourcePropDefinition.getSchemaType(); @@ -451,10 +461,12 @@ public class NodeFilterValidator { private Either<Boolean, ResponseFormat> validateStaticValueAndOperator(final Component parentComponent, final String componentInstanceId, final FilterConstraintDto filterConstraint) { - final ComponentInstanceProperty componentInstanceProperty = parentComponent.getComponentInstancesProperties() - .get(componentInstanceId).stream().filter(property -> filterConstraint.getPropertyName().equals(property.getName())) - .findFirst() - .orElse(null); + OriginTypeEnum componentInstanceType = getComponentInstanceOriginType(parentComponent, componentInstanceId); + if (componentInstanceType == null) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND, componentInstanceId)); + } + PropertyDefinition componentInstanceProperty = + getComponentInstanceProperty(componentInstanceType, parentComponent, componentInstanceId, filterConstraint); if (componentInstanceProperty == null) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.SELECTED_PROPERTY_NOT_PRESENT, filterConstraint.getPropertyName())); } @@ -474,6 +486,34 @@ public class NodeFilterValidator { filterConstraint.getValue(), filterConstraint.getPropertyName()); } + private PropertyDefinition getComponentInstanceProperty(OriginTypeEnum componentInstanceType, Component parentComponent, + String componentInstanceId, FilterConstraintDto filterConstraint) { + if (isInput(componentInstanceType)) { + return parentComponent.getComponentInstancesInputs() + .get(componentInstanceId).stream().filter(input -> filterConstraint.getPropertyName().equals(input.getName())) + .findFirst() + .orElse(null); + } + return parentComponent.getComponentInstancesProperties() + .get(componentInstanceId).stream().filter(property -> filterConstraint.getPropertyName().equals(property.getName())) + .findFirst() + .orElse(null); + } + + private OriginTypeEnum getComponentInstanceOriginType(Component parentComponent, String componentInstanceId) { + Optional<ComponentInstance> componentInstanceOptional = parentComponent.getComponentInstanceById(componentInstanceId); + if (componentInstanceOptional.isPresent()) { + ComponentInstance componentInstance = componentInstanceOptional.get(); + return componentInstance.getOriginType(); + } + return null; + } + + private boolean isInput(OriginTypeEnum instanceType) { + return OriginTypeEnum.VF.equals(instanceType) || OriginTypeEnum.PNF.equals(instanceType) || OriginTypeEnum.CVFC.equals(instanceType) || + OriginTypeEnum.CR.equals(instanceType); + } + private Either<Boolean, ResponseFormat> validateStaticSubstitutionFilter(final Component component, final FilterConstraintDto filterConstraint) { diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/NodeFilterValidatorTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/NodeFilterValidatorTest.java index ef5f7a0a84..cf6bc72256 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/NodeFilterValidatorTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/NodeFilterValidatorTest.java @@ -34,12 +34,14 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; @@ -48,11 +50,13 @@ import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ConstraintType; import org.openecomp.sdc.be.datatypes.enums.FilterValueType; +import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum; import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType; import org.openecomp.sdc.be.datatypes.enums.PropertySource; import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.ComponentInstanceInput; import org.openecomp.sdc.be.model.ComponentInstanceProperty; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; @@ -82,6 +86,33 @@ class NodeFilterValidatorTest { private NodeFilterValidator nodeFilterValidator; private FilterConstraintDto baseFilterConstraintDto; + protected static ToscaGetFunctionDataDefinition createToscaGetFunction(final String sourceName, + final PropertySource propertySource, + final ToscaGetFunctionType toscaGetFunctionType, + final List<String> propertyPathFromSource, + final List<Object> toscaIndexList) { + final var toscaGetFunction = new ToscaGetFunctionDataDefinition(); + toscaGetFunction.setFunctionType(toscaGetFunctionType); + toscaGetFunction.setPropertyPathFromSource(propertyPathFromSource); + toscaGetFunction.setSourceName(sourceName); + toscaGetFunction.setPropertySource(propertySource); + toscaGetFunction.setPropertyName(propertyPathFromSource.get(0)); + toscaGetFunction.setToscaIndexList(toscaIndexList); + return toscaGetFunction; + } + + private static FilterConstraintDto buildFilterConstraintDto(final String propertyName, final FilterValueType valueType, + final ConstraintType constraintType, + final PropertyFilterTargetType targetType, Object value) { + final var filterConstraintDto = new FilterConstraintDto(); + filterConstraintDto.setPropertyName(propertyName); + filterConstraintDto.setValueType(valueType); + filterConstraintDto.setOperator(constraintType); + filterConstraintDto.setTargetType(targetType); + filterConstraintDto.setValue(value); + return filterConstraintDto; + } + @BeforeEach void setup() { componentsUtils = Mockito.mock(ComponentsUtils.class); @@ -106,9 +137,9 @@ class NodeFilterValidatorTest { assertEquals(expectedResponse, either.right().value()); Service service = createService("booleanIncorrect"); - when(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND, service.getName(), INNER_SERVICE)) + when(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND, service.getName(), "uniqueId")) .thenReturn(expectedResponse); - either = nodeFilterValidator.validateComponentInstanceExist(service, INNER_SERVICE); + either = nodeFilterValidator.validateComponentInstanceExist(service, "uniqueId"); assertTrue(either.isRight()); assertEquals(expectedResponse, either.right().value()); @@ -451,11 +482,8 @@ class NodeFilterValidatorTest { PropertyFilterTargetType.PROPERTY, toscaGetFunction ); - final Either<Boolean, ResponseFormat> validationResult = - nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto)); - - assertTrue(validationResult.isRight()); - assertEquals(expectedResponse, validationResult.right().value()); + Assertions.assertThrows(ComponentException.class, + () -> nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto))); } @Test @@ -474,26 +502,8 @@ class NodeFilterValidatorTest { final ResponseFormat expectedResponse = new ResponseFormat(); when(componentsUtils.getResponseFormat(ActionStatus.FILTER_PROPERTY_NOT_FOUND, "Target", PROPERTY_NAME)) .thenReturn(expectedResponse); - Either<Boolean, ResponseFormat> either = - nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto)); - - assertTrue(either.isRight()); - assertEquals(expectedResponse, either.right().value()); - } - - protected static ToscaGetFunctionDataDefinition createToscaGetFunction(final String sourceName, - final PropertySource propertySource, - final ToscaGetFunctionType toscaGetFunctionType, - final List<String> propertyPathFromSource, - final List<Object> toscaIndexList) { - final var toscaGetFunction = new ToscaGetFunctionDataDefinition(); - toscaGetFunction.setFunctionType(toscaGetFunctionType); - toscaGetFunction.setPropertyPathFromSource(propertyPathFromSource); - toscaGetFunction.setSourceName(sourceName); - toscaGetFunction.setPropertySource(propertySource); - toscaGetFunction.setPropertyName(propertyPathFromSource.get(0)); - toscaGetFunction.setToscaIndexList(toscaIndexList); - return toscaGetFunction; + Assertions.assertThrows(ComponentException.class, + () -> nodeFilterValidator.validateFilter(service, COMPONENT1_ID, List.of(filterConstraintDto))); } @Test @@ -528,6 +538,71 @@ class NodeFilterValidatorTest { assertEquals(expectedResponse, either.right().value()); } + @Test + void testValidateNodeFilterForVfStaticValue() { + Service service = createService(ToscaPropertyType.INTEGER.getType()); + addComponentInstanceToService(service, OriginTypeEnum.VF, "vfInstance", ToscaPropertyType.INTEGER.getType()); + baseFilterConstraintDto.setValue(1); + Either<Boolean, ResponseFormat> validationResult = + nodeFilterValidator.validateFilter(service, "vfInstance", List.of(baseFilterConstraintDto)); + + assertTrue(validationResult.isLeft()); + } + + @Test + void testValidateNodeFilterForVfToscaGetProperty() { + Service service = createService(ToscaPropertyType.INTEGER.getType()); + addComponentInstanceToService(service, OriginTypeEnum.VF, "vfInstance", ToscaPropertyType.INTEGER.getType()); + final ToscaGetFunctionDataDefinition toscaGetFunction = + createToscaGetFunction(PARENT_SERVICE_ID, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, List.of(PROPERTY_NAME), null); + final var filterConstraintDto = buildFilterConstraintDto( + PROPERTY_NAME, + FilterValueType.GET_PROPERTY, + ConstraintType.EQUAL, + PropertyFilterTargetType.PROPERTY, + toscaGetFunction + ); + Either<Boolean, ResponseFormat> validationResult = + nodeFilterValidator.validateFilter(service, "vfInstance", List.of(filterConstraintDto)); + + assertTrue(validationResult.isLeft()); + } + + private void addComponentInstanceToService(Service service, OriginTypeEnum originTypeEnum, String instanceName, String type) { + ComponentInstance componentInstance = new ComponentInstance(); + componentInstance.setUniqueId(instanceName); + componentInstance.setName(instanceName); + componentInstance.setOriginType(originTypeEnum); + + List<ComponentInstance> compInstances = new ArrayList<>(); + service.getComponentInstances().forEach(compInstance -> compInstances.add(compInstance)); + compInstances.add(componentInstance); + service.setComponentInstances(compInstances); + + if (isInput(originTypeEnum)) { + ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput(); + componentInstanceInput.setName(PROPERTY_NAME); + componentInstanceInput.setType(type); + if (service.getComponentInstancesInputs() == null) { + service.setComponentInstancesInputs(new HashMap<>()); + } + service.getComponentInstancesInputs().put(instanceName, Collections.singletonList(componentInstanceInput)); + } else { + ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(); + componentInstanceProperty.setName(PROPERTY_NAME); + componentInstanceProperty.setType(type); + if (service.getComponentInstancesProperties() == null) { + service.setComponentInstancesProperties(new HashMap<>()); + } + service.getComponentInstancesProperties().put(instanceName, Collections.singletonList(componentInstanceProperty)); + } + } + + private boolean isInput(OriginTypeEnum instanceType) { + return OriginTypeEnum.VF.equals(instanceType) || OriginTypeEnum.PNF.equals(instanceType) || OriginTypeEnum.CVFC.equals(instanceType) || + OriginTypeEnum.CR.equals(instanceType); + } + private Service createService(String type) { return createService(type, null); } @@ -551,12 +626,19 @@ class NodeFilterValidatorTest { ComponentInstance componentInstance = new ComponentInstance(); componentInstance.setUniqueId(COMPONENT1_ID); componentInstance.setName(COMPONENT1_ID); + componentInstance.setOriginType(OriginTypeEnum.VFC); ComponentInstance componentInstance2 = new ComponentInstance(); componentInstance2.setUniqueId(COMPONENT2_ID); componentInstance2.setName(COMPONENT2_ID); + componentInstance2.setOriginType(OriginTypeEnum.VFC); + + ComponentInstance componentInstance3 = new ComponentInstance(); + componentInstance3.setUniqueId(INNER_SERVICE); + componentInstance3.setName(INNER_SERVICE); + componentInstance3.setOriginType(OriginTypeEnum.ServiceProxy); - service.setComponentInstances(Arrays.asList(componentInstance, componentInstance2)); + service.setComponentInstances(Arrays.asList(componentInstance, componentInstance2, componentInstance3)); ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty(); componentInstanceProperty.setName(PROPERTY_NAME); @@ -578,16 +660,4 @@ class NodeFilterValidatorTest { return service; } - private static FilterConstraintDto buildFilterConstraintDto(final String propertyName, final FilterValueType valueType, - final ConstraintType constraintType, - final PropertyFilterTargetType targetType, Object value) { - final var filterConstraintDto = new FilterConstraintDto(); - filterConstraintDto.setPropertyName(propertyName); - filterConstraintDto.setValueType(valueType); - filterConstraintDto.setOperator(constraintType); - filterConstraintDto.setTargetType(targetType); - filterConstraintDto.setValue(value); - return filterConstraintDto; - } - } |