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