2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright © 2018 IBM.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.aai.query.builder;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import com.google.common.base.Joiner;
28 import com.google.common.collect.ArrayListMultimap;
29 import com.google.common.collect.Multimap;
33 import org.apache.tinkerpop.gremlin.process.traversal.Path;
34 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
35 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
36 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
37 import org.apache.tinkerpop.gremlin.structure.Direction;
38 import org.apache.tinkerpop.gremlin.structure.Edge;
39 import org.apache.tinkerpop.gremlin.structure.Vertex;
40 import org.onap.aai.db.props.AAIProperties;
41 import org.onap.aai.edges.EdgeRule;
42 import org.onap.aai.edges.EdgeRuleQuery;
43 import org.onap.aai.edges.enums.EdgeType;
44 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
45 import org.onap.aai.exceptions.AAIException;
46 import org.onap.aai.introspection.Introspector;
47 import org.onap.aai.introspection.Loader;
48 import org.onap.aai.restcore.search.GremlinGroovyShell;
49 import org.onap.aai.schema.enums.ObjectMetadata;
50 import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
53 * The Class GremlinQueryBuilder.
55 public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> {
57 private static final String ARGUMENT2 = "#!#argument#!#";
58 private static final String HAS = ".has('";
59 private static final String SINGLE_QUOTE = "'";
60 private static final String ESCAPE_SINGLE_QUOTE = "\\\'";
61 private GremlinGroovyShell gremlinGroovy = new GremlinGroovyShell();
62 private GraphTraversal<?, ?> completeTraversal = null;
63 protected List<String> list = null;
65 private static final Logger LOGGER = LoggerFactory.getLogger(QueryBuilder.class);
68 * Instantiates a new gremlin query builder.
70 * @param loader the loader
72 public GremlinQueryBuilder(Loader loader, GraphTraversalSource source) {
73 super(loader, source);
74 list = new ArrayList<>();
78 * Instantiates a new gremlin query builder.
80 * @param loader the loader
81 * @param start the start
83 public GremlinQueryBuilder(Loader loader, GraphTraversalSource source, Vertex start) {
84 super(loader, source, start);
85 list = new ArrayList<>();
90 public QueryBuilder<Vertex> exactMatchQuery(Introspector obj) {
91 // TODO not implemented because this is implementation is no longer used
92 this.createKeyQuery(obj);
93 this.createContainerQuery(obj);
94 return (QueryBuilder<Vertex>) this;
98 protected void vertexHas(String key, Object value) {
99 list.add(HAS + key + "', " + value + ")");
103 protected void vertexHasNot(String key) {
104 list.add(".hasNot('" + key + "')");
109 protected void vertexHas(String key) {
110 list.add(HAS + key + "')");
117 public QueryBuilder<Vertex> getVerticesByProperty(String key, Object value) {
120 if (value != null && !(value instanceof String)) {
121 String valueString = value.toString();
123 if (valueString.indexOf('\'') != -1) {
124 value = valueString.replace(SINGLE_QUOTE, ESCAPE_SINGLE_QUOTE);
126 LOGGER.trace("Inside getVerticesByProperty(): key = {}, value = {}", key, value);
127 term = value.toString();
128 } else if (value != null && value instanceof String) {
129 String valueString = value.toString();
131 if (valueString.indexOf('\'') != -1) {
132 value = valueString.replace(SINGLE_QUOTE, ESCAPE_SINGLE_QUOTE);
134 LOGGER.trace("Inside getVerticesByProperty(): key = {}, value = {}", key, value);
135 term = "'" + value + "'";
137 term = "'" + value + "'";
139 this.vertexHas(key, term);
141 return (QueryBuilder<Vertex>) this;
148 public QueryBuilder<Vertex> getVerticesByNumberProperty(String key, Object value) {
149 this.vertexHas(key, value);
151 return (QueryBuilder<Vertex>) this;
155 public QueryBuilder<Vertex> getVerticesByBooleanProperty(String key, Object value) {
157 if (value != null && !"".equals(value)) {
158 boolean bValue = false;
159 if (value instanceof String) {
160 bValue = Boolean.valueOf(value.toString());
161 } else if (value instanceof Boolean) {
162 bValue = (Boolean) value;
165 this.vertexHas(key, bValue);
168 return (QueryBuilder<Vertex>) this;
175 public QueryBuilder<Vertex> getVerticesByProperty(String key, List<?> values) {
177 String predicate = "P.within(#!#argument#!#)";
178 List<String> arguments = new ArrayList<>();
179 for (Object item : values) {
180 if (item != null && !(item instanceof String)) {
181 arguments.add(item.toString());
183 arguments.add("'" + item + "'");
186 String argument = Joiner.on(",").join(arguments);
187 predicate = predicate.replace(ARGUMENT2, argument);
188 this.vertexHas(key, predicate);
190 return (QueryBuilder<Vertex>) this;
197 public QueryBuilder<Vertex> getVerticesByCommaSeperatedValue(String key, String value) {
198 ArrayList<String> arguments = new ArrayList<>(Arrays.asList(value.split(",")));
199 //add the single quotes
200 for (int i = 0; i < arguments.size(); i++) {
201 if(arguments.get(i) != null && !arguments.get(i).startsWith("'")
202 && !arguments.get(i).endsWith("'")) {
203 arguments.set(i,"'" + arguments.get(i).trim() + "'");
206 arguments.set(i, arguments.get(i).trim());
209 String predicate = "P.within(#!#argument#!#)";
210 String argument = Joiner.on(",").join(arguments);
211 predicate = predicate.replace(ARGUMENT2, argument);
212 this.vertexHas(key, predicate);
214 return (QueryBuilder<Vertex>) this;
221 public QueryBuilder<Vertex> getVerticesByProperty(String key) {
224 return (QueryBuilder<Vertex>) this;
231 public QueryBuilder<Vertex> getVerticesExcludeByProperty(String key) {
232 this.vertexHasNot(key);
234 return (QueryBuilder<Vertex>) this;
241 public QueryBuilder<Vertex> getVerticesStartsWithProperty(String key, Object value) {
243 String predicate = "org.janusgraph.core.attribute.Text.textPrefix(#!#argument#!#)";
244 if (value != null && !(value instanceof String)) {
245 term = value.toString();
247 term = "'" + value + "'";
249 predicate = predicate.replace(ARGUMENT2, term);
250 this.vertexHas(key, predicate);
252 return (QueryBuilder<Vertex>) this;
259 public QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, Object value) {
262 String predicate = "P.neq(#!#argument#!#)";
263 if (value != null && !(value instanceof String)) {
264 term = value.toString();
266 term = "'" + value + "'";
268 predicate = predicate.replace(ARGUMENT2, term);
269 this.vertexHas(key, predicate);
271 return (QueryBuilder<Vertex>) this;
278 public QueryBuilder<Vertex> getVerticesExcludeByProperty(String key, List<?> values) {
280 String predicate = "P.without(#!#argument#!#)";
281 List<String> arguments = new ArrayList<>();
282 for (Object item : values) {
283 if (item != null && !(item instanceof String)) {
284 arguments.add(item.toString());
286 arguments.add("'" + item + "'");
289 String argument = Joiner.on(",").join(arguments);
290 predicate = predicate.replace(ARGUMENT2, argument);
291 this.vertexHas(key, predicate);
293 return (QueryBuilder<Vertex>) this;
297 public QueryBuilder<Vertex> getVerticesGreaterThanProperty(String key, Object value) {
298 String predicate = "P.gte(#!#argument1#!#)";
300 if (value != null && !(value instanceof String)) {
301 term = value.toString();
303 term = "'" + value + "'";
305 predicate = predicate.replace("#!#argument1#!#", term);
306 this.vertexHas(key, predicate);
308 return (QueryBuilder<Vertex>) this;
312 public QueryBuilder<Vertex> getVerticesLessThanProperty(String key, Object value) {
313 String predicate = "P.lte(#!#argument1#!#)";
315 if (value != null && !(value instanceof String)) {
316 term = value.toString();
318 term = "'" + value + "'";
320 predicate = predicate.replace("#!#argument1#!#", term);
321 this.vertexHas(key, predicate);
323 return (QueryBuilder<Vertex>) this;
330 public QueryBuilder<Vertex> getChildVerticesFromParent(String parentKey, String parentValue, String childType) {
332 return (QueryBuilder<Vertex>) this;
339 public QueryBuilder<Vertex> getTypedVerticesByMap(String type, Map<String, String> map) {
341 for (Map.Entry<String, String> es : map.entrySet()) {
342 //TODO what is this and where is it used - need to check
343 list.add(HAS + es.getKey() + "', '" + es.getValue() + "')");
346 list.add(".has('aai-node-type', '" + type + "')");
348 return (QueryBuilder<Vertex>) this;
355 public QueryBuilder<Vertex> createKeyQuery(Introspector obj) {
356 Set<String> keys = obj.getKeys();
358 for (String key : keys) {
360 this.getVerticesByProperty(key, obj.<Object>getValue(key));
363 return (QueryBuilder<Vertex>) this;
367 * @throws NoEdgeRuleFoundException
368 * @throws AAIException
372 public QueryBuilder createEdgeTraversal(EdgeType type, Introspector parent, Introspector child)
373 throws AAIException {
374 String parentName = parent.getDbName();
375 String childName = child.getDbName();
376 if (parent.isContainer()) {
377 parentName = parent.getChildDBName();
379 if (child.isContainer()) {
380 childName = child.getChildDBName();
382 this.edgeQueryToVertex(type, parentName, childName, null);
388 public QueryBuilder createPrivateEdgeTraversal(EdgeType type, Introspector parent, Introspector child)
389 throws AAIException {
390 String parentName = parent.getDbName();
391 String childName = child.getDbName();
392 if (parent.isContainer()) {
393 parentName = parent.getChildDBName();
395 if (child.isContainer()) {
396 childName = child.getChildDBName();
398 this.edgeQueryToVertex(type, parentName, childName, null, true);
407 public QueryBuilder<Vertex> createEdgeTraversalWithLabels(EdgeType type, Introspector out, Introspector in,
408 List<String> labels) throws AAIException {
409 String parentName = out.getDbName();
410 String childName = in.getDbName();
411 if (out.isContainer()) {
412 parentName = out.getChildDBName();
414 if (in.isContainer()) {
415 childName = in.getChildDBName();
417 this.edgeQueryToVertex(type, parentName, childName, labels);
418 return (QueryBuilder<Vertex>) this;
421 public QueryBuilder<Edge> getEdgesBetweenWithLabels(EdgeType type, String outNodeType, String inNodeType,
422 List<String> labels) throws AAIException {
423 this.edgeQuery(type, outNodeType, inNodeType, labels);
424 return (QueryBuilder<Edge>) this;
427 private void edgeQueryToVertex(EdgeType type, String outType, String inType, List<String> labels)
428 throws AAIException {
429 this.edgeQueryToVertex(type, outType, inType, labels, false);
435 * @param outType the out type
436 * @param inType the in type
437 * @throws NoEdgeRuleFoundException
438 * @throws AAIException
440 private void edgeQueryToVertex(EdgeType type, String outType, String inType, List<String> labels,
441 boolean isPrivateEdge) throws AAIException {
442 markParentBoundary();
443 Multimap<String, EdgeRule> rules = ArrayListMultimap.create();
444 EdgeRuleQuery.Builder qB = new EdgeRuleQuery.Builder(outType, inType).edgeType(type).setPrivate(isPrivateEdge);
447 if (labels == null) {
448 rules.putAll(edgeRules.getRules(qB.build()));
450 for (String label : labels) {
451 rules.putAll(edgeRules.getRules(qB.label(label).build()));
454 } catch (EdgeRuleNotFoundException e) {
455 throw new NoEdgeRuleFoundException(e);
458 final List<String> inLabels = new ArrayList<>();
459 final List<String> outLabels = new ArrayList<>();
461 for (EdgeRule rule : rules.values()) {
462 if (labels != null && !labels.contains(rule.getLabel())) {
465 if (Direction.IN.equals(rule.getDirection())) {
466 inLabels.add(rule.getLabel());
467 if (inType.equals(outType)) {// code to handle when a type edges to itself, to add both in and out
468 outLabels.add(rule.getLabel());
471 outLabels.add(rule.getLabel());
472 if (inType.equals(outType)) {// code to handle when a type edges to itself, to add both in and out
473 inLabels.add(rule.getLabel());
479 if (inLabels.isEmpty() && outLabels.isEmpty()) {
480 throw new NoEdgeRuleFoundException(
481 "no " + type.toString() + " edge rule between " + outType + " and " + inType);
482 } else if (inLabels.isEmpty() && !outLabels.isEmpty()) {
483 list.add(".out('" + String.join("','", outLabels) + "')");
484 } else if (outLabels.isEmpty() && !inLabels.isEmpty()) {
485 list.add(".in('" + String.join("','", inLabels) + "')");
487 list.add(".union(__.in('" + String.join("','", inLabels) + "')" + ", __.out('"
488 + String.join("','", outLabels) + "'))");
491 list.add(HAS + AAIProperties.NODE_TYPE + "', '" + inType + "')");
499 * @param outType the out type
500 * @param inType the in type
501 * @throws NoEdgeRuleFoundException
502 * @throws AAIException
504 private void edgeQuery(EdgeType type, String outType, String inType, List<String> labels) throws AAIException {
505 markParentBoundary();
506 Multimap<String, EdgeRule> rules = ArrayListMultimap.create();
507 EdgeRuleQuery.Builder qB = new EdgeRuleQuery.Builder(outType, inType).edgeType(type);
509 if (labels == null) {
510 rules.putAll(edgeRules.getRules(qB.build()));
512 for (String label : labels) {
513 rules.putAll(edgeRules.getRules(qB.label(label).build()));
516 } catch (EdgeRuleNotFoundException e) {
517 throw new NoEdgeRuleFoundException(e);
520 final List<String> inLabels = new ArrayList<>();
521 final List<String> outLabels = new ArrayList<>();
523 for (EdgeRule rule : rules.values()) {
524 if (labels != null && !labels.contains(rule.getLabel())) {
527 if (Direction.IN.equals(rule.getDirection())) {
528 inLabels.add(rule.getLabel());
530 outLabels.add(rule.getLabel());
535 if (inLabels.isEmpty() && outLabels.isEmpty()) {
536 throw new NoEdgeRuleFoundException(
537 "no " + type.toString() + " edge rule between " + outType + " and " + inType);
538 } else if (inLabels.isEmpty() && !outLabels.isEmpty()) {
539 list.add(".outE('" + String.join("','", outLabels) + "')");
540 } else if (outLabels.isEmpty() && !inLabels.isEmpty()) {
541 list.add(".inE('" + String.join("','", inLabels) + "')");
543 list.add(".union(__.inE('" + String.join("','", inLabels) + "')" + ", __.outE('"
544 + String.join("','", outLabels) + "'))");
552 public QueryBuilder<E> limit(long amount) {
553 list.add(".limit(" + amount + ")");
561 public QueryBuilder<Vertex> createContainerQuery(Introspector obj) {
562 String type = obj.getChildDBName();
563 String abstractType = obj.getMetadata(ObjectMetadata.ABSTRACT);
564 if (abstractType != null) {
565 String[] inheritors = obj.getMetadata(ObjectMetadata.INHERITORS).split(",");
566 String[] wrapped = new String[inheritors.length];
567 StringBuilder command = new StringBuilder();
568 command.append("P.within(");
569 for (int i = 0; i < inheritors.length; i++) {
570 wrapped[i] = "'" + inheritors[i] + "'";
572 command.append(Joiner.on(",").join(wrapped));
574 list.add(".has('aai-node-type', " + command + ")");
577 list.add(".has('aai-node-type', '" + type + "')");
580 this.markContainer();
581 return (QueryBuilder<Vertex>) this;
585 public QueryBuilder<E> union(QueryBuilder<E>... builder) {
586 markParentBoundary();
587 String[] traversals = new String[builder.length];
588 StringBuilder command = new StringBuilder();
589 for (int i = 0; i < builder.length; i++) {
590 traversals[i] = "__" + builder[i].getQuery();
592 command.append(".union(");
593 command.append(Joiner.on(",").join(traversals));
595 list.add(command.toString());
602 public QueryBuilder<E> where(QueryBuilder<E>... builder) {
603 markParentBoundary();
604 List<String> traversals = new ArrayList<>();
605 for (int i = 0; i < builder.length; i++) {
606 traversals.add(".where(__" + builder[i].getQuery() + ")");
609 list.addAll(traversals);
615 public QueryBuilder<E> or(QueryBuilder<E>... builder) {
616 markParentBoundary();
617 String[] traversals = new String[builder.length];
618 StringBuilder command = new StringBuilder();
619 for (int i = 0; i < builder.length; i++) {
620 traversals[i] = "__" + builder[i].getQuery();
622 command.append(".or(");
623 command.append(Joiner.on(",").join(traversals));
625 list.add(command.toString());
632 public QueryBuilder<E> store(String name) {
633 this.list.add(".store('" + name + "')");
640 public QueryBuilder<E> cap(String name) {
641 this.list.add(".cap('" + name + "')");
648 public QueryBuilder<E> unfold() {
649 this.list.add(".unfold()");
656 public QueryBuilder<E> fold() {
657 this.list.add(".fold()");
663 public QueryBuilder<E> id() {
664 this.list.add(".id()");
670 public QueryBuilder<E> dedup() {
671 this.list.add(".dedup()");
678 public QueryBuilder<E> emit() {
679 this.list.add(".emit()");
686 public QueryBuilder<E> repeat(QueryBuilder<E> builder) {
687 this.list.add(".repeat(__" + builder.getQuery() + ")");
694 public QueryBuilder<E> until(QueryBuilder<E> builder) {
695 this.list.add(".until(__" + builder.getQuery() + ")");
702 public QueryBuilder<E> groupCount() {
703 this.list.add(".groupCount()");
710 public QueryBuilder<E> both() {
711 this.list.add(".both()");
718 public QueryBuilder<Tree> tree() {
719 this.list.add(".tree()");
722 return (QueryBuilder<Tree>) this;
726 public QueryBuilder<E> by(String name) {
727 this.list.add(".by('" + name + "')");
734 public QueryBuilder<E> valueMap() {
735 this.list.add(".valueMap()");
742 public QueryBuilder<E> valueMap(String... names) {
743 String stepString = ".valueMap('";
744 for (int i = 0; i < names.length; i++) {
745 stepString = stepString + names[i] + "'";
746 if (i != (names.length - 1)) {
747 stepString = stepString + ",'";
750 stepString = stepString + ")";
751 this.list.add(stepString);
762 public QueryBuilder<E> simplePath() {
763 this.list.add(".simplePath()");
772 public QueryBuilder<Path> path() {
773 this.list.add(".path()");
775 return (QueryBuilder<Path>) this;
779 public QueryBuilder<Edge> outE() {
780 this.list.add(".outE()");
783 return (QueryBuilder<Edge>) this;
787 public QueryBuilder<Edge> inE() {
788 this.list.add(".inE()");
791 return (QueryBuilder<Edge>) this;
795 public QueryBuilder<Vertex> outV() {
796 this.list.add(".outV()");
799 return (QueryBuilder<Vertex>) this;
803 public QueryBuilder<Vertex> inV() {
804 this.list.add(".inV()");
807 return (QueryBuilder<Vertex>) this;
811 public QueryBuilder<E> not(QueryBuilder<E> builder) {
812 this.list.add(".not(" + "__" + builder.getQuery() + ")");
819 public QueryBuilder<E> as(String name) {
820 this.list.add(".as('" + name + "')");
827 public QueryBuilder<E> select(String name) {
828 this.list.add(".select('" + name + "')");
835 public QueryBuilder<E> select(String... names) {
836 String stepString = ".select('";
837 for (int i = 0; i < names.length; i++) {
838 stepString = stepString + names[i] + "'";
839 if (i != (names.length - 1)) {
840 stepString = stepString + ",'";
843 stepString = stepString + ")";
844 this.list.add(stepString);
854 public QueryBuilder<E> getParentQuery() {
855 return cloneQueryAtStep(parentStepIndex);
859 public QueryBuilder<E> getContainerQuery() {
860 return cloneQueryAtStep(containerStepIndex);
867 public <T2> T2 getQuery() {
868 StringBuilder sb = new StringBuilder();
870 for (String piece : this.list) {
874 return (T2) sb.toString();
881 public void markParentBoundary() {
882 parentStepIndex = stepIndex;
886 public void markContainer() {
887 this.containerStepIndex = stepIndex;
894 public Vertex getStart() {
898 protected int getParentStepIndex() {
899 return parentStepIndex;
902 protected int getContainerStepIndex() {
903 return containerStepIndex;
906 protected int getStepIndex() {
910 private void executeQuery() {
911 String queryString = "g" + Joiner.on("").join(list);
912 Map<String, Object> params = new HashMap<>();
913 if (this.start == null) {
914 params.put("g", source.V());
916 params.put("g", source.V(this.start));
918 this.completeTraversal = this.gremlinGroovy.executeTraversal(queryString, params);
922 public boolean hasNext() {
923 if (this.completeTraversal == null) {
927 return this.completeTraversal.hasNext();
932 if (this.completeTraversal == null) {
936 return (E) this.completeTraversal.next();
940 public List<E> toList() {
941 if (this.completeTraversal == null) {
945 return (List<E>) this.completeTraversal.toList();
948 protected QueryBuilder<Edge> has(String key, String value) {
949 this.list.add(HAS + key + "','" + value + "')");
951 return (QueryBuilder<Edge>) this;
955 * This is required for the subgraphstrategies to work