diff options
author | KrupaNagabhushan <krupa.nagabhushan@est.tech> | 2020-09-17 15:11:30 +0100 |
---|---|---|
committer | S�bastien Determe <sebastien.determe@intl.att.com> | 2020-10-23 14:49:33 +0000 |
commit | a5a46f26d043b70cd9a44fed05b0e8ba184318fb (patch) | |
tree | 97582d3263a48658f8a84367684e1eb40ba4624e /catalog-be/src/main/java/org | |
parent | 9714961d2950d00ae66be01e20c1ebb8552a82e9 (diff) |
Fix for substitution filter properties
Issue-ID: SDC-3325
Change-Id: If26bf895a0a2f914fb9c2b36e53466df9b7999fe
Signed-off-by: KrupaNagabhushan <krupa.nagabhushan@est.tech>
Diffstat (limited to 'catalog-be/src/main/java/org')
6 files changed, 285 insertions, 210 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentSubstitutionFilterBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentSubstitutionFilterBusinessLogic.java index 55c70ed271..74de13c952 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentSubstitutionFilterBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentSubstitutionFilterBusinessLogic.java @@ -36,7 +36,6 @@ import org.openecomp.sdc.be.datatypes.elements.RequirementSubstitutionFilterProp import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.ComponentInstance; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation; @@ -69,7 +68,8 @@ public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic final InterfaceLifecycleOperation interfaceLifecycleTypeOperation, final ArtifactsOperations artifactToscaOperation, final SubstitutionFilterOperation substitutionFilterOperation, - final NodeFilterValidator nodeFilterValidator) { + NodeFilterValidator nodeFilterValidator) + { super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation); this.substitutionFilterOperation = substitutionFilterOperation; @@ -77,21 +77,14 @@ public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic } public Optional<SubstitutionFilterDataDefinition> createSubstitutionFilterIfNotExist(final String componentId, - final String componentInstanceId, final boolean shouldLock, final ComponentTypeEnum componentTypeEnum) - throws BusinessLogicException { + throws BusinessLogicException { final Component component = getComponent(componentId); - final Optional<ComponentInstance> componentInstanceOptional = - getComponentInstance(componentInstanceId, component); - - Optional<SubstitutionFilterDataDefinition> substitutionFilterDataDefinition; - if (componentInstanceOptional.isPresent()) { - substitutionFilterDataDefinition = getSubstitutionFilterDataDefinition(componentInstanceOptional.get()); - if (substitutionFilterDataDefinition.isPresent()) { - return substitutionFilterDataDefinition; - } + Optional<SubstitutionFilterDataDefinition> substitutionFilterDataDefinition = Optional.ofNullable(component.getSubstitutionFilter()); + if (substitutionFilterDataDefinition.isPresent()) { + return substitutionFilterDataDefinition; } boolean wasLocked = false; try { @@ -100,24 +93,22 @@ public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic wasLocked = true; } final Either<SubstitutionFilterDataDefinition, StorageOperationStatus> result = substitutionFilterOperation - .createSubstitutionFilter(componentId, componentInstanceId); + .createSubstitutionFilter(componentId); if (result.isRight()) { janusGraphDao.rollback(); LOGGER.error(BUSINESS_PROCESS_ERROR, - "Failed to Create Substitution filter on component with id {}", componentId); + "Failed to Create Substitution filter on component with id {}", componentId); throw new BusinessLogicException(componentsUtils.getResponseFormatByResource(componentsUtils - .convertFromStorageResponse(result.right().value()), component.getSystemName())); + .convertFromStorageResponse(result.right().value()), component.getSystemName())); } substitutionFilterDataDefinition = Optional.ofNullable(result.left().value()); - if (componentInstanceOptional.isPresent() && substitutionFilterDataDefinition.isPresent()) { - componentInstanceOptional.get().setSubstitutionFilter(substitutionFilterDataDefinition.get()); - } + component.setSubstitutionFilter(substitutionFilterDataDefinition.get()); janusGraphDao.commit(); LOGGER.debug("Substitution filter successfully created in component {} . ", component.getSystemName()); } catch (final Exception e) { janusGraphDao.rollback(); LOGGER.error(BUSINESS_PROCESS_ERROR, - "Exception occurred during add Component Substitution filter property values: {}", e.getMessage(), e); + "Exception occurred during add Component Substitution filter property values: {}", e.getMessage(), e); throw new BusinessLogicException(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)); } finally { @@ -130,8 +121,6 @@ public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic } public Optional<SubstitutionFilterDataDefinition> addSubstitutionFilter(final String componentId, - final String componentInstanceId, - final NodeFilterConstraintAction action, final String propertyName, final String constraint, final boolean shouldLock, @@ -139,9 +128,14 @@ public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic throws BusinessLogicException { final Component component = getComponent(componentId); - SubstitutionFilterDataDefinition substitutionFilterDataDefinition = validateAndReturnSubstitutionFilterDefinition( - componentInstanceId, - action, constraint, component); + + final Either<Boolean, ResponseFormat> response = nodeFilterValidator + .validateComponentFilter(component, Collections.singletonList(constraint), NodeFilterConstraintAction.ADD); + if (response.isRight()) { + throw new BusinessLogicException(componentsUtils + .getResponseFormat(ActionStatus.SUBSTITUTION_FILTER_NOT_FOUND, response.right().value().getFormattedMessage())); + } + boolean wasLocked = false; try { if (shouldLock) { @@ -152,19 +146,19 @@ public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic new RequirementSubstitutionFilterPropertyDataDefinition(); newProperty.setName(propertyName); newProperty.setConstraints(Collections.singletonList(constraint)); - final Either<SubstitutionFilterDataDefinition, StorageOperationStatus> result = substitutionFilterOperation - .addNewProperty(componentId, componentInstanceId, substitutionFilterDataDefinition, newProperty); + final Either<SubstitutionFilterDataDefinition, StorageOperationStatus> resultEither = + substitutionFilterOperation + .addPropertyFilter(componentId, component.getSubstitutionFilter(), newProperty); - if (result.isRight()) { + if (resultEither.isRight()) { janusGraphDao.rollback(); throw new BusinessLogicException(componentsUtils.getResponseFormatByResource(componentsUtils - .convertFromStorageResponse(result.right().value()), component.getSystemName())); - } else { - substitutionFilterDataDefinition = result.left().value(); + .convertFromStorageResponse(resultEither.right().value()), component.getSystemName())); } + janusGraphDao.commit(); LOGGER.debug("Substitution filter successfully created in component {} . ", component.getSystemName()); - + return Optional.ofNullable(resultEither.left().value()); } catch (final Exception e) { janusGraphDao.rollback(); LOGGER.error(BUSINESS_PROCESS_ERROR, @@ -176,11 +170,10 @@ public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic unlockComponent(component.getUniqueId(), componentTypeEnum); } } - return Optional.ofNullable(substitutionFilterDataDefinition); + } public Optional<SubstitutionFilterDataDefinition> updateSubstitutionFilter(final String componentId, - final String componentInstanceId, final List<String> constraints, final boolean shouldLock, final ComponentTypeEnum componentTypeEnum) @@ -189,19 +182,13 @@ public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic final Component component = getComponent(componentId); final Either<Boolean, ResponseFormat> response = nodeFilterValidator - .validateFilter(component, componentInstanceId, constraints, NodeFilterConstraintAction.UPDATE); + .validateComponentFilter(component, constraints, NodeFilterConstraintAction.UPDATE); if (response.isRight()) { throw new BusinessLogicException(componentsUtils - .getResponseFormat(SUBSTITUTION_FILTER_NOT_FOUND, response.right().value().getFormattedMessage())); - } - final Optional<ComponentInstance> componentInstance = getComponentInstance(componentInstanceId, - component); - if (!componentInstance.isPresent()) { - throw new BusinessLogicException(ResponseFormatManager.getInstance() - .getResponseFormat(ActionStatus.GENERAL_ERROR)); + .getResponseFormat(ActionStatus.SUBSTITUTION_FILTER_NOT_FOUND, response.right().value().getFormattedMessage())); } - SubstitutionFilterDataDefinition substitutionFilterDataDefinition = componentInstance.get() - .getSubstitutionFilter(); + + SubstitutionFilterDataDefinition substitutionFilterDataDefinition = component.getSubstitutionFilter(); if (substitutionFilterDataDefinition == null) { throw new BusinessLogicException(componentsUtils.getResponseFormat(SUBSTITUTION_FILTER_NOT_FOUND)); } @@ -212,9 +199,9 @@ public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic wasLocked = true; } final List<RequirementSubstitutionFilterPropertyDataDefinition> properties = constraints.stream() - .map(this::getRequirementSubstitutionFilterPropertyDataDefinition).collect(Collectors.toList()); + .map(this::getRequirementSubstitutionFilterPropertyDataDefinition).collect(Collectors.toList()); final Either<SubstitutionFilterDataDefinition, StorageOperationStatus> result = substitutionFilterOperation - .updateSubstitutionFilter(componentId, componentInstanceId, substitutionFilterDataDefinition, properties); + .updateProperties(componentId, substitutionFilterDataDefinition, properties); if (result.isRight()) { janusGraphDao.rollback(); @@ -241,17 +228,13 @@ public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic } public Optional<SubstitutionFilterDataDefinition> deleteSubstitutionFilter(final String componentId, - final String componentInstanceId, - final NodeFilterConstraintAction action, - final String constraint, final int position, final boolean shouldLock, final ComponentTypeEnum componentTypeEnum) throws BusinessLogicException { final Component component = getComponent(componentId); - SubstitutionFilterDataDefinition substitutionFilterDataDefinition = - validateAndReturnSubstitutionFilterDefinition(componentInstanceId, action, constraint, component); + SubstitutionFilterDataDefinition substitutionFilterDataDefinition = component.getSubstitutionFilter(); boolean wasLocked = false; try { if (shouldLock) { @@ -259,7 +242,7 @@ public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic wasLocked = true; } final Either<SubstitutionFilterDataDefinition, StorageOperationStatus> result = substitutionFilterOperation - .deleteConstraint(componentId, componentInstanceId, substitutionFilterDataDefinition, position); + .deleteConstraint(componentId, substitutionFilterDataDefinition, position); if (result.isRight()) { janusGraphDao.rollback(); throw new BusinessLogicException(componentsUtils.getResponseFormatByResource(componentsUtils @@ -285,17 +268,6 @@ public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic return Optional.ofNullable(substitutionFilterDataDefinition); } - private Optional<SubstitutionFilterDataDefinition> getSubstitutionFilterDataDefinition( - final ComponentInstance componentInstance) { - - final SubstitutionFilterDataDefinition substitutionFilterDataDefinition = - componentInstance.getSubstitutionFilter(); - if (componentInstance.getSubstitutionFilter() != null) { - return Optional.ofNullable(substitutionFilterDataDefinition); - } - return Optional.empty(); - } - private void unlockComponent(final String componentUniqueId, final ComponentTypeEnum componentType) { graphLockOperation.unlockComponent(componentUniqueId, componentType.getNodeType()); @@ -307,47 +279,6 @@ public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic return user; } - private Optional<ComponentInstance> getComponentInstance(final String componentInstanceId, - final Component component) { - return component.getComponentInstanceById(componentInstanceId); - } - - private Optional<SubstitutionFilterDataDefinition> getComponentInstanceSubstitutionFilterDataDefinition( - final String componentInstanceId, final Component component) - throws BusinessLogicException { - - if (nodeFilterValidator.validateComponentInstanceExist(component, componentInstanceId).isRight()) { - throw new BusinessLogicException(componentsUtils - .getResponseFormat(SUBSTITUTION_FILTER_NOT_FOUND)); - } - return getComponentInstance(componentInstanceId, component).map(ComponentInstance::getSubstitutionFilter); - } - - private SubstitutionFilterDataDefinition validateAndReturnSubstitutionFilterDefinition( - final String componentInstanceId, final NodeFilterConstraintAction action, final String constraint, - final Component component) throws BusinessLogicException { - - validateSubstitutionFilter(component, componentInstanceId, action, constraint); - final Optional<SubstitutionFilterDataDefinition> substitutionFilterDataDefinition = - getComponentInstanceSubstitutionFilterDataDefinition(componentInstanceId, component); - if (!substitutionFilterDataDefinition.isPresent()) { - throw new BusinessLogicException(componentsUtils.getResponseFormat(SUBSTITUTION_FILTER_NOT_FOUND)); - } - return substitutionFilterDataDefinition.get(); - } - - private void validateSubstitutionFilter(final Component component, - final String componentInstanceId, - final NodeFilterConstraintAction action, - final String constraint) throws BusinessLogicException { - final Either<Boolean, ResponseFormat> response = nodeFilterValidator - .validateFilter(component, componentInstanceId, Collections.singletonList(constraint), action); - if (response.isRight()) { - throw new BusinessLogicException(componentsUtils - .getResponseFormat(SUBSTITUTION_FILTER_NOT_FOUND, response.right().value().getFormattedMessage())); - } - } - private RequirementSubstitutionFilterPropertyDataDefinition getRequirementSubstitutionFilterPropertyDataDefinition( final String constraint) { @@ -356,4 +287,4 @@ public class ComponentSubstitutionFilterBusinessLogic extends BaseBusinessLogic requirementSubstitutionFilterPropertyDataDefinition.setConstraints(Arrays.asList(constraint)); return requirementSubstitutionFilterPropertyDataDefinition; } -}
\ No newline at end of file +} 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 9716dfae23..c6848030cd 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 @@ -53,11 +53,11 @@ public class NodeFilterValidator { private static final String SOURCE = "Source"; public static final Set<String> comparableTypes = ImmutableSet.of(ToscaPropertyType.STRING.getType(), - ToscaPropertyType.INTEGER.getType(), ToscaPropertyType.FLOAT.getType()); + ToscaPropertyType.INTEGER.getType(), ToscaPropertyType.FLOAT.getType()); public static final Set<String> schemableTypes = - ImmutableSet.of(ToscaPropertyType.MAP.getType(), ToscaPropertyType.LIST.getType()); + ImmutableSet.of(ToscaPropertyType.MAP.getType(), ToscaPropertyType.LIST.getType()); public static final Set<String> comparableConstraintsOperators = - ImmutableSet.of(ConstraintConvertor.GREATER_THAN_OPERATOR, ConstraintConvertor.LESS_THAN_OPERATOR); + ImmutableSet.of(ConstraintConvertor.GREATER_THAN_OPERATOR, ConstraintConvertor.LESS_THAN_OPERATOR); protected final ToscaOperationFacade toscaOperationFacade; protected final ComponentsUtils componentsUtils; @@ -78,8 +78,8 @@ public class NodeFilterValidator { return getErrorResponse(ActionStatus.FILTER_NOT_FOUND); } if (CollectionUtils.isEmpty(component.getComponentInstances()) || - component.getComponentInstances().stream() - .noneMatch(ci -> ci.getUniqueId().equals(componentInstanceId))) { + component.getComponentInstances().stream() + .noneMatch(ci -> ci.getUniqueId().equals(componentInstanceId))) { LOGGER.error("Component Instance list is empty"); return getErrorResponse(ActionStatus.FILTER_NOT_FOUND); } @@ -101,13 +101,13 @@ public class NodeFilterValidator { final UIConstraint constraint = new ConstraintConvertor().convert(uiConstraint); if (ConstraintConvertor.PROPERTY_CONSTRAINT.equals(constraint.getSourceType())) { final Either<Boolean, ResponseFormat> booleanResponseFormatEither = - validatePropertyConstraint(parentComponent, componentInstanceId, constraint); + validatePropertyConstraint(parentComponent, componentInstanceId, constraint); if (booleanResponseFormatEither.isRight()) { return booleanResponseFormatEither; } } else if (ConstraintConvertor.STATIC_CONSTRAINT.equals(constraint.getSourceType())) { final Either<Boolean, ResponseFormat> booleanResponseFormatEither = - validateStaticValueAndOperator(parentComponent, componentInstanceId, constraint); + validateStaticValueAndOperator(parentComponent, componentInstanceId, constraint); if (booleanResponseFormatEither.isRight()) { return booleanResponseFormatEither; } @@ -130,36 +130,36 @@ public class NodeFilterValidator { final List<PropertyDefinition> propertyDefinitions = parentComponent.getProperties(); List<? extends PropertyDefinition> sourcePropertyDefinition = - parentComponent.getName().equals(uiConstraint.getSourceName()) && - propertyDefinitions != null ? propertyDefinitions : Collections.emptyList(); + parentComponent.getName().equals(uiConstraint.getSourceName()) && + propertyDefinitions != null ? propertyDefinitions : Collections.emptyList(); if (sourcePropertyDefinition.isEmpty() && !parentComponent.getName().equals(uiConstraint.getSourceName())) { optionalComponentInstance = parentComponent.getComponentInstances().stream() - .filter(componentInstance -> uiConstraint.getSourceName() - .equals(componentInstance - .getName())) - .findFirst(); + .filter(componentInstance -> uiConstraint.getSourceName() + .equals(componentInstance + .getName())) + .findFirst(); if (optionalComponentInstance.isPresent()) { final List<ComponentInstanceProperty> componentInstanceProperties = - parentComponent.getComponentInstancesProperties() - .get(optionalComponentInstance.get().getUniqueId()); + parentComponent.getComponentInstancesProperties() + .get(optionalComponentInstance.get().getUniqueId()); sourcePropertyDefinition = - componentInstanceProperties == null ? new ArrayList<>() : componentInstanceProperties; + componentInstanceProperties == null ? new ArrayList<>() : componentInstanceProperties; } } if (!CollectionUtils.isEmpty(sourcePropertyDefinition)) { final Optional<? extends PropertyDefinition> sourceSelectedProperty = sourcePropertyDefinition.stream() - .filter(property -> uiConstraint - .getValue() - .equals(property.getName())) - .findFirst(); + .filter(property -> uiConstraint + .getValue() + .equals(property.getName())) + .findFirst(); final Optional<? extends PropertyDefinition> targetComponentInstanceProperty = - parentComponent.getComponentInstancesProperties().get(componentInstanceId).stream() - .filter(property -> uiConstraint.getServicePropertyName().equals(property.getName())) - .findFirst(); + parentComponent.getComponentInstancesProperties().get(componentInstanceId).stream() + .filter(property -> uiConstraint.getServicePropertyName().equals(property.getName())) + .findFirst(); source = !targetComponentInstanceProperty.isPresent() ? "Target" : SOURCE; if (sourceSelectedProperty.isPresent() && targetComponentInstanceProperty.isPresent()) { @@ -168,10 +168,10 @@ public class NodeFilterValidator { } final String missingProperty = - source.equals(SOURCE) ? uiConstraint.getValue().toString() : uiConstraint.getServicePropertyName(); + source.equals(SOURCE) ? uiConstraint.getValue().toString() : uiConstraint.getServicePropertyName(); return Either.right( - componentsUtils.getResponseFormat(ActionStatus.MAPPED_PROPERTY_NOT_FOUND, source, missingProperty)); + componentsUtils.getResponseFormat(ActionStatus.MAPPED_PROPERTY_NOT_FOUND, source, missingProperty)); } private Either<Boolean, ResponseFormat> validatePropertyData(UIConstraint uiConstraint, @@ -188,48 +188,48 @@ public class NodeFilterValidator { final SchemaDefinition targetSchemaDefinition = targetPropDefinition.getSchema(); if (!sourceSchemaDefinition.equals(targetSchemaDefinition)) { return Either - .right(componentsUtils.getResponseFormat(ActionStatus.SOURCE_TARGET_SCHEMA_MISMATCH, - uiConstraint.getServicePropertyName(), uiConstraint.getValue().toString())); + .right(componentsUtils.getResponseFormat(ActionStatus.SOURCE_TARGET_SCHEMA_MISMATCH, + uiConstraint.getServicePropertyName(), uiConstraint.getValue().toString())); } } return Either.left(Boolean.TRUE); } else { return Either.right(componentsUtils.getResponseFormat(ActionStatus.SOURCE_TARGET_PROPERTY_TYPE_MISMATCH, - uiConstraint.getServicePropertyName(), uiConstraint.getValue().toString())); + uiConstraint.getServicePropertyName(), uiConstraint.getValue().toString())); } } else { LOGGER.debug( - "Null value passed to `validatePropertyData` - sourceSelectedProperty: '{}' - targetComponentInstanceProperty: '{}'", - sourceSelectedProperty, targetComponentInstanceProperty); + "Null value passed to `validatePropertyData` - sourceSelectedProperty: '{}' - targetComponentInstanceProperty: '{}'", + sourceSelectedProperty, targetComponentInstanceProperty); return Either.right(componentsUtils - .getResponseFormat(ActionStatus.GENERAL_ERROR, uiConstraint.getServicePropertyName(), - uiConstraint.getValue().toString())); + .getResponseFormat(ActionStatus.GENERAL_ERROR, uiConstraint.getServicePropertyName(), + uiConstraint.getValue().toString())); } } private Either<Boolean, ResponseFormat> validateStaticValueAndOperator( - final Component parentComponent, - final String componentInstanceId, final UIConstraint uiConstraint) { + final Component parentComponent, + final String componentInstanceId, final UIConstraint uiConstraint) { if (!(Objects.nonNull(uiConstraint) && uiConstraint.getValue() instanceof String)) { return Either.left(false); } final Optional<ComponentInstanceProperty> componentInstanceProperty = - parentComponent.getComponentInstancesProperties().get(componentInstanceId).stream() - .filter(property -> uiConstraint.getServicePropertyName().equals(property.getName())) - .findFirst(); + parentComponent.getComponentInstancesProperties().get(componentInstanceId).stream() + .filter(property -> uiConstraint.getServicePropertyName().equals(property.getName())) + .findFirst(); if (!componentInstanceProperty.isPresent()) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.SELECTED_PROPERTY_NOT_PRESENT, - uiConstraint.getServicePropertyName())); + uiConstraint.getServicePropertyName())); } if (comparableConstraintsOperators.contains(uiConstraint.getConstraintOperator()) && !comparableTypes.contains( - componentInstanceProperty.get().getType())) { + componentInstanceProperty.get().getType())) { return Either.right(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_OPERATOR_PROVIDED, - uiConstraint.getServicePropertyName(), uiConstraint.getConstraintOperator())); + uiConstraint.getServicePropertyName(), uiConstraint.getConstraintOperator())); } return isValidValueCheck(componentInstanceProperty.get().getType(), String.valueOf(uiConstraint.getValue()), - uiConstraint.getServicePropertyName()); + uiConstraint.getServicePropertyName()); } private Either<Boolean, ResponseFormat> isValidValueCheck(String type, String value, String propertyName) { @@ -237,16 +237,101 @@ public class NodeFilterValidator { ToscaPropertyType toscaPropertyType = ToscaPropertyType.isValidType(type); if (Objects.isNull(toscaPropertyType)) { return Either.right( - componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_PROPERTY_TYPE, type, propertyName)); + componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_PROPERTY_TYPE, type, propertyName)); } if (toscaPropertyType.getValidator().isValid(value, null)) { return Either.left(Boolean.TRUE); } return Either.right( - componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, type, propertyName, value)); + componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_VALUE_PROVIDED, type, propertyName, value)); } + public Either<Boolean, ResponseFormat> validateComponentFilter(final Component component, + final List<String> uiConstraints, + final NodeFilterConstraintAction action) { + try { + if (NodeFilterConstraintAction.ADD == action || NodeFilterConstraintAction.UPDATE == action) { + for (final String uiConstraint : uiConstraints) { + final UIConstraint constraint = new ConstraintConvertor().convert(uiConstraint); + if (ConstraintConvertor.PROPERTY_CONSTRAINT.equals(constraint.getSourceType())) { + final Either<Boolean, ResponseFormat> booleanResponseFormatEither = + validateComponentPropertyConstraint(component, constraint); + if (booleanResponseFormatEither.isRight()) { + return booleanResponseFormatEither; + } + } else if (ConstraintConvertor.STATIC_CONSTRAINT.equals(constraint.getSourceType())) { + final Either<Boolean, ResponseFormat> booleanResponseFormatEither = + validateComponentStaticValueAndOperator(component, constraint); + if (booleanResponseFormatEither.isRight()) { + return booleanResponseFormatEither; + } + } + } + } + } catch (final Exception e) { + LOGGER.debug("Provided constraint" + uiConstraints, e); + return Either.right(componentsUtils.getResponseFormat(ActionStatus.CONSTRAINT_FORMAT_INCORRECT)); + } -} + return Either.left(true); + } + private Either<Boolean, ResponseFormat> validateComponentPropertyConstraint(final Component component, + final UIConstraint uiConstraint) { + String source = SOURCE; + + final List<PropertyDefinition> propertyDefinitions = component.getProperties(); + List<? extends PropertyDefinition> sourcePropertyDefinition = + component.getName().equals(uiConstraint.getSourceName()) && + propertyDefinitions != null ? propertyDefinitions : Collections.emptyList(); + + if (!CollectionUtils.isEmpty(sourcePropertyDefinition)) { + final Optional<? extends PropertyDefinition> sourceSelectedProperty = sourcePropertyDefinition.stream() + .filter(property -> uiConstraint + .getValue() + .equals(property.getName())) + .findFirst(); + + final Optional<? extends PropertyDefinition> targetComponentProperty = + component.getProperties().stream() + .filter(property -> uiConstraint.getServicePropertyName().equals(property.getName())) + .findFirst(); + source = !targetComponentProperty.isPresent() ? "Target" : SOURCE; + if (sourceSelectedProperty.isPresent() && targetComponentProperty.isPresent()) { + return validatePropertyData(uiConstraint, sourceSelectedProperty, targetComponentProperty); + } + } + + final String missingProperty = + source.equals(SOURCE) ? uiConstraint.getValue().toString() : uiConstraint.getServicePropertyName(); + + return Either.right( + componentsUtils.getResponseFormat(ActionStatus.MAPPED_PROPERTY_NOT_FOUND, source, missingProperty)); + } + + private Either<Boolean, ResponseFormat> validateComponentStaticValueAndOperator( + final Component component, final UIConstraint uiConstraint) { + if (!(Objects.nonNull(uiConstraint) && uiConstraint.getValue() instanceof String)) { + return Either.left(false); + } + final Optional<PropertyDefinition> componentProperty = + component.getProperties().stream() + .filter(property -> uiConstraint.getServicePropertyName().equals(property.getName())) + .findFirst(); + + if (componentProperty.isEmpty()) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.SELECTED_PROPERTY_NOT_PRESENT, + uiConstraint.getServicePropertyName())); + } + if (comparableConstraintsOperators.contains(uiConstraint.getConstraintOperator()) && !comparableTypes.contains( + componentProperty.get().getType())) { + return Either.right(componentsUtils.getResponseFormat(ActionStatus.UNSUPPORTED_OPERATOR_PROVIDED, + uiConstraint.getServicePropertyName(), uiConstraint.getConstraintOperator())); + } + + return isValidValueCheck(componentProperty.get().getType(), String.valueOf(uiConstraint.getValue()), + uiConstraint.getServicePropertyName()); + } + +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/UiComponentDataConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/UiComponentDataConverter.java index b3db99dc47..65c3b0e78e 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/UiComponentDataConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/UiComponentDataConverter.java @@ -50,6 +50,7 @@ import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.Service; import org.openecomp.sdc.be.tosca.utils.NodeFilterConverter; import org.openecomp.sdc.be.tosca.utils.SubstitutionFilterConverter; +import org.openecomp.sdc.be.ui.model.UINodeFilter; import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer; import org.openecomp.sdc.be.ui.model.UiComponentMetadata; import org.openecomp.sdc.be.ui.model.UiResourceDataTransfer; @@ -441,12 +442,14 @@ public class UiComponentDataConverter { } break; case SUBSTITUTION_FILTER: - if (service.getSubstitutionFilterComponents() == null) { + if (service.getSubstitutionFilter() == null) { dataTransfer.setSubstitutionFilterForTopologyTemplate(null); } else { final SubstitutionFilterConverter substitutionFilterConverter = new SubstitutionFilterConverter(); - dataTransfer.setSubstitutionFilterForTopologyTemplate(substitutionFilterConverter - .convertDataMapToUI(service.getSubstitutionFilterComponents())); + final Map<String, UINodeFilter> filterUiMap = new HashMap<>(); + filterUiMap.put(service.getUniqueId(), + substitutionFilterConverter.convertToUi(service.getSubstitutionFilter())); + dataTransfer.setSubstitutionFilterForTopologyTemplate(filterUiMap); } break; default: diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentSubstitutionFilterServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentSubstitutionFilterServlet.java index b451b33fb9..4a44c202f3 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentSubstitutionFilterServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ComponentSubstitutionFilterServlet.java @@ -47,12 +47,12 @@ import org.openecomp.sdc.be.components.impl.ComponentSubstitutionFilterBusinessL import org.openecomp.sdc.be.components.impl.ResourceImportManager; import org.openecomp.sdc.be.components.impl.aaf.AafPermission; import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed; -import org.openecomp.sdc.be.components.impl.utils.NodeFilterConstraintAction; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datamodel.utils.ConstraintConvertor; import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.NodeFilterConstraintType; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.impl.ServletUtils; import org.openecomp.sdc.be.model.User; @@ -64,7 +64,7 @@ import org.openecomp.sdc.common.api.Constants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -@Path("/v1/catalog/{componentType}/{componentId}/resourceInstances/{componentInstanceId}/substitutionFilter") +@Path("/v1/catalog/{componentType}/{componentId}/substitutionFilter/{constraintType}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @Singleton @@ -74,6 +74,7 @@ public class ComponentSubstitutionFilterServlet extends AbstractValidationsServl private static final String START_HANDLE_REQUEST_OF = "Start handle {} request of {}"; private static final String MODIFIER_ID_IS = "Modifier id is {}"; private static final String FAILED_TO_PARSE_COMPONENT = "Failed to parse component"; + private static final String INVALID_CONSTRAINTYPE_ENUM = "Invalid value for NodeFilterConstraintType enum %s"; private static final String FAILED_TO_ADD_SUBSTITUTION_FILTER = "Failed to add substitution filter"; private static final String ADD_SUBSTITUTION_FILTER = "Add Substitution Filter"; @@ -103,7 +104,6 @@ public class ComponentSubstitutionFilterServlet extends AbstractValidationsServl @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - @Path("/") @Operation(description = "Add Component Substitution Filter Constraint", method = "POST", summary = "Add Component Substitution Filter Constraint", responses = { @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))), @@ -114,10 +114,14 @@ public class ComponentSubstitutionFilterServlet extends AbstractValidationsServl public Response addSubstitutionFilter( @Parameter(description = "UIConstraint data", required = true) String constraintData, @Parameter(description = "Component Id") @PathParam("componentId") String componentId, - @Parameter(description = "Component Instance Id") @PathParam("componentInstanceId") String componentInstanceId, - @Parameter(description = "valid value: services", + @Parameter(description = "valid value: resources / services", schema = @Schema(allowableValues = { + ComponentTypeEnum.SERVICE_PARAM_NAME, ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType, + @Parameter(description = "Constraint type. Valid values: properties / capabilities", + schema = @Schema(allowableValues = {NodeFilterConstraintType.PROPERTIES_PARAM_NAME, + NodeFilterConstraintType.CAPABILITIES_PARAM_NAME})) + @PathParam("constraintType") final String constraintType, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) { @@ -129,28 +133,26 @@ public class ComponentSubstitutionFilterServlet extends AbstractValidationsServl try { final Optional<UIConstraint> convertResponse = componentsUtils .parseToConstraint(constraintData, userModifier, componentTypeEnum); - if (!convertResponse.isPresent()) { + if (convertResponse.isEmpty()) { LOGGER.error(FAILED_TO_PARSE_COMPONENT); return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); } - final Optional<SubstitutionFilterDataDefinition> substitutionFilter = - componentSubstitutionFilterBusinessLogic.createSubstitutionFilterIfNotExist(componentId, - componentInstanceId, true, componentTypeEnum); - if (!substitutionFilter.isPresent()) { - LOGGER.error("Failed to create substitution filter."); - BeEcompErrorManager.getInstance().logBeRestApiGeneralError(ADD_SUBSTITUTION_FILTER); - return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); - } - final UIConstraint uiConstraint = convertResponse.get(); final String constraint = new ConstraintConvertor().convert(uiConstraint); + final Optional<NodeFilterConstraintType> nodeFilterConstraintType = + NodeFilterConstraintType.parse(constraintType); + if (nodeFilterConstraintType.isEmpty()) { + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM, + INVALID_CONSTRAINTYPE_ENUM, constraintType)); + } + final Optional<SubstitutionFilterDataDefinition> actionResponse = componentSubstitutionFilterBusinessLogic - .addSubstitutionFilter(componentId.toLowerCase(), componentInstanceId, NodeFilterConstraintAction.ADD, + .addSubstitutionFilter(componentId.toLowerCase(), uiConstraint.getServicePropertyName(), constraint, true, componentTypeEnum); - if (!actionResponse.isPresent()) { + if (actionResponse.isEmpty()) { LOGGER.error(FAILED_TO_ADD_SUBSTITUTION_FILTER); return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); } @@ -168,7 +170,6 @@ public class ComponentSubstitutionFilterServlet extends AbstractValidationsServl @PUT @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) - @Path("/") @Operation(description = "Update Component Substitution Filter Constraint", method = "PUT", summary = "Update Component Substitution Filter Constraint", responses = { @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))), @@ -179,10 +180,14 @@ public class ComponentSubstitutionFilterServlet extends AbstractValidationsServl public Response updateSubstitutionFilter( @Parameter(description = "UIConstraint data", required = true) String constraintData, @Parameter(description = "Component Id") @PathParam("componentId") String componentId, - @Parameter(description = "Component Instance Id") @PathParam("componentInstanceId") String componentInstanceId, - @Parameter(description = "valid value: services", + @Parameter(description = "valid value: resources / services", schema = @Schema(allowableValues = { + ComponentTypeEnum.SERVICE_PARAM_NAME, ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType, + @Parameter(description = "Constraint type. Valid values: properties / capabilities", + schema = @Schema(allowableValues = {NodeFilterConstraintType.PROPERTIES_PARAM_NAME, + NodeFilterConstraintType.CAPABILITIES_PARAM_NAME})) + @PathParam("constraintType") final String constraintType, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) { LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI()); @@ -201,8 +206,14 @@ public class ComponentSubstitutionFilterServlet extends AbstractValidationsServl } final List<String> constraints = new ConstraintConvertor().convertToList(uiConstraints); + final Optional<NodeFilterConstraintType> nodeFilterConstraintType = + NodeFilterConstraintType.parse(constraintType); + if (!nodeFilterConstraintType.isPresent()) { + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM, + INVALID_CONSTRAINTYPE_ENUM, constraintType)); + } final Optional<SubstitutionFilterDataDefinition> actionResponse = componentSubstitutionFilterBusinessLogic - .updateSubstitutionFilter(componentId.toLowerCase(), componentInstanceId, constraints, + .updateSubstitutionFilter(componentId.toLowerCase(), constraints, true, componentTypeEnum); if (!actionResponse.isPresent()) { @@ -233,22 +244,31 @@ public class ComponentSubstitutionFilterServlet extends AbstractValidationsServl @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) public Response deleteSubstitutionFilterConstraint( @Parameter(description = "Component Id") @PathParam("componentId") String componentId, - @Parameter(description = "Component Instance Id") @PathParam("componentInstanceId") String componentInstanceId, @Parameter(description = "Constraint Index") @PathParam("constraintIndex") int index, - @Parameter(description = "valid value: services", + @Parameter(description = "valid value: resources / services", schema = @Schema(allowableValues = { + ComponentTypeEnum.SERVICE_PARAM_NAME, ComponentTypeEnum.SERVICE_PARAM_NAME})) @PathParam("componentType") final String componentType, + @Parameter(description = "Constraint type. Valid values: properties / capabilities", + schema = @Schema(allowableValues = {NodeFilterConstraintType.PROPERTIES_PARAM_NAME, + NodeFilterConstraintType.CAPABILITIES_PARAM_NAME})) + @PathParam("constraintType") final String constraintType, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) { LOGGER.debug(START_HANDLE_REQUEST_OF, request.getMethod(), request.getRequestURI()); LOGGER.debug(MODIFIER_ID_IS, userId); componentSubstitutionFilterBusinessLogic.validateUser(userId); + final Optional<NodeFilterConstraintType> nodeFilterConstraintType = + NodeFilterConstraintType.parse(constraintType); + if (!nodeFilterConstraintType.isPresent()) { + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT_PARAM, + INVALID_CONSTRAINTYPE_ENUM, constraintType)); + } + try { final Optional<SubstitutionFilterDataDefinition> actionResponse = componentSubstitutionFilterBusinessLogic - .deleteSubstitutionFilter(componentId.toLowerCase(), componentInstanceId, - NodeFilterConstraintAction.DELETE, - null, index, true, ComponentTypeEnum.findByParamName(componentType)); + .deleteSubstitutionFilter(componentId.toLowerCase(), index, true, ComponentTypeEnum.findByParamName(componentType)); if (!actionResponse.isPresent()) { LOGGER.debug(FAILED_TO_DELETE_SUBSTITUTION_FILTER); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java index 533ed7a6f0..0907dd1f9b 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java @@ -57,6 +57,7 @@ import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterCapabilityDataDefinition; import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterPropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.RequirementSubstitutionFilterCapabilityDataDefinition; import org.openecomp.sdc.be.datatypes.elements.RequirementSubstitutionFilterPropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ToscaArtifactDataDefinition; @@ -314,7 +315,6 @@ public class ToscaExportHandler { component.getComponentInstancesProperties(); Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = component.getComponentInstancesInterfaces(); - SubstitutionMapping substitutionMapping = new SubstitutionMapping(); if (CollectionUtils.isNotEmpty(componentInstances)) { final Either<Map<String, ToscaNodeTemplate>, ToscaError> nodeTemplates = convertNodeTemplates(component, componentInstances, @@ -325,10 +325,11 @@ public class ToscaExportHandler { } log.debug("node templates converted"); topologyTemplate.setNode_templates(nodeTemplates.left().value()); - - convertSubstitutionMappingFilter(componentInstances, substitutionMapping); } + SubstitutionMapping substitutionMapping = new SubstitutionMapping(); + convertSubstitutionMappingFilter(component, substitutionMapping); + addGroupsToTopologyTemplate(component, topologyTemplate); try { @@ -377,19 +378,13 @@ public class ToscaExportHandler { return Either.left(toscaNode); } - private void convertSubstitutionMappingFilter(final List<ComponentInstance> componentInstances, + private void convertSubstitutionMappingFilter(final Component component, final SubstitutionMapping substitutionMapping) { - componentInstances.stream() - .filter(componentInstance -> hasSubstitutionFilterDataDefinition(componentInstance.getSubstitutionFilter())) - .forEach(componentInstance -> substitutionMapping - .setSubstitution_filter(convertToSubstitutionFilterComponent(componentInstance.getSubstitutionFilter()))); - } - - private boolean hasSubstitutionFilterDataDefinition( - final SubstitutionFilterDataDefinition substitutionFilterDataDefinition) { - - return substitutionFilterDataDefinition != null && substitutionFilterDataDefinition.getProperties() != null - && CollectionUtils.isNotEmpty(substitutionFilterDataDefinition.getProperties().getListToscaDataDefinition()); + if(component.getSubstitutionFilter() != null + && (component.getSubstitutionFilter().getProperties()).getListToscaDataDefinition() != null) { + substitutionMapping + .setSubstitution_filter(convertToSubstitutionFilterComponent(component.getSubstitutionFilter())); + } } private void addGroupsToTopologyTemplate(Component component, ToscaTopolgyTemplate topologyTemplate) { @@ -1521,18 +1516,23 @@ public class ToscaExportHandler { private NodeFilter convertToSubstitutionFilterComponent( final SubstitutionFilterDataDefinition substitutionFilterDataDefinition) { + if (substitutionFilterDataDefinition == null) { + return null; + } NodeFilter nodeFilter = new NodeFilter(); - final List<Map<String, List<Object>>> propertiesCopy = new ArrayList<>(); - copySubstitutionFilterProperties(substitutionFilterDataDefinition.getProperties(), propertiesCopy); + ListDataDefinition<RequirementSubstitutionFilterPropertyDataDefinition> origProperties = + substitutionFilterDataDefinition.getProperties(); + List<Map<String, List<Object>>> propertiesCopy = new ArrayList<>(); + + copySubstitutionFilterProperties(origProperties, propertiesCopy); if (CollectionUtils.isNotEmpty(propertiesCopy)) { nodeFilter.setProperties(propertiesCopy); } nodeFilter.setTosca_id(cloneToscaId(substitutionFilterDataDefinition.getTosca_id())); - nodeFilter = (NodeFilter) cloneObjectFromYml(nodeFilter, NodeFilter.class); - return nodeFilter; + return (NodeFilter) cloneObjectFromYml(nodeFilter, NodeFilter.class); } private Object cloneToscaId(Object toscaId) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/SubstitutionFilterConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/SubstitutionFilterConverter.java index 326ebf3709..d43ba4334c 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/SubstitutionFilterConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/SubstitutionFilterConverter.java @@ -19,32 +19,68 @@ package org.openecomp.sdc.be.tosca.utils; +import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; +import org.apache.commons.collections.CollectionUtils; import org.openecomp.sdc.be.datamodel.utils.ConstraintConvertor; +import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.RequirementSubstitutionFilterCapabilityDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.RequirementSubstitutionFilterPropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterDataDefinition; import org.openecomp.sdc.be.ui.model.UIConstraint; import org.openecomp.sdc.be.ui.model.UINodeFilter; public class SubstitutionFilterConverter { - public Map<String, UINodeFilter> convertDataMapToUI(Map<String, SubstitutionFilterDataDefinition> inMap) { - return inMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, o -> convertToUi(o.getValue()))); - } - public UINodeFilter convertToUi(final SubstitutionFilterDataDefinition inSubstitutionFilter) { final UINodeFilter uiNodeFilter = new UINodeFilter(); final ConstraintConvertor constraintConvertor = new ConstraintConvertor(); - if (inSubstitutionFilter.getProperties() == null || inSubstitutionFilter.getProperties().isEmpty()) { - return uiNodeFilter; + final List<UIConstraint> uiPropertyFilters = extractPropertyFilter(inSubstitutionFilter, constraintConvertor); + if (!uiPropertyFilters.isEmpty()) { + uiNodeFilter.setProperties(uiPropertyFilters); + } + + final List<UIConstraint> uiCapabilityFilters = + extractCapabilitiesFilter(inSubstitutionFilter, constraintConvertor); + if (!uiCapabilityFilters.isEmpty()) { + uiNodeFilter.setCapabilities(uiCapabilityFilters); } - final List<UIConstraint> constraints = inSubstitutionFilter.getProperties().getListToscaDataDefinition() - .stream() - .map(property -> property.getConstraints().iterator().next()) - .map(constraintConvertor::convert) - .collect(Collectors.toList()); - uiNodeFilter.setProperties(constraints); + return uiNodeFilter; } + + private List<UIConstraint> extractPropertyFilter(final SubstitutionFilterDataDefinition substitutionFilter, + final ConstraintConvertor constraintConvertor) { + final ListDataDefinition<RequirementSubstitutionFilterPropertyDataDefinition> substitutionFilterProperties = + substitutionFilter.getProperties(); + if (substitutionFilterProperties != null && !substitutionFilterProperties.isEmpty() + && CollectionUtils.isNotEmpty(substitutionFilterProperties.getListToscaDataDefinition())) { + return substitutionFilterProperties.getListToscaDataDefinition() + .stream() + .map(property -> property.getConstraints().iterator().next()) + .map(constraintConvertor::convert) + .collect(Collectors.toList()); + } + + return Collections.emptyList(); + } + + private List<UIConstraint> extractCapabilitiesFilter(final SubstitutionFilterDataDefinition substitutionFilter, + final ConstraintConvertor constraintConvertor) { + final ListDataDefinition<RequirementSubstitutionFilterCapabilityDataDefinition> substitutionFilterCapabilities = + substitutionFilter.getCapabilities(); + if (substitutionFilterCapabilities != null && !substitutionFilterCapabilities.isEmpty() + && CollectionUtils.isNotEmpty(substitutionFilterCapabilities.getListToscaDataDefinition())) { + return substitutionFilterCapabilities + .getListToscaDataDefinition() + .stream() + .map(capabilities -> capabilities.getProperties().getListToscaDataDefinition().iterator().next()) + .map(property -> property.getConstraints().iterator().next()) + .map(constraintConvertor::convert) + .collect(Collectors.toList()); + } + + return Collections.emptyList(); + } } |