Publish swagger files for SDC APIs
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / GenericArtifactBrowserServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Nokia 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.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.servers.Servers;
32 import io.swagger.v3.oas.annotations.tags.Tag;
33 import io.swagger.v3.oas.annotations.tags.Tags;
34 import org.apache.commons.lang3.tuple.ImmutablePair;
35 import org.onap.sdc.gab.model.GABQuery;
36 import org.onap.sdc.gab.model.GABQuery.GABQueryType;
37 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
38 import org.openecomp.sdc.be.components.impl.GenericArtifactBrowserBusinessLogic;
39 import org.openecomp.sdc.be.impl.ComponentsUtils;
40 import org.openecomp.sdc.be.info.GenericArtifactQueryInfo;
41 import org.openecomp.sdc.be.user.UserBusinessLogic;
42 import org.openecomp.sdc.common.log.wrappers.Logger;
43 import org.owasp.esapi.ESAPI;
44 import org.springframework.stereotype.Controller;
45
46 import javax.inject.Inject;
47 import javax.servlet.ServletContext;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.ws.rs.Consumes;
50 import javax.ws.rs.POST;
51 import javax.ws.rs.Path;
52 import javax.ws.rs.Produces;
53 import javax.ws.rs.core.Context;
54 import javax.ws.rs.core.MediaType;
55 import javax.ws.rs.core.Response;
56 import java.io.IOException;
57 import java.util.Set;
58 import java.util.stream.Collectors;
59
60 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
61 @Path("/v1/catalog/gab")
62 @Consumes(MediaType.APPLICATION_JSON)
63 @Produces(MediaType.APPLICATION_JSON)
64 @Tags({@Tag(name = "SDCE-2 APIs")})
65 @Servers({@Server(url = "/sdc2/rest")})
66 @Controller
67 public class GenericArtifactBrowserServlet extends BeGenericServlet {
68
69     private static final Logger LOGGER = Logger.getLogger(GenericArtifactBrowserServlet.class);
70     private final GenericArtifactBrowserBusinessLogic gabLogic;
71     private final ArtifactsBusinessLogic artifactsBusinessLogic;
72
73     @Inject
74     public GenericArtifactBrowserServlet(UserBusinessLogic userBusinessLogic,
75         ComponentsUtils componentsUtils,
76         ArtifactsBusinessLogic artifactsBusinessLogic,
77         GenericArtifactBrowserBusinessLogic gabLogic) {
78         super(userBusinessLogic, componentsUtils);
79         this.artifactsBusinessLogic = artifactsBusinessLogic;
80         this.gabLogic = gabLogic;
81     }
82
83     @POST
84     @Path("/searchFor")
85     @Operation(description = "Search json paths inside the yaml", method = "POST",
86             summary = "Returns found entries of json paths", responses = {
87             @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
88             @ApiResponse(responseCode = "200", description = "Returned yaml entries"),
89             @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
90     public Response searchFor(
91         @Parameter(description = "Generic Artifact search model", required = true) GenericArtifactQueryInfo query,
92         @Context final HttpServletRequest request) {
93         try {
94             ServletContext context = request.getSession().getServletContext();
95             ImmutablePair<String, byte[]> immutablePairResponseFormatEither = getArtifactBL(context)
96                 .downloadArtifact(ESAPI.encoder().canonicalize(query.getParentId()), ESAPI.encoder().canonicalize(query.getArtifactUniqueId()));
97             GABQuery gabQuery = prepareGabQuery(query, immutablePairResponseFormatEither);
98             return buildOkResponse(getGenericArtifactBrowserBL(context).searchFor(gabQuery));
99         } catch (IOException e) {
100             LOGGER.error("Cannot search for a given queries in the yaml file", e);
101             return buildGeneralErrorResponse();
102         }
103     }
104
105     private GABQuery prepareGabQuery(GenericArtifactQueryInfo query,
106         ImmutablePair<String, byte[]> immutablePairResponseFormatEither) {
107         byte[] content = immutablePairResponseFormatEither.getRight();
108         Set<String> queryFields = query.getFields().stream().map(ESAPI.encoder()::canonicalize).collect(Collectors.toSet());
109         return new GABQuery(queryFields, new String(content), GABQueryType.CONTENT);
110     }
111
112 }