Update the license for 2017-2018 license
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / rest / dsl / DslQueryProcessor.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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 package org.onap.aai.rest.dsl;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.InputStream;
24 import java.nio.charset.StandardCharsets;
25
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;
30
31 import org.onap.aai.AAIDslLexer;
32 import org.onap.aai.AAIDslParser;
33
34 import org.onap.aai.rest.dsl.DslListener;
35 import org.antlr.v4.runtime.Token;
36
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39 /**
40  * The Class DslQueryProcessor.
41  */
42 public class DslQueryProcessor {
43
44         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(DslQueryProcessor.class);
45
46         public String parseAaiQuery(String aaiQuery) {
47                 try {
48                         // Create a input stream that reads our string
49                         InputStream stream = new ByteArrayInputStream(aaiQuery.getBytes(StandardCharsets.UTF_8));
50
51                         // Create a lexer from the input CharStream
52                         AAIDslLexer lexer = new AAIDslLexer(CharStreams.fromStream(stream, StandardCharsets.UTF_8));
53
54                         // Get a list of tokens pulled from the lexer
55                         CommonTokenStream tokens = new CommonTokenStream(lexer);
56
57                         
58                         // Parser that feeds off of the tokens buffer
59                         AAIDslParser parser = new AAIDslParser(tokens);
60
61                         // Specify our entry point
62                         ParseTree ptree = parser.aaiquery();
63                         LOGGER.info("QUERY-interim" + ptree.toStringTree(parser));
64
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);
70
71                         /*
72                          * TODO - Visitor patternQueryDslVisitor visitor = new
73                          * QueryDslVisitor(); String query = visitor.visit(ptree);
74                          * 
75                          */
76                         return listener.query;
77                 } catch (Exception e) {
78                         LOGGER.error("Error while processing the query"+e.getMessage());
79                 }
80                 return "";
81         }
82
83         public static class Builder {
84
85                 /*
86                  * Builder constructor doesnt do anything
87                  */
88                 public Builder() {
89                         // Do nothing
90                 }
91
92                 public String build(String aaiQuery) {
93
94                         return new DslQueryProcessor().parseAaiQuery(aaiQuery);
95                 }
96         }
97 }