Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / ResourceUploadServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.servlets;
22
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;
50
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;
64 import java.io.File;
65 import java.io.FileNotFoundException;
66
67 /**
68  * Root resource (exposed at "/" path)
69  */
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"))
73 @Controller
74 public class ResourceUploadServlet extends AbstractValidationsServlet {
75
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";
81
82     @Inject
83     public ResourceUploadServlet(UserBusinessLogic userBusinessLogic,
84         ComponentInstanceBusinessLogic componentInstanceBL,
85         ComponentsUtils componentsUtils, ServletUtils servletUtils,
86         ResourceImportManager resourceImportManager) {
87         super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
88     }
89
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);
92
93         private String urlPath;
94         private boolean isBackEndImport, isUserTypeResource;
95
96         public static ResourceAuthorityTypeEnum findByUrlPath(String urlPath) {
97             ResourceAuthorityTypeEnum found = null;
98             for (ResourceAuthorityTypeEnum curr : ResourceAuthorityTypeEnum.values()) {
99                 if (curr.getUrlPath().equals(urlPath)) {
100                     found = curr;
101                     break;
102                 }
103             }
104             return found;
105         }
106
107         private ResourceAuthorityTypeEnum(String urlPath, boolean isBackEndImport, boolean isUserTypeResource) {
108             this.urlPath = urlPath;
109             this.isBackEndImport = isBackEndImport;
110             this.isUserTypeResource = isUserTypeResource;
111         }
112
113         public String getUrlPath() {
114             return urlPath;
115         }
116
117         public boolean isBackEndImport() {
118             return isBackEndImport;
119         }
120
121         public boolean isUserTypeResource() {
122             return isUserTypeResource;
123         }
124     }
125
126     @POST
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 {
149
150         try {
151
152             Wrapper<Response> responseWrapper = new Wrapper<>();
153             Wrapper<User> userWrapper = new Wrapper<>();
154             Wrapper<UploadResourceInfo> uploadResourceInfoWrapper = new Wrapper<>();
155             Wrapper<String> yamlStringWrapper = new Wrapper<>();
156
157             String url = request.getMethod() + " " + request.getRequestURI();
158             log.debug("Start handle request of {}", url);
159
160             // When we get an errorResponse it will be filled into the
161             // responseWrapper
162             validateAuthorityType(responseWrapper, resourceAuthority);
163
164             ResourceAuthorityTypeEnum resourceAuthorityEnum = ResourceAuthorityTypeEnum.findByUrlPath(resourceAuthority);
165
166             commonGeneralValidations(responseWrapper, userWrapper, uploadResourceInfoWrapper, resourceAuthorityEnum, userId, resourceInfoJsonString);
167
168             fillPayload(responseWrapper, uploadResourceInfoWrapper, yamlStringWrapper, userWrapper.getInnerElement(), resourceInfoJsonString, resourceAuthorityEnum, file);
169
170             // PayLoad Validations
171             if(resourceAuthorityEnum != ResourceAuthorityTypeEnum.CSAR_TYPE_BE){
172                 commonPayloadValidations(responseWrapper, yamlStringWrapper, userWrapper.getInnerElement(), uploadResourceInfoWrapper.getInnerElement());
173
174                 specificResourceAuthorityValidations(responseWrapper, uploadResourceInfoWrapper, yamlStringWrapper, userWrapper.getInnerElement(), request, resourceInfoJsonString, resourceAuthorityEnum);
175             }
176
177             if (responseWrapper.isEmpty()) {
178                 handleImport(responseWrapper, userWrapper.getInnerElement(), uploadResourceInfoWrapper.getInnerElement(), yamlStringWrapper.getInnerElement(), resourceAuthorityEnum, createNewVersion, null);
179             }
180
181             return responseWrapper.getInnerElement();
182
183         } catch (Exception e) {
184             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Upload Resource");
185             log.debug("upload resource failed with exception", e);
186             throw e;
187         }
188     }
189
190 }