blob: 368f2af1c2144834119aed66ef33a2626dd4b63c [file] [log] [blame]
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053025
26import static org.onosproject.yangutils.parser.ParsableDataType.YANGBASE_DATA;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053033
34/*
35 * Reference: RFC6020 and YANG ANTLR Grammar
36 *
37 * ANTLR grammar rule
38 * yangfile : module_stmt
39 * | submodule_stmt;
40 */
41
42/**
43 * Implements call back function corresponding to the "base rule" defined in
44 * ANTLR grammar file.
45 */
46public final class BaseFileListener {
47
48 /**
49 * Creates a new base listener.
50 */
51 private BaseFileListener() {
52 }
53
54 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053055 * It is called when parser receives an input matching the grammar rule
56 * (yangfile), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053057 *
58 * @param listener Listener's object.
59 * @param ctx context object of the grammar rule.
60 */
61 public static void processYangFileEntry(TreeWalkListener listener, GeneratedYangParser.YangfileContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053062
63 // Check if stack is empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053064 checkStackIsEmpty(listener, INVALID_HOLDER, YANGBASE_DATA, "", ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053065
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053066 }
67
68 /**
69 * It is called when parser exits from grammar rule (yangfile), it perform
70 * validations and update the data model tree.
71 *
72 * @param listener Listener's object.
73 * @param ctx context object of the grammar rule.
74 */
75 public static void processYangFileExit(TreeWalkListener listener, GeneratedYangParser.YangfileContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053076
77 // Check for stack to be non empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053078 checkStackIsNotEmpty(listener, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053079
80 // Data Model tree root node is set.
81 if (listener.getParsedDataStack().peek() instanceof YangModule
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053082 || listener.getParsedDataStack().peek() instanceof YangSubModule) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053083 listener.setRootNode((YangNode) listener.getParsedDataStack().pop());
84 } else {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053085 throw new ParserException(constructListenerErrorMessage(INVALID_CHILD, YANGBASE_DATA, "", EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +053086 }
87
88 // Check if stack is empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053089 checkStackIsEmpty(listener, INVALID_HOLDER, YANGBASE_DATA, "", EXIT);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053090 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053091}