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