Specify model at service creation
[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 package org.openecomp.sdc.be.servlets;
21
22 import com.jcabi.aspects.Loggable;
23 import io.swagger.v3.oas.annotations.Operation;
24 import io.swagger.v3.oas.annotations.Parameter;
25 import io.swagger.v3.oas.annotations.media.ArraySchema;
26 import io.swagger.v3.oas.annotations.media.Content;
27 import io.swagger.v3.oas.annotations.media.Schema;
28 import io.swagger.v3.oas.annotations.responses.ApiResponse;
29 import io.swagger.v3.oas.annotations.servers.Server;
30 import io.swagger.v3.oas.annotations.tags.Tag;
31 import java.io.File;
32 import javax.inject.Inject;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.ws.rs.Consumes;
35 import javax.ws.rs.DefaultValue;
36 import javax.ws.rs.HeaderParam;
37 import javax.ws.rs.POST;
38 import javax.ws.rs.Path;
39 import javax.ws.rs.PathParam;
40 import javax.ws.rs.Produces;
41 import javax.ws.rs.QueryParam;
42 import javax.ws.rs.core.Context;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
45 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
46 import org.glassfish.jersey.media.multipart.FormDataParam;
47 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
48 import org.openecomp.sdc.be.components.impl.ModelBusinessLogic;
49 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
50 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
51 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
52 import org.openecomp.sdc.be.config.BeEcompErrorManager;
53 import org.openecomp.sdc.be.dao.api.ActionStatus;
54 import org.openecomp.sdc.be.exception.BusinessException;
55 import org.openecomp.sdc.be.impl.ComponentsUtils;
56 import org.openecomp.sdc.be.impl.ServletUtils;
57 import org.openecomp.sdc.be.model.UploadResourceInfo;
58 import org.openecomp.sdc.be.model.User;
59 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier;
60 import org.openecomp.sdc.be.user.UserBusinessLogic;
61 import org.openecomp.sdc.common.api.Constants;
62 import org.openecomp.sdc.common.datastructure.Wrapper;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65 import org.springframework.stereotype.Controller;
66
67 /**
68  * Root resource (exposed at "/" path)
69  */
70 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
71 @Path("/v1/catalog/upload")
72 @Tag(name = "SDCE-2 APIs")
73 @Server(url = "/sdc2/rest")
74 @Controller
75 public class ResourceUploadServlet extends AbstractValidationsServlet {
76
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     private static final Logger log = LoggerFactory.getLogger(ResourceUploadServlet.class);
82
83     private ModelBusinessLogic modelBusinessLogic;
84
85     @Inject
86     public ResourceUploadServlet(UserBusinessLogic userBusinessLogic, ComponentInstanceBusinessLogic componentInstanceBL,
87                                  ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager,
88                                  ModelBusinessLogic modelBusinessLogic) {
89         super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
90         this.modelBusinessLogic = modelBusinessLogic;
91     }
92
93     @POST
94     @Path("/{resourceAuthority}")
95     @Consumes(MediaType.MULTIPART_FORM_DATA)
96     @Produces(MediaType.APPLICATION_JSON)
97     @Operation(description = "Create Resource from yaml", method = "POST", summary = "Returns created resource", responses = {
98         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
99         @ApiResponse(responseCode = "201", description = "Resource created"),
100         @ApiResponse(responseCode = "403", description = "Restricted operation"),
101         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
102         @ApiResponse(responseCode = "409", description = "Resource already exist")})
103     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
104     public Response uploadMultipart(
105         @Parameter(description = "validValues: normative-resource / user-resource", schema = @Schema(allowableValues = {NORMATIVE_TYPE_RESOURCE,
106             USER_TYPE_RESOURCE, USER_TYPE_RESOURCE_UI_IMPORT})) @PathParam(value = "resourceAuthority") final String resourceAuthority,
107         @Parameter(description = "FileInputStream") @FormDataParam("resourceZip") File file,
108         @Parameter(description = "ContentDisposition") @FormDataParam("resourceZip") FormDataContentDisposition contentDispositionHeader,
109         @Parameter(description = "resourceMetadata") @FormDataParam("resourceMetadata") String resourceInfoJsonString,
110         @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
111         // updateResource Query Parameter if false checks if already exist
112         @DefaultValue("true") @QueryParam("createNewVersion") boolean createNewVersion) {
113         try {
114             Wrapper<Response> responseWrapper = new Wrapper<>();
115             Wrapper<User> userWrapper = new Wrapper<>();
116             Wrapper<UploadResourceInfo> uploadResourceInfoWrapper = new Wrapper<>();
117             Wrapper<String> yamlStringWrapper = new Wrapper<>();
118             String url = request.getMethod() + " " + request.getRequestURI();
119             log.debug("Start handle request of {}", url);
120             // When we get an errorResponse it will be filled into the responseWrapper
121             validateAuthorityType(responseWrapper, resourceAuthority);
122             ResourceAuthorityTypeEnum resourceAuthorityEnum = ResourceAuthorityTypeEnum.findByUrlPath(resourceAuthority);
123             commonGeneralValidations(responseWrapper, userWrapper, uploadResourceInfoWrapper, resourceAuthorityEnum, userId, resourceInfoJsonString);
124             final String modelNameToBeAssociated = uploadResourceInfoWrapper.getInnerElement().getModel();
125             if (modelNameToBeAssociated != null) {
126                 log.debug("Model Name to be validated {}", modelNameToBeAssociated);
127                 validateModel(modelNameToBeAssociated);
128             }
129             fillPayload(responseWrapper, uploadResourceInfoWrapper, yamlStringWrapper, userWrapper.getInnerElement(), resourceInfoJsonString,
130                 resourceAuthorityEnum, file);
131             // PayLoad Validations
132             if (resourceAuthorityEnum != ResourceAuthorityTypeEnum.CSAR_TYPE_BE) {
133                 commonPayloadValidations(responseWrapper, yamlStringWrapper, userWrapper.getInnerElement(),
134                     uploadResourceInfoWrapper.getInnerElement());
135                 specificResourceAuthorityValidations(responseWrapper, uploadResourceInfoWrapper, yamlStringWrapper, userWrapper.getInnerElement(),
136                     request, resourceInfoJsonString, resourceAuthorityEnum);
137             }
138             if (responseWrapper.isEmpty()) {
139                 handleImport(responseWrapper, userWrapper.getInnerElement(), uploadResourceInfoWrapper.getInnerElement(),
140                     yamlStringWrapper.getInnerElement(), resourceAuthorityEnum, createNewVersion, null);
141             }
142             return responseWrapper.getInnerElement();
143         } catch (final BusinessException e) {
144             throw e;
145         } catch (final Exception e) {
146             var errorMsg = String.format("Unexpected error while uploading Resource '%s'", resourceInfoJsonString);
147             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(errorMsg);
148             log.error(errorMsg, e);
149             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
150         }
151     }
152
153     /**
154      * The Model field is an optional entry when uploading a resource. If the field is present, it validates if the Model name exists.
155      * @param modelName Model names declared on the resource json representation
156      */
157     private void validateModel(final String modelName) {
158         if (modelBusinessLogic.findModel(modelName).isEmpty()) {
159             log.error("Could not find model name {}", modelName);
160             throw ModelOperationExceptionSupplier.invalidModel(modelName).get();
161         }
162     }
163
164     public enum ResourceAuthorityTypeEnum {
165         // @formatter:off
166         NORMATIVE_TYPE_BE(NORMATIVE_TYPE_RESOURCE, true, false),
167         USER_TYPE_BE(USER_TYPE_RESOURCE, true, true),
168         USER_TYPE_UI(USER_TYPE_RESOURCE_UI_IMPORT, false, true),
169         CSAR_TYPE_BE(CSAR_TYPE_RESOURCE, true, true);
170         // @formatter:on
171
172         private String urlPath;
173         private boolean isBackEndImport;
174         private boolean isUserTypeResource;
175
176         public static ResourceAuthorityTypeEnum findByUrlPath(String urlPath) {
177             ResourceAuthorityTypeEnum found = null;
178             for (ResourceAuthorityTypeEnum curr : ResourceAuthorityTypeEnum.values()) {
179                 if (curr.getUrlPath().equals(urlPath)) {
180                     found = curr;
181                     break;
182                 }
183             }
184             return found;
185         }
186
187         ResourceAuthorityTypeEnum(String urlPath, boolean isBackEndImport, boolean isUserTypeResource) {
188             this.urlPath = urlPath;
189             this.isBackEndImport = isBackEndImport;
190             this.isUserTypeResource = isUserTypeResource;
191         }
192
193         public String getUrlPath() {
194             return urlPath;
195         }
196
197         public boolean isBackEndImport() {
198             return isBackEndImport;
199         }
200
201         public boolean isUserTypeResource() {
202             return isUserTypeResource;
203         }
204     }
205 }