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.YangImport; |
| 20 | import org.onosproject.yangutils.datamodel.YangInclude; |
| 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; |
| 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.isDateValid; |
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; |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.utils.YangConstructType.REVISION_DATE_DATA; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 37 | * |
| 38 | * ABNF grammar as per RFC6020 |
| 39 | * import-stmt = import-keyword sep identifier-arg-str optsep |
| 40 | * "{" stmtsep |
| 41 | * prefix-stmt stmtsep |
| 42 | * [revision-date-stmt stmtsep] |
| 43 | * "}" |
| 44 | * include-stmt = include-keyword sep identifier-arg-str optsep |
| 45 | * (";" / |
| 46 | * "{" stmtsep |
| 47 | * [revision-date-stmt stmtsep] |
| 48 | * "}") |
| 49 | * revision-date-stmt = revision-date-keyword sep revision-date stmtend |
| 50 | * |
| 51 | * ANTLR grammar rule |
| 52 | * import_stmt : IMPORT_KEYWORD IDENTIFIER LEFT_CURLY_BRACE import_stmt_body |
| 53 | * RIGHT_CURLY_BRACE; |
| 54 | * import_stmt_body : prefix_stmt revision_date_stmt?; |
| 55 | * |
| 56 | * include_stmt : INCLUDE_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE |
| 57 | * revision_date_stmt_body? RIGHT_CURLY_BRACE); |
| 58 | * |
| 59 | * revision_date_stmt : REVISION_DATE_KEYWORD DATE_ARG STMTEND; |
| 60 | * |
| 61 | */ |
| 62 | |
| 63 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 64 | * Implements listener based call back function corresponding to the |
| 65 | * "revision date" rule defined in ANTLR grammar file for corresponding ABNF |
| 66 | * rule in RFC 6020. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 67 | */ |
| 68 | public final class RevisionDateListener { |
| 69 | |
| 70 | /** |
| 71 | * Creates a new revision date listener. |
| 72 | */ |
| 73 | private RevisionDateListener() { |
| 74 | } |
| 75 | |
| 76 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 77 | * It is called when parser receives an input matching the grammar rule |
| 78 | * (revision date),perform validations and update the data model tree. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 79 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 80 | * @param listener Listener's object |
| 81 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 82 | */ |
| 83 | public static void processRevisionDateEntry(TreeWalkListener listener, |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 84 | GeneratedYangParser.RevisionDateStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 85 | |
| 86 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 87 | checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATE_DATA, ctx.dateArgumentString().getText(), |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 88 | ENTRY); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 89 | |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 90 | String date = removeQuotesAndHandleConcat(ctx.dateArgumentString().getText()); |
| 91 | if (!isDateValid(date)) { |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 92 | ParserException parserException = new ParserException("YANG file error: Input date is not correct"); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 93 | parserException.setLine(ctx.getStart().getLine()); |
| 94 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 95 | throw parserException; |
| 96 | } |
| 97 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 98 | // Obtain the node of the stack. |
| 99 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 100 | switch (tmpNode.getYangConstructType()) { |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 101 | case IMPORT_DATA: { |
| 102 | YangImport importNode = (YangImport) tmpNode; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 103 | importNode.setRevision(date); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 104 | break; |
| 105 | } |
| 106 | case INCLUDE_DATA: { |
| 107 | YangInclude includeNode = (YangInclude) tmpNode; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 108 | includeNode.setRevision(date); |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 109 | break; |
| 110 | } |
| 111 | default: |
| 112 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATE_DATA, |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 113 | ctx.dateArgumentString().getText(), ENTRY)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 114 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 115 | } |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 116 | } |