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 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 30 | import static org.onosproject.yangutils.utils.YangConstructType.REVISION_DATA; |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 35 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 36 | import java.text.ParseException; |
| 37 | import java.text.SimpleDateFormat; |
| 38 | import java.util.Date; |
| 39 | |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 40 | /* |
| 41 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 42 | * |
| 43 | * ABNF grammar as per RFC6020 |
| 44 | * module-stmt = optsep module-keyword sep identifier-arg-str |
| 45 | * optsep |
| 46 | * "{" stmtsep |
| 47 | * module-header-stmts |
| 48 | * linkage-stmts |
| 49 | * meta-stmts |
| 50 | * revision-stmts |
| 51 | * body-stmts |
| 52 | * "}" optsep |
| 53 | * |
| 54 | * revision-stmt = revision-keyword sep revision-date optsep |
| 55 | * (";" / |
| 56 | * "{" stmtsep |
| 57 | * [description-stmt stmtsep] |
| 58 | * [reference-stmt stmtsep] |
| 59 | * "}") |
| 60 | * |
| 61 | * ANTLR grammar rule |
| 62 | * module_stmt : MODULE_KEYWORD IDENTIFIER LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE; |
| 63 | * |
| 64 | * revision_stmt : REVISION_KEYWORD DATE_ARG (STMTEND | LEFT_CURLY_BRACE revision_stmt_body RIGHT_CURLY_BRACE); |
| 65 | * revision_stmt_body : description_stmt? reference_stmt?; |
| 66 | */ |
| 67 | |
| 68 | /** |
| 69 | * Implements listener based call back function corresponding to the "revision" |
| 70 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 71 | */ |
| 72 | public final class RevisionListener { |
| 73 | |
| 74 | /** |
| 75 | * Creates a new revision listener. |
| 76 | */ |
| 77 | private RevisionListener() { |
| 78 | } |
| 79 | |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 80 | /** |
| 81 | * It is called when parser receives an input matching the grammar rule |
| 82 | * (revision),perform validations and update the data model tree. |
| 83 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 84 | * @param listener Listener's object |
| 85 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 86 | */ |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 87 | public static void processRevisionEntry(TreeWalkListener listener, |
| 88 | GeneratedYangParser.RevisionStatementContext ctx) { |
| 89 | |
| 90 | // Check for stack to be non empty. |
Gaurav Agrawal | b5a1c13 | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 91 | checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(), ENTRY); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 92 | |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 93 | // Validate for reverse chronological order of revision & for revision |
| 94 | // value. |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 95 | if (!validateRevision(listener, ctx)) { |
| 96 | return; |
| 97 | // TODO to be implemented. |
| 98 | } |
| 99 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 100 | if (!isDateValid(ctx.DATE_ARG().getText())) { |
| 101 | ParserException parserException = new ParserException("YANG file error: Input date is not correct"); |
| 102 | parserException.setLine(ctx.DATE_ARG().getSymbol().getLine()); |
| 103 | parserException.setCharPosition(ctx.DATE_ARG().getSymbol().getCharPositionInLine()); |
| 104 | throw parserException; |
| 105 | } |
| 106 | |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 107 | YangRevision revisionNode = new YangRevision(); |
Gaurav Agrawal | b5a1c13 | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 108 | revisionNode.setRevDate(ctx.DATE_ARG().getText()); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 109 | |
| 110 | listener.getParsedDataStack().push(revisionNode); |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
| 114 | * It is called when parser exits from grammar rule (revision), it perform |
| 115 | * validations and update the data model tree. |
| 116 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 117 | * @param listener Listener's object |
| 118 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 119 | */ |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 120 | public static void processRevisionExit(TreeWalkListener listener, GeneratedYangParser.RevisionStatementContext |
| 121 | ctx) { |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 122 | |
| 123 | // Check for stack to be non empty. |
Gaurav Agrawal | b5a1c13 | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 124 | checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(), EXIT); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 125 | |
| 126 | Parsable tmpRevisionNode = listener.getParsedDataStack().peek(); |
| 127 | if (tmpRevisionNode instanceof YangRevision) { |
| 128 | listener.getParsedDataStack().pop(); |
| 129 | |
| 130 | // Check for stack to be non empty. |
Gaurav Agrawal | b5a1c13 | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 131 | checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATA, ctx.DATE_ARG().getText(), |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 132 | EXIT); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 133 | |
| 134 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 135 | switch (tmpNode.getYangConstructType()) { |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 136 | case MODULE_DATA: { |
| 137 | YangModule module = (YangModule) tmpNode; |
| 138 | module.setRevision((YangRevision) tmpRevisionNode); |
| 139 | break; |
| 140 | } |
| 141 | case SUB_MODULE_DATA: { |
| 142 | YangSubModule subModule = (YangSubModule) tmpNode; |
| 143 | subModule.setRevision((YangRevision) tmpRevisionNode); |
| 144 | break; |
| 145 | } |
| 146 | default: |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 147 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATA, |
Gaurav Agrawal | b5a1c13 | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 148 | ctx.DATE_ARG().getText(), |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 149 | EXIT)); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 150 | } |
| 151 | } else { |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 152 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, REVISION_DATA, |
Gaurav Agrawal | b5a1c13 | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 153 | ctx.DATE_ARG().getText(), EXIT)); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 154 | } |
Gaurav Agrawal | 2cbb950 | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 155 | } |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 156 | |
| 157 | /** |
| 158 | * Validate revision. |
| 159 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 160 | * @param listener Listener's object |
| 161 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 162 | * @return validation result |
| 163 | */ |
| 164 | private static boolean validateRevision(TreeWalkListener listener, |
| 165 | GeneratedYangParser.RevisionStatementContext ctx) { |
| 166 | // TODO to be implemented |
| 167 | return true; |
| 168 | } |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * Validates the revision date. |
| 172 | * |
| 173 | * @param dateToValidate input revision date |
| 174 | * @return validation result, true for success, false for failure |
| 175 | */ |
| 176 | private static boolean isDateValid(String dateToValidate) { |
| 177 | |
| 178 | if (dateToValidate == null) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| 183 | sdf.setLenient(false); |
| 184 | |
| 185 | try { |
| 186 | //if not valid, it will throw ParseException |
| 187 | Date date = sdf.parse(dateToValidate); |
| 188 | System.out.println(date); |
| 189 | } catch (ParseException e) { |
| 190 | return false; |
| 191 | } |
| 192 | return true; |
| 193 | } |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 194 | } |