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