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