Integrate aai-schema-ingest library into aai-core
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / engines / query / GraphTraversalQueryEngine.java
index 872b0c5..2158c89 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * org.onap.aai
  * ================================================================================
- * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 AT&T Intellectual Property. 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.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END=========================================================
- *
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  */
 package org.onap.aai.serialization.engines.query;
 
 
-import static org.onap.aai.serialization.db.AAIDirection.IN;
-import static org.onap.aai.serialization.db.AAIDirection.NONE;
-import static org.onap.aai.serialization.db.AAIDirection.OUT;
-import static org.onap.aai.serialization.db.EdgeProperty.CONTAINS;
-import static org.onap.aai.serialization.db.EdgeProperty.DELETE_OTHER_V;
+import static org.onap.aai.edges.enums.AAIDirection.IN;
+import static org.onap.aai.edges.enums.AAIDirection.NONE;
+import static org.onap.aai.edges.enums.AAIDirection.OUT;
+import static org.onap.aai.edges.enums.EdgeField.PRIVATE;
+import static org.onap.aai.edges.enums.EdgeProperty.CONTAINS;
+import static org.onap.aai.edges.enums.EdgeProperty.DELETE_OTHER_V;
 
 import java.util.List;
 import java.util.Set;
@@ -42,6 +41,7 @@ import org.apache.tinkerpop.gremlin.structure.Element;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.onap.aai.db.props.AAIProperties;
 import org.onap.aai.introspection.Loader;
+import org.onap.aai.logging.StopWatch;
 
 /*
  * This class needs some big explanation despite its compact size.
@@ -68,9 +68,15 @@ public class GraphTraversalQueryEngine extends QueryEngine {
         */
        @Override
        public List<Vertex> findParents(Vertex start) {
-               
-               final GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).emit(v -> true).repeat(__.union(__.inE().has(CONTAINS.toString(), OUT.toString()).outV(), __.outE().has(CONTAINS.toString(), IN.toString()).inV()));
-               return pipe.toList();
+               try {
+                       StopWatch.conditionalStart();
+                        @SuppressWarnings("unchecked")
+                        final GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).emit(v -> true).repeat(__.union(__.inE().has(CONTAINS.toString(), OUT.toString()).outV(), __.outE().has(CONTAINS.toString(), IN.toString()).inV()));
+                       return pipe.toList();
+               }
+               finally {
+                       dbTimeMsecs += StopWatch.stopIfStarted();
+               }
        }
 
        /**
@@ -79,6 +85,7 @@ public class GraphTraversalQueryEngine extends QueryEngine {
        @Override
        public List<Vertex> findAllChildren(Vertex start) {
                
+               @SuppressWarnings("unchecked")
                GraphTraversal<Vertex, Vertex> pipe =  this.g
                                .V(start).emit(v -> true).repeat(__.union(__.outE().has(CONTAINS.toString(), OUT.toString()).inV(), __.inE().has(CONTAINS.toString(), IN.toString()).outV()));
                
@@ -87,7 +94,12 @@ public class GraphTraversalQueryEngine extends QueryEngine {
                
        }
 
+       /**
+        * {@inheritDoc}
+        */
+       @Override
        public List<Vertex> findChildrenOfType(Vertex start, String type) {
+               @SuppressWarnings("unchecked")
                GraphTraversal<Vertex, Vertex> pipe =  this.g.V(start).union(
                                        __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
                                        __.inE().has(CONTAINS.toString(), IN.toString()).outV()
@@ -96,7 +108,12 @@ public class GraphTraversalQueryEngine extends QueryEngine {
                return pipe.toList();
        }
        
+       /**
+        * {@inheritDoc}
+        */
+       @Override
        public List<Vertex> findChildren(Vertex start) {
+               @SuppressWarnings("unchecked")
                GraphTraversal<Vertex, Vertex> pipe =  this.g.V(start).union(
                                        __.outE().has(CONTAINS.toString(), OUT.toString()),
                                        __.inE().has(CONTAINS.toString(), IN.toString())
@@ -110,19 +127,14 @@ public class GraphTraversalQueryEngine extends QueryEngine {
         */
        @Override
        public List<Vertex> findDeletable(Vertex start) {
+               @SuppressWarnings("unchecked")
                GraphTraversal<Vertex, Vertex> pipe = this.g
                                .V(start).emit(v -> true).repeat(
                                        __.union(
-                                               __.outE().or(
-                                                       __.has(CONTAINS.toString(), OUT.toString()),
-                                                       __.has(DELETE_OTHER_V.toString(), OUT.toString())
-                                               ).inV(),
-                                               __.inE().or(
-                                                               __.has(CONTAINS.toString(), IN.toString()),
-                                                               __.has(DELETE_OTHER_V.toString(), IN.toString())
-                                               ).outV()
+                                               __.outE().has(DELETE_OTHER_V.toString(), OUT.toString()).inV(),
+                                               __.inE().has(DELETE_OTHER_V.toString(), IN.toString()).outV()
                                        )
-                               );
+                               ).dedup();
                
                return pipe.toList();
        }
@@ -158,7 +170,7 @@ public class GraphTraversalQueryEngine extends QueryEngine {
                                        __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
                                        __.inE().has(CONTAINS.toString(), IN.toString()).outV())
                                );
-                       
+
                if (!nodeOnly) {
                        t.union(
                                        __.identity(),
@@ -175,11 +187,23 @@ public class GraphTraversalQueryEngine extends QueryEngine {
 
        @Override
        public List<Edge> findEdgesForVersion(Vertex start, Loader loader) {
+           // From the given start vertex find both the
+               // out edges that has property CONTAINS set to NONE
+               // whose in vertexes has an object that is declared in the oxm
+               // And do the same thing vice versa to get a list of edges
+               // Then check that the edge should not have the property private set to true
+               // and remove the duplicates and return the list of edges
                final Set<String> objects = loader.getAllObjects().keySet();
-               GraphTraversal<Vertex, Edge> pipeline = this.g.V(start).union(
+               GraphTraversal<Vertex, Edge> pipeline = this.g
+                       .V(start)
+                       .union(
                                __.inE().has(CONTAINS.toString(), NONE.toString()).where(__.outV().has(AAIProperties.NODE_TYPE, P.within(objects))),
                                __.outE().has(CONTAINS.toString(), NONE.toString()).where(__.inV().has(AAIProperties.NODE_TYPE, P.within(objects)))
-                       ).dedup();
+                       )
+                       .not(
+                               __.has("private", true)
+                       )
+                       .dedup();
                
                return pipeline.toList();
        }
@@ -187,12 +211,32 @@ public class GraphTraversalQueryEngine extends QueryEngine {
 
        @Override
        public List<Vertex> findCousinVertices(Vertex start) {
-               GraphTraversal<Vertex, Vertex> pipeline = this.g.V(start).union(
-                               __.inE().has(CONTAINS.toString(), NONE.toString()),
-                               __.outE().has(CONTAINS.toString(), NONE.toString())).otherV().dedup();
-                               
+           // Start at the given vertex
+               // Do a union to copy the start vertex to be run against all
+               // so for the start vertex it gets all of in edges that contains other v set to none
+               // and also all the other out edges with contains other v set to none
+               // And filter the edges based on the property private not set
+        // so that means it will be a regular edge
+               // and find the other end of the vertex so if setup like this:
+               // v2 -> e1 -> v3
+               // It will return v3
+               GraphTraversal<Vertex, Vertex> pipeline = this.g
+            .V(start)
+            .union(
+                __.inE().has(CONTAINS.toString(), NONE.toString()),
+                __.outE().has(CONTAINS.toString(), NONE.toString())
+            )
+                       .not(
+                               __.has(PRIVATE.toString(), true)
+                       )
+                       .otherV()
+                       .dedup();
+
                return pipeline.toList();
        }
        
+       public double getDBTimeMsecs() {
+               return (dbTimeMsecs);
+       }
 }