Merge "fixed sonar issues in ExecuteNodeExecutor.java"
[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                 LOG.trace("Pushing expression ["+expr.getClass().getName()+"]");
83                 if (curExpr != null)
84                 {
85                         exprStack.push(curExpr);
86                 }
87                 curExpr = expr;
88         }
89         
90         private void popExpr()
91         {
92                 if (exprStack.isEmpty())
93                 {
94                         LOG.trace("Popping last expression");
95                         topExpr = curExpr;
96                 }
97                 else
98                 {
99                         SvcLogicExpression lastExpr = curExpr;
100                         curExpr = exprStack.pop();
101                         curExpr.addOperand(lastExpr);
102                         LOG.trace("New curExpr is ["+curExpr.getClass().getName()+"]");
103                 }
104                 
105         }
106         
107         @Override
108         public void enterAtom(AtomContext ctx) {
109                 
110                 String atomText = ctx.getText();
111                 
112                 LOG.trace("enterAtom: text = "+atomText);
113
114                 
115                 SvcLogicAtom newAtom = new SvcLogicAtom(atomText);
116                 
117                 pushExpr(newAtom);
118         }
119
120
121         @Override
122         public void enterMultExpr(MultExprContext ctx) {
123                 LOG.trace("enterMultExpr: text = "+ctx.getText());
124                 
125                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
126                 pushExpr(curBinExpr);
127                 
128                 List<TerminalNode> opList = ctx.MULTOP();
129                 
130                 for (TerminalNode nd : opList)
131                 {
132                         LOG.trace("enterMultExpr: operator - "+nd.getText());
133                         curBinExpr.addOperator(nd.getText());
134                 }
135
136         }
137
138         @Override
139         public void exitMultExpr(MultExprContext ctx) {
140
141                 LOG.trace("exitMultExpr: text = "+ctx.getText());
142
143                 popExpr();
144                 
145         }
146
147         @Override
148         public void exitAtom(AtomContext ctx) {
149                 LOG.trace("exitAtom: text = "+ctx.getText());
150                 popExpr();
151         }
152
153         @Override
154         public void enterAddExpr(AddExprContext ctx) {
155                 LOG.trace("enterAddExpr: text = "+ctx.getText());
156                 List<TerminalNode> opList = ctx.ADDOP();
157                 
158
159                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
160                 pushExpr(curBinExpr);
161
162                 
163                 for (TerminalNode nd : opList)
164                 {
165                         LOG.trace("enterAddExpr: operator - "+nd.getText());
166                         curBinExpr.addOperator(nd.getText());
167                 }
168                 
169         }
170
171         @Override
172         public void exitAddExpr(AddExprContext ctx) {
173                 LOG.trace("exitAddExpr: text = "+ctx.getText());
174                 
175                 popExpr();
176         }
177
178         @Override
179         public void enterFuncExpr(FuncExprContext ctx) {
180                 LOG.trace("enterFuncExpr: text = "+ctx.getText());
181                 LOG.trace("enterFuncExpr - IDENTIFIER : "+ctx.IDENTIFIER().getText());
182                 
183                 for (ExprContext expr: ctx.expr())
184                 {
185                         LOG.trace("enterFuncExpr - expr = "+expr.getText());
186                 }
187                 
188
189                 pushExpr(new SvcLogicFunctionCall(ctx.IDENTIFIER().getText()));
190         }
191
192         @Override
193         public void exitFuncExpr(FuncExprContext ctx) {
194                 LOG.trace("exitFuncExpr: text = "+ctx.getText());
195                 
196                 popExpr();
197         }
198
199         @Override
200         public void enterParenExpr(ParenExprContext ctx) {
201                 LOG.trace("enterParenExpr: text = "+ctx.getText());
202                 LOG.trace("enterParenExpr: expr = "+ctx.expr().getText());
203         }
204
205         @Override
206         public void exitParenExpr(ParenExprContext ctx) {
207                 LOG.trace("exitParenExpr: text = "+ctx.getText());
208         }
209
210         @Override
211         public void enterRelExpr(RelExprContext ctx) {
212                 LOG.trace("enterRelExpr: text = "+ctx.getText());
213                 
214                 List<TerminalNode> opList = ctx.RELOP();
215                 
216
217                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
218                 pushExpr(curBinExpr);
219
220                 
221                 for (TerminalNode nd : opList)
222                 {
223                         LOG.trace("enterRelExpr: operator - "+nd.getText());
224                         curBinExpr.addOperator(nd.getText());
225                 }
226                 
227         }
228
229         @Override
230         public void exitRelExpr(RelExprContext ctx) {
231                 LOG.trace("exitRelExpr: text = "+ctx.getText());
232                 
233                 popExpr();
234         }
235
236         @Override
237         public void enterCompareExpr(CompareExprContext ctx) {
238                 LOG.trace("enterCompareExpr: text = "+ctx.getText());
239                 
240                 TerminalNode nd = ctx.COMPAREOP();
241
242                 SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression();
243                 pushExpr(curBinExpr);
244
245                 LOG.trace("enterCompareExpr: operator - "+nd.getText());
246                 curBinExpr.addOperator(nd.getText());
247
248         }
249
250         @Override
251         public void exitCompareExpr(CompareExprContext ctx) {
252                 LOG.trace("exitCompareExpr : text = "+ctx.getText());
253                 
254                 popExpr();
255         }
256
257
258         
259         @Override 
260         public void enterConstant(ConstantContext ctx) {
261                 LOG.trace("enterConstant: text = "+ctx.getText());
262         }
263
264         @Override
265         public void exitConstant(ConstantContext ctx) {
266                 LOG.trace("exitConstant: text = "+ctx.getText());
267         }
268
269
270         @Override
271         public void enterVariable(VariableContext ctx) {
272                 LOG.trace("enterVariable: text = "+ctx.getText());
273                 
274                 
275         }
276
277         @Override
278         public void exitVariable(VariableContext ctx) {
279                 LOG.debug("exitVariable: text ="+ctx.getText());
280                 
281         }
282         
283
284         @Override
285         public void enterVariableLead(VariableLeadContext ctx) {
286
287                 LOG.debug("enterVariableLead: text ="+ctx.getText());
288                 
289
290         }
291
292         @Override
293         public void exitVariableLead(VariableLeadContext ctx) {
294
295                 LOG.trace("exitVariableLead: text ="+ctx.getText());
296         }
297
298         @Override
299         public void enterVariableTerm(VariableTermContext ctx) {
300                 LOG.trace("enterVariableTerm: text ="+ctx.getText());
301                 
302                 String name = ctx.getText();
303                 
304                 int subscrStart = name.indexOf("[");
305                 if (subscrStart > -1)
306                 {
307                         name = name.substring(0, subscrStart);
308                 }
309                 SvcLogicVariableTerm vterm = new SvcLogicVariableTerm(name);
310                 pushExpr(vterm);
311         }
312
313         @Override
314         public void exitVariableTerm(VariableTermContext ctx) {
315                 LOG.trace("exitVariableTerm: text="+ctx.getText());
316                 popExpr();
317         }
318 }