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 | |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangModule; |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangRevision; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
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; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 26 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
| 28 | import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangModuleNode; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.setCurrentDateForRevision; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty; |
| 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.utils.YangConstructType.MODULE_DATA; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 43 | * |
| 44 | * ABNF grammar as per RFC6020 |
| 45 | * module-stmt = optsep module-keyword sep identifier-arg-str |
| 46 | * optsep |
| 47 | * "{" stmtsep |
| 48 | * module-header-stmts |
| 49 | * linkage-stmts |
| 50 | * meta-stmts |
| 51 | * revision-stmts |
| 52 | * body-stmts |
| 53 | * "}" optsep |
| 54 | * |
| 55 | * ANTLR grammar rule |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 56 | * module_stmt : MODULE_KEYWORD identifier LEFT_CURLY_BRACE module_body* RIGHT_CURLY_BRACE; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 57 | */ |
| 58 | |
| 59 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 60 | * Represents listener based call back function corresponding to the "module" |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 61 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 62 | */ |
| 63 | public final class ModuleListener { |
| 64 | |
| 65 | /** |
| 66 | * Creates a new module listener. |
| 67 | */ |
| 68 | private ModuleListener() { |
| 69 | } |
| 70 | |
| 71 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 72 | * It is called when parser receives an input matching the grammar rule |
| 73 | * (module), perform validations and update the data model tree. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 74 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 75 | * @param listener Listener's object |
| 76 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 77 | */ |
| 78 | public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 79 | |
| 80 | // Check if stack is empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 81 | checkStackIsEmpty(listener, INVALID_HOLDER, MODULE_DATA, ctx.identifier().getText(), ENTRY); |
| 82 | |
| 83 | String identifier = getValidIdentifier(ctx.identifier().getText(), MODULE_DATA, ctx); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 84 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 85 | YangModule yangModule = getYangModuleNode(JAVA_GENERATION); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 86 | yangModule.setName(identifier); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 87 | |
Vidyashree Rama | 07021a2 | 2016-03-09 20:41:44 +0530 | [diff] [blame] | 88 | if (ctx.moduleBody().moduleHeaderStatement().yangVersionStatement() == null) { |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 89 | yangModule.setVersion((byte) 1); |
| 90 | } |
| 91 | |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame] | 92 | if (ctx.moduleBody().revisionStatements().revisionStatement().isEmpty()) { |
| 93 | String currentDate = setCurrentDateForRevision(); |
| 94 | YangRevision currentRevision = new YangRevision(); |
| 95 | currentRevision.setRevDate(currentDate); |
| 96 | yangModule.setRevision(currentRevision); |
| 97 | } |
| 98 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 99 | listener.getParsedDataStack().push(yangModule); |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * It is called when parser exits from grammar rule (module), it perform |
| 104 | * validations and update the data model tree. |
| 105 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 106 | * @param listener Listener's object |
| 107 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 108 | */ |
| 109 | public static void processModuleExit(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 110 | |
| 111 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 112 | checkStackIsNotEmpty(listener, MISSING_HOLDER, MODULE_DATA, ctx.identifier().getText(), EXIT); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 113 | |
| 114 | if (!(listener.getParsedDataStack().peek() instanceof YangModule)) { |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 115 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, MODULE_DATA, |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 116 | ctx.identifier().getText(), EXIT)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 117 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 118 | try { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 119 | ((YangReferenceResolver) listener.getParsedDataStack().peek()).resolveSelfFileLinking(); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 120 | } catch (DataModelException e) { |
| 121 | ParserException parserException = new ParserException(e.getMessage()); |
| 122 | parserException.setLine(e.getLineNumber()); |
| 123 | parserException.setCharPosition(e.getCharPositionInLine()); |
| 124 | throw parserException; |
| 125 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 126 | } |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 127 | } |