Brian O'Connor | 75218c2 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | */ |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 16 | package org.onosproject.yangutils.parser.impl.listeners; |
| 17 | |
| 18 | import org.onosproject.yangutils.datamodel.YangAugment; |
| 19 | import org.onosproject.yangutils.datamodel.YangCase; |
| 20 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 21 | import org.onosproject.yangutils.datamodel.YangGrouping; |
| 22 | import org.onosproject.yangutils.datamodel.YangInput; |
| 23 | import org.onosproject.yangutils.datamodel.YangList; |
| 24 | import org.onosproject.yangutils.datamodel.YangModule; |
| 25 | import org.onosproject.yangutils.datamodel.YangNode; |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangNodeIdentifier; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangNotification; |
| 28 | import org.onosproject.yangutils.datamodel.YangOutput; |
| 29 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 30 | import org.onosproject.yangutils.datamodel.YangUses; |
| 31 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.linker.impl.YangResolutionInfoImpl; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 35 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 36 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 37 | |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 39 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 40 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.DESCRIPTION_DATA; |
| 41 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.REFERENCE_DATA; |
| 42 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.STATUS_DATA; |
| 43 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.USES_DATA; |
| 44 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.WHEN_DATA; |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 45 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 46 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 47 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 48 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 49 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 50 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 51 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 52 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 53 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidNodeIdentifier; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 54 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 55 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 56 | import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangUsesNode; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 60 | * |
| 61 | * ABNF grammar as per RFC6020 |
| 62 | * data-def-stmt = container-stmt / |
| 63 | * leaf-stmt / |
| 64 | * leaf-list-stmt / |
| 65 | * list-stmt / |
| 66 | * choice-stmt / |
| 67 | * anyxml-stmt / |
| 68 | * uses-stmt |
| 69 | * |
| 70 | * uses-stmt = uses-keyword sep identifier-ref-arg-str optsep |
| 71 | * (";" / |
| 72 | * "{" stmtsep |
| 73 | * ;; these stmts can appear in any order |
| 74 | * [when-stmt stmtsep] |
| 75 | * *(if-feature-stmt stmtsep) |
| 76 | * [status-stmt stmtsep] |
| 77 | * [description-stmt stmtsep] |
| 78 | * [reference-stmt stmtsep] |
| 79 | * *(refine-stmt stmtsep) |
| 80 | * *(uses-augment-stmt stmtsep) |
| 81 | * "}") |
| 82 | * |
| 83 | * ANTLR grammar rule |
| 84 | * dataDefStatement : containerStatement |
| 85 | * | leafStatement |
| 86 | * | leafListStatement |
| 87 | * | listStatement |
| 88 | * | choiceStatement |
| 89 | * | usesStatement; |
| 90 | * |
| 91 | * usesStatement : USES_KEYWORD string (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement |
| 92 | * | statusStatement | descriptionStatement | referenceStatement | refineStatement |
| 93 | * | usesAugmentStatement)* RIGHT_CURLY_BRACE); |
| 94 | */ |
| 95 | |
| 96 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 97 | * Represents listener based call back function corresponding to the "uses" |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 98 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 99 | */ |
| 100 | public final class UsesListener { |
| 101 | |
| 102 | /** |
| 103 | * Creates a new uses listener. |
| 104 | */ |
| 105 | private UsesListener() { |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * It is called when parser enters grammar rule (uses), it perform |
| 110 | * validations and updates the data model tree. |
| 111 | * |
| 112 | * @param listener listener's object |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 113 | * @param ctx context object of the grammar rule |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 114 | */ |
| 115 | public static void processUsesEntry(TreeWalkListener listener, GeneratedYangParser.UsesStatementContext ctx) { |
| 116 | |
| 117 | // Check for stack to be non empty. |
| 118 | checkStackIsNotEmpty(listener, MISSING_HOLDER, USES_DATA, ctx.string().getText(), ENTRY); |
| 119 | |
| 120 | // Validate sub statement cardinality. |
| 121 | validateSubStatementsCardinality(ctx); |
| 122 | |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 123 | // Check for identifier collision |
| 124 | int line = ctx.getStart().getLine(); |
| 125 | int charPositionInLine = ctx.getStart().getCharPositionInLine(); |
| 126 | |
| 127 | detectCollidingChildUtil(listener, line, charPositionInLine, ctx.string().getText(), USES_DATA); |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 128 | Parsable curData = listener.getParsedDataStack().peek(); |
| 129 | |
| 130 | if (curData instanceof YangModule || curData instanceof YangSubModule |
| 131 | || curData instanceof YangContainer || curData instanceof YangList |
| 132 | || curData instanceof YangUses || curData instanceof YangAugment |
| 133 | || curData instanceof YangCase || curData instanceof YangGrouping |
| 134 | || curData instanceof YangInput || curData instanceof YangOutput |
| 135 | || curData instanceof YangNotification) { |
| 136 | |
| 137 | YangUses usesNode = getYangUsesNode(JAVA_GENERATION); |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 138 | YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.string().getText(), USES_DATA, ctx); |
| 139 | usesNode.setNodeIdentifier(nodeIdentifier); |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 140 | YangNode curNode = (YangNode) curData; |
| 141 | |
| 142 | try { |
| 143 | curNode.addChild(usesNode); |
| 144 | } catch (DataModelException e) { |
| 145 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 146 | USES_DATA, ctx.string().getText(), ENTRY, e.getMessage())); |
| 147 | } |
| 148 | listener.getParsedDataStack().push(usesNode); |
| 149 | } else { |
| 150 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, |
| 151 | USES_DATA, ctx.string().getText(), ENTRY)); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * It is called when parser exits from grammar rule (uses), it perform |
| 157 | * validations and update the data model tree. |
| 158 | * |
| 159 | * @param listener Listener's object |
| 160 | * @param ctx context object of the grammar rule |
| 161 | */ |
| 162 | public static void processUsesExit(TreeWalkListener listener, |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 163 | GeneratedYangParser.UsesStatementContext ctx) { |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 164 | |
| 165 | // Check for stack to be non empty. |
| 166 | checkStackIsNotEmpty(listener, MISSING_HOLDER, USES_DATA, ctx.string().getText(), EXIT); |
| 167 | |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 168 | Parsable parsableUses = listener.getParsedDataStack().pop(); |
| 169 | if (!(parsableUses instanceof YangUses)) { |
| 170 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, USES_DATA, |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 171 | ctx.string().getText(), EXIT)); |
| 172 | } |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 173 | YangUses uses = (YangUses) parsableUses; |
| 174 | int errorLine = ctx.getStart().getLine(); |
| 175 | int errorPosition = ctx.getStart().getCharPositionInLine(); |
| 176 | |
| 177 | // Parent YANG node of uses to be added in resolution information. |
| 178 | Parsable parentNode = listener.getParsedDataStack().peek(); |
| 179 | |
| 180 | // Verify parent node of leaf |
| 181 | if (!(parentNode instanceof YangNode)) { |
| 182 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, USES_DATA, |
| 183 | ctx.string().getText(), EXIT)); |
| 184 | } |
| 185 | |
| 186 | // Add resolution information to the list |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 187 | YangResolutionInfoImpl resolutionInfo = new YangResolutionInfoImpl<YangUses>(uses, |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 188 | (YangNode) parentNode, errorLine, |
| 189 | errorPosition); |
| 190 | addToResolutionList(resolutionInfo, ctx); |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // TODO linker to handle collision scenarios like leaf obtained by uses, conflicts with some existing leaf. |
| 194 | |
| 195 | /** |
| 196 | * Validates the cardinality of case sub-statements as per grammar. |
| 197 | * |
| 198 | * @param ctx context object of the grammar rule |
| 199 | */ |
| 200 | private static void validateSubStatementsCardinality(GeneratedYangParser.UsesStatementContext ctx) { |
| 201 | validateCardinalityMaxOne(ctx.whenStatement(), WHEN_DATA, USES_DATA, ctx.string().getText()); |
| 202 | validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, USES_DATA, ctx.string().getText()); |
| 203 | validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, USES_DATA, ctx.string().getText()); |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 204 | validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, USES_DATA, ctx.string().getText()); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Add to resolution list. |
| 209 | * |
| 210 | * @param resolutionInfo resolution information. |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 211 | * @param ctx context object of the grammar rule |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 212 | */ |
Gaurav Agrawal | 58b348e | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 213 | private static void addToResolutionList(YangResolutionInfoImpl<YangUses> resolutionInfo, |
| 214 | GeneratedYangParser.UsesStatementContext ctx) { |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 215 | |
| 216 | try { |
| 217 | addResolutionInfo(resolutionInfo); |
| 218 | } catch (DataModelException e) { |
| 219 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 220 | USES_DATA, ctx.string().getText(), EXIT, e.getMessage())); |
| 221 | } |
| 222 | } |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 223 | } |