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