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