summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaharajh, Robby (rx2202) <rx2202@us.att.com>2018-03-22 16:12:12 -0400
committerMaharajh, Robby (rx2202) <rx2202@att.com>2018-03-28 18:20:49 -0400
commitf0d0c453645d19788da666d0e3c7c6ed9bb2516a (patch)
tree34c3cdb142b76320d356624f0991d9d1ae3df687
parent8824d311caf6023340e3663f4ff9f61dea8e4241 (diff)
Add to aai-common query builder get parent step
Issue-ID: AAI-962 Change-Id: Ib4aa4ec3453f2c7099e1d7191917b44b323f2bb6 Signed-off-by: Maharajh, Robby (rx2202) <rx2202@att.com>
-rw-r--r--aai-core/src/main/java/org/onap/aai/query/builder/GraphTraversalBuilder.java16
-rw-r--r--aai-core/src/main/java/org/onap/aai/query/builder/GremlinQueryBuilder.java18
-rw-r--r--aai-core/src/main/java/org/onap/aai/query/builder/QueryBuilder.java39
-rw-r--r--aai-core/src/test/java/org/onap/aai/query/builder/GremlinTraversalTest.java11
-rw-r--r--aai-core/src/test/java/org/onap/aai/query/builder/QueryBuilderTestAbstraction.java40
-rw-r--r--aai-core/src/test/java/org/onap/aai/query/builder/TraversalQueryTest.java11
-rw-r--r--aai-core/src/test/resources/dbedgerules/DbEdgeRules_TraversalQueryTest.json12
7 files changed, 144 insertions, 3 deletions
diff --git a/aai-core/src/main/java/org/onap/aai/query/builder/GraphTraversalBuilder.java b/aai-core/src/main/java/org/onap/aai/query/builder/GraphTraversalBuilder.java
index 85b484a7..b298dbb9 100644
--- a/aai-core/src/main/java/org/onap/aai/query/builder/GraphTraversalBuilder.java
+++ b/aai-core/src/main/java/org/onap/aai/query/builder/GraphTraversalBuilder.java
@@ -26,6 +26,7 @@ import java.util.Optional;
import java.util.Set;
import org.apache.tinkerpop.gremlin.process.traversal.P;
+import org.apache.tinkerpop.gremlin.process.traversal.Path;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal.Admin;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
@@ -477,6 +478,16 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> {
stepIndex++;
return this;
}
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public QueryBuilder<Path> path(){
+ this.traversal.path();
+ stepIndex++;
+ return (QueryBuilder<Path>)this;
+ }
@Override
public QueryBuilder<Edge> outE() {
@@ -757,4 +768,9 @@ public abstract class GraphTraversalBuilder<E> extends QueryBuilder<E> {
return this.completeTraversal.toList();
}
+ protected QueryBuilder<Edge> has(String key, String value) {
+ traversal.has(key, value);
+
+ return (QueryBuilder<Edge>)this;
+ }
}
diff --git a/aai-core/src/main/java/org/onap/aai/query/builder/GremlinQueryBuilder.java b/aai-core/src/main/java/org/onap/aai/query/builder/GremlinQueryBuilder.java
index 55faa4dc..010dfa50 100644
--- a/aai-core/src/main/java/org/onap/aai/query/builder/GremlinQueryBuilder.java
+++ b/aai-core/src/main/java/org/onap/aai/query/builder/GremlinQueryBuilder.java
@@ -25,6 +25,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
+import com.google.common.base.Joiner;
+import org.apache.tinkerpop.gremlin.process.traversal.Path;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Direction;
@@ -525,6 +527,16 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> {
stepIndex++;
return this;
}
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public QueryBuilder<Path> path(){
+ this.list.add(".path()");
+ stepIndex++;
+ return (QueryBuilder<Path>)this;
+ }
@Override
public QueryBuilder<Edge> outE() {
@@ -678,5 +690,11 @@ public abstract class GremlinQueryBuilder<E> extends QueryBuilder<E> {
return (List<E>)this.completeTraversal.toList();
}
+
+ protected QueryBuilder<Edge> has(String key, String value) {
+ this.list.add(".has('" + key + "','" + value + "')");
+
+ return (QueryBuilder<Edge>)this;
+ }
}
diff --git a/aai-core/src/main/java/org/onap/aai/query/builder/QueryBuilder.java b/aai-core/src/main/java/org/onap/aai/query/builder/QueryBuilder.java
index 89c15f25..0350aba2 100644
--- a/aai-core/src/main/java/org/onap/aai/query/builder/QueryBuilder.java
+++ b/aai-core/src/main/java/org/onap/aai/query/builder/QueryBuilder.java
@@ -27,6 +27,7 @@ import java.util.Map;
import javax.ws.rs.core.MultivaluedMap;
+import org.apache.tinkerpop.gremlin.process.traversal.Path;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
@@ -36,6 +37,8 @@ import org.onap.aai.introspection.Introspector;
import org.onap.aai.introspection.Loader;
import org.onap.aai.parsers.query.QueryParser;
import org.onap.aai.parsers.query.QueryParserStrategy;
+import org.onap.aai.serialization.db.AAIDirection;
+import org.onap.aai.serialization.db.EdgeProperty;
import org.onap.aai.serialization.db.EdgeType;
/**
@@ -405,16 +408,48 @@ public abstract class QueryBuilder<E> implements Iterator<E> {
* @return a QueryBuilder with the simplePath step appended to its traversal
*/
public abstract QueryBuilder<E> simplePath();
+
+ /**
+ *
+ * @return QueryBuilder with the path step appended to its traversal
+ */
+ public abstract QueryBuilder<Path> path();
public abstract void markContainer();
public abstract QueryBuilder<E> getContainerQuery();
public abstract List<E> toList();
-
+
+ /**
+ * Used to skip step if there is an optional property missing.
+ * @param key
+ * @param value
+ * @return
+ */
public QueryBuilder<Vertex> getVerticesByProperty(String key, MissingOptionalParameter value) {
return (QueryBuilder<Vertex>) this;
}
-
+ /**
+ * TODO the edge direction is hardcoded here, make it more generic
+ * Returns the parent edge of the vertex
+ * @return
+ */
+ public QueryBuilder<Edge> getParentEdge() {
+ this.outE().has(EdgeProperty.CONTAINS.toString(), AAIDirection.IN.toString());
+ return (QueryBuilder<Edge>)this;
+ }
+
+ /**
+ * TODO the edge direction is hardcoded here, make it more generic
+ * Returns the parent vertex of the vertex
+ * @return
+ */
+ public QueryBuilder<Vertex> getParentVertex() {
+ this.getParentEdge().inV();
+ return (QueryBuilder<Vertex>)this;
+ }
+
+ protected abstract QueryBuilder<Edge> has(String key, String value);
}
diff --git a/aai-core/src/test/java/org/onap/aai/query/builder/GremlinTraversalTest.java b/aai-core/src/test/java/org/onap/aai/query/builder/GremlinTraversalTest.java
index 3632543f..18f4714c 100644
--- a/aai-core/src/test/java/org/onap/aai/query/builder/GremlinTraversalTest.java
+++ b/aai-core/src/test/java/org/onap/aai/query/builder/GremlinTraversalTest.java
@@ -19,6 +19,7 @@
*/
package org.onap.aai.query.builder;
+import org.apache.tinkerpop.gremlin.process.traversal.Path;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
@@ -55,5 +56,15 @@ public class GremlinTraversalTest extends QueryBuilderTestAbstraction {
return new GremlinTraversal<>(loader, g, testEdgeRules);
}
+ @Override
+ protected QueryBuilder<Path> getNewPathTraversalWithTestEdgeRules(Vertex v) {
+ return new GremlinTraversal<>(loader, g, v, testEdgeRules);
+ }
+
+ @Override
+ protected QueryBuilder<Path> getNewPathTraversalWithTestEdgeRules() {
+ return new GremlinTraversal<>(loader, g, testEdgeRules);
+ }
+
}
diff --git a/aai-core/src/test/java/org/onap/aai/query/builder/QueryBuilderTestAbstraction.java b/aai-core/src/test/java/org/onap/aai/query/builder/QueryBuilderTestAbstraction.java
index 6d45d947..de1ed44c 100644
--- a/aai-core/src/test/java/org/onap/aai/query/builder/QueryBuilderTestAbstraction.java
+++ b/aai-core/src/test/java/org/onap/aai/query/builder/QueryBuilderTestAbstraction.java
@@ -19,13 +19,14 @@
*/
package org.onap.aai.query.builder;
-import org.janusgraph.core.JanusGraphFactory;
+import org.apache.tinkerpop.gremlin.process.traversal.Path;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.BulkSet;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.janusgraph.core.JanusGraphFactory;
import org.junit.*;
import org.onap.aai.AAISetup;
import org.onap.aai.db.props.AAIProperties;
@@ -42,6 +43,8 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.junit.Assert.*;
public abstract class QueryBuilderTestAbstraction extends AAISetup {
@@ -105,6 +108,38 @@ public abstract class QueryBuilderTestAbstraction extends AAISetup {
}
+
+ @SuppressWarnings("rawtypes")
+ @Test
+ public void createEdgeLinterfaceToLogicalLinkTraversal_Path() throws AAIException {
+ Vertex pInterface = g.addV("aai-node-type","p-interface","interface-name","p-interface-a").next();
+ Vertex lInterface = g.addV("aai-node-type","l-interface","interface-name","l-interface-a").next();
+ Vertex logicalLink = g.addV("aai-node-type","logical-link","link-name","logical-link-a").next();
+ testEdgeRules.addEdge(g, lInterface, logicalLink);
+ testEdgeRules.addTreeEdge(g, pInterface, lInterface);
+
+ QueryBuilder<Path> tQ = getNewPathTraversalWithTestEdgeRules(pInterface).createEdgeTraversal(EdgeType.TREE,
+ loader.introspectorFromName("p-interface" ), loader.introspectorFromName("l-interface")).createEdgeTraversal(EdgeType.COUSIN,
+ loader.introspectorFromName("l-interface" ), loader.introspectorFromName("logical-link")).path();
+
+ Path path = tQ.next();
+ assertThat(path.objects(), contains(pInterface, lInterface, logicalLink));
+ }
+
+ @SuppressWarnings("rawtypes")
+ @Test
+ public void parentVertexTest() throws AAIException {
+ Vertex pInterface = g.addV("aai-node-type","p-interface","interface-name","p-interface-a").next();
+ Vertex lInterface = g.addV("aai-node-type","l-interface","interface-name","l-interface-a").next();
+
+ testEdgeRules.addTreeEdge(g, pInterface, lInterface);
+
+ QueryBuilder<Vertex> tQ = getNewEdgeTraversalWithTestEdgeRules(lInterface).getParentVertex();
+
+ Vertex parent = tQ.next();
+ assertThat(parent, is(pInterface));
+ }
+
@Test
public void createEdgeLinterfaceToLogicalLinkIntrospectorTraversal() throws AAIException {
@@ -644,5 +679,8 @@ public abstract class QueryBuilderTestAbstraction extends AAISetup {
protected abstract QueryBuilder<Tree> getNewTreeTraversalWithTestEdgeRules();
+ protected abstract QueryBuilder<Path> getNewPathTraversalWithTestEdgeRules(Vertex v);
+
+ protected abstract QueryBuilder<Path> getNewPathTraversalWithTestEdgeRules();
}
diff --git a/aai-core/src/test/java/org/onap/aai/query/builder/TraversalQueryTest.java b/aai-core/src/test/java/org/onap/aai/query/builder/TraversalQueryTest.java
index 1eeca6f0..b38d13d6 100644
--- a/aai-core/src/test/java/org/onap/aai/query/builder/TraversalQueryTest.java
+++ b/aai-core/src/test/java/org/onap/aai/query/builder/TraversalQueryTest.java
@@ -19,6 +19,7 @@
*/
package org.onap.aai.query.builder;
+import org.apache.tinkerpop.gremlin.process.traversal.Path;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
@@ -74,6 +75,16 @@ public class TraversalQueryTest extends QueryBuilderTestAbstraction {
protected QueryBuilder<Tree> getNewTreeTraversalWithTestEdgeRules() {
return new TraversalQuery<>(loader, g, testEdgeRules);
}
+
+ @Override
+ protected QueryBuilder<Path> getNewPathTraversalWithTestEdgeRules(Vertex v) {
+ return new TraversalQuery<>(loader, g, v, testEdgeRules);
+ }
+
+ @Override
+ protected QueryBuilder<Path> getNewPathTraversalWithTestEdgeRules() {
+ return new TraversalQuery<>(loader, g, testEdgeRules);
+ }
@Test
public void unionQuery() {
diff --git a/aai-core/src/test/resources/dbedgerules/DbEdgeRules_TraversalQueryTest.json b/aai-core/src/test/resources/dbedgerules/DbEdgeRules_TraversalQueryTest.json
index 515831e9..d68ad35b 100644
--- a/aai-core/src/test/resources/dbedgerules/DbEdgeRules_TraversalQueryTest.json
+++ b/aai-core/src/test/resources/dbedgerules/DbEdgeRules_TraversalQueryTest.json
@@ -152,6 +152,18 @@
"prevent-delete": "NONE",
"default": "true",
"description": "Hard to describe"
+ },
+ {
+ "from": "l-interface",
+ "to": "p-interface",
+ "label": "tosca.relationships.network.BindsTo",
+ "direction": "OUT",
+ "multiplicity": "MANY2ONE",
+ "contains-other-v": "!${direction}",
+ "SVC-INFRA": "${direction}",
+ "delete-other-v": "!${direction}",
+ "prevent-delete": "NONE",
+ "default": "true"
}
]
}