Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +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 | f6ccc97 | 2016-03-25 11:25:36 +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 | |
| 19 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 20 | import org.onosproject.yangutils.datamodel.YangGrouping; |
| 21 | import org.onosproject.yangutils.datamodel.YangInput; |
| 22 | import org.onosproject.yangutils.datamodel.YangList; |
| 23 | import org.onosproject.yangutils.datamodel.YangModule; |
| 24 | import org.onosproject.yangutils.datamodel.YangNode; |
| 25 | import org.onosproject.yangutils.datamodel.YangNotification; |
| 26 | import org.onosproject.yangutils.datamodel.YangOutput; |
| 27 | import org.onosproject.yangutils.datamodel.YangRpc; |
| 28 | import org.onosproject.yangutils.datamodel.YangSubModule; |
| 29 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 30 | import org.onosproject.yangutils.parser.Parsable; |
| 31 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 32 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 33 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 34 | |
| 35 | import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; |
| 36 | import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangGroupingNode; |
| 37 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil; |
| 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 39 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 40 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
| 41 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 42 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 43 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 44 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 45 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
| 46 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; |
| 47 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 48 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 49 | import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA; |
| 50 | import static org.onosproject.yangutils.utils.YangConstructType.GROUPING_DATA; |
| 51 | import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA; |
| 52 | import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA; |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 53 | |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 54 | /* |
| 55 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 56 | * |
| 57 | * ABNF grammar as per RFC6020 |
| 58 | * grouping-stmt = grouping-keyword sep identifier-arg-str optsep |
| 59 | * (";" / |
| 60 | * "{" stmtsep |
| 61 | * ;; these stmts can appear in any order |
| 62 | * [status-stmt stmtsep] |
| 63 | * [description-stmt stmtsep] |
| 64 | * [reference-stmt stmtsep] |
| 65 | * *((typedef-stmt / |
| 66 | * grouping-stmt) stmtsep) |
| 67 | * *(data-def-stmt stmtsep) |
| 68 | * "}") |
| 69 | * |
| 70 | * ANTLR grammar rule |
| 71 | * groupingStatement : GROUPING_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE |
| 72 | * (statusStatement | descriptionStatement | referenceStatement | typedefStatement | groupingStatement |
| 73 | * | dataDefStatement)* RIGHT_CURLY_BRACE); |
| 74 | */ |
| 75 | |
| 76 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 77 | * Represents listener based call back function corresponding to the "grouping" |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 78 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 79 | */ |
| 80 | public final class GroupingListener { |
| 81 | |
| 82 | /** |
| 83 | * Creates a new grouping listener. |
| 84 | */ |
| 85 | private GroupingListener() { |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * It is called when parser enters grammar rule (grouping), it perform |
| 90 | * validations and updates the data model tree. |
| 91 | * |
| 92 | * @param listener listener's object |
| 93 | * @param ctx context object of the grammar rule |
| 94 | */ |
| 95 | public static void processGroupingEntry(TreeWalkListener listener, |
| 96 | GeneratedYangParser.GroupingStatementContext ctx) { |
| 97 | |
| 98 | // Check for stack to be non empty. |
| 99 | checkStackIsNotEmpty(listener, MISSING_HOLDER, GROUPING_DATA, ctx.identifier().getText(), ENTRY); |
| 100 | |
| 101 | // Check validity of identifier and remove double quotes. |
| 102 | String identifier = getValidIdentifier(ctx.identifier().getText(), GROUPING_DATA, ctx); |
| 103 | |
| 104 | // Validate sub statement cardinality. |
| 105 | validateSubStatementsCardinality(ctx); |
| 106 | |
| 107 | Parsable curData = listener.getParsedDataStack().peek(); |
| 108 | |
| 109 | // Check for identifier collision |
| 110 | int line = ctx.getStart().getLine(); |
| 111 | int charPositionInLine = ctx.getStart().getCharPositionInLine(); |
| 112 | detectCollidingChildUtil(listener, line, charPositionInLine, identifier, GROUPING_DATA); |
| 113 | |
| 114 | if (curData instanceof YangModule || curData instanceof YangSubModule |
| 115 | || curData instanceof YangContainer || curData instanceof YangNotification |
| 116 | || curData instanceof YangList || curData instanceof YangGrouping |
| 117 | || curData instanceof YangRpc || curData instanceof YangInput |
| 118 | || curData instanceof YangOutput) { |
| 119 | |
| 120 | YangGrouping groupingNode = getYangGroupingNode(JAVA_GENERATION); |
| 121 | groupingNode.setName(identifier); |
| 122 | |
| 123 | YangNode curNode = (YangNode) curData; |
| 124 | try { |
| 125 | curNode.addChild(groupingNode); |
| 126 | } catch (DataModelException e) { |
| 127 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 128 | GROUPING_DATA, ctx.identifier().getText(), ENTRY, e.getMessage())); |
| 129 | } |
| 130 | listener.getParsedDataStack().push(groupingNode); |
| 131 | } else { |
| 132 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, |
| 133 | GROUPING_DATA, ctx.identifier().getText(), ENTRY)); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * It is called when parser exits from grammar rule (grouping), it perform |
| 139 | * validations and update the data model tree. |
| 140 | * |
| 141 | * @param listener Listener's object |
| 142 | * @param ctx context object of the grammar rule |
| 143 | */ |
| 144 | public static void processGroupingExit(TreeWalkListener listener, |
| 145 | GeneratedYangParser.GroupingStatementContext ctx) { |
| 146 | |
| 147 | // Check for stack to be non empty. |
| 148 | checkStackIsNotEmpty(listener, MISSING_HOLDER, GROUPING_DATA, ctx.identifier().getText(), EXIT); |
| 149 | |
| 150 | if (listener.getParsedDataStack().peek() instanceof YangGrouping) { |
| 151 | listener.getParsedDataStack().pop(); |
| 152 | } else { |
| 153 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, GROUPING_DATA, |
| 154 | ctx.identifier().getText(), EXIT)); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Validates the cardinality of case sub-statements as per grammar. |
| 160 | * |
| 161 | * @param ctx context object of the grammar rule |
| 162 | */ |
| 163 | private static void validateSubStatementsCardinality(GeneratedYangParser.GroupingStatementContext ctx) { |
| 164 | |
| 165 | validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, GROUPING_DATA, ctx.identifier().getText()); |
| 166 | validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, GROUPING_DATA, |
| 167 | ctx.identifier().getText()); |
| 168 | validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, GROUPING_DATA, ctx.identifier().getText()); |
Gaurav Agrawal | f6ccc97 | 2016-03-25 11:25:36 +0530 | [diff] [blame] | 169 | } |
| 170 | } |