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.OpenAPIDefinition;
26 import io.swagger.v3.oas.annotations.Operation;
27 import io.swagger.v3.oas.annotations.Parameter;
28 import io.swagger.v3.oas.annotations.info.Info;
29 import io.swagger.v3.oas.annotations.media.ArraySchema;
30 import io.swagger.v3.oas.annotations.media.Content;
31 import io.swagger.v3.oas.annotations.media.Schema;
32 import io.swagger.v3.oas.annotations.responses.ApiResponse;
33 import io.swagger.v3.oas.annotations.responses.ApiResponses;
34 import org.apache.commons.lang3.tuple.ImmutablePair;
35 import org.glassfish.jersey.media.multipart.FormDataParam;
36 import org.openecomp.sdc.be.components.impl.CommonImportManager;
37 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
38 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
39 import org.openecomp.sdc.be.components.validation.AccessValidations;
40 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
41 import org.openecomp.sdc.be.impl.ComponentsUtils;
42 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
43 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
44 import org.openecomp.sdc.be.user.UserBusinessLogic;
45 import org.openecomp.sdc.be.utils.TypeUtils;
46 import org.openecomp.sdc.common.datastructure.Wrapper;
47 import org.openecomp.sdc.common.zip.exception.ZipException;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.http.HttpStatus;
51 import org.springframework.stereotype.Controller;
53 import javax.ws.rs.Consumes;
54 import javax.ws.rs.HeaderParam;
55 import javax.ws.rs.POST;
56 import javax.ws.rs.Path;
57 import javax.ws.rs.Produces;
58 import javax.ws.rs.core.MediaType;
59 import javax.ws.rs.core.Response;
61 import java.util.List;
64 * Here new APIs for types upload written in an attempt to gradually servlet code
66 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
67 @Path("/v1/catalog/uploadType")
68 @Consumes(MediaType.MULTIPART_FORM_DATA)
69 @Produces(MediaType.APPLICATION_JSON)
70 @OpenAPIDefinition(info = @Info(title = "Catalog Types Upload"))
72 public class TypesUploadEndpoint extends BeGenericServlet{
73 private static final Logger LOGGER = LoggerFactory.getLogger(TypesUploadEndpoint.class);
75 private final CommonImportManager commonImportManager;
76 private final AnnotationTypeOperations annotationTypeOperations;
77 private final AccessValidations accessValidations;
79 public TypesUploadEndpoint(UserBusinessLogic userBusinessLogic,
80 ComponentsUtils componentsUtils, CommonImportManager commonImportManager, AnnotationTypeOperations annotationTypeOperations, AccessValidations accessValidations) {
81 super(userBusinessLogic, componentsUtils);
82 this.commonImportManager = commonImportManager;
83 this.annotationTypeOperations = annotationTypeOperations;
84 this.accessValidations = accessValidations;
88 @Path("/annotationtypes")
89 @Operation(description = "Create AnnotationTypes from yaml", method = "POST",
90 summary = "Returns created annotation types",responses = @ApiResponse(
91 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
92 @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "annotation types created"),
93 @ApiResponse(responseCode = "403", description = "Restricted operation"),
94 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
95 @ApiResponse(responseCode = "409", description = "annotation types already exist")})
96 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
97 public Response uploadAnnotationTypes(@Parameter(description = "FileInputStream") @FormDataParam("annotationTypesZip") File file,
98 @HeaderParam("USER_ID") String userId) {
99 accessValidations.validateUserExists(userId, "Annotation Types Creation");
100 final Wrapper<String> yamlStringWrapper = new Wrapper<>();
102 AbstractValidationsServlet.extractZipContents(yamlStringWrapper, file);
103 } catch (final ZipException e) {
104 LOGGER.error("Could not extract zip contents", e);
106 List<ImmutablePair<AnnotationTypeDefinition, Boolean>> typesResults = commonImportManager.createElementTypes(yamlStringWrapper.getInnerElement(), TypesUploadEndpoint::buildAnnotationFromFieldMap, annotationTypeOperations);
107 HttpStatus status = getHttpStatus(typesResults);
108 return Response.status(status.value())
109 .entity(typesResults)
114 static <T extends ToscaDataDefinition> HttpStatus getHttpStatus(List<ImmutablePair<T, Boolean>> typesResults) {
115 boolean typeActionFailed = false;
116 boolean typeExists = false;
117 boolean typeActionSucceeded = false;
118 for (ImmutablePair<T, Boolean> typeResult : typesResults) {
119 Boolean result = typeResult.getRight();
123 typeActionSucceeded = true;
125 typeActionFailed = true;
128 HttpStatus status = HttpStatus.OK;
129 if (typeActionFailed) {
130 status = HttpStatus.BAD_REQUEST;
131 } else if (typeActionSucceeded) {
132 status = HttpStatus.CREATED;
133 } else if (typeExists) {
134 status = HttpStatus.CONFLICT;
139 private static <T extends ToscaDataDefinition> T buildAnnotationFromFieldMap(String typeName, Map<String, Object> toscaJson) {
140 AnnotationTypeDefinition annotationType = new AnnotationTypeDefinition();
141 annotationType.setVersion(TypeUtils.getFirstCertifiedVersionVersion());
142 annotationType.setHighestVersion(true);
143 annotationType.setType(typeName);
144 TypeUtils.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, annotationType::setDescription);
145 CommonImportManager.setProperties(toscaJson, annotationType::setProperties);
146 return (T) annotationType;