From b08daeee5442d8422e2e23047ed13aa10d8303f9 Mon Sep 17 00:00:00 2001 From: MichaelMorris Date: Tue, 25 May 2021 09:32:31 +0100 Subject: Support adding data types to model Signed-off-by: MichaelMorris Issue-ID: SDC-3611 Change-Id: Ia9fd2437b71a2cd4fc853ff6a1f4fd37fe09a685 --- .../sdc/be/components/impl/BaseBusinessLogic.java | 2 +- .../be/components/impl/ComponentBusinessLogic.java | 2 +- .../be/components/impl/DataTypeImportManager.java | 23 ++++--- .../be/components/impl/ResourceBusinessLogic.java | 2 +- .../components/validation/PropertyValidator.java | 71 +--------------------- .../sdc/be/servlets/TypesUploadServlet.java | 10 +-- .../components/impl/InputsBusinessLogicTest.java | 8 +-- .../components/impl/ResourceBusinessLogicTest.java | 2 +- .../validation/PropertyValidatorTest.java | 18 +----- 9 files changed, 30 insertions(+), 108 deletions(-) (limited to 'catalog-be/src') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java index fbfeaa5320..792e6963c0 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java @@ -451,7 +451,7 @@ public abstract class BaseBusinessLogic { Either validatePropertyDefaultValue(IComplexDefaultValue property, Map dataTypes) { String type; String innerType = null; - if (!propertyOperation.isPropertyTypeValid(property)) { + if (!propertyOperation.isPropertyTypeValid(property, null)) { log.info("Invalid type for property '{}' type '{}'", property.getName(), property.getType()); ResponseFormat responseFormat = componentsUtils .getResponseFormat(ActionStatus.INVALID_PROPERTY_TYPE, property.getType(), property.getName()); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java index afb84a9ea8..d0d40e9db0 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java @@ -821,7 +821,7 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic { if (ToscaPropertyType.isScalarType(propertyType)) { return false; } - Either getDataTypeByNameRes = propertyOperation.getDataTypeByName(propertyType); + Either getDataTypeByNameRes = propertyOperation.getDataTypeByName(propertyType, null); if (getDataTypeByNameRes.isRight()) { return false; } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/DataTypeImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/DataTypeImportManager.java index 4c7f072f71..6343eb9b3d 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/DataTypeImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/DataTypeImportManager.java @@ -27,6 +27,7 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import javax.annotation.Resource; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.openecomp.sdc.be.components.impl.CommonImportManager.ElementTypeEnum; import org.openecomp.sdc.be.dao.api.ActionStatus; @@ -34,8 +35,10 @@ import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; +import org.openecomp.sdc.be.model.RelationshipTypeDefinition; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.PropertyOperation; +import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.utils.TypeUtils; import org.openecomp.sdc.common.log.wrappers.Logger; @@ -53,20 +56,24 @@ public class DataTypeImportManager { @Resource private CommonImportManager commonImportManager; - public Either>, ResponseFormat> createDataTypes(String dataTypeYml) { + public Either>, ResponseFormat> createDataTypes(final String dataTypeYml, final String modelName) { return commonImportManager - .createElementTypes(dataTypeYml, this::createDataTypesFromYml, this::createDataTypesByDao, ElementTypeEnum.DATA_TYPE); + .createElementTypes(dataTypeYml, dataTypesFromYml -> createDataTypesFromYml(dataTypeYml, modelName), this::createDataTypesByDao, ElementTypeEnum.DATA_TYPE); } - - private Either, ActionStatus> createDataTypesFromYml(String dataTypesYml) { - return commonImportManager.createElementTypesFromYml(dataTypesYml, this::createDataType); + + private Either, ActionStatus> createDataTypesFromYml(final String dataTypesYml, final String modelName) { + final Either, ActionStatus> dataTypes = commonImportManager.createElementTypesFromYml(dataTypesYml, this::createDataType); + if (dataTypes.isLeft() && StringUtils.isNotEmpty(modelName)){ + dataTypes.left().value().forEach(dataType -> dataType.setModel(modelName)); + } + return dataTypes; } private Either>, ResponseFormat> createDataTypesByDao( List dataTypesToCreate) { return commonImportManager.createElementTypesByDao(dataTypesToCreate, this::validateDataType, - dataType -> new ImmutablePair<>(ElementTypeEnum.DATA_TYPE, dataType.getName()), - dataTypeName -> propertyOperation.getDataTypeByNameWithoutDerived(dataTypeName), dataType -> propertyOperation.addDataType(dataType), + dataType -> new ImmutablePair<>(ElementTypeEnum.DATA_TYPE, UniqueIdBuilder.buildDataTypeUid(dataType.getModel(), dataType.getName())), + dataTypeUid -> propertyOperation.getDataTypeByUidWithoutDerived(dataTypeUid, true), dataType -> propertyOperation.addDataType(dataType), (newDataType, oldDataType) -> propertyOperation.updateDataType(newDataType, oldDataType)); } @@ -119,7 +126,7 @@ public class DataTypeImportManager { } String derivedDataType = dataType.getDerivedFromName(); if (derivedDataType != null) { - Either derivedDataTypeByName = propertyOperation.getDataTypeByName(derivedDataType, true); + Either derivedDataTypeByName = propertyOperation.getDataTypeByName(derivedDataType, dataType.getModel()); if (derivedDataTypeByName.isRight()) { StorageOperationStatus status = derivedDataTypeByName.right().value(); if (status == StorageOperationStatus.NOT_FOUND) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java index d692b4fc81..99ceb2158e 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java @@ -4736,7 +4736,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { String type = null; String innerType = null; for (PropertyDefinition property : properties) { - if (!propertyOperation.isPropertyTypeValid(property)) { + if (!propertyOperation.isPropertyTypeValid(property, null)) { log.info("Invalid type for property {}", property); throw new ByActionStatusComponentException(ActionStatus.INVALID_PROPERTY_TYPE, property.getType(), property.getName()); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/PropertyValidator.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/PropertyValidator.java index bdb6799cb6..33cba4f4a4 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/PropertyValidator.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/validation/PropertyValidator.java @@ -19,26 +19,16 @@ */ package org.openecomp.sdc.be.components.validation; -import fj.data.Either; import java.util.List; import java.util.Map; import java.util.Optional; -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic; import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils; -import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; -import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache; -import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; import org.openecomp.sdc.be.model.operations.impl.PropertyOperation; -import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.exception.ResponseFormat; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component @@ -46,16 +36,13 @@ public class PropertyValidator { private final PropertyOperation propertyOperation; private final ComponentsUtils componentsUtils; - private final ApplicationDataTypeCache applicationDataTypeCache; private final ExceptionUtils exceptionUtils; - private static final Logger log = LoggerFactory.getLogger(ResourceBusinessLogic.class); - public PropertyValidator(PropertyOperation propertyOperation, ComponentsUtils componentsUtils, ApplicationDataTypeCache applicationDataTypeCache, + public PropertyValidator(PropertyOperation propertyOperation, ComponentsUtils componentsUtils, ExceptionUtils exceptionUtils) { this.exceptionUtils = exceptionUtils; this.propertyOperation = propertyOperation; this.componentsUtils = componentsUtils; - this.applicationDataTypeCache = applicationDataTypeCache; } public void thinPropertiesValidator(List properties, List dbAnnotationTypeDefinitionProperties, @@ -87,60 +74,4 @@ public class PropertyValidator { exceptionUtils.rollBackAndThrow(responseFormat); return null; } - - public Either iterateOverProperties(List properties) { - Either eitherResult = Either.left(true); - String type = null; - String innerType = null; - for (PropertyDefinition property : properties) { - if (!propertyOperation.isPropertyTypeValid(property)) { - log.info("Invalid type for property {}", property); - ResponseFormat responseFormat = componentsUtils - .getResponseFormat(ActionStatus.INVALID_PROPERTY_TYPE, property.getType(), property.getName()); - eitherResult = Either.right(responseFormat); - break; - } - Either, JanusGraphOperationStatus> allDataTypes = applicationDataTypeCache.getAll(); - if (allDataTypes.isRight()) { - JanusGraphOperationStatus status = allDataTypes.right().value(); - BeEcompErrorManager.getInstance().logInternalFlowError("AddPropertyToGroup", "Failed to validate property. Status is " + status, - BeEcompErrorManager.ErrorSeverity.ERROR); - return Either.right(componentsUtils.getResponseFormat( - componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)))); - } - type = property.getType(); - if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) { - ResponseFormat responseFormat = validateMapOrListPropertyType(property, allDataTypes.left().value()); - if (responseFormat != null) { - break; - } - } - if (!propertyOperation.isPropertyDefaultValueValid(property, allDataTypes.left().value())) { - log.info("Invalid default value for property {}", property); - ResponseFormat responseFormat; - if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) { - responseFormat = componentsUtils - .getResponseFormat(ActionStatus.INVALID_COMPLEX_DEFAULT_VALUE, property.getName(), type, innerType, - property.getDefaultValue()); - } else { - responseFormat = componentsUtils - .getResponseFormat(ActionStatus.INVALID_DEFAULT_VALUE, property.getName(), type, property.getDefaultValue()); - } - eitherResult = Either.right(responseFormat); - break; - } - } - return eitherResult; - } - - private ResponseFormat validateMapOrListPropertyType(PropertyDefinition property, Map allDataTypes) { - ResponseFormat responseFormat = null; - ImmutablePair propertyInnerTypeValid = propertyOperation.isPropertyInnerTypeValid(property, allDataTypes); - String propertyInnerType = propertyInnerTypeValid.getLeft(); - if (!propertyInnerTypeValid.getRight().booleanValue()) { - log.info("Invalid inner type for property {}", property); - responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY_INNER_TYPE, propertyInnerType, property.getName()); - } - return responseFormat; - } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java index 1ca18334c4..fed26cccde 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadServlet.java @@ -193,9 +193,9 @@ public class TypesUploadServlet extends AbstractValidationsServlet { @ApiResponse(responseCode = "409", description = "Data types already exist")}) @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) public Response uploadDataTypes(@Parameter(description = "FileInputStream") @FormDataParam("dataTypesZip") File file, - @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator) { - ConsumerTwoParam, String> createElementsMethod = this::createDataTypes; - return uploadElementTypeServletLogic(createElementsMethod, file, request, creator, NodeTypeEnum.DataType.getName()); + @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator, + @Parameter(description = "model") @FormDataParam("model") String modelName) { + return uploadElementTypeServletLogic(this::createDataTypes, file, request, creator, NodeTypeEnum.DataType.getName(), modelName); } @POST @@ -338,9 +338,9 @@ public class TypesUploadServlet extends AbstractValidationsServlet { } // data types - private void createDataTypes(Wrapper responseWrapper, String dataTypesYml) { + private void createDataTypes(Wrapper responseWrapper, String dataTypesYml, final String modelName) { final Supplier>, ResponseFormat>> generateElementTypeFromYml = () -> dataTypeImportManager - .createDataTypes(dataTypesYml); + .createDataTypes(dataTypesYml, modelName); buildStatusForElementTypeCreate(responseWrapper, generateElementTypeFromYml, ActionStatus.DATA_TYPE_ALREADY_EXIST, NodeTypeEnum.DataType.name()); } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/InputsBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/InputsBusinessLogicTest.java index 548ef1ace9..308d272a79 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/InputsBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/InputsBusinessLogicTest.java @@ -469,7 +469,7 @@ public class InputsBusinessLogicTest { // for BaseOperation.getAllDataTypes: when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap // for BaseOperation.validatePropertyDefaultValue: - when(propertyOperation.isPropertyTypeValid(any())).thenReturn(true); + when(propertyOperation.isPropertyTypeValid(any(), any())).thenReturn(true); when(propertyOperation.isPropertyInnerTypeValid(any(),any())).thenReturn(new ImmutablePair<>(listInput.getSchemaType(), true)); when(propertyOperation.isPropertyDefaultValueValid(any(), any())).thenReturn(true); // for createListInputsInGraph: @@ -565,12 +565,12 @@ public class InputsBusinessLogicTest { when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID); when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap // for BaseOperation.validatePropertyDefaultValue: - when(propertyOperation.isPropertyTypeValid(any())).thenReturn(false); + when(propertyOperation.isPropertyTypeValid(any(), any())).thenReturn(false); Either, ResponseFormat> result = testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false); assertThat(result.isRight()).isTrue(); - verify(propertyOperation, times(1)).isPropertyTypeValid(any()); + verify(propertyOperation, times(1)).isPropertyTypeValid(any(), any()); } @Test @@ -585,7 +585,7 @@ public class InputsBusinessLogicTest { when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID); when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap // for BaseOperation.validatePropertyDefaultValue: - when(propertyOperation.isPropertyTypeValid(any())).thenReturn(true); + when(propertyOperation.isPropertyTypeValid(any(), any())).thenReturn(true); when(propertyOperation.isPropertyInnerTypeValid(any(),any())).thenReturn(new ImmutablePair<>(listInput.getSchemaType(), true)); when(propertyOperation.isPropertyDefaultValueValid(any(), any())).thenReturn(true); when(toscaOperationFacadeMock.addInputsToComponent(anyMap(), eq(COMPONENT_ID))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java index 09f4ac72de..17078bcb75 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java @@ -1720,7 +1720,7 @@ public class ResourceBusinessLogicTest { List properties = new ArrayList<>(); properties.add(property); basic.setProperties(properties); - when(propertyOperation.isPropertyTypeValid(property)).thenReturn(true); + when(propertyOperation.isPropertyTypeValid(property, null)).thenReturn(true); when(propertyOperation.isPropertyDefaultValueValid(property, emptyDataTypes)).thenReturn(true); Boolean validatePropertiesDefaultValues = bl.validatePropertiesDefaultValues(basic); assertTrue(validatePropertiesDefaultValues); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/PropertyValidatorTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/PropertyValidatorTest.java index 9a9e9578b6..f85ec2953a 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/PropertyValidatorTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/PropertyValidatorTest.java @@ -59,8 +59,7 @@ public class PropertyValidatorTest { Mockito.when(propertyOperation.validateAndUpdatePropertyValue( Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(Either.left("")); - PropertyValidator propertyValidator = new PropertyValidator(propertyOperation, componentsUtils, - applicationDataTypeCache, exceptionUtils); + PropertyValidator propertyValidator = new PropertyValidator(propertyOperation, componentsUtils, exceptionUtils); List props = new ArrayList<>(); List dbProps = new ArrayList<>(); PropertyDefinition prop = new PropertyDefinition(); @@ -73,19 +72,4 @@ public class PropertyValidatorTest { Mockito.verify(propertyOperation).validateAndUpdatePropertyValue(TYPE, VALUE, null, Collections.emptyMap()); } - - @Test - public void shouldIterateOverPropertiesOnInvalidType() { - PropertyValidator propertyValidator = new PropertyValidator(propertyOperation, componentsUtils, - applicationDataTypeCache, exceptionUtils); - List props = new ArrayList<>(); - PropertyDefinition prop = new PropertyDefinition(); - prop.setName(TEST); - prop.setValue(VALUE); - prop.setType(TYPE); - props.add(prop); - Either booleanResponseFormatEither = propertyValidator.iterateOverProperties(props); - - assertTrue(booleanResponseFormatEither.isRight()); - } } \ No newline at end of file -- cgit 1.2.3-korg