Update the aai-common with the latest code
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / query / builder / GremlinPipelineBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 /*
22 package org.openecomp.aai.query.builder;
23
24 import java.util.LinkedHashMap;
25 import java.util.List;
26
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28
29 import org.openecomp.aai.db.AAIProperties;
30 import org.openecomp.aai.exceptions.AAIException;
31 import org.openecomp.aai.introspection.Introspector;
32 import org.openecomp.aai.introspection.Loader;
33 import org.openecomp.aai.serialization.db.EdgeRule;
34 import org.openecomp.aai.serialization.db.EdgeRules;
35
36 public abstract class GremlinPipelineBuilder extends QueryBuilder {
37
38         private GremlinPipeline pipeline = null;
39         private EdgeRules edgeRules = EdgeRules.getInstance();
40         private int parentStepIndex = 0;
41         private int stepIndex = 0;
42         
43         public GremlinPipelineBuilder(Loader loader) {
44                 super(loader);
45                 
46                 pipeline = new GremlinPipeline(new IdentityPipe()).V();
47                 
48         }
49         
50         public GremlinPipelineBuilder(Loader loader, Vertex start) {
51                 super(loader, start);
52                 
53                 pipeline = new GremlinPipeline(start);
54                 
55         }
56
57         @Override
58         public QueryBuilder getVerticesByIndexedProperty(String key, Object value) {
59         
60                 return this.getVerticesByProperty(key, value);
61         }
62
63         @Override
64         public QueryBuilder getVerticesByProperty(String key, Object value) {
65                 
66                 //this is because the index is registered as an Integer
67                 if (value != null && value.getClass().equals(Long.class)) {
68                         pipeline.has(key,new Integer(value.toString()));
69                 } else {
70                         pipeline.has(key, value);
71                 }
72                 stepIndex++;
73                 return this;
74         }
75
76         @Override
77         public QueryBuilder getChildVerticesFromParent(String parentKey, String parentValue, String childType) {
78                 pipeline.has(parentKey, parentValue).has(AAIProperties.NODE_TYPE, childType);
79                 stepIndex++;
80                 return this;
81         }
82
83         @Override
84         public QueryBuilder getTypedVerticesByMap(String type, LinkedHashMap<String, String> map) {
85                 
86                 for (String key : map.keySet()) {
87                         pipeline.has(key, map.get(key));
88                         stepIndex++;
89                 }
90                 pipeline.has(AAIProperties.NODE_TYPE, type);
91                 stepIndex++;
92                 return this;
93         }
94
95         @Override
96         public QueryBuilder createDBQuery(Introspector obj) {
97                 this.createKeyQuery(obj);
98                 this.createContainerQuery(obj);
99                 return this;
100         }
101
102         @Override
103         public QueryBuilder createKeyQuery(Introspector obj) {
104                 List<String> keys = obj.getKeys();
105                 Object val = null;
106                 for (String key : keys) {
107                         val = obj.getValue(key);
108                         //this is because the index is registered as an Integer
109                         if (val != null && val.getClass().equals(Long.class)) {
110                                 pipeline.has(key,new Integer(val.toString()));
111                         } else {
112                                 pipeline.has(key, val);
113                         }
114                         stepIndex++;
115                 }
116                 return this;
117         }
118
119         @Override
120         
121         public QueryBuilder createContainerQuery(Introspector obj) {
122                 String type = obj.getChildDBName();
123                 String abstractType = obj.getMetadata("abstract");
124                 if (abstractType != null) {
125                         String[] inheritors = obj.getMetadata("inheritors").split(",");
126                         GremlinPipeline[] pipes = new GremlinPipeline[inheritors.length];
127                         for (int i = 0; i < inheritors.length; i++) {
128                                 pipes[i] = new GremlinPipeline(new IdentityPipe()).has(AAIProperties.NODE_TYPE, inheritors[i]);
129                         }
130                         pipeline.or(pipes);
131                 } else {
132                         pipeline.has(AAIProperties.NODE_TYPE, type);
133                 }
134                 stepIndex++;
135                 return this;
136         }
137
138         @Override
139         public QueryBuilder createEdgeTraversal(Introspector parent, Introspector child) {
140                 String parentName = parent.getDbName();
141                 String childName = child.getDbName();
142                 String isAbstractType = parent.getMetadata("abstract");
143                 if ("true".equals(isAbstractType)) {
144                         formBoundary();
145                         pipeline.outE().has("isParent", true).inV();
146                 } else {
147                         if (parent.isContainer()) {
148                                 parentName = parent.getChildDBName();
149                         }
150                         if (child.isContainer()) {
151                                 childName = child.getChildDBName();
152                         }
153                         this.edgeQuery(parentName, childName);
154                 }
155                 return this;
156                         
157         }
158         
159         @Override
160         public QueryBuilder createEdgeTraversal(Vertex parent, Introspector child) {
161                 
162                 String nodeType = parent.getProperty(AAIProperties.NODE_TYPE);
163                 this.edgeQuery(nodeType, child.getDbName());
164                 return this;
165                         
166         }
167         
168         private void edgeQuery(String outType, String inType) {
169                 formBoundary();
170                 EdgeRule rule;
171                 String label = "";
172                 try {
173                         rule = edgeRules.getEdgeRule(outType, inType);
174                         label = rule.getLabel();
175                 } catch (AAIException e) {
176                         // TODO Auto-generated catch block
177                 }
178                 pipeline = pipeline.out(label);
179                 stepIndex++;
180         }
181
182         @Override
183         public Object getQuery() {
184                 return this.pipeline;
185         }
186         
187         @Override
188         public Object getParentQuery() {
189                 GremlinPipeline parent = new GremlinPipeline();
190                 if (parentStepIndex == 0) {
191                         parentStepIndex = stepIndex;
192                 }
193                 List<Pipe> pipes = this.pipeline.getPipes();
194                 //add two for the garbage identity pipes
195                 for (int i = 0; i < parentStepIndex + 2; i++) {
196                         parent.add(pipes.get(i));
197                 }
198
199                 return parent;
200         }
201         
202         @Override
203         public void formBoundary() {
204                 parentStepIndex = stepIndex;
205         }
206         
207         
208         @Override
209         public Vertex getStart() {
210                 return this.start;
211         }
212         
213 }
214 */