From 4701612ba26c4b8c7c7e9fbfa38f582a4b74669f Mon Sep 17 00:00:00 2001 From: vasraz Date: Thu, 26 Aug 2021 12:17:08 +0100 Subject: Add types to imports Change-Id: Ifa1dea17841d8667044aff4e7a9a17b2a75b78e4 Signed-off-by: Vasyl Razinkov Issue-ID: SDC-3684 --- .../impl/CapabilityTypeImportManager.java | 28 ++-- .../be/components/impl/CommonImportManager.java | 11 +- .../be/components/impl/DataTypeImportManager.java | 26 ++-- .../be/components/impl/GroupTypeImportManager.java | 18 ++- .../impl/InterfaceLifecycleTypeImportManager.java | 10 +- .../sdc/be/components/impl/ModelBusinessLogic.java | 6 +- .../components/impl/PolicyTypeImportManager.java | 19 ++- .../impl/RelationshipTypeImportManager.java | 27 ++-- .../sdc/be/servlets/TypesUploadServlet.java | 144 +++++++++++---------- 9 files changed, 180 insertions(+), 109 deletions(-) (limited to 'catalog-be/src/main/java') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CapabilityTypeImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CapabilityTypeImportManager.java index 04a8fc88db..c0188d79c3 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CapabilityTypeImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CapabilityTypeImportManager.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Optional; 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; import org.openecomp.sdc.be.model.CapabilityTypeDefinition; import org.openecomp.sdc.be.model.Model; @@ -48,20 +49,30 @@ public class CapabilityTypeImportManager { private final ModelOperation modelOperation; @Autowired - public CapabilityTypeImportManager(CapabilityTypeOperation capabilityTypeOperation, CommonImportManager commonImportManager, ModelOperation modelOperation) { + public CapabilityTypeImportManager(CapabilityTypeOperation capabilityTypeOperation, CommonImportManager commonImportManager, + ModelOperation modelOperation) { this.capabilityTypeOperation = capabilityTypeOperation; this.commonImportManager = commonImportManager; this.modelOperation = modelOperation; } - public Either>, ResponseFormat> createCapabilityTypes(final String capabilityTypesYml, final String modelName) { - return commonImportManager.createElementTypes(capabilityTypesYml, capabilityTypesFromYml -> createCapabilityTypesFromYml(capabilityTypesYml, modelName), this::upsertCapabilityTypesByDao, - CommonImportManager.ElementTypeEnum.CAPABILITY_TYPE); + public Either>, ResponseFormat> createCapabilityTypes(final String capabilityTypesYml, + final String modelName, + final boolean includeToModelDefaultImports) { + final Either>, ResponseFormat> elementTypes = commonImportManager.createElementTypes( + capabilityTypesYml, capabilityTypesFromYml -> createCapabilityTypesFromYml(capabilityTypesYml, modelName), + this::upsertCapabilityTypesByDao, ElementTypeEnum.CAPABILITY_TYPE); + if (includeToModelDefaultImports && StringUtils.isNotEmpty(modelName)) { + commonImportManager.addTypesToDefaultImports(capabilityTypesYml, modelName); + } + return elementTypes; } - private Either, ActionStatus> createCapabilityTypesFromYml(final String capabilityTypesYml, final String modelName) { - final Either, ActionStatus> capabilityTypes = commonImportManager.createElementTypesFromYml(capabilityTypesYml, this::createCapabilityType); - if (capabilityTypes.isLeft() && StringUtils.isNotEmpty(modelName)){ + private Either, ActionStatus> createCapabilityTypesFromYml(final String capabilityTypesYml, + final String modelName) { + final Either, ActionStatus> capabilityTypes = commonImportManager.createElementTypesFromYml(capabilityTypesYml, + this::createCapabilityType); + if (capabilityTypes.isLeft() && StringUtils.isNotEmpty(modelName)) { final Optional modelOptional = modelOperation.findModelByName(modelName); if (modelOptional.isPresent()) { capabilityTypes.left().value().forEach(capabilityType -> capabilityType.setModel(modelName)); @@ -75,7 +86,8 @@ public class CapabilityTypeImportManager { private Either>, ResponseFormat> upsertCapabilityTypesByDao( List capabilityTypesToCreate) { return commonImportManager.createElementTypesByDao(capabilityTypesToCreate, capabilityType -> Either.left(ActionStatus.OK), - capabilityType -> new ImmutablePair<>(CommonImportManager.ElementTypeEnum.CAPABILITY_TYPE, UniqueIdBuilder.buildCapabilityTypeUid(capabilityType.getModel(), capabilityType.getType())), + capabilityType -> new ImmutablePair<>(CommonImportManager.ElementTypeEnum.CAPABILITY_TYPE, + UniqueIdBuilder.buildCapabilityTypeUid(capabilityType.getModel(), capabilityType.getType())), capabilityTypeOperation::getCapabilityType, capabilityTypeOperation::addCapabilityType, this::updateCapabilityType); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CommonImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CommonImportManager.java index 9a77aad0b2..c0da9eb17b 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CommonImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CommonImportManager.java @@ -51,6 +51,7 @@ import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.normatives.ToscaTypeMetadata; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.api.TypeOperations; +import org.openecomp.sdc.be.model.operations.impl.ModelOperation; import org.openecomp.sdc.be.model.operations.impl.PropertyOperation; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.exception.ResponseFormat; @@ -64,11 +65,15 @@ public class CommonImportManager { private static final Logger log = Logger.getLogger(CommonImportManager.class.getName()); private final ComponentsUtils componentsUtils; private final PropertyOperation propertyOperation; + private final ModelOperation modelOperation; @Autowired - public CommonImportManager(ComponentsUtils componentsUtils, PropertyOperation propertyOperation) { + public CommonImportManager(final ComponentsUtils componentsUtils, + final PropertyOperation propertyOperation, + final ModelOperation modelOperation) { this.componentsUtils = componentsUtils; this.propertyOperation = propertyOperation; + this.modelOperation = modelOperation; } public static void setProperties(Map toscaJson, Consumer> consumer) { @@ -529,6 +534,10 @@ public class CommonImportManager { } + public void addTypesToDefaultImports(final String typesYaml, final String modelName) { + modelOperation.addTypesToDefaultImports(typesYaml, modelName); + } + public enum ElementTypeEnum { POLICY_TYPE, GROUP_TYPE, DATA_TYPE, CAPABILITY_TYPE, INTERFACE_LIFECYCLE_TYPE, RELATIONSHIP_TYPE } 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 95ef74e93c..b24d81bcbc 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 @@ -37,7 +37,6 @@ import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.Model; 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.ModelOperation; import org.openecomp.sdc.be.model.operations.impl.PropertyOperation; @@ -61,14 +60,21 @@ public class DataTypeImportManager { @Resource private ModelOperation modelOperation; - public Either>, ResponseFormat> createDataTypes(final String dataTypeYml, final String modelName) { - return commonImportManager - .createElementTypes(dataTypeYml, dataTypesFromYml -> createDataTypesFromYml(dataTypeYml, modelName), this::createDataTypesByDao, ElementTypeEnum.DATA_TYPE); + public Either>, ResponseFormat> createDataTypes(final String dataTypeYml, final String modelName, + final boolean includeToModelDefaultImports) { + final var elementTypes = commonImportManager.createElementTypes( + dataTypeYml, dataTypesFromYml -> createDataTypesFromYml(dataTypeYml, modelName), this::createDataTypesByDao, ElementTypeEnum.DATA_TYPE); + + if (includeToModelDefaultImports && StringUtils.isNotEmpty(modelName)) { + commonImportManager.addTypesToDefaultImports(dataTypeYml, modelName); + } + return elementTypes; } - + 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)){ + final Either, ActionStatus> dataTypes = commonImportManager.createElementTypesFromYml(dataTypesYml, + this::createDataType); + if (dataTypes.isLeft() && StringUtils.isNotEmpty(modelName)) { final Optional modelOptional = modelOperation.findModelByName(modelName); if (modelOptional.isPresent()) { dataTypes.left().value().forEach(dataType -> dataType.setModel(modelName)); @@ -83,7 +89,8 @@ public class DataTypeImportManager { List dataTypesToCreate) { return commonImportManager.createElementTypesByDao(dataTypesToCreate, this::validateDataType, dataType -> new ImmutablePair<>(ElementTypeEnum.DATA_TYPE, UniqueIdBuilder.buildDataTypeUid(dataType.getModel(), dataType.getName())), - dataTypeUid -> propertyOperation.getDataTypeByUidWithoutDerived(dataTypeUid, true), dataType -> propertyOperation.addDataType(dataType), + dataTypeUid -> propertyOperation.getDataTypeByUidWithoutDerived(dataTypeUid, true), + dataType -> propertyOperation.addDataType(dataType), (newDataType, oldDataType) -> propertyOperation.updateDataType(newDataType, oldDataType)); } @@ -136,7 +143,8 @@ public class DataTypeImportManager { } String derivedDataType = dataType.getDerivedFromName(); if (derivedDataType != null) { - Either derivedDataTypeByName = propertyOperation.getDataTypeByName(derivedDataType, dataType.getModel()); + 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/GroupTypeImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/GroupTypeImportManager.java index e57c46bbe6..e043bc2619 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/GroupTypeImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/GroupTypeImportManager.java @@ -69,13 +69,20 @@ public class GroupTypeImportManager { this.modelOperation = modelOperation; } - public Either>, ResponseFormat> createGroupTypes(ToscaTypeImportData toscaTypeImportData, String modelName) { - return commonImportManager.createElementTypes(toscaTypeImportData, this::createGroupTypesFromYml, this::upsertGroupTypesByDao, modelName); + public Either>, ResponseFormat> createGroupTypes(ToscaTypeImportData toscaTypeImportData, + String modelName, final boolean includeToModelDefaultImports) { + final Either>, ResponseFormat> elementTypes = commonImportManager.createElementTypes( + toscaTypeImportData, this::createGroupTypesFromYml, this::upsertGroupTypesByDao, modelName); + if (includeToModelDefaultImports && StringUtils.isNotEmpty(modelName)) { + commonImportManager.addTypesToDefaultImports(toscaTypeImportData.getToscaTypesYml(), modelName); + } + return elementTypes; } private Either, ActionStatus> createGroupTypesFromYml(String groupTypesYml, String modelName) { - Either, ActionStatus> groupTypes = commonImportManager.createElementTypesFromYml(groupTypesYml, this::createGroupType); - if (groupTypes.isLeft() && StringUtils.isNotEmpty(modelName)){ + Either, ActionStatus> groupTypes = commonImportManager.createElementTypesFromYml(groupTypesYml, + this::createGroupType); + if (groupTypes.isLeft() && StringUtils.isNotEmpty(modelName)) { final Optional modelOptional = modelOperation.findModelByName(modelName); if (modelOptional.isPresent()) { groupTypes.left().value().forEach(groupType -> groupType.setModel(modelName)); @@ -90,7 +97,8 @@ public class GroupTypeImportManager { List groupTypesToCreate, String modelName) { return commonImportManager.createElementTypesWithVersionByDao(groupTypesToCreate, this::validateGroupType, groupType -> new ImmutablePair<>(ElementTypeEnum.GROUP_TYPE, UniqueIdBuilder.buildGroupTypeUid(groupType.getModel(), - groupType.getType(), groupType.getVersion(), NodeTypeEnum.GroupType.getName()).toLowerCase()), groupTypeOperation::getLatestGroupTypeByType, + groupType.getType(), groupType.getVersion(), NodeTypeEnum.GroupType.getName()).toLowerCase()), + groupTypeOperation::getLatestGroupTypeByType, groupTypeOperation::addGroupType, this::updateGroupType, modelName); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceLifecycleTypeImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceLifecycleTypeImportManager.java index ef00d9a92d..5cc8985b90 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceLifecycleTypeImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceLifecycleTypeImportManager.java @@ -42,7 +42,6 @@ import org.openecomp.sdc.be.model.Operation; import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.ModelOperation; -import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.exception.ResponseFormat; @@ -63,14 +62,19 @@ public class InterfaceLifecycleTypeImportManager { @Resource private ModelOperation modelOperation; - public Either, ResponseFormat> createLifecycleTypes(String interfaceLifecycleTypesYml, final String modelName) { + public Either, ResponseFormat> createLifecycleTypes(String interfaceLifecycleTypesYml, final String modelName, + final boolean includeToModelDefaultImports) { Either, ActionStatus> interfaces = createInterfaceTypeFromYml(interfaceLifecycleTypesYml, modelName); if (interfaces.isRight()) { ActionStatus status = interfaces.right().value(); ResponseFormat responseFormat = componentsUtils.getResponseFormatByGroupType(status, null); return Either.right(responseFormat); } - return createInterfacesByDao(interfaces.left().value()); + final Either, ResponseFormat> elementTypes = createInterfacesByDao(interfaces.left().value()); + if (includeToModelDefaultImports && StringUtils.isNotEmpty(modelName)) { + commonImportManager.addTypesToDefaultImports(interfaceLifecycleTypesYml, modelName); + } + return elementTypes; } private Either, ActionStatus> createInterfaceTypeFromYml(final String interfaceTypesYml, final String modelName) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ModelBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ModelBusinessLogic.java index 99349395b9..1d34f39b74 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ModelBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ModelBusinessLogic.java @@ -50,10 +50,10 @@ public class ModelBusinessLogic { this.modelOperation = modelOperation; this.dataTypeImportManager = dataTypeImportManager; } - + public Model createModel(final Model model, final String datatypesYaml) { createModel(model); - dataTypeImportManager.createDataTypes(datatypesYaml, model.getName()); + dataTypeImportManager.createDataTypes(datatypesYaml, model.getName(), false); return model; } @@ -119,4 +119,4 @@ public class ModelBusinessLogic { throw ModelOperationExceptionSupplier.couldNotReadImports().get(); } } -} \ No newline at end of file +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/PolicyTypeImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/PolicyTypeImportManager.java index a3a2c013eb..54d57800be 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/PolicyTypeImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/PolicyTypeImportManager.java @@ -69,13 +69,21 @@ public class PolicyTypeImportManager { this.modelOperation = modelOperation; } - public Either>, ResponseFormat> createPolicyTypes(ToscaTypeImportData toscaTypeImportData, String modelName) { - return commonImportManager.createElementTypes(toscaTypeImportData, this::createPolicyTypesFromYml, this::upsertPolicyTypesByDao, modelName); + public Either>, ResponseFormat> createPolicyTypes(final ToscaTypeImportData toscaTypeImportData, + final String modelName, + final boolean includeToModelDefaultImports) { + final Either>, ResponseFormat> elementTypes = commonImportManager.createElementTypes( + toscaTypeImportData, this::createPolicyTypesFromYml, this::upsertPolicyTypesByDao, modelName); + if (includeToModelDefaultImports && StringUtils.isNotEmpty(modelName)) { + commonImportManager.addTypesToDefaultImports(toscaTypeImportData.getToscaTypesYml(), modelName); + } + return elementTypes; } private Either, ActionStatus> createPolicyTypesFromYml(String policyTypesYml, String modelName) { - Either, ActionStatus> policyTypes = commonImportManager.createElementTypesFromYml(policyTypesYml, this::createPolicyType); - if (policyTypes.isLeft() && StringUtils.isNotEmpty(modelName)){ + Either, ActionStatus> policyTypes = commonImportManager.createElementTypesFromYml(policyTypesYml, + this::createPolicyType); + if (policyTypes.isLeft() && StringUtils.isNotEmpty(modelName)) { final Optional modelOptional = modelOperation.findModelByName(modelName); if (modelOptional.isPresent()) { policyTypes.left().value().forEach(policyType -> policyType.setModel(modelName)); @@ -90,7 +98,8 @@ public class PolicyTypeImportManager { List policyTypesToCreate, String modelName) { return commonImportManager.createElementTypesWithVersionByDao(policyTypesToCreate, this::validatePolicyType, policyType -> new ImmutablePair<>(ElementTypeEnum.POLICY_TYPE, UniqueIdBuilder.buildPolicyTypeUid(policyType.getModel(), - policyType.getType(), policyType.getVersion(), NodeTypeEnum.PolicyType.getName()).toLowerCase()), policyTypeOperation::getLatestPolicyTypeByType, + policyType.getType(), policyType.getVersion(), NodeTypeEnum.PolicyType.getName()).toLowerCase()), + policyTypeOperation::getLatestPolicyTypeByType, policyTypeOperation::addPolicyType, this::updatePolicyType, modelName); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/RelationshipTypeImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/RelationshipTypeImportManager.java index 2165b9f7d1..564eb3a794 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/RelationshipTypeImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/RelationshipTypeImportManager.java @@ -52,21 +52,31 @@ public class RelationshipTypeImportManager { this.modelOperation = modelOperation; } - public Either>, ResponseFormat> createRelationshipTypes(final String relationshipYml, final String modelName) { - return createRelationshipTypes(relationshipYml, modelName, false); + public Either>, ResponseFormat> createRelationshipTypes(final String relationshipYml, + final String modelName, + final boolean includeToModelDefaultImports) { + return createRelationshipTypes(relationshipYml, modelName, false, includeToModelDefaultImports); } private Either>, ResponseFormat> createRelationshipTypes(final String relationshipTypeYml, - final String modelName, final boolean inTransaction) { - return commonImportManager + final String modelName, + final boolean inTransaction, + final boolean includeToModelDefaultImports) { + final Either>, ResponseFormat> elementTypes = commonImportManager .createElementTypes(relationshipTypeYml, relationshipTypesFromYml -> createRelationshipTypesFromYml(relationshipTypeYml, modelName), relationshipTypesToCreate -> createRelationshipTypesByDao(relationshipTypesToCreate, inTransaction), ElementTypeEnum.RELATIONSHIP_TYPE); + if (includeToModelDefaultImports && StringUtils.isNotEmpty(modelName)) { + commonImportManager.addTypesToDefaultImports(relationshipTypeYml, modelName); + } + return elementTypes; } - private Either, ActionStatus> createRelationshipTypesFromYml(final String relationshipTypeYml, final String modelName) { - final Either, ActionStatus> relationshipTypes = commonImportManager.createElementTypesFromYml(relationshipTypeYml, this::createRelationshipType); - if (relationshipTypes.isLeft() && StringUtils.isNotEmpty(modelName)){ + private Either, ActionStatus> createRelationshipTypesFromYml(final String relationshipTypeYml, + final String modelName) { + final Either, ActionStatus> relationshipTypes = commonImportManager.createElementTypesFromYml( + relationshipTypeYml, this::createRelationshipType); + if (relationshipTypes.isLeft() && StringUtils.isNotEmpty(modelName)) { final Optional modelOptional = modelOperation.findModelByName(modelName); if (modelOptional.isPresent()) { relationshipTypes.left().value().forEach(relationshipType -> relationshipType.setModel(modelName)); @@ -80,7 +90,8 @@ public class RelationshipTypeImportManager { private Either>, ResponseFormat> createRelationshipTypesByDao( List relationshipTypesToCreate, boolean inTransaction) { return commonImportManager.createElementTypesByDao(relationshipTypesToCreate, this::validateRelationshipType, - relationshipType -> new ImmutablePair<>(ElementTypeEnum.RELATIONSHIP_TYPE, UniqueIdBuilder.buildRelationshipTypeUid(relationshipType.getModel(), relationshipType.getType())), + relationshipType -> new ImmutablePair<>(ElementTypeEnum.RELATIONSHIP_TYPE, + UniqueIdBuilder.buildRelationshipTypeUid(relationshipType.getModel(), relationshipType.getType())), relationshipTypeUid -> relationshipTypeOperation.getRelationshipTypeByUid(relationshipTypeUid).right() .map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus), relationshipType -> relationshipTypeOperation.addRelationshipType(relationshipType, inTransaction), 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 79982eaa3a..b8917890d2 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 @@ -29,9 +29,7 @@ import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.servers.Server; -import io.swagger.v3.oas.annotations.servers.Servers; import io.swagger.v3.oas.annotations.tags.Tag; -import io.swagger.v3.oas.annotations.tags.Tags; import java.io.File; import java.io.IOException; import java.util.List; @@ -66,6 +64,7 @@ import org.openecomp.sdc.be.components.impl.model.ToscaTypeImportData; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; +import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.impl.ServletUtils; import org.openecomp.sdc.be.model.DataTypeDefinition; @@ -76,11 +75,10 @@ import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.normatives.ToscaTypeMetadata; import org.openecomp.sdc.be.user.UserBusinessLogic; import org.openecomp.sdc.common.api.Constants; -import org.openecomp.sdc.common.datastructure.FunctionalInterfaces.ConsumerThreeParam; +import org.openecomp.sdc.common.datastructure.FunctionalInterfaces.ConsumerFourParam; import org.openecomp.sdc.common.datastructure.FunctionalInterfaces.ConsumerTwoParam; import org.openecomp.sdc.common.datastructure.Wrapper; import org.openecomp.sdc.common.log.wrappers.Logger; -import org.openecomp.sdc.common.util.ValidationUtils; import org.openecomp.sdc.exception.ResponseFormat; import org.springframework.stereotype.Controller; @@ -88,12 +86,14 @@ import org.springframework.stereotype.Controller; @Path("/v1/catalog/uploadType") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) -@Tags({@Tag(name = "SDCE-2 APIs")}) -@Servers({@Server(url = "/sdc2/rest")}) +@Tag(name = "SDCE-2 APIs") +@Server(url = "/sdc2/rest") @Controller public class TypesUploadServlet extends AbstractValidationsServlet { - public static final String CREATE = "Create "; + private static final String CREATE = "Create "; + private static final String START_HANDLE_REQUEST_OF = "Start handle request of {}"; + private static final String CREATE_FAILED_WITH_EXCEPTION = "create {} failed with exception:"; private static final Logger log = Logger.getLogger(TypesUploadServlet.class); private final CapabilityTypeImportManager capabilityTypeImportManager; private final InterfaceLifecycleTypeImportManager interfaceLifecycleTypeImportManager; @@ -132,11 +132,13 @@ public class TypesUploadServlet extends AbstractValidationsServlet { @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) public Response uploadCapabilityType(@Parameter(description = "FileInputStream") @FormDataParam("capabilityTypeZip") File file, @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator, - @Parameter(description = "model") @FormDataParam("model") String modelName) { - final String sanitizedModelName = ValidationUtils.sanitizeInputString(modelName); - ConsumerThreeParam, String, String> createElementsMethod = (responseWrapper, ymlPayload, model) -> createElementsType(responseWrapper, - () -> capabilityTypeImportManager.createCapabilityTypes(ymlPayload, sanitizedModelName)); - return uploadElementTypeServletLogic(createElementsMethod, file, request, creator, NodeTypeEnum.CapabilityType.name(), sanitizedModelName); + @Parameter(description = "model") @FormDataParam("model") String modelName, + @Parameter(description = "includeToModelImport") @FormDataParam("includeToModelImport") boolean includeToModelDefaultImports) { + ConsumerFourParam, String, String, Boolean> createElementsMethod = (responseWrapper, ymlPayload, model, includeToModelImport) -> + createElementsType(responseWrapper, () -> capabilityTypeImportManager.createCapabilityTypes(ymlPayload, modelName, + includeToModelDefaultImports)); + return uploadElementTypeServletLogic(createElementsMethod, file, request, creator, NodeTypeEnum.CapabilityType.name(), modelName, + includeToModelDefaultImports); } @POST @@ -150,9 +152,10 @@ public class TypesUploadServlet extends AbstractValidationsServlet { @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) public Response uploadRelationshipType(@Parameter(description = "FileInputStream") @FormDataParam("relationshipTypeZip") File file, @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator, - @Parameter(description = "model") @FormDataParam("model") String modelName) { - modelName = ValidationUtils.sanitizeInputString(modelName); - return uploadElementTypeServletLogic(this::createRelationshipTypes, file, request, creator, NodeTypeEnum.RelationshipType.getName(), modelName); + @Parameter(description = "model") @FormDataParam("model") String modelName, + @Parameter(description = "includeToModelImport") @FormDataParam("includeToModelImport") boolean includeToModelDefaultImports) { + return uploadElementTypeServletLogic( + this::createRelationshipTypes, file, request, creator, NodeTypeEnum.RelationshipType.getName(), modelName, includeToModelDefaultImports); } @POST @@ -166,10 +169,11 @@ public class TypesUploadServlet extends AbstractValidationsServlet { @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) public Response uploadInterfaceLifecycleType(@Parameter(description = "FileInputStream") @FormDataParam("interfaceLifecycleTypeZip") File file, @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator, - @Parameter(description = "model") @FormDataParam("model") String modelName) { - final String sanitizedModelName = ValidationUtils.sanitizeInputString(modelName); - ConsumerTwoParam, String> createElementsMethod = (responseWrapper, ymlPayload) -> createElementsType(responseWrapper, - () -> interfaceLifecycleTypeImportManager.createLifecycleTypes(ymlPayload, sanitizedModelName)); + @Parameter(description = "model") @FormDataParam("model") String modelName, + @Parameter(description = "includeToModelImport") @FormDataParam("includeToModelImport") boolean includeToModelDefaultImports) { + ConsumerTwoParam, String> createElementsMethod = (responseWrapper, ymlPayload) -> + createElementsType(responseWrapper, () -> interfaceLifecycleTypeImportManager.createLifecycleTypes(ymlPayload, modelName, + includeToModelDefaultImports)); return uploadElementTypeServletLogic(createElementsMethod, file, request, creator, "Interface Types"); } @@ -184,8 +188,8 @@ public class TypesUploadServlet extends AbstractValidationsServlet { @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) public Response uploadCategories(@Parameter(description = "FileInputStream") @FormDataParam("categoriesZip") File file, @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator) { - ConsumerTwoParam, String> createElementsMethod = (responseWrapper, ymlPayload) -> createElementsType(responseWrapper, - () -> categoriesImportManager.createCategories(ymlPayload)); + ConsumerTwoParam, String> createElementsMethod = (responseWrapper, ymlPayload) -> + createElementsType(responseWrapper, () -> categoriesImportManager.createCategories(ymlPayload)); return uploadElementTypeServletLogic(createElementsMethod, file, request, creator, "categories"); } @@ -200,9 +204,10 @@ public class TypesUploadServlet extends AbstractValidationsServlet { @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, - @Parameter(description = "model") @FormDataParam("model") String modelName) { - modelName = ValidationUtils.sanitizeInputString(modelName); - return uploadElementTypeServletLogic(this::createDataTypes, file, request, creator, NodeTypeEnum.DataType.getName(), modelName); + @Parameter(description = "model") @FormDataParam("model") String modelName, + @Parameter(description = "includeToModelImport") @FormDataParam("includeToModelImport") boolean includeToModelDefaultImports) { + return uploadElementTypeServletLogic(this::createDataTypes, file, request, creator, NodeTypeEnum.DataType.getName(), modelName, + includeToModelDefaultImports); } @POST @@ -217,10 +222,11 @@ public class TypesUploadServlet extends AbstractValidationsServlet { public Response uploadGroupTypes(@Parameter(description = "toscaTypeMetadata") @FormDataParam("toscaTypeMetadata") String toscaTypesMetaData, @Parameter(description = "model") @FormDataParam("model") String modelName, @Parameter(description = "FileInputStream") @FormDataParam("groupTypesZip") File file, - @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator) { - modelName = ValidationUtils.sanitizeInputString(modelName); + @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator, + @Parameter(description = "includeToModelImport") @FormDataParam("includeToModelImport") boolean includeToModelDefaultImports) { Map typesMetadata = getTypesMetadata(toscaTypesMetaData); - return uploadTypesWithMetaData(this::createGroupTypes, typesMetadata, file, request, creator, NodeTypeEnum.GroupType.getName(), modelName); + return uploadTypesWithMetaData(this::createGroupTypes, typesMetadata, file, request, creator, NodeTypeEnum.GroupType.getName(), modelName, + includeToModelDefaultImports); } @POST @@ -235,10 +241,11 @@ public class TypesUploadServlet extends AbstractValidationsServlet { public Response uploadPolicyTypes(@Parameter(description = "toscaTypeMetadata") @FormDataParam("toscaTypeMetadata") String toscaTypesMetaData, @Parameter(description = "model") @FormDataParam("model") String modelName, @Parameter(description = "FileInputStream") @FormDataParam("policyTypesZip") File file, - @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator) { - modelName = ValidationUtils.sanitizeInputString(modelName); + @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator, + @Parameter(description = "includeToModelImport") @FormDataParam("includeToModelImport") boolean includeToModelDefaultImports) { Map typesMetadata = getTypesMetadata(toscaTypesMetaData); - return uploadTypesWithMetaData(this::createPolicyTypes, typesMetadata, file, request, creator, NodeTypeEnum.PolicyType.getName(), modelName); + return uploadTypesWithMetaData(this::createPolicyTypes, typesMetadata, file, request, creator, NodeTypeEnum.PolicyType.getName(), modelName, + includeToModelDefaultImports); } private Map getTypesMetadata(String toscaTypesMetaData) { @@ -247,13 +254,13 @@ public class TypesUploadServlet extends AbstractValidationsServlet { } private Response uploadElementTypeServletLogic(ConsumerTwoParam, String> createElementsMethod, File file, - final HttpServletRequest request, String creator, String elementTypeName) { + final HttpServletRequest request, String creator, String elementTypeName) { init(); String userId = initHeaderParam(creator, request, Constants.USER_ID_HEADER); try { Wrapper yamlStringWrapper = new Wrapper<>(); String url = request.getMethod() + " " + request.getRequestURI(); - log.debug("Start handle request of {}", url); + log.debug(START_HANDLE_REQUEST_OF, url); Wrapper responseWrapper = doUploadTypeValidations(request, userId, file); if (responseWrapper.isEmpty()) { fillZipContents(yamlStringWrapper, file); @@ -263,30 +270,31 @@ public class TypesUploadServlet extends AbstractValidationsServlet { } return responseWrapper.getInnerElement(); } catch (Exception e) { - log.debug("create {} failed with exception:", elementTypeName, e); + log.debug(CREATE_FAILED_WITH_EXCEPTION, elementTypeName, e); BeEcompErrorManager.getInstance().logBeRestApiGeneralError(CREATE + elementTypeName); return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); } } - - private Response uploadElementTypeServletLogic(final ConsumerThreeParam, String, String> createElementsMethod, - final File file, final HttpServletRequest request, final String creator, final String elementTypeName, final String modelName) { + + private Response uploadElementTypeServletLogic(final ConsumerFourParam, String, String, Boolean> createElementsMethod, + final File file, final HttpServletRequest request, final String creator, + final String elementTypeName, final String modelName, final boolean includeToModelDefaultImports) { init(); final String userId = initHeaderParam(creator, request, Constants.USER_ID_HEADER); try { final Wrapper yamlStringWrapper = new Wrapper<>(); final String url = request.getMethod() + " " + request.getRequestURI(); - log.debug("Start handle request of {}", url); + log.debug(START_HANDLE_REQUEST_OF, url); final Wrapper responseWrapper = doUploadTypeValidations(request, userId, file); if (responseWrapper.isEmpty()) { fillZipContents(yamlStringWrapper, file); } if (responseWrapper.isEmpty()) { - createElementsMethod.accept(responseWrapper, yamlStringWrapper.getInnerElement(), modelName); + createElementsMethod.accept(responseWrapper, yamlStringWrapper.getInnerElement(), modelName, includeToModelDefaultImports); } return responseWrapper.getInnerElement(); } catch (final Exception e) { - log.debug("create {} failed with exception:", elementTypeName, e); + log.debug(CREATE_FAILED_WITH_EXCEPTION, elementTypeName, e); BeEcompErrorManager.getInstance().logBeRestApiGeneralError(CREATE + elementTypeName); return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); } @@ -296,7 +304,7 @@ public class TypesUploadServlet extends AbstractValidationsServlet { Wrapper responseWrapper = new Wrapper<>(); Wrapper userWrapper = new Wrapper<>(); String url = request.getMethod() + " " + request.getRequestURI(); - log.debug("Start handle request of {}", url); + log.debug(START_HANDLE_REQUEST_OF, url); validateUserExist(responseWrapper, userWrapper, userId); if (responseWrapper.isEmpty()) { validateUserRole(responseWrapper, userWrapper.getInnerElement()); @@ -307,9 +315,9 @@ public class TypesUploadServlet extends AbstractValidationsServlet { return responseWrapper; } - private Response uploadTypesWithMetaData(ConsumerThreeParam, ToscaTypeImportData, String> createElementsMethod, + private Response uploadTypesWithMetaData(ConsumerFourParam, ToscaTypeImportData, String, Boolean> createElementsMethod, Map typesMetaData, File file, final HttpServletRequest request, - String creator, String elementTypeName, String modelName) { + String creator, String elementTypeName, String modelName, final boolean includeToModelDefaultImports) { init(); String userId = initHeaderParam(creator, request, Constants.USER_ID_HEADER); Wrapper yamlStringWrapper = new Wrapper<>(); @@ -320,11 +328,11 @@ public class TypesUploadServlet extends AbstractValidationsServlet { } if (responseWrapper.isEmpty()) { ToscaTypeImportData toscaTypeImportData = new ToscaTypeImportData(yamlStringWrapper.getInnerElement(), typesMetaData); - createElementsMethod.accept(responseWrapper, toscaTypeImportData, modelName); + createElementsMethod.accept(responseWrapper, toscaTypeImportData, modelName, includeToModelDefaultImports); } return responseWrapper.getInnerElement(); } catch (Exception e) { - log.debug("create {} failed with exception:", elementTypeName, e); + log.debug(CREATE_FAILED_WITH_EXCEPTION, elementTypeName, e); BeEcompErrorManager.getInstance().logBeRestApiGeneralError(CREATE + elementTypeName); return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); } @@ -349,55 +357,54 @@ public class TypesUploadServlet extends AbstractValidationsServlet { } // data types - private void createDataTypes(Wrapper responseWrapper, String dataTypesYml, final String modelName) { - final Supplier>, ResponseFormat>> generateElementTypeFromYml = () -> dataTypeImportManager - .createDataTypes(dataTypesYml, modelName); + private void createDataTypes(Wrapper responseWrapper, String dataTypesYml, final String modelName, final boolean includeToModelDefaultImports) { + final Supplier>, ResponseFormat>> generateElementTypeFromYml = () -> + dataTypeImportManager.createDataTypes(dataTypesYml, modelName, includeToModelDefaultImports); buildStatusForElementTypeCreate(responseWrapper, generateElementTypeFromYml, ActionStatus.DATA_TYPE_ALREADY_EXIST, NodeTypeEnum.DataType.name()); } // group types - private void createGroupTypes(Wrapper responseWrapper, ToscaTypeImportData toscaTypeImportData, String modelName) { - final Supplier>, ResponseFormat>> generateElementTypeFromYml = () -> groupTypeImportManager - .createGroupTypes(toscaTypeImportData, modelName); + private void createGroupTypes(Wrapper responseWrapper, ToscaTypeImportData toscaTypeImportData, String modelName, + final boolean includeToModelDefaultImports) { + final Supplier>, ResponseFormat>> generateElementTypeFromYml = () -> + groupTypeImportManager.createGroupTypes(toscaTypeImportData, modelName, includeToModelDefaultImports); buildStatusForElementTypeCreate(responseWrapper, generateElementTypeFromYml, ActionStatus.GROUP_TYPE_ALREADY_EXIST, NodeTypeEnum.GroupType.name()); } // policy types - private void createPolicyTypes(Wrapper responseWrapper, ToscaTypeImportData toscaTypeImportData, String modelName) { - final Supplier>, ResponseFormat>> generateElementTypeFromYml = () -> policyTypeImportManager - .createPolicyTypes(toscaTypeImportData, modelName); + private void createPolicyTypes(Wrapper responseWrapper, ToscaTypeImportData toscaTypeImportData, String modelName, + final boolean includeToModelDefaultImports) { + final Supplier>, ResponseFormat>> generateElementTypeFromYml = () -> + policyTypeImportManager.createPolicyTypes(toscaTypeImportData, modelName, includeToModelDefaultImports); buildStatusForElementTypeCreate(responseWrapper, generateElementTypeFromYml, ActionStatus.POLICY_TYPE_ALREADY_EXIST, NodeTypeEnum.PolicyType.name()); } // data types - private void buildStatusForElementTypeCreate(Wrapper responseWrapper, - Supplier>, ResponseFormat>> generateElementTypeFromYml, - ActionStatus alreadyExistStatus, String elementTypeName) { - Either>, ResponseFormat> eitherResult = generateElementTypeFromYml.get(); + private void buildStatusForElementTypeCreate(Wrapper responseWrapper, + Supplier>, ResponseFormat>> generateElementTypeFromYml, + ActionStatus alreadyExistStatus, String elementTypeName) { + Either>, ResponseFormat> eitherResult = generateElementTypeFromYml.get(); if (eitherResult.isRight()) { Response response = buildErrorResponse(eitherResult.right().value()); responseWrapper.setInnerElement(response); } else { Object representation; try { - List> list = eitherResult.left().value(); + List> list = eitherResult.left().value(); ActionStatus status = ActionStatus.OK; if (list != null) { // Group result by the right value - true or false. - - // I.e., get the number of data types which are new and - - // which are old. - Map>> collect = list.stream() - .collect(Collectors.groupingBy(ImmutablePair::getRight)); + // I.e., get the number of data types which are new and which are old. + Map>> collect = list.stream() + .collect(Collectors.groupingBy(ImmutablePair::getRight)); if (collect != null) { Set keySet = collect.keySet(); if (keySet.size() == 1) { Boolean isNew = keySet.iterator().next(); - if (isNew) { + if (Boolean.TRUE.equals(isNew)) { // all data types created at the first time status = ActionStatus.CREATED; } else { @@ -420,9 +427,12 @@ public class TypesUploadServlet extends AbstractValidationsServlet { } // relationship types - private void createRelationshipTypes(final Wrapper responseWrapper, final String relationshipTypesYml, final String modelName) { + private void createRelationshipTypes(final Wrapper responseWrapper, + final String relationshipTypesYml, + final String modelName, + final boolean includeToModelDefaultImports) { final Supplier>, ResponseFormat>> generateElementTypeFromYml = () -> relationshipTypeImportManager - .createRelationshipTypes(relationshipTypesYml, modelName); + .createRelationshipTypes(relationshipTypesYml, modelName, includeToModelDefaultImports); buildStatusForElementTypeCreate(responseWrapper, generateElementTypeFromYml, ActionStatus.RELATIONSHIP_TYPE_ALREADY_EXIST, NodeTypeEnum.RelationshipType.name()); } -- cgit 1.2.3-korg