blob: 7b41b77b2eb28ee65c193f0f22c1cb552789ca18 [file] [log] [blame]
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +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.listeners;
18
Gaurav Agrawala04483c2016-02-13 14:23:40 +053019import org.onosproject.yangutils.datamodel.YangModule;
20import org.onosproject.yangutils.datamodel.YangNode;
21import org.onosproject.yangutils.datamodel.YangSubModule;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053022import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053023import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053024import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Bharat saraswald9822e92016-04-05 15:13:44 +053025
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053026import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053029import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CHILD;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053034import static org.onosproject.yangutils.utils.YangConstructType.YANGBASE_DATA;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053035
36/*
37 * Reference: RFC6020 and YANG ANTLR Grammar
38 *
39 * ANTLR grammar rule
40 * yangfile : module_stmt
41 * | submodule_stmt;
42 */
43
44/**
Bharat saraswald9822e92016-04-05 15:13:44 +053045 * Representation of call back function corresponding to the "base rule" defined in
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053046 * ANTLR grammar file.
47 */
48public final class BaseFileListener {
49
50 /**
51 * Creates a new base listener.
52 */
53 private BaseFileListener() {
54 }
55
56 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053057 * It is called when parser receives an input matching the grammar rule
58 * (yangfile), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053059 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053060 * @param listener Listener's object
61 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053062 */
63 public static void processYangFileEntry(TreeWalkListener listener, GeneratedYangParser.YangfileContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053064
65 // Check if stack is empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053066 checkStackIsEmpty(listener, INVALID_HOLDER, YANGBASE_DATA, "", ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053067
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053068 }
69
70 /**
71 * It is called when parser exits from grammar rule (yangfile), it perform
72 * validations and update the data model tree.
73 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053074 * @param listener Listener's object
75 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053076 */
77 public static void processYangFileExit(TreeWalkListener listener, GeneratedYangParser.YangfileContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053078
79 // Check for stack to be non empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053080 checkStackIsNotEmpty(listener, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053081
82 // Data Model tree root node is set.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053083 if ((listener.getParsedDataStack().peek() instanceof YangModule)
84 || (listener.getParsedDataStack().peek() instanceof YangSubModule)) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053085 listener.setRootNode((YangNode) listener.getParsedDataStack().pop());
86 } else {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053087 throw new ParserException(constructListenerErrorMessage(INVALID_CHILD, YANGBASE_DATA, "", EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +053088 }
89
90 // Check if stack is empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053091 checkStackIsEmpty(listener, INVALID_HOLDER, YANGBASE_DATA, "", EXIT);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053092 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053093}