blob: 26bc82c4ba1677cd1ac6a1e9e6ad5fad181ce9e8 [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.YangBelongsTo;
20import org.onosproject.yangutils.datamodel.YangImport;
21import org.onosproject.yangutils.datamodel.YangModule;
22import org.onosproject.yangutils.parser.Parsable;
23import org.onosproject.yangutils.parser.ParsableDataType;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053024import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053025import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053026import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053027import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
28import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
29import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
30import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +053031
32/*
33 * Reference: RFC6020 and YANG ANTLR Grammar
34 *
35 * ABNF grammar as per RFC6020
36 * module-header-stmts = ;; these stmts can appear in any order
37 * [yang-version-stmt stmtsep]
38 * namespace-stmt stmtsep
39 * prefix-stmt stmtsep
40 *
41 * prefix-stmt = prefix-keyword sep prefix-arg-str
42 * optsep stmtend
43 *
44 * ANTLR grammar rule
45 * module_header_statement : yang_version_stmt? namespace_stmt prefix_stmt
46 * | yang_version_stmt? prefix_stmt namespace_stmt
47 * | namespace_stmt yang_version_stmt? prefix_stmt
48 * | namespace_stmt prefix_stmt yang_version_stmt?
49 * | prefix_stmt namespace_stmt yang_version_stmt?
50 * | prefix_stmt yang_version_stmt? namespace_stmt
51 * ;
52 * prefix_stmt : PREFIX_KEYWORD IDENTIFIER STMTEND;
53 */
54
55/**
56 * Implements listener based call back function corresponding to the "prefix"
57 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
58 */
59public final class PrefixListener {
60
61 /**
62 * Creates a new prefix listener.
63 */
64 private PrefixListener() {
65 }
66
67 /**
68 * It is called when parser receives an input matching the grammar
69 * rule (prefix),perform validations and update the data model
70 * tree.
71 *
72 * @param listener Listener's object.
73 * @param ctx context object of the grammar rule.
74 */
75 public static void processPrefixEntry(TreeWalkListener listener, GeneratedYangParser.PrefixStatementContext ctx) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053076
77 // Check for stack to be non empty.
78 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
79 ParsableDataType.PREFIX_DATA,
80 String.valueOf(ctx.IDENTIFIER().getText()),
81 ListenerErrorLocation.ENTRY);
82
83 // Obtain the node of the stack.
84 Parsable tmpNode = listener.getParsedDataStack().peek();
85 switch (tmpNode.getParsableDataType()) {
86 case MODULE_DATA: {
87 YangModule module = (YangModule) tmpNode;
88 module.setPrefix(ctx.IDENTIFIER().getText());
89 break;
90 }
91 case IMPORT_DATA: {
92 YangImport importNode = (YangImport) tmpNode;
93 importNode.setPrefixId(ctx.IDENTIFIER().getText());
94 break;
95 }
96 case BELONGS_TO_DATA: {
97 YangBelongsTo belongstoNode = (YangBelongsTo) tmpNode;
98 belongstoNode.setPrefix(ctx.IDENTIFIER().getText());
99 break;
100 }
101 default:
102 throw new ParserException(
103 ListenerErrorMessageConstruction
104 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
105 ParsableDataType.PREFIX_DATA, String
106 .valueOf(ctx.IDENTIFIER()
107 .getText()),
108 ListenerErrorLocation.ENTRY));
109 }
Gaurav Agrawal2cbb9502016-02-12 16:50:55 +0530110 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530111}