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