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