2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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=========================================================
20 package org.onap.aai.rest.dsl;
22 import java.util.ArrayList;
23 import java.util.HashMap;
25 import java.util.List;
27 import org.antlr.v4.runtime.tree.TerminalNode;
29 import org.onap.aai.AAIDslParser;
30 import org.onap.aai.serialization.db.EdgeRules;
31 import org.onap.aai.AAIDslBaseListener;
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
37 * The Class DslListener.
39 public class DslListener extends AAIDslBaseListener {
41 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(DslQueryProcessor.class);
42 private final EdgeRules edgeRules = EdgeRules.getInstance();
44 protected List<String> list = null;
45 //TODO Use StringBuilder to build the query than concat
48 Map<Integer, String> unionMap = new HashMap<>();
49 Map<String, String> flags = new HashMap<>();
51 String currentNode = "";
52 String prevsNode = "";
57 boolean isUnionBeg = false;
58 boolean isUnionTraversal = false;
60 boolean isTraversal = false;
61 boolean isWhereTraversal = false;
62 String whereTraversalNode = "";
64 String limitQuery = "";
65 boolean isNot = false;
68 * Instantiates a new DslListener.
71 public DslListener() {
72 list = new ArrayList<>();
76 public void enterAaiquery(AAIDslParser.AaiqueryContext ctx) {
81 public void enterDslStatement(AAIDslParser.DslStatementContext ctx) {
82 // LOGGER.info("Statement Enter"+ctx.getText());
84 * This block of code is entered for every query statement
88 isUnionTraversal = true;
90 } else if (unionMembers > 0) {
92 query += ",builder.newInstance()";
93 isUnionTraversal = true;
99 public void exitDslStatement(AAIDslParser.DslStatementContext ctx) {
101 * Nothing to be done here for now
102 * LOGGER.info("Statement Exit"+ctx.getText());
107 public void exitAaiquery(AAIDslParser.AaiqueryContext ctx) {
109 * dedup is by default for all queries If the query has limit in it
110 * include this as well LOGGER.info("Statement Exit"+ctx.getText());
113 query += ".cap('x').unfold().dedup()" + limitQuery;
117 * TODO: The contexts are not inherited from a single parent in AAIDslParser
118 * Need to find a way to do that
121 public void enterSingleNodeStep(AAIDslParser.SingleNodeStepContext ctx) {
123 prevsNode = currentNode;
124 currentNode = ctx.NODE().getText();
126 this.generateQuery();
127 if (ctx.STORE() != null && ctx.STORE().getText().equals("*")) {
128 flags.put(currentNode, "store");
134 public void enterSingleQueryStep(AAIDslParser.SingleQueryStepContext ctx) {
136 prevsNode = currentNode;
137 currentNode = ctx.NODE().getText();
138 this.generateQuery();
140 if (ctx.STORE() != null && ctx.STORE().getText().equals("*")) {
141 flags.put(currentNode, "store");
146 public void enterMultiQueryStep(AAIDslParser.MultiQueryStepContext ctx) {
148 prevsNode = currentNode;
149 currentNode = ctx.NODE().getText();
150 this.generateQuery();
152 if (ctx.STORE() != null && ctx.STORE().getText().equals("*")) {
153 flags.put(currentNode, "store");
159 * Generates the QueryBuilder syntax for the dsl query
161 private void generateQuery() {
162 String edgeType = "";
164 if (isUnionTraversal || isTraversal || isWhereTraversal) {
165 String previousNode = prevsNode;
166 if (isUnionTraversal) {
167 previousNode = unionMap.get(unionKey);
168 isUnionTraversal = false;
171 if (edgeRules.hasTreeEdgeRule(previousNode, currentNode)) {
172 edgeType = "EdgeType.TREE";
173 }else if (edgeRules.hasCousinEdgeRule(previousNode, currentNode, "")) {
174 edgeType = "EdgeType.COUSIN";
176 edgeType = "EdgeType.COUSIN";
178 query += ".createEdgeTraversal(" + edgeType + ", '" + previousNode + "','" + currentNode + "')";
183 query += ".getVerticesByProperty('aai-node-type', '" + currentNode + "')";
187 public void exitSingleNodeStep(AAIDslParser.SingleNodeStepContext ctx) {
193 public void exitSingleQueryStep(AAIDslParser.SingleQueryStepContext ctx) {
198 public void exitMultiQueryStep(AAIDslParser.MultiQueryStepContext ctx) {
203 private void generateExitStep() {
204 if (flags.containsKey(currentNode)) {
205 String storeFlag = flags.get(currentNode);
206 if (storeFlag != null && storeFlag.equals("store"))
207 query += ".store('x')";
208 flags.remove(currentNode);
213 public void enterUnionQueryStep(AAIDslParser.UnionQueryStepContext ctx) {
217 unionMap.put(unionKey, currentNode);
218 query += ".union(builder.newInstance()";
220 List<TerminalNode> commaNodes = ctx.COMMA();
222 for (TerminalNode node : commaNodes) {
228 public void exitUnionQueryStep(AAIDslParser.UnionQueryStepContext ctx) {
230 unionMap.remove(unionKey);
238 public void enterFilterTraverseStep(AAIDslParser.FilterTraverseStepContext ctx) {
239 isWhereTraversal = true;
240 whereTraversalNode = currentNode;
241 query += ".where(builder.newInstance()";
245 public void exitFilterTraverseStep(AAIDslParser.FilterTraverseStepContext ctx) {
247 isWhereTraversal = false;
248 currentNode = whereTraversalNode;
252 public void enterFilterStep(AAIDslParser.FilterStepContext ctx) {
253 if (ctx.NOT() != null && ctx.NOT().getText().equals("!"))
256 List<TerminalNode> nodes = ctx.KEY();
257 String key = ctx.KEY(0).getText();
260 query += ".getVerticesExcludeByProperty(";
263 query += ".getVerticesByProperty(";
265 if (nodes.size() == 2) {
266 query += key + "," + ctx.KEY(1).getText();
270 if (nodes.size() > 2) {
272 for (TerminalNode node : nodes) {
273 if (node.getText().equals(key))
276 query += key + "," + node.getText();
285 public void exitFilterStep(AAIDslParser.FilterStepContext ctx) {
286 // For now do nothing
290 public void enterTraverseStep(AAIDslParser.TraverseStepContext ctx) {
295 public void exitTraverseStep(AAIDslParser.TraverseStepContext ctx) {
300 public void enterLimitStep(AAIDslParser.LimitStepContext ctx) {
301 String value = ctx.NODE().getText();
302 limitQuery += ".limit(" + value + ")";