From 2a62c13ecfcf3fe0c22d8cd21d66d46e58105018 Mon Sep 17 00:00:00 2001 From: MichaelMorris Date: Wed, 19 May 2021 17:23:13 +0100 Subject: Support adding of relationship type to model Signed-off-by: MichaelMorris Issue-ID: SDC-3610 Change-Id: If315e21fa40e491cb1977dd9ceaf3e9b607e2f6f --- .../impl/RelationshipTypeImportManager.java | 23 ++++++++------ .../sdc/be/servlets/TypesUploadServlet.java | 35 ++++++++++++++++++---- 2 files changed, 44 insertions(+), 14 deletions(-) (limited to 'catalog-be/src/main/java/org') 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 6e4d554d98..dafd7919c1 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 @@ -25,6 +25,7 @@ import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.model.RelationshipTypeDefinition; import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; import org.openecomp.sdc.be.model.operations.impl.RelationshipTypeOperation; +import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; import org.openecomp.sdc.be.utils.TypeUtils; import org.openecomp.sdc.exception.ResponseFormat; import org.springframework.beans.factory.annotation.Autowired; @@ -45,27 +46,31 @@ public class RelationshipTypeImportManager { this.componentsUtils = componentsUtils; } - public Either>, ResponseFormat> createRelationshipTypes(String relationshipYml) { - return createRelationshipTypes(relationshipYml, false); + public Either>, ResponseFormat> createRelationshipTypes(final String relationshipYml, final String modelName) { + return createRelationshipTypes(relationshipYml, modelName, false); } - private Either>, ResponseFormat> createRelationshipTypes(String relationshipTypeYml, - boolean inTransaction) { + private Either>, ResponseFormat> createRelationshipTypes(final String relationshipTypeYml, + final String modelName, final boolean inTransaction) { return commonImportManager - .createElementTypes(relationshipTypeYml, relationshipTypesFromYml -> createRelationshipTypesFromYml(relationshipTypeYml), + .createElementTypes(relationshipTypeYml, relationshipTypesFromYml -> createRelationshipTypesFromYml(relationshipTypeYml, modelName), relationshipTypesToCreate -> createRelationshipTypesByDao(relationshipTypesToCreate, inTransaction), ElementTypeEnum.RELATIONSHIP_TYPE); } - private Either, ActionStatus> createRelationshipTypesFromYml(String relationshipTypeYml) { - return commonImportManager.createElementTypesFromYml(relationshipTypeYml, this::createRelationshipType); + private Either, ActionStatus> createRelationshipTypesFromYml(final String relationshipTypeYml, final String modelName) { + final Either, ActionStatus> relationshipTypes = commonImportManager.createElementTypesFromYml(relationshipTypeYml, this::createRelationshipType); + if (relationshipTypes.isLeft()){ + relationshipTypes.left().value().forEach(relationshipType -> relationshipType.setModel(modelName)); + } + return relationshipTypes; } private Either>, ResponseFormat> createRelationshipTypesByDao( List relationshipTypesToCreate, boolean inTransaction) { return commonImportManager.createElementTypesByDao(relationshipTypesToCreate, this::validateRelationshipType, - relationshipType -> new ImmutablePair<>(ElementTypeEnum.RELATIONSHIP_TYPE, relationshipType.getType()), - relationshipTypeName -> relationshipTypeOperation.getRelationshipTypeByName(relationshipTypeName).right() + 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), (newRelationshipType, oldRelationshipType) -> relationshipTypeOperation 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 d98928c4e4..1ca18334c4 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 @@ -76,6 +76,7 @@ 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.ConsumerTwoParam; import org.openecomp.sdc.common.datastructure.Wrapper; import org.openecomp.sdc.common.log.wrappers.Logger; @@ -145,8 +146,9 @@ public class TypesUploadServlet extends AbstractValidationsServlet { @ApiResponse(responseCode = "409", description = "Relationship Type already exist")}) @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) { - return uploadElementTypeServletLogic(this::createRelationshipTypes, file, request, creator, NodeTypeEnum.RelationshipType.getName()); + @Context final HttpServletRequest request, @HeaderParam("USER_ID") String creator, + @Parameter(description = "model") @FormDataParam("model") String modelName) { + return uploadElementTypeServletLogic(this::createRelationshipTypes, file, request, creator, NodeTypeEnum.RelationshipType.getName(), modelName); } @POST @@ -234,7 +236,7 @@ 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 { @@ -255,6 +257,29 @@ public class TypesUploadServlet extends AbstractValidationsServlet { 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) { + 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); + final Wrapper responseWrapper = doUploadTypeValidations(request, userId, file); + if (responseWrapper.isEmpty()) { + fillZipContents(yamlStringWrapper, file); + } + if (responseWrapper.isEmpty()) { + createElementsMethod.accept(responseWrapper, yamlStringWrapper.getInnerElement(), modelName); + } + return responseWrapper.getInnerElement(); + } catch (final Exception e) { + log.debug("create {} failed with exception:", elementTypeName, e); + BeEcompErrorManager.getInstance().logBeRestApiGeneralError(CREATE + elementTypeName); + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); + } + } private Wrapper doUploadTypeValidations(final HttpServletRequest request, String userId, File file) { Wrapper responseWrapper = new Wrapper<>(); @@ -384,9 +409,9 @@ public class TypesUploadServlet extends AbstractValidationsServlet { } // relationship types - private void createRelationshipTypes(Wrapper responseWrapper, String relationshipTypesYml) { + private void createRelationshipTypes(final Wrapper responseWrapper, final String relationshipTypesYml, final String modelName) { final Supplier>, ResponseFormat>> generateElementTypeFromYml = () -> relationshipTypeImportManager - .createRelationshipTypes(relationshipTypesYml); + .createRelationshipTypes(relationshipTypesYml, modelName); buildStatusForElementTypeCreate(responseWrapper, generateElementTypeFromYml, ActionStatus.RELATIONSHIP_TYPE_ALREADY_EXIST, NodeTypeEnum.RelationshipType.name()); } -- cgit 1.2.3-korg