2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.rest.search;
24 import java.io.FileNotFoundException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.Optional;
33 import java.util.Vector;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
37 import javax.ws.rs.core.MultivaluedHashMap;
38 import javax.ws.rs.core.MultivaluedMap;
40 import org.apache.tinkerpop.gremlin.process.traversal.P;
41 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
42 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
43 import org.apache.tinkerpop.gremlin.structure.Graph;
44 import org.apache.tinkerpop.gremlin.structure.Vertex;
45 import org.javatuples.Pair;
46 import org.onap.aai.query.builder.MissingOptionalParameter;
47 import org.onap.aai.restcore.util.URITools;
48 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
49 import org.onap.aai.serialization.queryformats.SubGraphStyle;
51 import jersey.repackaged.com.google.common.base.Joiner;
53 public abstract class GenericQueryProcessor {
55 protected final Optional<URI> uri;
56 protected final MultivaluedMap<String, String> queryParams;
57 protected final Optional<Collection<Vertex>> vertices;
58 protected static Pattern p = Pattern.compile("query/(.*+)");
59 protected Optional<String> gremlin;
60 protected final TransactionalGraphEngine dbEngine;
61 protected static GremlinServerSingleton gremlinServerSingleton = GremlinServerSingleton.getInstance();
62 protected static GroovyQueryBuilderSingleton queryBuilderSingleton = GroovyQueryBuilderSingleton.getInstance();
63 protected final boolean isGremlin;
65 protected GenericQueryProcessor(Builder builder) {
66 this.uri = builder.getUri();
67 this.dbEngine = builder.getDbEngine();
68 this.vertices = builder.getVertices();
69 this.gremlin = builder.getGremlin();
70 this.isGremlin = builder.isGremlin();
71 if (uri.isPresent()) {
72 queryParams = URITools.getQueryMap(uri.get());
74 queryParams = new MultivaluedHashMap<>();
78 protected abstract GraphTraversal<?,?> runQuery(String query, Map<String, Object> params);
80 protected List<Object> processSubGraph(SubGraphStyle style, GraphTraversal<?,?> g) {
81 final List<Object> resultVertices = new Vector<>();
84 if (SubGraphStyle.prune.equals(style) || SubGraphStyle.star.equals(style)) {
86 if (SubGraphStyle.prune.equals(style)) {
87 g.where(__.otherV().where(P.within("x")));
89 g.dedup().subgraph("subGraph").cap("subGraph").map(x -> (Graph)x.get()).next().traversal().V().forEachRemaining(x -> {
90 resultVertices.add(x);
93 resultVertices.addAll(g.toList());
95 return resultVertices;
98 public List<Object> execute(SubGraphStyle style) throws FileNotFoundException {
99 final List<Object> resultVertices;
101 Pair<String, Map<String, Object>> tuple = this.createQuery();
102 String query = tuple.getValue0();
103 Map<String, Object> params = tuple.getValue1();
105 if (query.equals("") && (vertices.isPresent() && vertices.get().isEmpty())) {
106 //nothing to do, just exit
107 return new ArrayList<>();
109 GraphTraversal<?,?> g = this.runQuery(query, params);
111 resultVertices = this.processSubGraph(style, g);
113 return resultVertices;
116 protected Pair<String, Map<String, Object>> createQuery() {
117 Map<String, Object> params = new HashMap<>();
119 if (!this.isGremlin) {
120 Matcher m = p.matcher(uri.get().getPath());
121 String queryName = "";
122 List<String> optionalParameters = Collections.emptyList();
124 queryName = m.group(1);
125 CustomQueryConfig queryConfig = gremlinServerSingleton.getCustomQueryConfig(queryName);
126 if ( queryConfig != null ) {
127 query = queryConfig.getQuery();
128 optionalParameters = queryConfig.getQueryOptionalProperties();
132 for (String key : queryParams.keySet()) {
133 params.put(key, queryParams.getFirst(key));
134 if ( optionalParameters.contains(key) ){
135 optionalParameters.remove(key);
139 if (!optionalParameters.isEmpty()){
140 MissingOptionalParameter missingParameter = MissingOptionalParameter.getInstance();
141 for ( String key : optionalParameters ) {
142 params.put(key, missingParameter);
146 List<Object> ids = new ArrayList<>();
148 if (vertices.isPresent() && !vertices.get().isEmpty()) {
149 for (Vertex v : vertices.get()) {
155 query = queryBuilderSingleton.executeTraversal(dbEngine, query, params);
157 StringBuilder sb = new StringBuilder();
159 sb.append(Joiner.on(",").join(ids));
161 String startPrefix = "aaiStartQuery = " + sb.toString() + " as Object[];g.V(aaiStartQuery)";
162 if (!"".equals(query)) {
163 query = startPrefix + query;
170 query = gremlin.get();
173 return new Pair<>(query, params);
176 public static class Builder {
178 private final TransactionalGraphEngine dbEngine;
179 private Optional<URI> uri = Optional.empty();
180 private Optional<String> gremlin = Optional.empty();
181 private boolean isGremlin = false;
182 private Optional<Collection<Vertex>> vertices = Optional.empty();
183 private QueryProcessorType processorType = QueryProcessorType.GREMLIN_SERVER;
185 public Builder(TransactionalGraphEngine dbEngine) {
186 this.dbEngine = dbEngine;
189 public Builder queryFrom(URI uri) {
190 this.uri = Optional.of(uri);
191 this.isGremlin = false;
195 public Builder startFrom(Collection<Vertex> vertices) {
196 this.vertices = Optional.of(vertices);
200 public Builder queryFrom(String gremlin) {
201 this.gremlin = Optional.of(gremlin);
202 this.isGremlin = true;
206 public Builder processWith(QueryProcessorType type) {
207 this.processorType = type;
210 public TransactionalGraphEngine getDbEngine() {
214 public Optional<URI> getUri() {
218 public Optional<String> getGremlin() {
222 public boolean isGremlin() {
226 public Optional<Collection<Vertex>> getVertices() {
230 public QueryProcessorType getProcessorType() {
231 return processorType;
234 public GenericQueryProcessor create() {
236 if (this.getProcessorType().equals(QueryProcessorType.GREMLIN_SERVER)) {
237 return new GremlinServerImpl(this);
238 } else if (this.getProcessorType().equals(QueryProcessorType.LOCAL_GROOVY)) {
239 return new GroovyShellImpl(this);
241 return new GremlinServerImpl(this);