blob: 99f68079350e1c062f0b5527f4f725bd8957836a [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 +053025import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
26import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053028import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CHILD;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053031import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053033import static org.onosproject.yangutils.utils.YangConstructType.YANGBASE_DATA;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053034
35/*
36 * Reference: RFC6020 and YANG ANTLR Grammar
37 *
38 * ANTLR grammar rule
39 * yangfile : module_stmt
40 * | submodule_stmt;
41 */
42
43/**
44 * Implements call back function corresponding to the "base rule" defined in
45 * ANTLR grammar file.
46 */
47public final class BaseFileListener {
48
49 /**
50 * Creates a new base listener.
51 */
52 private BaseFileListener() {
53 }
54
55 /**
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053056 * It is called when parser receives an input matching the grammar rule
57 * (yangfile), perform validations and update the data model tree.
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053058 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053059 * @param listener Listener's object
60 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053061 */
62 public static void processYangFileEntry(TreeWalkListener listener, GeneratedYangParser.YangfileContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053063
64 // Check if stack is empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053065 checkStackIsEmpty(listener, INVALID_HOLDER, YANGBASE_DATA, "", ENTRY);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053066
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053067 }
68
69 /**
70 * It is called when parser exits from grammar rule (yangfile), it perform
71 * validations and update the data model tree.
72 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053073 * @param listener Listener's object
74 * @param ctx context object of the grammar rule
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053075 */
76 public static void processYangFileExit(TreeWalkListener listener, GeneratedYangParser.YangfileContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053077
78 // Check for stack to be non empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053079 checkStackIsNotEmpty(listener, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053080
81 // Data Model tree root node is set.
Gaurav Agrawalb5a1c132016-02-21 02:56:46 +053082 if ((listener.getParsedDataStack().peek() instanceof YangModule)
83 || (listener.getParsedDataStack().peek() instanceof YangSubModule)) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053084 listener.setRootNode((YangNode) listener.getParsedDataStack().pop());
85 } else {
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053086 throw new ParserException(constructListenerErrorMessage(INVALID_CHILD, YANGBASE_DATA, "", EXIT));
Gaurav Agrawala04483c2016-02-13 14:23:40 +053087 }
88
89 // Check if stack is empty.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053090 checkStackIsEmpty(listener, INVALID_HOLDER, YANGBASE_DATA, "", EXIT);
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053091 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053092}