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.annotations.*;
25 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
26 import org.glassfish.jersey.media.multipart.FormDataParam;
27 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
28 import org.openecomp.sdc.be.config.BeEcompErrorManager;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
31 import org.openecomp.sdc.be.model.UploadResourceInfo;
32 import org.openecomp.sdc.be.model.User;
33 import org.openecomp.sdc.common.api.Constants;
34 import org.openecomp.sdc.common.datastructure.Wrapper;
35 import org.openecomp.sdc.common.log.wrappers.Logger;
36 import org.springframework.web.context.WebApplicationContext;
38 import javax.inject.Singleton;
39 import javax.servlet.ServletContext;
40 import javax.servlet.http.HttpServletRequest;
42 import javax.ws.rs.core.Context;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
48 * Root resource (exposed at "/" path)
50 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
51 @Path("/v1/catalog/upload")
52 @Api(value = "Resources Catalog Upload", description = "Upload resource yaml")
54 public class ResourceUploadServlet extends AbstractValidationsServlet {
56 private static final Logger log = Logger.getLogger(ResourceUploadServlet.class);
57 public static final String NORMATIVE_TYPE_RESOURCE = "multipart";
58 public static final String CSAR_TYPE_RESOURCE = "csar";
59 public static final String USER_TYPE_RESOURCE = "user-resource";
60 public static final String USER_TYPE_RESOURCE_UI_IMPORT = "user-resource-ui-import";
62 public enum ResourceAuthorityTypeEnum {
63 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);
65 private String urlPath;
66 private boolean isBackEndImport, isUserTypeResource;
68 public static ResourceAuthorityTypeEnum findByUrlPath(String urlPath) {
69 ResourceAuthorityTypeEnum found = null;
70 for (ResourceAuthorityTypeEnum curr : ResourceAuthorityTypeEnum.values()) {
71 if (curr.getUrlPath().equals(urlPath)) {
79 private ResourceAuthorityTypeEnum(String urlPath, boolean isBackEndImport, boolean isUserTypeResource) {
80 this.urlPath = urlPath;
81 this.isBackEndImport = isBackEndImport;
82 this.isUserTypeResource = isUserTypeResource;
85 public String getUrlPath() {
89 public boolean isBackEndImport() {
90 return isBackEndImport;
93 public boolean isUserTypeResource() {
94 return isUserTypeResource;
99 @Path("/{resourceAuthority}")
100 @Consumes(MediaType.MULTIPART_FORM_DATA)
101 @Produces(MediaType.APPLICATION_JSON)
102 @ApiOperation(value = "Create Resource from yaml", httpMethod = "POST", notes = "Returns created resource", response = Response.class)
103 @ApiResponses(value = { @ApiResponse(code = 201, message = "Resource created"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 400, message = "Invalid content / Missing content"),
104 @ApiResponse(code = 409, message = "Resource already exist") })
105 public Response uploadMultipart(
106 @ApiParam(value = "validValues: normative-resource / user-resource", allowableValues = NORMATIVE_TYPE_RESOURCE + "," + USER_TYPE_RESOURCE + ","
107 + USER_TYPE_RESOURCE_UI_IMPORT) @PathParam(value = "resourceAuthority") final String resourceAuthority,
108 @ApiParam("FileInputStream") @FormDataParam("resourceZip") File file, @ApiParam("ContentDisposition") @FormDataParam("resourceZip") FormDataContentDisposition contentDispositionHeader,
109 @ApiParam("resourceMetadata") @FormDataParam("resourceMetadata") String resourceInfoJsonString, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
110 // updateResourse Query Parameter if false checks if already exist
111 @DefaultValue("true") @QueryParam("createNewVersion") boolean createNewVersion) {
113 init(request.getSession().getServletContext());
116 Wrapper<Response> responseWrapper = new Wrapper<>();
117 Wrapper<User> userWrapper = new Wrapper<>();
118 Wrapper<UploadResourceInfo> uploadResourceInfoWrapper = new Wrapper<>();
119 Wrapper<String> yamlStringWrapper = new Wrapper<>();
121 String url = request.getMethod() + " " + request.getRequestURI();
122 log.debug("Start handle request of {}", url);
124 // When we get an errorResponse it will be filled into the
126 validateAuthorityType(responseWrapper, resourceAuthority);
128 ResourceAuthorityTypeEnum resourceAuthorityEnum = ResourceAuthorityTypeEnum.findByUrlPath(resourceAuthority);
130 commonGeneralValidations(responseWrapper, userWrapper, uploadResourceInfoWrapper, resourceAuthorityEnum, userId, resourceInfoJsonString);
132 fillPayload(responseWrapper, uploadResourceInfoWrapper, yamlStringWrapper, userWrapper.getInnerElement(), resourceInfoJsonString, resourceAuthorityEnum, file);
134 // PayLoad Validations
135 if(!resourceAuthorityEnum.equals(ResourceAuthorityTypeEnum.CSAR_TYPE_BE)){
136 commonPayloadValidations(responseWrapper, yamlStringWrapper, userWrapper.getInnerElement(), uploadResourceInfoWrapper.getInnerElement());
138 specificResourceAuthorityValidations(responseWrapper, uploadResourceInfoWrapper, yamlStringWrapper, userWrapper.getInnerElement(), request, resourceInfoJsonString, resourceAuthorityEnum);
141 if (responseWrapper.isEmpty()) {
142 handleImport(responseWrapper, userWrapper.getInnerElement(), uploadResourceInfoWrapper.getInnerElement(), yamlStringWrapper.getInnerElement(), resourceAuthorityEnum, createNewVersion, null);
145 return responseWrapper.getInnerElement();
147 } catch (Exception e) {
148 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Upload Resource");
149 log.debug("upload resource failed with exception", e);
150 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
154 /********************************************************************************************************************/
156 private void init(ServletContext context) {
158 WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
159 WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
160 resourceImportManager = webApplicationContext.getBean(ResourceImportManager.class);
161 resourceImportManager.init(context);