2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import com.google.common.annotations.VisibleForTesting;
24 import com.jcabi.aspects.Loggable;
25 import io.swagger.v3.oas.annotations.Operation;
26 import io.swagger.v3.oas.annotations.Parameter;
27 import io.swagger.v3.oas.annotations.media.ArraySchema;
28 import io.swagger.v3.oas.annotations.media.Content;
29 import io.swagger.v3.oas.annotations.media.Schema;
30 import io.swagger.v3.oas.annotations.responses.ApiResponse;
31 import io.swagger.v3.oas.annotations.servers.Server;
32 import io.swagger.v3.oas.annotations.servers.Servers;
33 import io.swagger.v3.oas.annotations.tags.Tag;
34 import io.swagger.v3.oas.annotations.tags.Tags;
35 import org.apache.commons.lang3.tuple.ImmutablePair;
36 import org.glassfish.jersey.media.multipart.FormDataParam;
37 import org.openecomp.sdc.be.components.impl.CommonImportManager;
38 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
39 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
40 import org.openecomp.sdc.be.components.validation.AccessValidations;
41 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
42 import org.openecomp.sdc.be.impl.ComponentsUtils;
43 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
44 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
45 import org.openecomp.sdc.be.user.UserBusinessLogic;
46 import org.openecomp.sdc.be.utils.TypeUtils;
47 import org.openecomp.sdc.common.datastructure.Wrapper;
48 import org.openecomp.sdc.common.zip.exception.ZipException;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.http.HttpStatus;
52 import org.springframework.stereotype.Controller;
54 import javax.ws.rs.Consumes;
55 import javax.ws.rs.HeaderParam;
56 import javax.ws.rs.POST;
57 import javax.ws.rs.Path;
58 import javax.ws.rs.Produces;
59 import javax.ws.rs.core.MediaType;
60 import javax.ws.rs.core.Response;
62 import java.util.List;
65 * Here new APIs for types upload written in an attempt to gradually servlet code
67 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
68 @Path("/v1/catalog/uploadType")
69 @Consumes(MediaType.MULTIPART_FORM_DATA)
70 @Produces(MediaType.APPLICATION_JSON)
71 @Tags({@Tag(name = "SDCE-2 APIs")})
72 @Servers({@Server(url = "/sdc2/rest")})
74 public class TypesUploadEndpoint extends BeGenericServlet{
75 private static final Logger LOGGER = LoggerFactory.getLogger(TypesUploadEndpoint.class);
77 private final CommonImportManager commonImportManager;
78 private final AnnotationTypeOperations annotationTypeOperations;
79 private final AccessValidations accessValidations;
81 public TypesUploadEndpoint(UserBusinessLogic userBusinessLogic,
82 ComponentsUtils componentsUtils, CommonImportManager commonImportManager, AnnotationTypeOperations annotationTypeOperations, AccessValidations accessValidations) {
83 super(userBusinessLogic, componentsUtils);
84 this.commonImportManager = commonImportManager;
85 this.annotationTypeOperations = annotationTypeOperations;
86 this.accessValidations = accessValidations;
90 @Path("/annotationtypes")
91 @Operation(description = "Create AnnotationTypes from yaml", method = "POST",
92 summary = "Returns created annotation types", responses = {
93 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
94 @ApiResponse(responseCode = "201", description = "annotation types created"),
95 @ApiResponse(responseCode = "403", description = "Restricted operation"),
96 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
97 @ApiResponse(responseCode = "409", description = "annotation types already exist")})
98 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
99 public Response uploadAnnotationTypes(@Parameter(description = "FileInputStream") @FormDataParam("annotationTypesZip") File file,
100 @HeaderParam("USER_ID") String userId) {
101 accessValidations.validateUserExists(userId, "Annotation Types Creation");
102 final Wrapper<String> yamlStringWrapper = new Wrapper<>();
104 AbstractValidationsServlet.extractZipContents(yamlStringWrapper, file);
105 } catch (final ZipException e) {
106 LOGGER.error("Could not extract zip contents", e);
108 List<ImmutablePair<AnnotationTypeDefinition, Boolean>> typesResults = commonImportManager.createElementTypes(yamlStringWrapper.getInnerElement(), TypesUploadEndpoint::buildAnnotationFromFieldMap, annotationTypeOperations);
109 HttpStatus status = getHttpStatus(typesResults);
110 return Response.status(status.value())
111 .entity(typesResults)
116 static <T extends ToscaDataDefinition> HttpStatus getHttpStatus(List<ImmutablePair<T, Boolean>> typesResults) {
117 boolean typeActionFailed = false;
118 boolean typeExists = false;
119 boolean typeActionSucceeded = false;
120 for (ImmutablePair<T, Boolean> typeResult : typesResults) {
121 Boolean result = typeResult.getRight();
125 typeActionSucceeded = true;
127 typeActionFailed = true;
130 HttpStatus status = HttpStatus.OK;
131 if (typeActionFailed) {
132 status = HttpStatus.BAD_REQUEST;
133 } else if (typeActionSucceeded) {
134 status = HttpStatus.CREATED;
135 } else if (typeExists) {
136 status = HttpStatus.CONFLICT;
141 private static <T extends ToscaDataDefinition> T buildAnnotationFromFieldMap(String typeName, Map<String, Object> toscaJson) {
142 AnnotationTypeDefinition annotationType = new AnnotationTypeDefinition();
143 annotationType.setVersion(TypeUtils.getFirstCertifiedVersionVersion());
144 annotationType.setHighestVersion(true);
145 annotationType.setType(typeName);
146 TypeUtils.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, annotationType::setDescription);
147 CommonImportManager.setProperties(toscaJson, annotationType::setProperties);
148 return (T) annotationType;