diff options
Diffstat (limited to 'aai-traversal/src/main')
7 files changed, 146 insertions, 86 deletions
diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/CQ2Gremlin.java b/aai-traversal/src/main/java/org/onap/aai/rest/CQ2Gremlin.java index c8f1f0b..6b8382b 100644 --- a/aai-traversal/src/main/java/org/onap/aai/rest/CQ2Gremlin.java +++ b/aai-traversal/src/main/java/org/onap/aai/rest/CQ2Gremlin.java @@ -103,7 +103,6 @@ public class CQ2Gremlin extends RESTAPI { SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class); traversalUriHttpEntry.setHttpEntryProperties(schemaVersions.getDefaultVersion()); - traversalUriHttpEntry.setPaginationParameters("-1", "-1"); TransactionalGraphEngine dbEngine = traversalUriHttpEntry.getDbEngine(); diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/CQ2GremlinTest.java b/aai-traversal/src/main/java/org/onap/aai/rest/CQ2GremlinTest.java index 8c6477f..bade2cd 100644 --- a/aai-traversal/src/main/java/org/onap/aai/rest/CQ2GremlinTest.java +++ b/aai-traversal/src/main/java/org/onap/aai/rest/CQ2GremlinTest.java @@ -66,9 +66,6 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.RequestBody; -import com.beust.jcommander.internal.Lists; -import com.beust.jcommander.internal.Maps; - @Path("/cq2gremlintest") public class CQ2GremlinTest extends RESTAPI { @@ -106,7 +103,6 @@ public class CQ2GremlinTest extends RESTAPI { String realTime = headers.getRequestHeaders().getFirst("Real-Time"); SchemaVersions schemaVersions = SpringContextAware.getBean(SchemaVersions.class); traversalUriHttpEntry.setHttpEntryProperties(schemaVersions.getDefaultVersion()); - traversalUriHttpEntry.setPaginationParameters("-1", "-1"); return processC2UnitTest(content); } @@ -159,11 +155,11 @@ public class CQ2GremlinTest extends RESTAPI { } private List<Vertex> createGraph(CustomQueryTestDTO content, Graph graph) { - Map<String, Vertex> verticesMap = Maps.newLinkedHashMap(); + Map<String, Vertex> verticesMap = new LinkedHashMap<>(); // Creating all the Vertices content.getVerticesDtos().forEach(vertex -> { StringBuilder vertexIdentifier = new StringBuilder(); - List<String> keyValues = Lists.newArrayList(); + List<String> keyValues = new ArrayList<>(); keyValues.add(T.id.toString()); keyValues.add(String.format("%02d", verticesMap.size() * 10)); AtomicInteger index = new AtomicInteger(0); @@ -199,7 +195,7 @@ public class CQ2GremlinTest extends RESTAPI { }); - List<Vertex> expectedVertices = Lists.newArrayList(); + List<Vertex> expectedVertices = new ArrayList<>(); content.getExpectedResultsDtos().getIds() .forEach(vertexId -> expectedVertices.add(verticesMap.get(vertexId))); return expectedVertices; diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/DslConsumer.java b/aai-traversal/src/main/java/org/onap/aai/rest/DslConsumer.java index 78ca452..8226ddd 100644 --- a/aai-traversal/src/main/java/org/onap/aai/rest/DslConsumer.java +++ b/aai-traversal/src/main/java/org/onap/aai/rest/DslConsumer.java @@ -22,8 +22,6 @@ package org.onap.aai.rest; import java.io.FileNotFoundException; import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; @@ -36,10 +34,10 @@ import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; -import org.onap.aai.edges.EdgeIngestor; +import org.javatuples.Pair; import org.onap.aai.exceptions.AAIException; -import org.onap.aai.introspection.LoaderFactory; import org.onap.aai.introspection.ModelType; +import org.onap.aai.query.builder.Pageable; import org.onap.aai.rest.db.HttpEntry; import org.onap.aai.rest.dsl.DslQueryProcessor; import org.onap.aai.rest.dsl.V1DslQueryProcessor; @@ -49,6 +47,7 @@ import org.onap.aai.rest.enums.QueryVersion; import org.onap.aai.rest.search.GenericQueryProcessor; import org.onap.aai.rest.search.GremlinServerSingleton; import org.onap.aai.rest.search.QueryProcessorType; +import org.onap.aai.rest.util.PaginationUtil; import org.onap.aai.serialization.db.DBSerializer; import org.onap.aai.serialization.engines.TransactionalGraphEngine; import org.onap.aai.serialization.queryformats.Format; @@ -122,19 +121,19 @@ public class DslConsumer extends TraversalConsumer { @RequestParam(defaultValue = "graphson") String format, @RequestParam(defaultValue = "no_op") String subgraph, @RequestParam(defaultValue = "all") String validate, - @RequestParam(defaultValue = "-1") String resultIndex, - @RequestParam(defaultValue = "-1") String resultSize, + @RequestParam(defaultValue = "-1") int resultIndex, + @RequestParam(defaultValue = "-1") int resultSize, @RequestHeader HttpHeaders headers, HttpServletRequest request) throws FileNotFoundException, AAIException { Set<String> roles = this.getRoles(request.getUserPrincipal()); return processExecuteQuery(dslQuery, request, versionParam, format, subgraph, - validate, headers, resultIndex, resultSize, roles); + validate, headers, new Pageable(resultIndex, resultSize), roles); } public ResponseEntity<String> processExecuteQuery(String dslQuery, HttpServletRequest request, String versionParam, String queryFormat, String subgraph, String validate, HttpHeaders headers, - String resultIndex, String resultSize, Set<String> roles) throws FileNotFoundException, AAIException { + Pageable pageable, Set<String> roles) throws FileNotFoundException, AAIException { final SchemaVersion version = new SchemaVersion(versionParam); final String sourceOfTruth = headers.getFirst("X-FromAppId"); @@ -151,8 +150,49 @@ public class DslConsumer extends TraversalConsumer { } } - String result = executeQuery(dslQuery, request, queryFormat, subgraph, validate, queryParams, resultIndex, resultSize, + Pair<List<Object>,Map<String,List<String>>> executionResult = executeQuery(dslQuery, request, queryFormat, subgraph, validate, queryParams, pageable, roles, version, sourceOfTruth, dslOverride); + List<Object> vertices = executionResult.getValue0(); + + String result = serializeResponse(request, queryFormat, headers, version, sourceOfTruth, queryParams, executionResult.getValue1(), vertices); + + if (PaginationUtil.hasValidPaginationParams(pageable)) { + int totalCount = vertices.size(); + long totalPages = PaginationUtil.getTotalPages(pageable, totalCount); + return ResponseEntity.ok() + .header("total-results", String.valueOf(totalCount)) + .header("total-pages", String.valueOf(totalPages)) + .body(result); + } else { + return ResponseEntity.ok(result); + } + } + + private String serializeResponse(HttpServletRequest request, String queryFormat, HttpHeaders headers, + final SchemaVersion version, final String sourceOfTruth, MultivaluedMap<String, String> queryParameters, final Map<String, List<String>> propertiesMap, + List<Object> vertices) throws AAIException { + DBSerializer serializer = + new DBSerializer(version, httpEntry.getDbEngine(), ModelType.MOXY, sourceOfTruth); + String serverBase = request.getRequestURL().toString().replaceAll("/(v[0-9]+|latest)/.*", "/"); + FormatFactory ff = new FormatFactory(httpEntry.getLoader(), serializer, + schemaVersions, this.basePath, serverBase); + + MultivaluedMap<String, String> mvm = new MultivaluedHashMap<>(); + mvm.putAll(queryParameters); + Format format = Format.getFormat(queryFormat); + if (isHistory(format)) { + mvm.putSingle("startTs", Long.toString(getStartTime(format, mvm))); + mvm.putSingle("endTs", Long.toString(getEndTime(mvm))); + } + Formatter formatter = ff.get(format, mvm); + + String result = ""; + if (propertiesMap != null && !propertiesMap.isEmpty()) { + result = formatter.output(vertices, propertiesMap).toString(); + } else { + result = formatter.output(vertices).toString(); + } + MediaType acceptType = headers.getAccept().stream() .filter(Objects::nonNull) .filter(header -> !header.equals(MediaType.ALL)) @@ -162,25 +202,16 @@ public class DslConsumer extends TraversalConsumer { if (MediaType.APPLICATION_XML.isCompatibleWith(acceptType)) { result = xmlFormatTransformer.transform(result); } - - if (httpEntry.isPaginated()) { - return ResponseEntity.ok() - .header("total-results", String.valueOf(httpEntry.getTotalVertices())) - .header("total-pages", String.valueOf(httpEntry.getTotalPaginationBuckets())) - .body(result); - } else { - return ResponseEntity.ok(result); - } + return result; } - private String executeQuery(String content, HttpServletRequest req, String queryFormat, String subgraph, - String validate, MultivaluedMap<String, String> queryParameters, String resultIndex, String resultSize, Set<String> roles, + private Pair<List<Object>,Map<String,List<String>>> executeQuery(String content, HttpServletRequest req, String queryFormat, String subgraph, + String validate, MultivaluedMap<String, String> queryParameters, Pageable pageable, Set<String> roles, final SchemaVersion version, final String sourceOfTruth, final String dslOverride) throws AAIException, FileNotFoundException { final String serverBase = req.getRequestURL().toString().replaceAll("/(v[0-9]+|latest)/.*", "/"); httpEntry.setHttpEntryProperties(version, serverBase); - httpEntry.setPaginationParameters(resultIndex, resultSize); JsonObject input = JsonParser.parseString(content).getAsJsonObject(); JsonElement dslElement = input.get("dsl"); @@ -215,7 +246,7 @@ public class DslConsumer extends TraversalConsumer { final TransactionalGraphEngine dbEngine = httpEntry.getDbEngine(); GraphTraversalSource traversalSource = getTraversalSource(dbEngine, format, queryParameters, roles); - + GenericQueryProcessor processor = new GenericQueryProcessor.Builder(dbEngine, gremlinServerSingleton) .queryFrom(dsl, "dsl").queryProcessor(dslQueryProcessor).version(dslApiVersion) @@ -229,34 +260,18 @@ public class DslConsumer extends TraversalConsumer { if (isAggregate(format)) { // Dedup if duplicate objects are returned in each array in the aggregate format // scenario. - List<Object> vertTempDedupedObjectList = dedupObjectInAggregateFormatResult(vertTemp); - vertices = httpEntry - .getPaginatedVertexListForAggregateFormat(vertTempDedupedObjectList); + List<Object> vertTempDedupedObjectList = dedupObjectInAggregateFormatResultStreams(vertTemp); + vertices = PaginationUtil.hasValidPaginationParams(pageable) + ? vertices = PaginationUtil.getPaginatedVertexListForAggregateFormat(vertTempDedupedObjectList, pageable) + : vertTempDedupedObjectList; } else { - vertices = httpEntry.getPaginatedVertexList(vertTemp); + int startIndex = pageable.getPage() * pageable.getPageSize(); + vertices = PaginationUtil.hasValidPaginationParams(pageable) + ? vertTemp.subList(startIndex, startIndex + pageable.getPageSize()) + : vertTemp; } - DBSerializer serializer = - new DBSerializer(version, dbEngine, ModelType.MOXY, sourceOfTruth); - FormatFactory ff = new FormatFactory(httpEntry.getLoader(), serializer, - schemaVersions, this.basePath, serverBase); - - MultivaluedMap<String, String> mvm = new MultivaluedHashMap<>(); - mvm.putAll(queryParameters); - if (isHistory(format)) { - mvm.putSingle("startTs", Long.toString(getStartTime(format, mvm))); - mvm.putSingle("endTs", Long.toString(getEndTime(mvm))); - } - Formatter formatter = ff.get(format, mvm); - - final Map<String, List<String>> propertiesMap = processor.getPropertiesMap(); - String result = ""; - if (propertiesMap != null && !propertiesMap.isEmpty()) { - result = formatter.output(vertices, propertiesMap).toString(); - } else { - result = formatter.output(vertices).toString(); - } - return result; + return Pair.with(vertices, processor.getPropertiesMap()); } private List<Object> dedupObjectInAggregateFormatResultStreams(List<Object> vertTemp) { @@ -265,18 +280,6 @@ public class DslConsumer extends TraversalConsumer { .map(o -> ((ArrayList<?>) o).stream().distinct().collect(Collectors.toList())) .collect(Collectors.toList()); } - private List<Object> dedupObjectInAggregateFormatResult(List<Object> vertTemp) { - List<Object> vertTempDedupedObjectList = new ArrayList<Object>(); - Iterator<Object> itr = vertTemp.listIterator(); - while (itr.hasNext()) { - Object o = itr.next(); - if (o instanceof ArrayList) { - vertTempDedupedObjectList - .add(((ArrayList) o).stream().distinct().collect(Collectors.toList())); - } - } - return vertTempDedupedObjectList; - } private MultivaluedMap<String, String> toMultivaluedMap(Map<String, String[]> map) { MultivaluedMap<String, String> multivaluedMap = new MultivaluedHashMap<>(); diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/QueryConsumer.java b/aai-traversal/src/main/java/org/onap/aai/rest/QueryConsumer.java index 5a2e19f..5aad81e 100644 --- a/aai-traversal/src/main/java/org/onap/aai/rest/QueryConsumer.java +++ b/aai-traversal/src/main/java/org/onap/aai/rest/QueryConsumer.java @@ -51,11 +51,13 @@ import org.onap.aai.exceptions.AAIException; import org.onap.aai.introspection.ModelType; import org.onap.aai.logging.ErrorLogHelper; import org.onap.aai.parsers.query.QueryParser; +import org.onap.aai.query.builder.Pageable; import org.onap.aai.rest.db.HttpEntry; import org.onap.aai.rest.search.CustomQueryConfig; import org.onap.aai.rest.search.GenericQueryProcessor; import org.onap.aai.rest.search.GremlinServerSingleton; import org.onap.aai.rest.search.QueryProcessorType; +import org.onap.aai.rest.util.PaginationUtil; import org.onap.aai.restcore.HttpMethod; import org.onap.aai.restcore.util.URITools; import org.onap.aai.serialization.db.DBSerializer; @@ -80,8 +82,8 @@ import com.google.gson.JsonParser; import io.micrometer.core.annotation.Timed; -@Path("{version: v[1-9][0-9]*|latest}/query") @Timed +@Path("{version: v[1-9][0-9]*|latest}/query") public class QueryConsumer extends TraversalConsumer { private QueryProcessorType processorType = QueryProcessorType.LOCAL_GROOVY; @@ -112,12 +114,16 @@ public class QueryConsumer extends TraversalConsumer { @PUT @Consumes({MediaType.APPLICATION_JSON}) @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) - public Response executeQuery(String content, @PathParam("version") String versionParam, + public Response executeQuery( + String content, + @PathParam("version") String versionParam, @DefaultValue("graphson") @QueryParam("format") String queryFormat, @DefaultValue("no_op") @QueryParam("subgraph") String subgraph, - @Context HttpHeaders headers, @Context HttpServletRequest req, @Context UriInfo info, - @DefaultValue("-1") @QueryParam("resultIndex") String resultIndex, - @DefaultValue("-1") @QueryParam("resultSize") String resultSize) { + @DefaultValue("-1") @QueryParam("resultIndex") int resultIndex, + @DefaultValue("-1") @QueryParam("resultSize") int resultSize, + @Context HttpHeaders headers, + @Context HttpServletRequest req, + @Context UriInfo info) { Set<String> roles = this.getRoles(req.getUserPrincipal()); return runner(TraversalConstants.AAI_TRAVERSAL_TIMEOUT_ENABLED, @@ -127,14 +133,13 @@ public class QueryConsumer extends TraversalConsumer { @Override public Response process() { return processExecuteQuery(content, req, versionParam, queryFormat, subgraph, - headers, info, resultIndex, resultSize, roles); + headers, info, new Pageable(resultIndex, resultSize), roles); } }); } public Response processExecuteQuery(String content, HttpServletRequest req, String versionParam, - String queryFormat, String subgraph, HttpHeaders headers, UriInfo info, String resultIndex, - String resultSize, Set<String> roles) { + String queryFormat, String subgraph, HttpHeaders headers, UriInfo info, Pageable pageable, Set<String> roles) { String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId"); String queryProcessor = headers.getRequestHeaders().getFirst("QueryProcessor"); @@ -166,7 +171,6 @@ public class QueryConsumer extends TraversalConsumer { * Changes for Pagination */ - traversalUriHttpEntry.setPaginationParameters(resultIndex, resultSize); dbEngine = traversalUriHttpEntry.getDbEngine(); if (startElement != null) { @@ -243,7 +247,10 @@ public class QueryConsumer extends TraversalConsumer { .traversalSource(isHistory(format), traversalSource).create(); } List<Object> vertTemp = processor.execute(subGraphStyle); - List<Object> vertices = traversalUriHttpEntry.getPaginatedVertexList(vertTemp); + int fromIndex = pageable.getPage() * pageable.getPageSize(); + List<Object> vertices = PaginationUtil.hasValidPaginationParams(pageable) + ? vertTemp.subList(fromIndex, fromIndex + pageable.getPageSize()) + : vertTemp; DBSerializer serializer = new DBSerializer(version, dbEngine, ModelType.MOXY, sourceOfTruth); @@ -270,10 +277,12 @@ public class QueryConsumer extends TraversalConsumer { result = xmlFormatTransformer.transform(result); } - if (traversalUriHttpEntry.isPaginated()) { + if (PaginationUtil.hasValidPaginationParams(pageable)) { + int totalCount = vertTemp.size(); + long totalPages = PaginationUtil.getTotalPages(pageable, totalCount); response = Response.status(Status.OK).type(acceptType) - .header("total-results", traversalUriHttpEntry.getTotalVertices()) - .header("total-pages", traversalUriHttpEntry.getTotalPaginationBuckets()) + .header("total-results", totalCount) + .header("total-pages", totalPages) .entity(result).build(); } else { response = Response.status(Status.OK).type(acceptType).entity(result).build(); diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/dsl/v1/DslListener.java b/aai-traversal/src/main/java/org/onap/aai/rest/dsl/v1/DslListener.java index 1ad1292..ce326df 100644 --- a/aai-traversal/src/main/java/org/onap/aai/rest/dsl/v1/DslListener.java +++ b/aai-traversal/src/main/java/org/onap/aai/rest/dsl/v1/DslListener.java @@ -40,7 +40,6 @@ import org.onap.aai.setup.SchemaVersions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Scope; -import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.stereotype.Component; /** diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/search/CustomQueryTestDTO.java b/aai-traversal/src/main/java/org/onap/aai/rest/search/CustomQueryTestDTO.java index b64f07f..5c2129a 100644 --- a/aai-traversal/src/main/java/org/onap/aai/rest/search/CustomQueryTestDTO.java +++ b/aai-traversal/src/main/java/org/onap/aai/rest/search/CustomQueryTestDTO.java @@ -28,9 +28,9 @@ package org.onap.aai.rest.search; * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -39,9 +39,9 @@ package org.onap.aai.rest.search; * ============LICENSE_END========================================================= */ -import com.beust.jcommander.internal.Maps; import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -58,10 +58,10 @@ public class CustomQueryTestDTO { private List<LinkedHashMap<String, String>> edgesDtos; @JsonProperty("optional-properties") - private Map<String, String> queryOptionalProperties = Maps.newHashMap(); + private Map<String, String> queryOptionalProperties = new HashMap<>(); @JsonProperty("required-properties") - private Map<String, String> queryRequiredProperties = Maps.newHashMap(); + private Map<String, String> queryRequiredProperties = new HashMap<>(); @JsonProperty("expected-result") private ExpectedResultsDto expectedResultsDtos; diff --git a/aai-traversal/src/main/java/org/onap/aai/rest/util/PaginationUtil.java b/aai-traversal/src/main/java/org/onap/aai/rest/util/PaginationUtil.java new file mode 100644 index 0000000..5e8c567 --- /dev/null +++ b/aai-traversal/src/main/java/org/onap/aai/rest/util/PaginationUtil.java @@ -0,0 +1,54 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2024 Deutsche Telekom. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.aai.rest.util; + +import java.util.Collections; +import java.util.List; + +import org.onap.aai.exceptions.AAIException; +import org.onap.aai.query.builder.Pageable; + +public class PaginationUtil { + + public static List<Object> getPaginatedVertexListForAggregateFormat(List<Object> aggregateVertexList, Pageable pageable) throws AAIException { + if (aggregateVertexList != null && !aggregateVertexList.isEmpty() && aggregateVertexList.size() == 1) { + List<Object> vertexList = (List<Object>) aggregateVertexList.get(0); + int fromIndex = pageable.getPage() * pageable.getPageSize(); + List<Object> page = vertexList.subList(fromIndex, fromIndex + pageable.getPageSize()); + return Collections.singletonList(page); + } + // If the list size is greater than 1 or if pagination is not needed, return the original list. + return aggregateVertexList; + } + + public static boolean hasValidPaginationParams(Pageable pageable) { + return pageable.getPage() >= 0 && pageable.getPageSize() > 0; + } + + public static long getTotalPages(Pageable pageable, long totalCount) { + int pageSize = pageable.getPageSize(); + long totalPages = totalCount / pageSize; + // conditionally add a page for the remainder + if (totalCount % pageSize > 0) { + totalPages++; + } + return totalPages; + } +} |