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.io.ByteArrayInputStream;
23 import java.io.InputStream;
24 import java.nio.charset.StandardCharsets;
26 import org.antlr.v4.runtime.CharStreams;
27 import org.antlr.v4.runtime.CommonTokenStream;
28 import org.antlr.v4.runtime.tree.ParseTree;
29 import org.antlr.v4.runtime.tree.ParseTreeWalker;
31 import org.onap.aai.AAIDslLexer;
32 import org.onap.aai.AAIDslParser;
34 import org.onap.aai.rest.dsl.DslListener;
35 import org.antlr.v4.runtime.Token;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
40 * The Class DslQueryProcessor.
42 public class DslQueryProcessor {
44 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(DslQueryProcessor.class);
46 public String parseAaiQuery(String aaiQuery) {
48 // Create a input stream that reads our string
49 InputStream stream = new ByteArrayInputStream(aaiQuery.getBytes(StandardCharsets.UTF_8));
51 // Create a lexer from the input CharStream
52 AAIDslLexer lexer = new AAIDslLexer(CharStreams.fromStream(stream, StandardCharsets.UTF_8));
54 // Get a list of tokens pulled from the lexer
55 CommonTokenStream tokens = new CommonTokenStream(lexer);
58 // Parser that feeds off of the tokens buffer
59 AAIDslParser parser = new AAIDslParser(tokens);
61 // Specify our entry point
62 ParseTree ptree = parser.aaiquery();
63 LOGGER.info("QUERY-interim" + ptree.toStringTree(parser));
65 // Walk it and attach our listener
66 ParseTreeWalker walker = new ParseTreeWalker();
67 DslListener listener = new DslListener();
68 walker.walk(listener, ptree);
69 LOGGER.info("Final QUERY" + listener.query);
72 * TODO - Visitor patternQueryDslVisitor visitor = new
73 * QueryDslVisitor(); String query = visitor.visit(ptree);
76 return listener.query;
77 } catch (Exception e) {
78 LOGGER.error("Error while processing the query"+e.getMessage());
83 public static class Builder {
86 * Builder constructor doesnt do anything
92 public String build(String aaiQuery) {
94 return new DslQueryProcessor().parseAaiQuery(aaiQuery);