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.annotations.*;
26 import org.apache.commons.lang3.tuple.ImmutablePair;
27 import org.glassfish.jersey.media.multipart.FormDataParam;
28 import org.openecomp.sdc.be.components.impl.CommonImportManager;
29 import org.openecomp.sdc.be.components.validation.AccessValidations;
30 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
31 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
32 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
33 import org.openecomp.sdc.be.utils.TypeUtils;
34 import org.openecomp.sdc.common.datastructure.Wrapper;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.stereotype.Controller;
39 import javax.ws.rs.core.MediaType;
40 import javax.ws.rs.core.Response;
42 import java.io.IOException;
43 import java.util.List;
47 * Here new APIs for types upload written in an attempt to gradually servlet code
49 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
50 @Path("/v1/catalog/uploadType")
51 @Consumes(MediaType.MULTIPART_FORM_DATA)
52 @Produces(MediaType.APPLICATION_JSON)
53 @Api(value = "Catalog Types Upload")
55 public class TypesUploadEndpoint {
57 private final CommonImportManager commonImportManager;
58 private final AnnotationTypeOperations annotationTypeOperations;
59 private final AccessValidations accessValidations;
61 public TypesUploadEndpoint(CommonImportManager commonImportManager, AnnotationTypeOperations annotationTypeOperations, AccessValidations accessValidations) {
62 this.commonImportManager = commonImportManager;
63 this.annotationTypeOperations = annotationTypeOperations;
64 this.accessValidations = accessValidations;
68 @Path("/annotationtypes")
69 @ApiOperation(value = "Create AnnotationTypes from yaml", httpMethod = "POST", notes = "Returns created annotation types", response = Response.class)
70 @ApiResponses(value = {
71 @ApiResponse(code = 201, message = "annotation types created"),
72 @ApiResponse(code = 403, message = "Restricted operation"),
73 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
74 @ApiResponse(code = 409, message = "annotation types already exist")})
75 public Response uploadAnnotationTypes(
76 @ApiParam("FileInputStream") @FormDataParam("annotationTypesZip") File file,
77 @HeaderParam("USER_ID") String userId) throws IOException {
78 accessValidations.validateUserExists(userId, "Annotation Types Creation");
79 Wrapper<String> yamlStringWrapper = new Wrapper<>();
80 AbstractValidationsServlet.extractZipContents(yamlStringWrapper, file);
81 List<ImmutablePair<AnnotationTypeDefinition, Boolean>> typesResults = commonImportManager.createElementTypes(yamlStringWrapper.getInnerElement(), TypesUploadEndpoint::buildAnnotationFromFieldMap, annotationTypeOperations);
82 HttpStatus status = getHttpStatus(typesResults);
83 return Response.status(status.value())
89 static <T extends ToscaDataDefinition> HttpStatus getHttpStatus(List<ImmutablePair<T, Boolean>> typesResults) {
90 boolean typeActionFailed = false;
91 boolean typeExists = false;
92 boolean typeActionSucceeded = false;
93 for (ImmutablePair<T, Boolean> typeResult : typesResults) {
94 Boolean result = typeResult.getRight();
98 typeActionSucceeded = true;
100 typeActionFailed = true;
103 HttpStatus status = HttpStatus.OK;
104 if (typeActionFailed) {
105 status = HttpStatus.BAD_REQUEST;
106 } else if (typeActionSucceeded) {
107 status = HttpStatus.CREATED;
108 } else if (typeExists) {
109 status = HttpStatus.CONFLICT;
114 private static <T extends ToscaDataDefinition> T buildAnnotationFromFieldMap(String typeName, Map<String, Object> toscaJson) {
115 AnnotationTypeDefinition annotationType = new AnnotationTypeDefinition();
116 annotationType.setVersion(TypeUtils.FIRST_CERTIFIED_VERSION_VERSION);
117 annotationType.setHighestVersion(true);
118 annotationType.setType(typeName);
119 TypeUtils.setField(toscaJson, TypeUtils.ToscaTagNamesEnum.DESCRIPTION, annotationType::setDescription);
120 CommonImportManager.setProperties(toscaJson, annotationType::setProperties);
121 return (T) annotationType;