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 | |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.utils.YangConstructType.VERSION_DATA; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 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 | * submodule-header-stmts = |
| 44 | * ;; these stmts can appear in any order |
| 45 | * [yang-version-stmt stmtsep] |
| 46 | * belongs-to-stmt stmtsep |
| 47 | * |
| 48 | * yang-version-stmt = yang-version-keyword sep yang-version-arg-str |
| 49 | * optsep stmtend |
| 50 | * |
| 51 | * |
| 52 | * ANTLR grammar rule |
| 53 | * module_header_statement : yang_version_stmt? namespace_stmt prefix_stmt |
| 54 | * | yang_version_stmt? prefix_stmt namespace_stmt |
| 55 | * | namespace_stmt yang_version_stmt? prefix_stmt |
| 56 | * | namespace_stmt prefix_stmt yang_version_stmt? |
| 57 | * | prefix_stmt namespace_stmt yang_version_stmt? |
| 58 | * | prefix_stmt yang_version_stmt? namespace_stmt? |
| 59 | * ; |
| 60 | * submodule_header_statement : yang_version_stmt? belongs_to_stmt |
| 61 | * | belongs_to_stmt yang_version_stmt? |
| 62 | * ; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 63 | * yang_version_stmt : YANG_VERSION_KEYWORD string STMTEND; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 64 | */ |
| 65 | |
| 66 | /** |
| 67 | * Implements listener based call back function corresponding to the "version" |
| 68 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 69 | */ |
| 70 | public final class VersionListener { |
| 71 | |
| 72 | /** |
| 73 | * Creates a new version listener. |
| 74 | */ |
| 75 | private VersionListener() { |
| 76 | } |
| 77 | |
| 78 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 79 | * It is called when parser receives an input matching the grammar rule |
| 80 | * (version), perform validations and update the data model tree. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 81 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 82 | * @param listener Listener's object |
| 83 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 84 | */ |
| 85 | public static void processVersionEntry(TreeWalkListener listener, |
| 86 | GeneratedYangParser.YangVersionStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 87 | |
| 88 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 89 | checkStackIsNotEmpty(listener, MISSING_HOLDER, VERSION_DATA, ctx.string().getText(), ENTRY); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 90 | |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 91 | String version = removeQuotesAndHandleConcat(ctx.string().getText()); |
| 92 | if (!isVersionValid(Integer.valueOf(version))) { |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 93 | ParserException parserException = new ParserException("YANG file error: Input version not supported"); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 94 | parserException.setLine(ctx.getStart().getLine()); |
| 95 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 96 | throw parserException; |
| 97 | } |
| 98 | |
| 99 | // Obtain the node of the stack. |
| 100 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 101 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 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: |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 113 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, VERSION_DATA, |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 114 | ctx.string().getText(), ENTRY)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 115 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 116 | } |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Validates whether the value of YANG version. |
| 120 | * |
| 121 | * @param version input yang version |
| 122 | * @return validation result |
| 123 | */ |
| 124 | private static boolean isVersionValid(Integer version) { |
| 125 | return version == 1; |
| 126 | } |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 127 | } |