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