Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +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 | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 20 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 21 | import org.onosproject.yangutils.parser.Parsable; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 25 | |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne; |
Gaurav Agrawal | 0cb696d | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 37 | import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA; |
| 38 | import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA; |
| 39 | import static org.onosproject.yangutils.utils.YangConstructType.LEAF_LIST_DATA; |
| 40 | import static org.onosproject.yangutils.utils.YangConstructType.MAX_ELEMENT_DATA; |
| 41 | import static org.onosproject.yangutils.utils.YangConstructType.MIN_ELEMENT_DATA; |
| 42 | import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA; |
| 43 | import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA; |
| 44 | import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA; |
| 45 | import static org.onosproject.yangutils.utils.YangConstructType.UNITS_DATA; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 49 | * |
| 50 | * ABNF grammar as per RFC6020 |
| 51 | * leaf-list-stmt = leaf-list-keyword sep identifier-arg-str optsep |
| 52 | * "{" stmtsep |
| 53 | * ;; these stmts can appear in any order |
| 54 | * [when-stmt stmtsep] |
| 55 | * *(if-feature-stmt stmtsep) |
| 56 | * type-stmt stmtsep |
| 57 | * [units-stmt stmtsep] |
| 58 | * *(must-stmt stmtsep) |
| 59 | * [config-stmt stmtsep] |
| 60 | * [min-elements-stmt stmtsep] |
| 61 | * [max-elements-stmt stmtsep] |
| 62 | * [ordered-by-stmt stmtsep] |
| 63 | * [status-stmt stmtsep] |
| 64 | * [description-stmt stmtsep] |
| 65 | * [reference-stmt stmtsep] |
| 66 | * "}" |
| 67 | * |
| 68 | * ANTLR grammar rule |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 69 | * leafListStatement : LEAF_LIST_KEYWORD identifier LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 70 | * typeStatement | unitsStatement | mustStatement | configStatement | minElementsStatement | maxElementsStatement | |
| 71 | * orderedByStatement | statusStatement | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE; |
| 72 | */ |
| 73 | |
| 74 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 75 | * Represents listener based call back function corresponding to the "leaf-list" |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 76 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 77 | */ |
| 78 | public final class LeafListListener { |
| 79 | |
| 80 | /** |
| 81 | * Creates a new leaf list listener. |
| 82 | */ |
| 83 | private LeafListListener() { |
| 84 | } |
| 85 | |
| 86 | /** |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 87 | * It is called when parser receives an input matching the grammar rule |
| 88 | * (leaf-list), performs validation and updates the data model tree. |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 89 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 90 | * @param listener listener's object |
| 91 | * @param ctx context object of the grammar rule |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 92 | */ |
| 93 | public static void processLeafListEntry(TreeWalkListener listener, |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 94 | GeneratedYangParser.LeafListStatementContext ctx) { |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 95 | |
| 96 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 97 | checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_LIST_DATA, ctx.identifier().getText(), ENTRY); |
| 98 | |
| 99 | String identifier = getValidIdentifier(ctx.identifier().getText(), LEAF_LIST_DATA, ctx); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 100 | |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 101 | // Validate sub statement cardinality. |
| 102 | validateSubStatementsCardinality(ctx); |
| 103 | |
| 104 | // Check for identifier collision |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 105 | int line = ctx.getStart().getLine(); |
| 106 | int charPositionInLine = ctx.getStart().getCharPositionInLine(); |
| 107 | detectCollidingChildUtil(listener, line, charPositionInLine, identifier, LEAF_LIST_DATA); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 108 | |
| 109 | YangLeafList leafList = new YangLeafList(); |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 110 | leafList.setLeafName(identifier); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 111 | |
| 112 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 113 | YangLeavesHolder leaves; |
| 114 | |
| 115 | if (tmpData instanceof YangLeavesHolder) { |
| 116 | leaves = (YangLeavesHolder) tmpData; |
| 117 | leaves.addLeafList(leafList); |
| 118 | } else { |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 119 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAF_LIST_DATA, |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 120 | ctx.identifier().getText(), ENTRY)); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 121 | } |
| 122 | listener.getParsedDataStack().push(leafList); |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
| 126 | * It is called when parser exits from grammar rule (leaf-list), it performs |
| 127 | * validation and updates the data model tree. |
| 128 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 129 | * @param listener listener's object |
| 130 | * @param ctx context object of the grammar rule |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 131 | */ |
| 132 | public static void processLeafListExit(TreeWalkListener listener, |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 133 | GeneratedYangParser.LeafListStatementContext ctx) { |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 134 | |
| 135 | // Check for stack to be non empty. |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 136 | checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_LIST_DATA, ctx.identifier().getText(), EXIT); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 137 | |
| 138 | if (listener.getParsedDataStack().peek() instanceof YangLeafList) { |
| 139 | listener.getParsedDataStack().pop(); |
| 140 | } else { |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 141 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LEAF_LIST_DATA, |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 142 | ctx.identifier().getText(), EXIT)); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Validates the cardinality of leaf-list sub-statements as per grammar. |
| 148 | * |
Vidyashree Rama | d322132 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 149 | * @param ctx context object of the grammar rule |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 150 | */ |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 151 | private static void validateSubStatementsCardinality(GeneratedYangParser.LeafListStatementContext ctx) { |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 152 | |
Gaurav Agrawal | 0cb696d | 2016-03-11 00:30:12 +0530 | [diff] [blame] | 153 | validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_LIST_DATA, ctx.identifier().getText(), ctx); |
| 154 | validateCardinalityMaxOne(ctx.unitsStatement(), UNITS_DATA, LEAF_LIST_DATA, ctx.identifier().getText()); |
| 155 | validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, LEAF_LIST_DATA, ctx.identifier().getText()); |
| 156 | validateCardinalityMaxOne(ctx.maxElementsStatement(), MAX_ELEMENT_DATA, LEAF_LIST_DATA, |
| 157 | ctx.identifier().getText()); |
| 158 | validateCardinalityMaxOne(ctx.minElementsStatement(), MIN_ELEMENT_DATA, LEAF_LIST_DATA, |
| 159 | ctx.identifier().getText()); |
| 160 | validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_LIST_DATA, |
| 161 | ctx.identifier().getText()); |
| 162 | validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, LEAF_LIST_DATA, ctx.identifier().getText()); |
| 163 | validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, LEAF_LIST_DATA, ctx.identifier().getText()); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 164 | //TODO ordered by |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 165 | } |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 166 | } |