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.YangRevision; |
| 21 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 22 | import org.onosproject.yangutils.parser.Parsable; |
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 | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 26 | |
| 27 | import static org.onosproject.yangutils.parser.ParsableDataType.REVISION_DATA; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.*; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | 2cbb950 | 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-stmt = optsep module-keyword sep identifier-arg-str |
| 39 | * optsep |
| 40 | * "{" stmtsep |
| 41 | * module-header-stmts |
| 42 | * linkage-stmts |
| 43 | * meta-stmts |
| 44 | * revision-stmts |
| 45 | * body-stmts |
| 46 | * "}" optsep |
| 47 | * |
| 48 | * revision-stmt = revision-keyword sep revision-date optsep |
| 49 | * (";" / |
| 50 | * "{" stmtsep |
| 51 | * [description-stmt stmtsep] |
| 52 | * [reference-stmt stmtsep] |
| 53 | * "}") |
| 54 | * |
| 55 | * ANTLR grammar rule |
| 56 | * module_stmt : MODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE; |
| 57 | * |
| 58 | * revision_stmt : REVISION_KEYWORD DATE_ARG (STMTEND | LEFT_CURLY_BRACE revision_stmt_body RIGHT_CURLY_BRACE); |
| 59 | * revision_stmt_body : description_stmt? reference_stmt?; |
| 60 | */ |
| 61 | |
| 62 | /** |
| 63 | * Implements listener based call back function corresponding to the "revision" |
| 64 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 65 | */ |
| 66 | public final class RevisionListener { |
| 67 | |
| 68 | /** |
| 69 | * Creates a new revision listener. |
| 70 | */ |
| 71 | private RevisionListener() { |
| 72 | } |
| 73 | |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 74 | /** |
| 75 | * It is called when parser receives an input matching the grammar rule |
| 76 | * (revision),perform validations and update the data model tree. |
| 77 | * |
| 78 | * @param listener Listener's object. |
| 79 | * @param ctx context object of the grammar rule. |
| 80 | */ |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 81 | public static void processRevisionEntry(TreeWalkListener listener, |
| 82 | GeneratedYangParser.RevisionStatementContext ctx) { |
| 83 | |
| 84 | // Check for stack to be non empty. |
Gaurav Agrawal | b5a1c13 | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 85 | checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(), ENTRY); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 86 | |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 87 | // Validate for reverse chronological order of revision & for revision |
| 88 | // value. |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 89 | if (!validateRevision(listener, ctx)) { |
| 90 | return; |
| 91 | // TODO to be implemented. |
| 92 | } |
| 93 | |
| 94 | YangRevision revisionNode = new YangRevision(); |
Gaurav Agrawal | b5a1c13 | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 95 | revisionNode.setRevDate(ctx.DATE_ARG().getText()); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 96 | |
| 97 | listener.getParsedDataStack().push(revisionNode); |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * It is called when parser exits from grammar rule (revision), it perform |
| 102 | * validations and update the data model tree. |
| 103 | * |
| 104 | * @param listener Listener's object. |
| 105 | * @param ctx context object of the grammar rule. |
| 106 | */ |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 107 | public static void processRevisionExit(TreeWalkListener listener, GeneratedYangParser.RevisionStatementContext |
| 108 | ctx) { |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 109 | |
| 110 | // Check for stack to be non empty. |
Gaurav Agrawal | b5a1c13 | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 111 | checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(), EXIT); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 112 | |
| 113 | Parsable tmpRevisionNode = listener.getParsedDataStack().peek(); |
| 114 | if (tmpRevisionNode instanceof YangRevision) { |
| 115 | listener.getParsedDataStack().pop(); |
| 116 | |
| 117 | // Check for stack to be non empty. |
Gaurav Agrawal | b5a1c13 | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 118 | checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(), |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 119 | EXIT); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 120 | |
| 121 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 122 | switch (tmpNode.getParsableDataType()) { |
| 123 | case MODULE_DATA: { |
| 124 | YangModule module = (YangModule) tmpNode; |
| 125 | module.setRevision((YangRevision) tmpRevisionNode); |
| 126 | break; |
| 127 | } |
| 128 | case SUB_MODULE_DATA: { |
| 129 | YangSubModule subModule = (YangSubModule) tmpNode; |
| 130 | subModule.setRevision((YangRevision) tmpRevisionNode); |
| 131 | break; |
| 132 | } |
| 133 | default: |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 134 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATA, |
Gaurav Agrawal | b5a1c13 | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 135 | ctx.DATE_ARG().getText(), |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 136 | EXIT)); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 137 | } |
| 138 | } else { |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 139 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, REVISION_DATA, |
Gaurav Agrawal | b5a1c13 | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 140 | ctx.DATE_ARG().getText(), EXIT)); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 141 | } |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 142 | } |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * Validate revision. |
| 146 | * |
| 147 | * @param listener Listener's object. |
| 148 | * @param ctx context object of the grammar rule. |
| 149 | * @return validation result |
| 150 | */ |
| 151 | private static boolean validateRevision(TreeWalkListener listener, |
| 152 | GeneratedYangParser.RevisionStatementContext ctx) { |
| 153 | // TODO to be implemented |
| 154 | return true; |
| 155 | } |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 156 | } |