c1eccdbef004252829b6ee91f20268fbeadd0dc1
[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 fj.data.Either;
24 import io.swagger.v3.oas.annotations.Operation;
25 import io.swagger.v3.oas.annotations.Parameter;
26 import io.swagger.v3.oas.annotations.media.ArraySchema;
27 import io.swagger.v3.oas.annotations.media.Content;
28 import io.swagger.v3.oas.annotations.media.Schema;
29 import io.swagger.v3.oas.annotations.responses.ApiResponse;
30 import io.swagger.v3.oas.annotations.servers.Server;
31 import io.swagger.v3.oas.annotations.tags.Tag;
32 import java.io.File;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.nio.charset.StandardCharsets;
36 import javax.inject.Inject;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.validation.constraints.NotNull;
39 import javax.ws.rs.Consumes;
40 import javax.ws.rs.DefaultValue;
41 import javax.ws.rs.HeaderParam;
42 import javax.ws.rs.POST;
43 import javax.ws.rs.Path;
44 import javax.ws.rs.PathParam;
45 import javax.ws.rs.Produces;
46 import javax.ws.rs.QueryParam;
47 import javax.ws.rs.core.Context;
48 import javax.ws.rs.core.MediaType;
49 import javax.ws.rs.core.Response;
50 import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
51 import org.glassfish.jersey.media.multipart.FormDataParam;
52 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
53 import org.openecomp.sdc.be.components.impl.ModelBusinessLogic;
54 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
55 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
56 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
57 import org.openecomp.sdc.be.config.BeEcompErrorManager;
58 import org.openecomp.sdc.be.dao.api.ActionStatus;
59 import org.openecomp.sdc.be.exception.BusinessException;
60 import org.openecomp.sdc.be.impl.ComponentsUtils;
61 import org.openecomp.sdc.be.impl.ServletUtils;
62 import org.openecomp.sdc.be.model.NodeTypesMetadataList;
63 import org.openecomp.sdc.be.model.UploadResourceInfo;
64 import org.openecomp.sdc.be.model.User;
65 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier;
66 import org.openecomp.sdc.common.api.Constants;
67 import org.openecomp.sdc.common.datastructure.Wrapper;
68 import org.openecomp.sdc.common.util.ValidationUtils;
69 import org.openecomp.sdc.exception.ResponseFormat;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
72 import org.springframework.stereotype.Controller;
73
74 /**
75  * Root resource (exposed at "/" path)
76  */
77 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
78 @Path("/v1/catalog/upload")
79 @Tag(name = "SDCE-2 APIs")
80 @Server(url = "/sdc2/rest")
81 @Controller
82 public class ResourceUploadServlet extends AbstractValidationsServlet {
83
84     public static final String NORMATIVE_TYPE_RESOURCE = "multipart";
85     public static final String CSAR_TYPE_RESOURCE = "csar";
86     public static final String USER_TYPE_RESOURCE = "user-resource";
87     public static final String USER_TYPE_RESOURCE_UI_IMPORT = "user-resource-ui-import";
88     private static final Logger log = LoggerFactory.getLogger(ResourceUploadServlet.class);
89
90     private final ModelBusinessLogic modelBusinessLogic;
91
92     @Inject
93     public ResourceUploadServlet(ComponentInstanceBusinessLogic componentInstanceBL,
94                                  ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager,
95                                  ModelBusinessLogic modelBusinessLogic) {
96         super(componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
97         this.modelBusinessLogic = modelBusinessLogic;
98     }
99
100     @POST
101     @Path("/{resourceAuthority}")
102     @Consumes(MediaType.MULTIPART_FORM_DATA)
103     @Produces(MediaType.APPLICATION_JSON)
104     @Operation(description = "Create Resource from yaml", method = "POST", summary = "Returns created resource", responses = {
105         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
106         @ApiResponse(responseCode = "201", description = "Resource created"),
107         @ApiResponse(responseCode = "403", description = "Restricted operation"),
108         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
109         @ApiResponse(responseCode = "409", description = "Resource already exist")})
110     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
111     public Response uploadMultipart(
112         @Parameter(description = "validValues: normative-resource / user-resource", schema = @Schema(allowableValues = {NORMATIVE_TYPE_RESOURCE,
113             USER_TYPE_RESOURCE, USER_TYPE_RESOURCE_UI_IMPORT})) @PathParam(value = "resourceAuthority") final String resourceAuthority,
114         @Parameter(description = "FileInputStream") @FormDataParam("resourceZip") File file,
115         @Parameter(description = "ContentDisposition") @FormDataParam("resourceZip") FormDataContentDisposition contentDispositionHeader,
116         @Parameter(description = "resourceMetadata") @FormDataParam("resourceMetadata") String resourceInfoJsonString,
117         @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
118         // updateResource Query Parameter if false checks if already exist
119         @DefaultValue("true") @QueryParam("createNewVersion") boolean createNewVersion) {
120         try {
121             Wrapper<Response> responseWrapper = new Wrapper<>();
122             Wrapper<User> userWrapper = new Wrapper<>();
123             Wrapper<UploadResourceInfo> uploadResourceInfoWrapper = new Wrapper<>();
124             Wrapper<String> yamlStringWrapper = new Wrapper<>();
125             String url = request.getMethod() + " " + request.getRequestURI();
126             log.debug("Start handle request of {}", url);
127             // When we get an errorResponse it will be filled into the responseWrapper
128             validateAuthorityType(responseWrapper, resourceAuthority);
129             ResourceAuthorityTypeEnum resourceAuthorityEnum = ResourceAuthorityTypeEnum.findByUrlPath(resourceAuthority);
130             commonGeneralValidations(responseWrapper, userWrapper, uploadResourceInfoWrapper, resourceAuthorityEnum, userId, resourceInfoJsonString);
131             final String modelNameToBeAssociated = uploadResourceInfoWrapper.getInnerElement().getModel();
132             if (modelNameToBeAssociated != null) {
133                 log.debug("Model Name to be validated {}", modelNameToBeAssociated);
134                 validateModel(modelNameToBeAssociated);
135             }
136             fillPayload(responseWrapper, uploadResourceInfoWrapper, yamlStringWrapper, userWrapper.getInnerElement(), resourceInfoJsonString,
137                 resourceAuthorityEnum, file);
138             // PayLoad Validations
139             if (resourceAuthorityEnum != ResourceAuthorityTypeEnum.CSAR_TYPE_BE) {
140                 commonPayloadValidations(responseWrapper, yamlStringWrapper, userWrapper.getInnerElement(),
141                     uploadResourceInfoWrapper.getInnerElement());
142                 specificResourceAuthorityValidations(responseWrapper, uploadResourceInfoWrapper, yamlStringWrapper, userWrapper.getInnerElement(),
143                     request, resourceInfoJsonString, resourceAuthorityEnum);
144             }
145             if (responseWrapper.isEmpty()) {
146                 handleImport(responseWrapper, userWrapper.getInnerElement(), uploadResourceInfoWrapper.getInnerElement(),
147                     yamlStringWrapper.getInnerElement(), resourceAuthorityEnum, createNewVersion, null);
148             }
149             return responseWrapper.getInnerElement();
150         } catch (final BusinessException e) {
151             throw e;
152         } catch (final Exception e) {
153             var errorMsg = String.format("Unexpected error while uploading Resource '%s'", resourceInfoJsonString);
154             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(errorMsg);
155             log.error(errorMsg, e);
156             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
157         }
158     }
159
160     @POST
161     @Path("/resource/import")
162     @Consumes(MediaType.MULTIPART_FORM_DATA)
163     @Produces(MediaType.APPLICATION_JSON)
164     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
165     @Operation(description = "Import node types from a TOSCA yaml, along with the types metadata", method = "POST",
166         summary = "Creates node types from a TOSCA yaml file", responses = {
167         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
168         @ApiResponse(responseCode = "201", description = "Resources created"),
169         @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
170         @ApiResponse(responseCode = "403", description = "Restricted operation"),
171         @ApiResponse(responseCode = "409", description = "One of the resources already exists")}
172     )
173     public Response bulkImport(@Parameter(description = "The nodes metadata JSON", required = true)
174                                @NotNull @FormDataParam("nodeTypeMetadataJson") final NodeTypesMetadataList nodeTypeMetadata,
175                                @Parameter(description = "The node types TOSCA definition yaml", required = true)
176                                @NotNull @FormDataParam("nodeTypesYaml") final InputStream nodeTypesYamlInputStream,
177                                @Parameter(description = "The model name to associate the node types to")
178                                @DefaultValue("true") @FormDataParam("createNewVersion") boolean createNewVersion,
179                                @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
180                                @Context final HttpServletRequest request) {
181         userId = ValidationUtils.sanitizeInputString(userId);
182         final Either<User, ResponseFormat> userEither = getUser(request, userId);
183         if (userEither.isRight()) {
184             return buildErrorResponse(userEither.right().value());
185         }
186
187         final User user = userEither.left().value();
188
189         final String nodeTypesYamlString;
190         try {
191             nodeTypesYamlString = new String(nodeTypesYamlInputStream.readAllBytes(), StandardCharsets.UTF_8);
192         } catch (final IOException e) {
193             var errorMsg = "Could not read the given node types yaml";
194             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(errorMsg);
195             log.error(errorMsg, e);
196             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_NODE_TYPES_YAML));
197         }
198
199         try {
200             resourceImportManager
201                 .importAllNormativeResource(nodeTypesYamlString, nodeTypeMetadata, user, createNewVersion, false);
202             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), null);
203         } catch (final BusinessException e) {
204             throw e;
205         } catch (final Exception e) {
206             var errorMsg = "Unexpected error while importing the node types";
207             BeEcompErrorManager.getInstance().logBeRestApiGeneralError(errorMsg);
208             log.error(errorMsg, e);
209             return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
210         }
211     }
212
213     /**
214      * The Model field is an optional entry when uploading a resource. If the field is present, it validates if the Model name exists.
215      *
216      * @param modelName Model names declared on the resource json representation
217      */
218     private void validateModel(final String modelName) {
219         if (modelBusinessLogic.findModel(modelName).isEmpty()) {
220             log.error("Could not find model name {}", modelName);
221             throw ModelOperationExceptionSupplier.invalidModel(modelName).get();
222         }
223     }
224
225     public enum ResourceAuthorityTypeEnum {
226         // @formatter:off
227         NORMATIVE_TYPE_BE(NORMATIVE_TYPE_RESOURCE, true, false),
228         USER_TYPE_BE(USER_TYPE_RESOURCE, true, true),
229         USER_TYPE_UI(USER_TYPE_RESOURCE_UI_IMPORT, false, true),
230         CSAR_TYPE_BE(CSAR_TYPE_RESOURCE, true, true);
231         // @formatter:on
232
233         private String urlPath;
234         private boolean isBackEndImport;
235         private boolean isUserTypeResource;
236
237         public static ResourceAuthorityTypeEnum findByUrlPath(String urlPath) {
238             ResourceAuthorityTypeEnum found = null;
239             for (ResourceAuthorityTypeEnum curr : ResourceAuthorityTypeEnum.values()) {
240                 if (curr.getUrlPath().equals(urlPath)) {
241                     found = curr;
242                     break;
243                 }
244             }
245             return found;
246         }
247
248         ResourceAuthorityTypeEnum(String urlPath, boolean isBackEndImport, boolean isUserTypeResource) {
249             this.urlPath = urlPath;
250             this.isBackEndImport = isBackEndImport;
251             this.isUserTypeResource = isUserTypeResource;
252         }
253
254         public String getUrlPath() {
255             return urlPath;
256         }
257
258         public boolean isBackEndImport() {
259             return isBackEndImport;
260         }
261
262         public boolean isUserTypeResource() {
263             return isUserTypeResource;
264         }
265     }
266 }