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