diff options
Diffstat (limited to 'aai-traversal/src/main/java/org/onap/aai/rest/QueryConsumer.java')
-rw-r--r-- | aai-traversal/src/main/java/org/onap/aai/rest/QueryConsumer.java | 253 |
1 files changed, 110 insertions, 143 deletions
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 633bc9c..d91e50d 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 @@ -19,38 +19,16 @@ */ package org.onap.aai.rest; -import java.net.URI; -import java.util.ArrayList; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Optional; -import java.util.Set; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; - -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.Consumes; -import javax.ws.rs.DefaultValue; -import javax.ws.rs.Encoded; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.Status; -import javax.ws.rs.core.UriInfo; - +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.onap.aai.concurrent.AaiCallable; -import org.onap.aai.dbmap.DBConnectionType; 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.rest.db.HttpEntry; import org.onap.aai.rest.search.CustomQueryConfig; @@ -58,7 +36,6 @@ 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.restcore.HttpMethod; -import org.onap.aai.restcore.RESTAPI; import org.onap.aai.restcore.util.URITools; import org.onap.aai.serialization.db.DBSerializer; import org.onap.aai.serialization.engines.QueryStyle; @@ -67,33 +44,28 @@ import org.onap.aai.serialization.queryformats.Format; import org.onap.aai.serialization.queryformats.FormatFactory; import org.onap.aai.serialization.queryformats.Formatter; import org.onap.aai.serialization.queryformats.SubGraphStyle; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import org.onap.aai.logging.LoggingContext; -import org.onap.aai.logging.StopWatch; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import org.onap.aai.setup.SchemaVersion; import org.onap.aai.setup.SchemaVersions; -import org.onap.aai.util.AAIConstants; +import org.onap.aai.transforms.XmlFormatTransformer; import org.onap.aai.util.TraversalConstants; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import javax.ws.rs.*; +import javax.ws.rs.core.*; +import javax.ws.rs.core.Response.Status; +import java.net.URI; +import java.util.*; +import java.util.stream.Collectors; + @Path("{version: v[1-9][0-9]*|latest}/query") -public class QueryConsumer extends RESTAPI { - - /** The introspector factory type. */ - private ModelType introspectorFactoryType = ModelType.MOXY; - +public class QueryConsumer extends TraversalConsumer { + private QueryProcessorType processorType = QueryProcessorType.LOCAL_GROOVY; - /** The query style. */ - private QueryStyle queryStyle = QueryStyle.TRAVERSAL; - - private static final String TARGET_ENTITY = "DB"; - private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(QueryConsumer.class); + + private static final Logger LOGGER = LoggerFactory.getLogger(QueryConsumer.class); private HttpEntry traversalUriHttpEntry; @@ -104,23 +76,29 @@ public class QueryConsumer extends RESTAPI { private GremlinServerSingleton gremlinServerSingleton; + private XmlFormatTransformer xmlFormatTransformer; + @Autowired - public QueryConsumer( - HttpEntry traversalUriHttpEntry, - SchemaVersions schemaVersions, - GremlinServerSingleton gremlinServerSingleton, - @Value("${schema.uri.base.path}") String basePath - ){ + public QueryConsumer(HttpEntry traversalUriHttpEntry, SchemaVersions schemaVersions, + GremlinServerSingleton gremlinServerSingleton, XmlFormatTransformer xmlFormatTransformer, @Value("${schema.uri.base.path}") String basePath) { this.traversalUriHttpEntry = traversalUriHttpEntry; this.schemaVersions = schemaVersions; this.gremlinServerSingleton = gremlinServerSingleton; this.basePath = basePath; + this.xmlFormatTransformer = xmlFormatTransformer; } @PUT @Consumes({ MediaType.APPLICATION_JSON}) - @Produces({ MediaType.APPLICATION_JSON}) - public Response executeQuery(String content, @PathParam("version")String versionParam, @PathParam("uri") @Encoded String uri, @DefaultValue("graphson") @QueryParam("format") String queryFormat,@DefaultValue("no_op") @QueryParam("subgraph") String subgraph, @Context HttpHeaders headers, @Context UriInfo info, @Context HttpServletRequest req, @DefaultValue("-1") @QueryParam("resultIndex") String resultIndex, @DefaultValue("-1") @QueryParam("resultSize") String resultSize){ + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + 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 UriInfo info, + @DefaultValue("-1") @QueryParam("resultIndex") String resultIndex, + @DefaultValue("-1") @QueryParam("resultSize") String resultSize) { return runner(TraversalConstants.AAI_TRAVERSAL_TIMEOUT_ENABLED, TraversalConstants.AAI_TRAVERSAL_TIMEOUT_APP, TraversalConstants.AAI_TRAVERSAL_TIMEOUT_LIMIT, @@ -128,34 +106,33 @@ public class QueryConsumer extends RESTAPI { info, HttpMethod.GET, new AaiCallable<Response>() { - @Override - public Response process() { - return processExecuteQuery(content, versionParam, uri, queryFormat, subgraph, headers, info, req, resultIndex, resultSize); - } - } - ); + @Override + public Response process() { + return processExecuteQuery(content, versionParam, queryFormat, subgraph, headers, info, resultIndex, resultSize); + } + }); } - public Response processExecuteQuery(String content, @PathParam("version")String versionParam, @PathParam("uri") @Encoded String uri, @DefaultValue("graphson") @QueryParam("format") String queryFormat,@DefaultValue("no_op") @QueryParam("subgraph") String subgraph, @Context HttpHeaders headers, @Context UriInfo info, @Context HttpServletRequest req, @DefaultValue("-1") @QueryParam("resultIndex") String resultIndex, @DefaultValue("-1") @QueryParam("resultSize") String resultSize) { - String methodName = "executeQuery"; + public Response processExecuteQuery(String content, String versionParam, String queryFormat, String subgraph, + HttpHeaders headers, UriInfo info, String resultIndex, + String resultSize) { + String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId"); - String realTime = headers.getRequestHeaders().getFirst("Real-Time"); String queryProcessor = headers.getRequestHeaders().getFirst("QueryProcessor"); QueryProcessorType processorType = this.processorType; - Response response = null; + Response response; TransactionalGraphEngine dbEngine = null; + try { - LoggingContext.save(); this.checkQueryParams(info.getQueryParameters()); Format format = Format.getFormat(queryFormat); if (queryProcessor != null) { processorType = QueryProcessorType.valueOf(queryProcessor); } SubGraphStyle subGraphStyle = SubGraphStyle.valueOf(subgraph); + JsonParser parser = new JsonParser(); - JsonObject input = parser.parse(content).getAsJsonObject(); - JsonElement startElement = input.get("start"); JsonElement queryElement = input.get("query"); JsonElement gremlinElement = input.get("gremlin"); @@ -164,8 +141,7 @@ public class QueryConsumer extends RESTAPI { String gremlin = ""; SchemaVersion version = new SchemaVersion(versionParam); - DBConnectionType type = this.determineConnectionType(sourceOfTruth, realTime); - traversalUriHttpEntry.setHttpEntryProperties(version, type); + traversalUriHttpEntry.setHttpEntryProperties(version); /* * Changes for Pagination */ @@ -196,76 +172,87 @@ public class QueryConsumer extends RESTAPI { List<String> missingRequiredQueryParameters = checkForMissingQueryParameters( customQueryConfig.getQueryRequiredProperties(), URITools.getQueryMap(queryURIObj)); if ( !missingRequiredQueryParameters.isEmpty() ) { - return( createMessageMissingQueryRequiredParameters( missingRequiredQueryParameters, headers, info, req)); + return( createMessageMissingQueryRequiredParameters( missingRequiredQueryParameters, headers)); } List<String> invalidQueryParameters = checkForInvalidQueryParameters( customQueryConfig, URITools.getQueryMap(queryURIObj)); if ( !invalidQueryParameters.isEmpty() ) { - return( createMessageInvalidQueryParameters( invalidQueryParameters, headers, info, req)); + return( createMessageInvalidQueryParameters( invalidQueryParameters, headers)); } } else if ( queryElement != null ) { - return( createMessageInvalidQuerySection( queryURI, headers, info, req)); + return (createMessageInvalidQuerySection(queryURI, headers)); } - - GenericQueryProcessor processor = null; - - LoggingContext.targetEntity(TARGET_ENTITY); - LoggingContext.targetServiceName(methodName); - LoggingContext.startTime(); - StopWatch.conditionalStart(); + GenericQueryProcessor processor; + + if(isHistory(format)){ + validateHistoryParams(format, info.getQueryParameters()); + } + GraphTraversalSource traversalSource = getTraversalSource(dbEngine, format, info); + QueryStyle queryStyle = getQueryStyle(format, traversalUriHttpEntry); if (!startURIs.isEmpty()) { Set<Vertex> vertexSet = new LinkedHashSet<>(); QueryParser uriQuery; List<Vertex> vertices; for (URI startUri : startURIs) { - uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(startUri, URITools.getQueryMap(startUri)); + uriQuery = dbEngine.getQueryBuilder(queryStyle, traversalSource).createQueryFromURI(startUri, URITools.getQueryMap(startUri)); vertices = uriQuery.getQueryBuilder().toList(); vertexSet.addAll(vertices); } - processor = new GenericQueryProcessor.Builder(dbEngine, gremlinServerSingleton) - .startFrom(vertexSet).queryFrom(queryURIObj) - .processWith(processorType).create(); + .startFrom(vertexSet).queryFrom(queryURIObj).format(format) + .processWith(processorType).traversalSource(isHistory(format), traversalSource).create(); } else if (!queryURI.equals("")){ processor = new GenericQueryProcessor.Builder(dbEngine, gremlinServerSingleton) .queryFrom(queryURIObj) - .processWith(processorType).create(); + .processWith(processorType).traversalSource(isHistory(format), traversalSource).create(); } else { processor = new GenericQueryProcessor.Builder(dbEngine, gremlinServerSingleton) .queryFrom(gremlin, "gremlin") - .processWith(processorType).create(); + .processWith(processorType).traversalSource(isHistory(format), traversalSource).create(); } - String result = ""; List<Object> vertTemp = processor.execute(subGraphStyle); List<Object> vertices = traversalUriHttpEntry.getPaginatedVertexList(vertTemp); - - DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, sourceOfTruth); + + DBSerializer serializer = new DBSerializer(version, dbEngine, ModelType.MOXY, sourceOfTruth); FormatFactory ff = new FormatFactory(traversalUriHttpEntry.getLoader(), serializer, schemaVersions, this.basePath); - - Formatter formater = ff.get(format, info.getQueryParameters()); - - result = formater.output(vertices).toString(); - double msecs = StopWatch.stopIfStarted(); - LoggingContext.elapsedTime((long)msecs,TimeUnit.MILLISECONDS); - LoggingContext.successStatusFields(); - LOGGER.info ("Completed"); - + MultivaluedMap<String, String> mvm = new MultivaluedHashMap<>(); + mvm.putAll(info.getQueryParameters()); + 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 = formatter.output(vertices).toString(); + + //LOGGER.info ("Completed"); + + String acceptType = headers.getHeaderString("Accept"); + + if(acceptType == null){ + acceptType = MediaType.APPLICATION_JSON; + } + + if(MediaType.APPLICATION_XML_TYPE.isCompatible(MediaType.valueOf(acceptType))){ + result = xmlFormatTransformer.transform(result); + } + if(traversalUriHttpEntry.isPaginated()){ response = Response.status(Status.OK) - .type(MediaType.APPLICATION_JSON) + .type(acceptType) .header("total-results", traversalUriHttpEntry.getTotalVertices()) .header("total-pages", traversalUriHttpEntry.getTotalPaginationBuckets()) .entity(result) .build(); }else { response = Response.status(Status.OK) - .type(MediaType.APPLICATION_JSON) + .type(acceptType) .entity(result).build(); } } catch (AAIException e) { @@ -274,8 +261,6 @@ public class QueryConsumer extends RESTAPI { AAIException ex = new AAIException("AAI_4000", e); response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET, ex); } finally { - LoggingContext.restoreIfPossible(); - LoggingContext.successStatusFields(); if (dbEngine != null) { dbEngine.rollback(); } @@ -289,7 +274,7 @@ public class QueryConsumer extends RESTAPI { if (params.containsKey("depth") && params.getFirst("depth").matches("\\d+")) { String depth = params.getFirst("depth"); - Integer i = Integer.parseInt(depth); + int i = Integer.parseInt(depth); if (i > 1) { throw new AAIException("AAI_3303"); } @@ -310,8 +295,6 @@ public class QueryConsumer extends RESTAPI { } private CustomQueryConfig getCustomQueryConfig(URI uriObj ) { - - CustomQueryConfig customQueryConfig; String path = uriObj.getPath(); String[] parts = path.split("/"); @@ -329,77 +312,61 @@ public class QueryConsumer extends RESTAPI { } - private Response createMessageMissingQueryRequiredParameters(List<String> missingRequiredQueryParams, HttpHeaders headers, UriInfo info, HttpServletRequest req) { + private Response createMessageMissingQueryRequiredParameters(List<String> missingRequiredQueryParams, HttpHeaders headers) { AAIException e = new AAIException("AAI_3013"); ArrayList<String> templateVars = new ArrayList<>(); + templateVars.add(missingRequiredQueryParams.toString()); - if (templateVars.isEmpty()) { - templateVars.add(missingRequiredQueryParams.toString()); - } - - Response response = Response + return Response .status(e.getErrorObject().getHTTPResponseCode()) .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), e, templateVars)).build(); - - return response; - } + } - private Response createMessageInvalidQuerySection(String invalidQuery, HttpHeaders headers, UriInfo info, HttpServletRequest req) { + private Response createMessageInvalidQuerySection(String invalidQuery, HttpHeaders headers) { AAIException e = new AAIException("AAI_3014"); ArrayList<String> templateVars = new ArrayList<>(); + templateVars.add(invalidQuery); - if (templateVars.isEmpty()) { - templateVars.add(invalidQuery); - } - - Response response = Response + return Response .status(e.getErrorObject().getHTTPResponseCode()) .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), e, templateVars)).build(); - - return response; - } + } - public List<String> checkForInvalidQueryParameters( CustomQueryConfig customQueryConfig, MultivaluedMap<String, String> queryParams) { + private List<String> checkForInvalidQueryParameters( CustomQueryConfig customQueryConfig, MultivaluedMap<String, String> queryParams) { - List<String> allParameters = new ArrayList<String>(); + List<String> allParameters = new ArrayList<>(); /* * Add potential Required and Optional to allParameters */ Optional.ofNullable(customQueryConfig.getQueryOptionalProperties()).ifPresent(allParameters::addAll); Optional.ofNullable(customQueryConfig.getQueryRequiredProperties()).ifPresent(allParameters::addAll); - if(queryParams.isEmpty()) + if(queryParams.isEmpty()) { return new ArrayList<>(); - List<String> invalidParameters = queryParams.keySet().stream() - .filter(param -> !allParameters.contains(param)) - .collect(Collectors.toList()); - - return invalidParameters; - + } + return queryParams.keySet().stream() + .filter(param -> !allParameters.contains(param)) + .collect(Collectors.toList()); } - private Response createMessageInvalidQueryParameters(List<String> invalidQueryParams, HttpHeaders headers, UriInfo info, HttpServletRequest req) { + private Response createMessageInvalidQueryParameters(List<String> invalidQueryParams, HttpHeaders headers) { AAIException e = new AAIException("AAI_3022"); ArrayList<String> templateVars = new ArrayList<>(); + templateVars.add(invalidQueryParams.toString()); - if (templateVars.isEmpty()) { - templateVars.add(invalidQueryParams.toString()); - } - - Response response = Response + return Response .status(e.getErrorObject().getHTTPResponseCode()) - .entity(ErrorLogHelper.getRESTAPIErrorResponse(headers.getAcceptableMediaTypes(), e, + .entity(ErrorLogHelper.getRESTAPIErrorResponse( + headers.getAcceptableMediaTypes(), + e, templateVars)).build(); - return response; - } - - + } } |