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.YangModule; |
| 20 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 21 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 25 | |
| 26 | import static org.onosproject.yangutils.parser.ParsableDataType.VERSION_DATA; |
| 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 32 | |
| 33 | /* |
| 34 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 35 | * |
| 36 | * ABNF grammar as per RFC6020 |
| 37 | * module-header-stmts = ;; these stmts can appear in any order |
| 38 | * [yang-version-stmt stmtsep] |
| 39 | * namespace-stmt stmtsep |
| 40 | * prefix-stmt stmtsep |
| 41 | * |
| 42 | * submodule-header-stmts = |
| 43 | * ;; these stmts can appear in any order |
| 44 | * [yang-version-stmt stmtsep] |
| 45 | * belongs-to-stmt stmtsep |
| 46 | * |
| 47 | * yang-version-stmt = yang-version-keyword sep yang-version-arg-str |
| 48 | * optsep stmtend |
| 49 | * |
| 50 | * |
| 51 | * ANTLR grammar rule |
| 52 | * module_header_statement : yang_version_stmt? namespace_stmt prefix_stmt |
| 53 | * | yang_version_stmt? prefix_stmt namespace_stmt |
| 54 | * | namespace_stmt yang_version_stmt? prefix_stmt |
| 55 | * | namespace_stmt prefix_stmt yang_version_stmt? |
| 56 | * | prefix_stmt namespace_stmt yang_version_stmt? |
| 57 | * | prefix_stmt yang_version_stmt? namespace_stmt? |
| 58 | * ; |
| 59 | * submodule_header_statement : yang_version_stmt? belongs_to_stmt |
| 60 | * | belongs_to_stmt yang_version_stmt? |
| 61 | * ; |
| 62 | * yang_version_stmt : YANG_VERSION_KEYWORD INTEGER STMTEND; |
| 63 | */ |
| 64 | |
| 65 | /** |
| 66 | * Implements listener based call back function corresponding to the "version" |
| 67 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 68 | */ |
| 69 | public final class VersionListener { |
| 70 | |
| 71 | /** |
| 72 | * Creates a new version listener. |
| 73 | */ |
| 74 | private VersionListener() { |
| 75 | } |
| 76 | |
| 77 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 78 | * It is called when parser receives an input matching the grammar rule |
| 79 | * (version), perform validations and update the data model tree. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 80 | * |
| 81 | * @param listener Listener's object. |
| 82 | * @param ctx context object of the grammar rule. |
| 83 | */ |
| 84 | public static void processVersionEntry(TreeWalkListener listener, |
| 85 | GeneratedYangParser.YangVersionStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 86 | |
| 87 | // Check for stack to be non empty. |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 88 | checkStackIsNotEmpty(listener, MISSING_HOLDER, VERSION_DATA, ctx.INTEGER().getText(), ENTRY); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 89 | |
| 90 | Integer version = Integer.valueOf(ctx.INTEGER().getText()); |
| 91 | if (!isVersionValid(version)) { |
| 92 | ParserException parserException = new ParserException("Input version not supported"); |
| 93 | parserException.setLine(ctx.INTEGER().getSymbol().getLine()); |
| 94 | parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine()); |
| 95 | throw parserException; |
| 96 | } |
| 97 | |
| 98 | // Obtain the node of the stack. |
| 99 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 100 | switch (tmpNode.getParsableDataType()) { |
| 101 | case MODULE_DATA: { |
| 102 | YangModule module = (YangModule) tmpNode; |
| 103 | module.setVersion((byte) 1); |
| 104 | break; |
| 105 | } |
| 106 | case SUB_MODULE_DATA: { |
| 107 | YangSubModule subModule = (YangSubModule) tmpNode; |
| 108 | subModule.setVersion((byte) 1); |
| 109 | break; |
| 110 | } |
| 111 | default: |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 112 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, VERSION_DATA, |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 113 | ctx.INTEGER().getText(), ENTRY)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 114 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 115 | } |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Validates whether the value of YANG version. |
| 119 | * |
| 120 | * @param version input yang version |
| 121 | * @return validation result |
| 122 | */ |
| 123 | private static boolean isVersionValid(Integer version) { |
| 124 | return version == 1; |
| 125 | } |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 126 | } |