Refactor dblib
[ccsdk/sli/core.git] / sli / common / src / main / java / org / onap / ccsdk / sli / core / sli / SvcLogicExprListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 ONAP
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 package org.onap.ccsdk.sli.core.sli;
22
23 import java.util.LinkedList;
24 import java.util.List;
25
26 import org.antlr.v4.runtime.tree.TerminalNode;
27 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.AddExprContext;
28 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.AtomContext;
29 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.CompareExprContext;
30 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ConstantContext;
31 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ExprContext;
32 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.FuncExprContext;
33 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.MultExprContext;
34 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ParenExprContext;
35 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.RelExprContext;
36 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableContext;
37 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableLeadContext;
38 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableTermContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class SvcLogicExprListener extends ExprGrammarBaseListener 
43 {
44
45
46
47
48         private static final Logger LOG = LoggerFactory
49                         .getLogger(SvcLogicExprListener.class);
50         
51         private SvcLogicExpression curExpr;
52         private SvcLogicExpression topExpr;
53         private LinkedList<SvcLogicExpression> exprStack;
54         
55         public SvcLogicExprListener()
56         {
57                 exprStack = new LinkedList<SvcLogicExpression>();
58         }
59         
60         public SvcLogicExpression getParsedExpr()
61         {
62                 return(curExpr);
63         }
64
65         private void pushOperand(SvcLogicExpression operand)
66         {
67                 if (curExpr == null)
68                 {
69                         curExpr = operand;
70                 }
71                 else
72                 {
73                         curExpr.addOperand(operand);
74                 }
75         }
76         
77         private void pushExpr(SvcLogicExpression expr)
78         {
79                 LOG.trace("Pushing expression ["+expr.getClass().getName()+"]");
80                 if (curExpr != null)
81                 {
82                         exprStack.push(curExpr);
83                 }
84                 curExpr = expr;
85         }
86         
87         private void popExpr()
88         {
89                 if (exprStack.isEmpty())
90                 {
91                         LOG.trace("Popping last expression");
92                         topExpr = curExpr;
93                 }
94                 else
95                 {
96                         SvcLogicExpression lastExpr = curExpr;
97                         curExpr = exprStack.pop();
98                         curExpr.addOperand(lastExpr);
99                         LOG.trace("New curExpr is ["+curExpr.getClass().getName()+"]");
100                 }
101                 
102         }
103         
104         @Override
105         public void enterAtom(AtomContext ctx) {
106                 
107                 String atomText = ctx.getText();
108                 
109                 LOG.trace("enterAtom: text = "+atomText);
110
111                 
112                 SvcLogicAtom newAtom = new SvcLogicAtom(atomText);
113                 
114                 pushExpr(newAtom);
115         }
116
117
118         @Override
119         public void enterMultExpr(MultExprContext ctx) {
120                 LOG.trace("enterMultExpr: text = "+ctx.getText());
121                 
122                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
123                 pushExpr(curBinExpr);
124                 
125                 List<TerminalNode> opList = ctx.MULTOP();
126                 
127                 for (TerminalNode nd : opList)
128                 {
129                         LOG.trace("enterMultExpr: operator - "+nd.getText());
130                         curBinExpr.addOperator(nd.getText());
131                 }
132
133         }
134
135         @Override
136         public void exitMultExpr(MultExprContext ctx) {
137
138                 LOG.trace("exitMultExpr: text = "+ctx.getText());
139
140                 popExpr();
141                 
142         }
143
144         @Override
145         public void exitAtom(AtomContext ctx) {
146                 LOG.trace("exitAtom: text = "+ctx.getText());
147                 popExpr();
148         }
149
150         @Override
151         public void enterAddExpr(AddExprContext ctx) {
152                 LOG.trace("enterAddExpr: text = "+ctx.getText());
153                 List<TerminalNode> opList = ctx.ADDOP();
154                 
155
156                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
157                 pushExpr(curBinExpr);
158
159                 
160                 for (TerminalNode nd : opList)
161                 {
162                         LOG.trace("enterAddExpr: operator - "+nd.getText());
163                         curBinExpr.addOperator(nd.getText());
164                 }
165                 
166         }
167
168         @Override
169         public void exitAddExpr(AddExprContext ctx) {
170                 LOG.trace("exitAddExpr: text = "+ctx.getText());
171                 
172                 popExpr();
173         }
174
175         @Override
176         public void enterFuncExpr(FuncExprContext ctx) {
177                 LOG.trace("enterFuncExpr: text = "+ctx.getText());
178                 LOG.trace("enterFuncExpr - IDENTIFIER : "+ctx.IDENTIFIER().getText());
179                 
180                 for (ExprContext expr: ctx.expr())
181                 {
182                         LOG.trace("enterFuncExpr - expr = "+expr.getText());
183                 }
184                 
185
186                 pushExpr(new SvcLogicFunctionCall(ctx.IDENTIFIER().getText()));
187         }
188
189         @Override
190         public void exitFuncExpr(FuncExprContext ctx) {
191                 LOG.trace("exitFuncExpr: text = "+ctx.getText());
192                 
193                 popExpr();
194         }
195
196         @Override
197         public void enterParenExpr(ParenExprContext ctx) {
198                 LOG.trace("enterParenExpr: text = "+ctx.getText());
199                 LOG.trace("enterParenExpr: expr = "+ctx.expr().getText());
200         }
201
202         @Override
203         public void exitParenExpr(ParenExprContext ctx) {
204                 LOG.trace("exitParenExpr: text = "+ctx.getText());
205         }
206
207         @Override
208         public void enterRelExpr(RelExprContext ctx) {
209                 LOG.trace("enterRelExpr: text = "+ctx.getText());
210                 
211                 List<TerminalNode> opList = ctx.RELOP();
212                 
213
214                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
215                 pushExpr(curBinExpr);
216
217                 
218                 for (TerminalNode nd : opList)
219                 {
220                         LOG.trace("enterRelExpr: operator - "+nd.getText());
221                         curBinExpr.addOperator(nd.getText());
222                 }
223                 
224         }
225
226         @Override
227         public void exitRelExpr(RelExprContext ctx) {
228                 LOG.trace("exitRelExpr: text = "+ctx.getText());
229                 
230                 popExpr();
231         }
232
233         @Override
234         public void enterCompareExpr(CompareExprContext ctx) {
235                 LOG.trace("enterCompareExpr: text = "+ctx.getText());
236                 
237                 TerminalNode nd = ctx.COMPAREOP();
238
239                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
240                 pushExpr(curBinExpr);
241
242                 LOG.trace("enterCompareExpr: operator - "+nd.getText());
243                 curBinExpr.addOperator(nd.getText());
244
245         }
246
247         @Override
248         public void exitCompareExpr(CompareExprContext ctx) {
249                 LOG.trace("exitCompareExpr : text = "+ctx.getText());
250                 
251                 popExpr();
252         }
253
254
255         
256         @Override 
257         public void enterConstant(ConstantContext ctx) {
258                 LOG.trace("enterConstant: text = "+ctx.getText());
259         }
260
261         @Override
262         public void exitConstant(ConstantContext ctx) {
263                 LOG.trace("exitConstant: text = "+ctx.getText());
264         }
265
266
267         @Override
268         public void enterVariable(VariableContext ctx) {
269                 LOG.trace("enterVariable: text = "+ctx.getText());
270                 
271                 
272         }
273
274         @Override
275         public void exitVariable(VariableContext ctx) {
276                 LOG.debug("exitVariable: text ="+ctx.getText());
277                 
278         }
279         
280
281         @Override
282         public void enterVariableLead(VariableLeadContext ctx) {
283
284                 LOG.debug("enterVariableLead: text ="+ctx.getText());
285                 
286
287         }
288
289         @Override
290         public void exitVariableLead(VariableLeadContext ctx) {
291
292                 LOG.trace("exitVariableLead: text ="+ctx.getText());
293         }
294
295         @Override
296         public void enterVariableTerm(VariableTermContext ctx) {
297                 LOG.trace("enterVariableTerm: text ="+ctx.getText());
298                 
299                 String name = ctx.getText();
300                 
301                 int subscrStart = name.indexOf("[");
302                 if (subscrStart > -1)
303                 {
304                         name = name.substring(0, subscrStart);
305                 }
306                 SvcLogicVariableTerm vterm = new SvcLogicVariableTerm(name);
307                 pushExpr(vterm);
308         }
309
310         @Override
311         public void exitVariableTerm(VariableTermContext ctx) {
312                 LOG.trace("exitVariableTerm: text="+ctx.getText());
313                 popExpr();
314         }
315 }