logging changes
[ccsdk/sli/core.git] / sli / common / src / main / java / org / onap / ccsdk / sli / core / sli / SvcLogicExpressionFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
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.onap.ccsdk.sli.core.sli;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27
28 import org.antlr.v4.runtime.ANTLRInputStream;
29 import org.antlr.v4.runtime.CharStream;
30 import org.antlr.v4.runtime.CommonTokenStream;
31 import org.antlr.v4.runtime.tree.ParseTreeWalker;
32 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ExprContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36
37 public class SvcLogicExpressionFactory {
38         
39         private static final Logger LOG = LoggerFactory
40                         .getLogger(SvcLogicExpressionFactory.class);
41
42         
43         public static SvcLogicExpression parse(String exprStr) throws IOException
44         {
45                 InputStream exprStream = new ByteArrayInputStream(exprStr.getBytes());
46                 CharStream input = new ANTLRInputStream(exprStream);
47                 ExprGrammarLexer lexer = new ExprGrammarLexer(input);
48                 CommonTokenStream tokens = new CommonTokenStream(lexer);
49                 ExprGrammarParser parser = new ExprGrammarParser(tokens);
50
51                 lexer.removeErrorListeners();
52                 lexer.addErrorListener(SvcLogicExprParserErrorListener.getInstance());          
53                 parser.removeErrorListeners();
54                 parser.addErrorListener(SvcLogicExprParserErrorListener.getInstance());
55
56                 ExprContext expression = null;
57                 
58                 try {
59                         expression = parser.expr();
60                 } catch (Exception e) {
61                         String errorMsg = e.getMessage();
62                         
63                         LOG.error(errorMsg);
64                         throw new SvcLogicParserException(errorMsg);
65                 }
66                 
67         
68                 ParseTreeWalker walker = new ParseTreeWalker();
69                 SvcLogicExprListener listener = new SvcLogicExprListener();
70                 walker.walk(listener, expression);
71                 
72                 
73                 return(listener.getParsedExpr());
74         }
75         
76         public static void main(String argv[]) {
77
78
79                 System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "debug");
80
81                 StringBuffer sbuff = new StringBuffer();
82                 
83                 for (int i = 0 ; i < argv.length ; i++)
84                 {
85                         if (sbuff.length() > 0)
86                         {
87                                 sbuff.append(" ");
88                         }
89                         sbuff.append(argv[i]);
90                 }
91                 
92                 try {
93                         SvcLogicExpressionFactory.parse(sbuff.toString());
94                 } catch (IOException e) {
95                         LOG.error("Exception in SvcLogicExpressionFactory.parse",e);
96                 }
97         }
98 }