diff options
Diffstat (limited to 'catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadEndpoint.java')
-rw-r--r-- | catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadEndpoint.java | 87 |
1 files changed, 41 insertions, 46 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadEndpoint.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadEndpoint.java index b0b897daf3..8d3f58e665 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadEndpoint.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/TypesUploadEndpoint.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,7 +17,6 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.be.servlets; import com.google.common.annotations.VisibleForTesting; @@ -32,6 +31,16 @@ 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.util.List; +import java.util.Map; +import javax.ws.rs.Consumes; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.apache.commons.lang3.tuple.ImmutablePair; import org.glassfish.jersey.media.multipart.FormDataParam; import org.openecomp.sdc.be.components.impl.CommonImportManager; @@ -51,16 +60,6 @@ import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; -import javax.ws.rs.Consumes; -import javax.ws.rs.HeaderParam; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.io.File; -import java.util.List; -import java.util.Map; /** * Here new APIs for types upload written in an attempt to gradually servlet code */ @@ -71,47 +70,21 @@ import java.util.Map; @Tags({@Tag(name = "SDC Internal APIs")}) @Servers({@Server(url = "/sdc2/rest")}) @Controller -public class TypesUploadEndpoint extends BeGenericServlet{ - private static final Logger LOGGER = LoggerFactory.getLogger(TypesUploadEndpoint.class); +public class TypesUploadEndpoint extends BeGenericServlet { + private static final Logger LOGGER = LoggerFactory.getLogger(TypesUploadEndpoint.class); private final CommonImportManager commonImportManager; private final AnnotationTypeOperations annotationTypeOperations; private final AccessValidations accessValidations; - public TypesUploadEndpoint(UserBusinessLogic userBusinessLogic, - ComponentsUtils componentsUtils, CommonImportManager commonImportManager, AnnotationTypeOperations annotationTypeOperations, AccessValidations accessValidations) { + public TypesUploadEndpoint(UserBusinessLogic userBusinessLogic, ComponentsUtils componentsUtils, CommonImportManager commonImportManager, + AnnotationTypeOperations annotationTypeOperations, AccessValidations accessValidations) { super(userBusinessLogic, componentsUtils); this.commonImportManager = commonImportManager; this.annotationTypeOperations = annotationTypeOperations; this.accessValidations = accessValidations; } - @POST - @Path("/annotationtypes") - @Operation(description = "Create AnnotationTypes from yaml", method = "POST", - summary = "Returns created annotation types", responses = { - @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))), - @ApiResponse(responseCode = "201", description = "annotation types created"), - @ApiResponse(responseCode = "403", description = "Restricted operation"), - @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"), - @ApiResponse(responseCode = "409", description = "annotation types already exist")}) - @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) - public Response uploadAnnotationTypes(@Parameter(description = "FileInputStream") @FormDataParam("annotationTypesZip") File file, - @HeaderParam("USER_ID") String userId) { - accessValidations.validateUserExists(userId, "Annotation Types Creation"); - final Wrapper<String> yamlStringWrapper = new Wrapper<>(); - try { - AbstractValidationsServlet.extractZipContents(yamlStringWrapper, file); - } catch (final ZipException e) { - LOGGER.error("Could not extract zip contents", e); - } - List<ImmutablePair<AnnotationTypeDefinition, Boolean>> typesResults = commonImportManager.createElementTypes(yamlStringWrapper.getInnerElement(), TypesUploadEndpoint::buildAnnotationFromFieldMap, annotationTypeOperations); - HttpStatus status = getHttpStatus(typesResults); - return Response.status(status.value()) - .entity(typesResults) - .build(); - } - @VisibleForTesting static <T extends ToscaDataDefinition> HttpStatus getHttpStatus(List<ImmutablePair<T, Boolean>> typesResults) { boolean typeActionFailed = false; @@ -119,7 +92,7 @@ public class TypesUploadEndpoint extends BeGenericServlet{ boolean typeActionSucceeded = false; for (ImmutablePair<T, Boolean> typeResult : typesResults) { Boolean result = typeResult.getRight(); - if (result==null) { + if (result == null) { typeExists = true; } else if (result) { typeActionSucceeded = true; @@ -129,7 +102,7 @@ public class TypesUploadEndpoint extends BeGenericServlet{ } HttpStatus status = HttpStatus.OK; if (typeActionFailed) { - status = HttpStatus.BAD_REQUEST; + status = HttpStatus.BAD_REQUEST; } else if (typeActionSucceeded) { status = HttpStatus.CREATED; } else if (typeExists) { @@ -148,5 +121,27 @@ public class TypesUploadEndpoint extends BeGenericServlet{ return (T) annotationType; } - + @POST + @Path("/annotationtypes") + @Operation(description = "Create AnnotationTypes from yaml", method = "POST", summary = "Returns created annotation types", responses = { + @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))), + @ApiResponse(responseCode = "201", description = "annotation types created"), + @ApiResponse(responseCode = "403", description = "Restricted operation"), + @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"), + @ApiResponse(responseCode = "409", description = "annotation types already exist")}) + @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE) + public Response uploadAnnotationTypes(@Parameter(description = "FileInputStream") @FormDataParam("annotationTypesZip") File file, + @HeaderParam("USER_ID") String userId) { + accessValidations.validateUserExists(userId, "Annotation Types Creation"); + final Wrapper<String> yamlStringWrapper = new Wrapper<>(); + try { + AbstractValidationsServlet.extractZipContents(yamlStringWrapper, file); + } catch (final ZipException e) { + LOGGER.error("Could not extract zip contents", e); + } + List<ImmutablePair<AnnotationTypeDefinition, Boolean>> typesResults = commonImportManager + .createElementTypes(yamlStringWrapper.getInnerElement(), TypesUploadEndpoint::buildAnnotationFromFieldMap, annotationTypeOperations); + HttpStatus status = getHttpStatus(typesResults); + return Response.status(status.value()).entity(typesResults).build(); + } } |