2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.jcabi.aspects.Loggable;
24 import io.swagger.v3.oas.annotations.OpenAPIDefinition;
25 import io.swagger.v3.oas.annotations.Operation;
26 import io.swagger.v3.oas.annotations.Parameter;
27 import io.swagger.v3.oas.annotations.info.Info;
28 import io.swagger.v3.oas.annotations.media.ArraySchema;
29 import io.swagger.v3.oas.annotations.media.Content;
30 import io.swagger.v3.oas.annotations.media.Schema;
31 import io.swagger.v3.oas.annotations.responses.ApiResponse;
32 import io.swagger.v3.oas.annotations.responses.ApiResponses;
33 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
34 import org.glassfish.jersey.media.multipart.FormDataParam;
35 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
36 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
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.config.BeEcompErrorManager;
40 import org.openecomp.sdc.be.impl.ComponentsUtils;
41 import org.openecomp.sdc.be.impl.ServletUtils;
42 import org.openecomp.sdc.be.model.UploadResourceInfo;
43 import org.openecomp.sdc.be.model.User;
44 import org.openecomp.sdc.be.user.UserBusinessLogic;
45 import org.openecomp.sdc.common.api.Constants;
46 import org.openecomp.sdc.common.datastructure.Wrapper;
47 import org.openecomp.sdc.common.log.wrappers.Logger;
48 import org.openecomp.sdc.common.zip.exception.ZipException;
49 import org.springframework.stereotype.Controller;
51 import javax.inject.Inject;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.ws.rs.Consumes;
54 import javax.ws.rs.DefaultValue;
55 import javax.ws.rs.HeaderParam;
56 import javax.ws.rs.POST;
57 import javax.ws.rs.Path;
58 import javax.ws.rs.PathParam;
59 import javax.ws.rs.Produces;
60 import javax.ws.rs.QueryParam;
61 import javax.ws.rs.core.Context;
62 import javax.ws.rs.core.MediaType;
63 import javax.ws.rs.core.Response;
65 import java.io.FileNotFoundException;
68 * Root resource (exposed at "/" path)
70 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
71 @Path("/v1/catalog/upload")
72 @OpenAPIDefinition(info = @Info(title = "Resources Catalog Upload", description = "Upload resource yaml"))
74 public class ResourceUploadServlet extends AbstractValidationsServlet {
76 private static final Logger log = Logger.getLogger(ResourceUploadServlet.class);
77 public static final String NORMATIVE_TYPE_RESOURCE = "multipart";
78 public static final String CSAR_TYPE_RESOURCE = "csar";
79 public static final String USER_TYPE_RESOURCE = "user-resource";
80 public static final String USER_TYPE_RESOURCE_UI_IMPORT = "user-resource-ui-import";
83 public ResourceUploadServlet(UserBusinessLogic userBusinessLogic,
84 ComponentInstanceBusinessLogic componentInstanceBL,
85 ComponentsUtils componentsUtils, ServletUtils servletUtils,
86 ResourceImportManager resourceImportManager) {
87 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
90 public enum ResourceAuthorityTypeEnum {
91 NORMATIVE_TYPE_BE(NORMATIVE_TYPE_RESOURCE, true, false), USER_TYPE_BE(USER_TYPE_RESOURCE, true, true), USER_TYPE_UI(USER_TYPE_RESOURCE_UI_IMPORT, false, true), CSAR_TYPE_BE(CSAR_TYPE_RESOURCE, true, true);
93 private String urlPath;
94 private boolean isBackEndImport, isUserTypeResource;
96 public static ResourceAuthorityTypeEnum findByUrlPath(String urlPath) {
97 ResourceAuthorityTypeEnum found = null;
98 for (ResourceAuthorityTypeEnum curr : ResourceAuthorityTypeEnum.values()) {
99 if (curr.getUrlPath().equals(urlPath)) {
107 private ResourceAuthorityTypeEnum(String urlPath, boolean isBackEndImport, boolean isUserTypeResource) {
108 this.urlPath = urlPath;
109 this.isBackEndImport = isBackEndImport;
110 this.isUserTypeResource = isUserTypeResource;
113 public String getUrlPath() {
117 public boolean isBackEndImport() {
118 return isBackEndImport;
121 public boolean isUserTypeResource() {
122 return isUserTypeResource;
127 @Path("/{resourceAuthority}")
128 @Consumes(MediaType.MULTIPART_FORM_DATA)
129 @Produces(MediaType.APPLICATION_JSON)
130 @Operation(description = "Create Resource from yaml", method = "POST", summary = "Returns created resource",
131 responses = @ApiResponse(
132 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
133 @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Resource created"),
134 @ApiResponse(responseCode = "403", description = "Restricted operation"),
135 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
136 @ApiResponse(responseCode = "409", description = "Resource already exist")})
137 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
138 public Response uploadMultipart(
139 @Parameter(description = "validValues: normative-resource / user-resource",
140 schema = @Schema(allowableValues = {NORMATIVE_TYPE_RESOURCE ,
141 USER_TYPE_RESOURCE,USER_TYPE_RESOURCE_UI_IMPORT})) @PathParam(
142 value = "resourceAuthority") final String resourceAuthority,
143 @Parameter(description = "FileInputStream") @FormDataParam("resourceZip") File file,
144 @Parameter(description = "ContentDisposition") @FormDataParam("resourceZip") FormDataContentDisposition contentDispositionHeader,
145 @Parameter(description = "resourceMetadata") @FormDataParam("resourceMetadata") String resourceInfoJsonString,
146 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
147 // updateResourse Query Parameter if false checks if already exist
148 @DefaultValue("true") @QueryParam("createNewVersion") boolean createNewVersion) throws FileNotFoundException, ZipException {
152 Wrapper<Response> responseWrapper = new Wrapper<>();
153 Wrapper<User> userWrapper = new Wrapper<>();
154 Wrapper<UploadResourceInfo> uploadResourceInfoWrapper = new Wrapper<>();
155 Wrapper<String> yamlStringWrapper = new Wrapper<>();
157 String url = request.getMethod() + " " + request.getRequestURI();
158 log.debug("Start handle request of {}", url);
160 // When we get an errorResponse it will be filled into the
162 validateAuthorityType(responseWrapper, resourceAuthority);
164 ResourceAuthorityTypeEnum resourceAuthorityEnum = ResourceAuthorityTypeEnum.findByUrlPath(resourceAuthority);
166 commonGeneralValidations(responseWrapper, userWrapper, uploadResourceInfoWrapper, resourceAuthorityEnum, userId, resourceInfoJsonString);
168 fillPayload(responseWrapper, uploadResourceInfoWrapper, yamlStringWrapper, userWrapper.getInnerElement(), resourceInfoJsonString, resourceAuthorityEnum, file);
170 // PayLoad Validations
171 if(resourceAuthorityEnum != ResourceAuthorityTypeEnum.CSAR_TYPE_BE){
172 commonPayloadValidations(responseWrapper, yamlStringWrapper, userWrapper.getInnerElement(), uploadResourceInfoWrapper.getInnerElement());
174 specificResourceAuthorityValidations(responseWrapper, uploadResourceInfoWrapper, yamlStringWrapper, userWrapper.getInnerElement(), request, resourceInfoJsonString, resourceAuthorityEnum);
177 if (responseWrapper.isEmpty()) {
178 handleImport(responseWrapper, userWrapper.getInnerElement(), uploadResourceInfoWrapper.getInnerElement(), yamlStringWrapper.getInnerElement(), resourceAuthorityEnum, createNewVersion, null);
181 return responseWrapper.getInnerElement();
183 } catch (Exception e) {
184 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Upload Resource");
185 log.debug("upload resource failed with exception", e);