blob: b04f617a8b830e0ddfe4ba716b62c8aea46facee [file] [log] [blame]
Gaurav Agrawal88897632016-02-12 18:37:50 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal88897632016-02-12 18:37:50 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.parser.impl;
18
Vinod Kumar S71cba682016-02-25 15:52:16 +053019import java.io.IOException;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053020
Gaurav Agrawal88897632016-02-12 18:37:50 +053021import org.antlr.v4.runtime.ANTLRFileStream;
22import org.antlr.v4.runtime.ANTLRInputStream;
23import org.antlr.v4.runtime.CommonTokenStream;
24import org.antlr.v4.runtime.tree.ParseTree;
25import org.antlr.v4.runtime.tree.ParseTreeWalker;
26import org.onosproject.yangutils.datamodel.YangNode;
27import org.onosproject.yangutils.parser.YangUtilsParser;
28import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangLexer;
29import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
30import org.onosproject.yangutils.parser.exceptions.ParserException;
31import org.onosproject.yangutils.parser.impl.parserutils.ParseTreeErrorListener;
32
Gaurav Agrawal88897632016-02-12 18:37:50 +053033/**
Bharat saraswald9822e92016-04-05 15:13:44 +053034 * Represents file parsing, parse tree creation and data model tree creation
Gaurav Agrawal88897632016-02-12 18:37:50 +053035 * corresponding to an input YANG file.
36 */
37public class YangUtilsParserManager implements YangUtilsParser {
38
39 @Override
40 public YangNode getDataModel(String yangFile) throws IOException, ParserException {
41
42 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +053043 * Create a char stream that reads from YANG file. Throws an exception
44 * in case input YANG file is either null or non existent.
45 */
Vidyashree Rama74453712016-04-18 12:29:39 +053046 ANTLRInputStream input;
Gaurav Agrawal88897632016-02-12 18:37:50 +053047 try {
48 input = new ANTLRFileStream(yangFile);
49 } catch (IOException e) {
Vidyashree Rama74453712016-04-18 12:29:39 +053050 throw new ParserException("YANG file error : YANG file does not exist.");
Gaurav Agrawal88897632016-02-12 18:37:50 +053051 }
52
53 // Create a lexer that feeds off of input char stream.
54 GeneratedYangLexer lexer = new GeneratedYangLexer(input);
55
56 // Create a buffer of tokens pulled from the lexer.
57 CommonTokenStream tokens = new CommonTokenStream(lexer);
58
59 // Create a parser that feeds off the tokens buffer.
60 GeneratedYangParser parser = new GeneratedYangParser(tokens);
61
62 // Remove console error listener.
63 parser.removeErrorListeners();
64
65 // Create instance of customized error listener.
66 ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener();
67
68 // Add customized error listener to catch errors during parsing.
69 parser.addErrorListener(parseTreeErrorListener);
70
Gaurav Agrawala04483c2016-02-13 14:23:40 +053071 ParseTree tree;
Gaurav Agrawal88897632016-02-12 18:37:50 +053072
Gaurav Agrawala04483c2016-02-13 14:23:40 +053073 try {
74 // Begin parsing YANG file and generate parse tree.
75 tree = parser.yangfile();
76 } catch (ParserException parserException) {
Gaurav Agrawal88897632016-02-12 18:37:50 +053077 parserException.setFileName(yangFile);
78 throw parserException;
79 }
80
81 // Create a walker to walk the parse tree.
82 ParseTreeWalker walker = new ParseTreeWalker();
83
84 // Create a listener implementation class object.
85 TreeWalkListener treeWalker = new TreeWalkListener();
86
87 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +053088 * Walk parse tree, provide call backs to methods in listener and build
89 * data model tree.
90 */
Gaurav Agrawala04483c2016-02-13 14:23:40 +053091 try {
92 walker.walk(treeWalker, tree);
93 } catch (ParserException listenerException) {
94 // TODO free incomplete data model tree.
Gaurav Agrawal88897632016-02-12 18:37:50 +053095 listenerException.setFileName(yangFile);
96 throw listenerException;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053097 } finally {
98 // TODO free parsable stack
Gaurav Agrawal88897632016-02-12 18:37:50 +053099 }
100
101 // Returns the Root Node of the constructed data model tree.
102 return treeWalker.getRootNode();
103 }
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530104}