logging changes
[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 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Modifications Copyright (C) 2018 IBM.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.ccsdk.sli.core.sli;
25
26 import java.util.LinkedList;
27 import java.util.List;
28
29 import org.antlr.v4.runtime.tree.TerminalNode;
30 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.AddExprContext;
31 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.AtomContext;
32 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.CompareExprContext;
33 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ConstantContext;
34 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ExprContext;
35 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.FuncExprContext;
36 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.MultExprContext;
37 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ParenExprContext;
38 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.RelExprContext;
39 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableContext;
40 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableLeadContext;
41 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.VariableTermContext;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class SvcLogicExprListener extends ExprGrammarBaseListener 
46 {
47
48
49
50
51         private static final Logger LOG = LoggerFactory
52                         .getLogger(SvcLogicExprListener.class);
53         
54         private SvcLogicExpression curExpr;
55         //private SvcLogicExpression topExpr;
56         private LinkedList<SvcLogicExpression> exprStack;
57         
58         public SvcLogicExprListener()
59         {
60                 exprStack = new LinkedList<>();
61         }
62         
63         public SvcLogicExpression getParsedExpr()
64         {
65                 return(curExpr);
66         }
67
68         private void pushOperand(SvcLogicExpression operand)
69         {
70                 if (curExpr == null)
71                 {
72                         curExpr = operand;
73                 }
74                 else
75                 {
76                         curExpr.addOperand(operand);
77                 }
78         }
79         
80         private void pushExpr(SvcLogicExpression expr)
81         {
82                 if (curExpr != null)
83                 {
84                         exprStack.push(curExpr);
85                 }
86                 curExpr = expr;
87         }
88         
89         private void popExpr()
90         {
91                 if (exprStack.isEmpty())
92                 {
93                         //topExpr = curExpr;
94                 }
95                 else
96                 {
97                         SvcLogicExpression lastExpr = curExpr;
98                         curExpr = exprStack.pop();
99                         curExpr.addOperand(lastExpr);
100                 }
101                 
102         }
103         
104         @Override
105         public void enterAtom(AtomContext ctx) {
106                 String atomText = ctx.getText();        
107                 SvcLogicAtom newAtom = new SvcLogicAtom(atomText);
108                 pushExpr(newAtom);
109         }
110
111
112         @Override
113         public void enterMultExpr(MultExprContext ctx) {
114                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
115                 pushExpr(curBinExpr);
116                 
117                 List<TerminalNode> opList = ctx.MULTOP();
118                 
119                 for (TerminalNode nd : opList)
120                 {
121                         curBinExpr.addOperator(nd.getText());
122                 }
123
124         }
125
126         @Override
127         public void exitMultExpr(MultExprContext ctx) {
128                 popExpr();
129         }
130
131         @Override
132         public void exitAtom(AtomContext ctx) {
133                 popExpr();
134         }
135
136         @Override
137         public void enterAddExpr(AddExprContext ctx) {
138                 List<TerminalNode> opList = ctx.ADDOP();
139                 
140
141                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
142                 pushExpr(curBinExpr);
143
144                 
145                 for (TerminalNode nd : opList)
146                 {
147                         curBinExpr.addOperator(nd.getText());
148                 }
149                 
150         }
151
152         @Override
153         public void exitAddExpr(AddExprContext ctx) {           
154                 popExpr();
155         }
156
157         @Override
158         public void enterFuncExpr(FuncExprContext ctx) {
159                 LOG.trace("enterFuncExpr: text = "+ctx.getText());
160                 LOG.trace("enterFuncExpr - IDENTIFIER : "+ctx.IDENTIFIER().getText());
161                 
162                 for (ExprContext expr: ctx.expr())
163                 {
164                         LOG.trace("enterFuncExpr - expr = "+expr.getText());
165                 }
166                 
167
168                 pushExpr(new SvcLogicFunctionCall(ctx.IDENTIFIER().getText()));
169         }
170
171         @Override
172         public void exitFuncExpr(FuncExprContext ctx) { 
173                 popExpr();
174         }
175
176         @Override
177         public void enterParenExpr(ParenExprContext ctx) {
178                 LOG.trace("enterParenExpr: text = "+ctx.getText());
179                 LOG.trace("enterParenExpr: expr = "+ctx.expr().getText());
180         }
181
182         @Override
183         public void exitParenExpr(ParenExprContext ctx) {
184                 LOG.trace("exitParenExpr: text = "+ctx.getText());
185         }
186
187         @Override
188         public void enterRelExpr(RelExprContext ctx) {  
189                 List<TerminalNode> opList = ctx.RELOP();
190                 
191
192                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
193                 pushExpr(curBinExpr);
194
195                 
196                 for (TerminalNode nd : opList)
197                 {
198                         curBinExpr.addOperator(nd.getText());
199                 }
200                 
201         }
202
203         @Override
204         public void exitRelExpr(RelExprContext ctx) {   
205                 popExpr();
206         }
207
208         @Override
209         public void enterCompareExpr(CompareExprContext ctx) {
210                 
211                 TerminalNode nd = ctx.COMPAREOP();
212
213                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
214                 pushExpr(curBinExpr);
215
216                 curBinExpr.addOperator(nd.getText());
217
218         }
219
220         @Override
221         public void exitCompareExpr(CompareExprContext ctx) {
222                 
223                 popExpr();
224         }
225
226
227         
228         @Override 
229         public void enterConstant(ConstantContext ctx) {
230         }
231
232         @Override
233         public void exitConstant(ConstantContext ctx) {
234         }
235
236
237         @Override
238         public void enterVariable(VariableContext ctx) {
239         }
240
241         @Override
242         public void exitVariable(VariableContext ctx) { 
243         }
244         
245
246         @Override
247         public void enterVariableLead(VariableLeadContext ctx) {
248         }
249
250         @Override
251         public void exitVariableLead(VariableLeadContext ctx) {
252         }
253
254         @Override
255         public void enterVariableTerm(VariableTermContext ctx) {                
256                 String name = ctx.getText();
257                 
258                 int subscrStart = name.indexOf("[");
259                 if (subscrStart > -1)
260                 {
261                         name = name.substring(0, subscrStart);
262                 }
263                 SvcLogicVariableTerm vterm = new SvcLogicVariableTerm(name);
264                 pushExpr(vterm);
265         }
266
267         @Override
268         public void exitVariableTerm(VariableTermContext ctx) {
269             popExpr();
270         }
271 }