X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=catalog-be%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fsdc%2Fbe%2Fservlets%2FResourceUploadServlet.java;h=6733267f4e06ac113c2b93e3d61734adf60c4190;hb=5f3e9912406897ee18c424b940881ce08d59bb44;hp=e7966e0d08a3e4b169fb6bcd2c431942240c7318;hpb=53df976426f8845adf58e8ff9355764343a38549;p=sdc.git diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourceUploadServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourceUploadServlet.java index e7966e0d08..6733267f4e 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourceUploadServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ResourceUploadServlet.java @@ -20,6 +20,7 @@ package org.openecomp.sdc.be.servlets; import com.jcabi.aspects.Loggable; +import fj.data.Either; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.ArraySchema; @@ -29,8 +30,12 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.servers.Server; import io.swagger.v3.oas.annotations.tags.Tag; import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; +import javax.validation.constraints.NotNull; import javax.ws.rs.Consumes; import javax.ws.rs.DefaultValue; import javax.ws.rs.HeaderParam; @@ -47,19 +52,19 @@ import org.glassfish.jersey.media.multipart.FormDataParam; import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic; import org.openecomp.sdc.be.components.impl.ModelBusinessLogic; 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.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.exception.BusinessException; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.impl.ServletUtils; +import org.openecomp.sdc.be.model.NodeTypesMetadataList; import org.openecomp.sdc.be.model.UploadResourceInfo; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier; -import org.openecomp.sdc.be.user.UserBusinessLogic; import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.common.datastructure.Wrapper; +import org.openecomp.sdc.common.util.ValidationUtils; +import org.openecomp.sdc.exception.ResponseFormat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; @@ -80,13 +85,13 @@ public class ResourceUploadServlet extends AbstractValidationsServlet { public static final String USER_TYPE_RESOURCE_UI_IMPORT = "user-resource-ui-import"; private static final Logger log = LoggerFactory.getLogger(ResourceUploadServlet.class); - private ModelBusinessLogic modelBusinessLogic; + private final ModelBusinessLogic modelBusinessLogic; @Inject - public ResourceUploadServlet(UserBusinessLogic userBusinessLogic, ComponentInstanceBusinessLogic componentInstanceBL, + public ResourceUploadServlet(ComponentInstanceBusinessLogic componentInstanceBL, ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager, ModelBusinessLogic modelBusinessLogic) { - super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager); + super(componentInstanceBL, componentsUtils, servletUtils, resourceImportManager); this.modelBusinessLogic = modelBusinessLogic; } @@ -100,7 +105,6 @@ public class ResourceUploadServlet extends AbstractValidationsServlet { @ApiResponse(responseCode = "403", description = "Restricted operation"), @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"), @ApiResponse(responseCode = "409", description = "Resource already exist")}) - @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) public Response uploadMultipart( @Parameter(description = "validValues: normative-resource / user-resource", schema = @Schema(allowableValues = {NORMATIVE_TYPE_RESOURCE, USER_TYPE_RESOURCE, USER_TYPE_RESOURCE_UI_IMPORT})) @PathParam(value = "resourceAuthority") final String resourceAuthority, @@ -150,8 +154,60 @@ public class ResourceUploadServlet extends AbstractValidationsServlet { } } + @POST + @Path("/resource/import") + @Consumes(MediaType.MULTIPART_FORM_DATA) + @Produces(MediaType.APPLICATION_JSON) + @Operation(description = "Import node types from a TOSCA yaml, along with the types metadata", method = "POST", + summary = "Creates node types from a TOSCA yaml file", responses = { + @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))), + @ApiResponse(responseCode = "201", description = "Resources created"), + @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"), + @ApiResponse(responseCode = "403", description = "Restricted operation"), + @ApiResponse(responseCode = "409", description = "One of the resources already exists")} + ) + public Response bulkImport(@Parameter(description = "The nodes metadata JSON", required = true) + @NotNull @FormDataParam("nodeTypeMetadataJson") final NodeTypesMetadataList nodeTypeMetadata, + @Parameter(description = "The node types TOSCA definition yaml", required = true) + @NotNull @FormDataParam("nodeTypesYaml") final InputStream nodeTypesYamlInputStream, + @Parameter(description = "The model name to associate the node types to") + @DefaultValue("true") @FormDataParam("createNewVersion") boolean createNewVersion, + @HeaderParam(value = Constants.USER_ID_HEADER) String userId, + @Context final HttpServletRequest request) { + userId = ValidationUtils.sanitizeInputString(userId); + final Either userEither = getUser(request, userId); + if (userEither.isRight()) { + return buildErrorResponse(userEither.right().value()); + } + + final User user = userEither.left().value(); + + final String nodeTypesYamlString; + try { + nodeTypesYamlString = new String(nodeTypesYamlInputStream.readAllBytes(), StandardCharsets.UTF_8); + } catch (final IOException e) { + var errorMsg = "Could not read the given node types yaml"; + BeEcompErrorManager.getInstance().logBeRestApiGeneralError(errorMsg); + log.error(errorMsg, e); + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_NODE_TYPES_YAML)); + } + + try { + resourceImportManager.importAllNormativeResource(nodeTypesYamlString, nodeTypeMetadata, user, createNewVersion, false); + return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), null); + } catch (final BusinessException e) { + throw e; + } catch (final Exception e) { + var errorMsg = "Unexpected error while importing the node types"; + BeEcompErrorManager.getInstance().logBeRestApiGeneralError(errorMsg); + log.error(errorMsg, e); + return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR)); + } + } + /** * The Model field is an optional entry when uploading a resource. If the field is present, it validates if the Model name exists. + * * @param modelName Model names declared on the resource json representation */ private void validateModel(final String modelName) {