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