Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 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.YangRevision; |
| 21 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 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 | |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.isDateValid; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.utils.YangConstructType.REVISION_DATA; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 40 | * |
| 41 | * ABNF grammar as per RFC6020 |
| 42 | * module-stmt = optsep module-keyword sep identifier-arg-str |
| 43 | * optsep |
| 44 | * "{" stmtsep |
| 45 | * module-header-stmts |
| 46 | * linkage-stmts |
| 47 | * meta-stmts |
| 48 | * revision-stmts |
| 49 | * body-stmts |
| 50 | * "}" optsep |
| 51 | * |
| 52 | * revision-stmt = revision-keyword sep revision-date optsep |
| 53 | * (";" / |
| 54 | * "{" stmtsep |
| 55 | * [description-stmt stmtsep] |
| 56 | * [reference-stmt stmtsep] |
| 57 | * "}") |
| 58 | * |
| 59 | * ANTLR grammar rule |
| 60 | * module_stmt : MODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE; |
| 61 | * |
| 62 | * revision_stmt : REVISION_KEYWORD DATE_ARG (STMTEND | LEFT_CURLY_BRACE revision_stmt_body RIGHT_CURLY_BRACE); |
| 63 | * revision_stmt_body : description_stmt? reference_stmt?; |
| 64 | */ |
| 65 | |
| 66 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 67 | * Represents listener based call back function corresponding to the "revision" |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 68 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 69 | */ |
| 70 | public final class RevisionListener { |
| 71 | |
| 72 | /** |
| 73 | * Creates a new revision listener. |
| 74 | */ |
| 75 | private RevisionListener() { |
| 76 | } |
| 77 | |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 78 | /** |
| 79 | * It is called when parser receives an input matching the grammar rule |
| 80 | * (revision),perform validations and update the data model tree. |
| 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 | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 84 | */ |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 85 | public static void processRevisionEntry(TreeWalkListener listener, |
| 86 | GeneratedYangParser.RevisionStatementContext ctx) { |
| 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, REVISION_DATA, ctx.dateArgumentString().getText(), ENTRY); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 90 | |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 91 | // Validate for reverse chronological order of revision & for revision |
| 92 | // value. |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 93 | if (!validateRevision(listener, ctx)) { |
| 94 | return; |
| 95 | // TODO to be implemented. |
| 96 | } |
| 97 | |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 98 | String date = removeQuotesAndHandleConcat(ctx.dateArgumentString().getText()); |
| 99 | if (!isDateValid(date)) { |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 100 | ParserException parserException = new ParserException("YANG file error: Input date is not correct"); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 101 | parserException.setLine(ctx.getStart().getLine()); |
| 102 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 103 | throw parserException; |
| 104 | } |
| 105 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 106 | YangRevision revisionNode = new YangRevision(); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 107 | revisionNode.setRevDate(date); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 108 | |
| 109 | listener.getParsedDataStack().push(revisionNode); |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * It is called when parser exits from grammar rule (revision), it perform |
| 114 | * validations and update the data model tree. |
| 115 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 116 | * @param listener Listener's object |
| 117 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 118 | */ |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 119 | public static void processRevisionExit(TreeWalkListener listener, GeneratedYangParser.RevisionStatementContext |
| 120 | ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 121 | |
| 122 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 123 | checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.dateArgumentString().getText(), EXIT); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 124 | |
| 125 | Parsable tmpRevisionNode = listener.getParsedDataStack().peek(); |
| 126 | if (tmpRevisionNode instanceof YangRevision) { |
| 127 | listener.getParsedDataStack().pop(); |
| 128 | |
| 129 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 130 | checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.dateArgumentString().getText(), |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 131 | EXIT); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 132 | |
| 133 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 134 | switch (tmpNode.getYangConstructType()) { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 135 | case MODULE_DATA: { |
| 136 | YangModule module = (YangModule) tmpNode; |
| 137 | module.setRevision((YangRevision) tmpRevisionNode); |
| 138 | break; |
| 139 | } |
| 140 | case SUB_MODULE_DATA: { |
| 141 | YangSubModule subModule = (YangSubModule) tmpNode; |
| 142 | subModule.setRevision((YangRevision) tmpRevisionNode); |
| 143 | break; |
| 144 | } |
| 145 | default: |
| 146 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATA, |
| 147 | ctx.dateArgumentString().getText(), |
| 148 | EXIT)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 149 | } |
| 150 | } else { |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 151 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, REVISION_DATA, |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 152 | ctx.dateArgumentString().getText(), EXIT)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 153 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 154 | } |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * Validate revision. |
| 158 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 159 | * @param listener Listener's object |
| 160 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 161 | * @return validation result |
| 162 | */ |
| 163 | private static boolean validateRevision(TreeWalkListener listener, |
| 164 | GeneratedYangParser.RevisionStatementContext ctx) { |
| 165 | // TODO to be implemented |
| 166 | return true; |
| 167 | } |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 168 | } |