2 * ============LICENSE_START=======================================================
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
11 http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.aai.query.builder;
24 import java.util.LinkedHashMap;
25 import java.util.List;
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
29 import org.openecomp.aai.db.props.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;
36 public abstract class GremlinPipelineBuilder extends QueryBuilder {
38 private GremlinPipeline pipeline = null;
39 private EdgeRules edgeRules = EdgeRules.getInstance();
40 private int parentStepIndex = 0;
41 private int stepIndex = 0;
43 public GremlinPipelineBuilder(Loader loader) {
46 pipeline = new GremlinPipeline(new IdentityPipe()).V();
50 public GremlinPipelineBuilder(Loader loader, Vertex start) {
53 pipeline = new GremlinPipeline(start);
58 public QueryBuilder getVerticesByIndexedProperty(String key, Object value) {
60 return this.getVerticesByProperty(key, value);
64 public QueryBuilder getVerticesByProperty(String key, Object value) {
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()));
70 pipeline.has(key, value);
77 public QueryBuilder getChildVerticesFromParent(String parentKey, String parentValue, String childType) {
78 pipeline.has(parentKey, parentValue).has(AAIProperties.NODE_TYPE, childType);
84 public QueryBuilder getTypedVerticesByMap(String type, LinkedHashMap<String, String> map) {
86 for (String key : map.keySet()) {
87 pipeline.has(key, map.get(key));
90 pipeline.has(AAIProperties.NODE_TYPE, type);
96 public QueryBuilder createDBQuery(Introspector obj) {
97 this.createKeyQuery(obj);
98 this.createContainerQuery(obj);
103 public QueryBuilder createKeyQuery(Introspector obj) {
104 List<String> keys = obj.getKeys();
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()));
112 pipeline.has(key, val);
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]);
132 pipeline.has(AAIProperties.NODE_TYPE, type);
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)) {
145 pipeline.outE().has("isParent", true).inV();
147 if (parent.isContainer()) {
148 parentName = parent.getChildDBName();
150 if (child.isContainer()) {
151 childName = child.getChildDBName();
153 this.edgeQuery(parentName, childName);
160 public QueryBuilder createEdgeTraversal(Vertex parent, Introspector child) {
162 String nodeType = parent.getProperty(AAIProperties.NODE_TYPE);
163 this.edgeQuery(nodeType, child.getDbName());
168 private void edgeQuery(String outType, String inType) {
173 rule = edgeRules.getEdgeRule(outType, inType);
174 label = rule.getLabel();
175 } catch (AAIException e) {
176 // TODO Auto-generated catch block
178 pipeline = pipeline.out(label);
183 public Object getQuery() {
184 return this.pipeline;
188 public Object getParentQuery() {
189 GremlinPipeline parent = new GremlinPipeline();
190 if (parentStepIndex == 0) {
191 parentStepIndex = stepIndex;
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));
203 public void formBoundary() {
204 parentStepIndex = stepIndex;
209 public Vertex getStart() {