[AAI-178 Amsterdam] Make Edge Properties to be
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / serialization / engines / query / GraphTraversalQueryEngine.java
index 2f485b1..074441c 100644 (file)
@@ -5,23 +5,21 @@
  * Copyright (C) 2017 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.
-You may obtain a copy of the License at
-
-     http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
*      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  * ============LICENSE_END=========================================================
  */
 
 package org.openecomp.aai.serialization.engines.query;
 
-import java.util.List;
-import java.util.Set;
 
 import org.apache.tinkerpop.gremlin.process.traversal.P;
 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
@@ -32,10 +30,16 @@ import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.apache.tinkerpop.gremlin.structure.Element;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
-
 import org.openecomp.aai.db.props.AAIProperties;
 import org.openecomp.aai.introspection.Loader;
 
+import java.util.List;
+import java.util.Set;
+
+import static org.openecomp.aai.serialization.db.AAIDirection.*;
+import static org.openecomp.aai.serialization.db.EdgeProperty.CONTAINS;
+import static org.openecomp.aai.serialization.db.EdgeProperty.DELETE_OTHER_V;
+
 /*
  * This class needs some big explanation despite its compact size.
  * This controls all the queries performed by the CRUD API in A&AI. 
@@ -62,7 +66,7 @@ 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(__.inE().has("isParent", true).outV());
+               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();
        }
 
@@ -73,7 +77,7 @@ public class GraphTraversalQueryEngine extends QueryEngine {
        public List<Vertex> findAllChildren(Vertex start) {
                
                GraphTraversal<Vertex, Vertex> pipe =  this.g
-                               .V(start).emit(v -> true).repeat(__.outE().has("isParent", true).inV());
+                               .V(start).emit(v -> true).repeat(__.union(__.outE().has(CONTAINS.toString(), OUT.toString()).inV(), __.inE().has(CONTAINS.toString(), IN.toString()).outV()));
                
 
                return pipe.toList();
@@ -82,12 +86,21 @@ public class GraphTraversalQueryEngine extends QueryEngine {
 
        public List<Vertex> findChildrenOfType(Vertex start, String type) {
                GraphTraversal<Vertex, Vertex> pipe =  this.g.V(start).union(
-                                       __.outE().has("isParent", true).inV(),
-                                       __.inE().has("isParent-REV", true).outV()
+                                       __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
+                                       __.inE().has(CONTAINS.toString(), IN.toString()).outV()
                                ).has(AAIProperties.NODE_TYPE, type).dedup();
  
                return pipe.toList();
        }
+       
+       public List<Vertex> findChildren(Vertex start) {
+               GraphTraversal<Vertex, Vertex> pipe =  this.g.V(start).union(
+                                       __.outE().has(CONTAINS.toString(), OUT.toString()),
+                                       __.inE().has(CONTAINS.toString(), IN.toString())
+                               ).otherV().dedup();
+               return pipe.toList();
+       }
 
        /**
         * {@inheritDoc}
@@ -95,9 +108,18 @@ public class GraphTraversalQueryEngine extends QueryEngine {
        @Override
        public List<Vertex> findDeletable(Vertex start) {
                GraphTraversal<Vertex, Vertex> pipe = this.g
-                               .V(start).emit(v -> true).repeat(__.outE().or(
-                                               __.has("isParent", true),
-                                               __.has("hasDelTarget", true)).inV());
+                               .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()
+                                       )
+                               );
                
                return pipe.toList();
        }
@@ -129,12 +151,15 @@ public class GraphTraversalQueryEngine extends QueryEngine {
        @Override
        public Tree<Element> findSubGraph(Vertex start, int iterations, boolean nodeOnly) {
                final GraphTraversal<Vertex, ?> t = this.g.V(start).emit(v -> true).times(iterations).repeat(
-                               __.outE().has("isParent", true).inV());
+                               __.union(
+                                       __.outE().has(CONTAINS.toString(), OUT.toString()).inV(),
+                                       __.inE().has(CONTAINS.toString(), IN.toString()).outV())
+                               );
                        
                if (!nodeOnly) {
                        t.union(
                                        __.identity(),
-                                       __.bothE().has("isParent", false).dedup().otherV()
+                                       __.bothE().has(CONTAINS.toString(), NONE.toString()).dedup().otherV()
                        );
                }
                t.tree();
@@ -149,8 +174,8 @@ public class GraphTraversalQueryEngine extends QueryEngine {
        public List<Edge> findEdgesForVersion(Vertex start, Loader loader) {
                final Set<String> objects = loader.getAllObjects().keySet();
                GraphTraversal<Vertex, Edge> pipeline = this.g.V(start).union(
-                               __.inE().has("isParent", false).has("isParent-REV", false).where(__.outV().has(AAIProperties.NODE_TYPE, P.within(objects))),
-                               __.outE().has("isParent", false).has("isParent-REV", false).where(__.inV().has(AAIProperties.NODE_TYPE, P.within(objects)))
+                               __.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();
                
                return pipeline.toList();
@@ -160,8 +185,8 @@ public class GraphTraversalQueryEngine extends QueryEngine {
        @Override
        public List<Vertex> findCousinVertices(Vertex start) {
                GraphTraversal<Vertex, Vertex> pipeline = this.g.V(start).union(
-                               __.inE().has("isParent", false).has("isParent-REV", false),
-                               __.outE().has("isParent", false).has("isParent-REV", false)).otherV().dedup();
+                               __.inE().has(CONTAINS.toString(), NONE.toString()),
+                               __.outE().has(CONTAINS.toString(), NONE.toString())).otherV().dedup();
                                
                return pipeline.toList();
        }