Vidyashree Rama | 92fc556 | 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 | 4f1f08b | 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; |
| 22 | import org.onosproject.yangutils.parser.ParsableDataType; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 27 | |
| 28 | import static org.onosproject.yangutils.parser.ParsableDataType.LEAF_LIST_DATA; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CARDINALITY; |
| 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
| 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 40 | * |
| 41 | * ABNF grammar as per RFC6020 |
| 42 | * leaf-list-stmt = leaf-list-keyword sep identifier-arg-str optsep |
| 43 | * "{" stmtsep |
| 44 | * ;; these stmts can appear in any order |
| 45 | * [when-stmt stmtsep] |
| 46 | * *(if-feature-stmt stmtsep) |
| 47 | * type-stmt stmtsep |
| 48 | * [units-stmt stmtsep] |
| 49 | * *(must-stmt stmtsep) |
| 50 | * [config-stmt stmtsep] |
| 51 | * [min-elements-stmt stmtsep] |
| 52 | * [max-elements-stmt stmtsep] |
| 53 | * [ordered-by-stmt stmtsep] |
| 54 | * [status-stmt stmtsep] |
| 55 | * [description-stmt stmtsep] |
| 56 | * [reference-stmt stmtsep] |
| 57 | * "}" |
| 58 | * |
| 59 | * ANTLR grammar rule |
| 60 | * leafListStatement : LEAF_LIST_KEYWORD IDENTIFIER LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | |
| 61 | * typeStatement | unitsStatement | mustStatement | configStatement | minElementsStatement | maxElementsStatement | |
| 62 | * orderedByStatement | statusStatement | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE; |
| 63 | */ |
| 64 | |
| 65 | /** |
| 66 | * Implements listener based call back function corresponding to the "leaf-list" |
| 67 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 68 | */ |
| 69 | public final class LeafListListener { |
| 70 | |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 71 | private static ParsableDataType yangConstruct; |
| 72 | |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 73 | /** |
| 74 | * Creates a new leaf list listener. |
| 75 | */ |
| 76 | private LeafListListener() { |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * It is called when parser receives an input matching the grammar |
| 81 | * rule (leaf-list), performs validation and updates the data model |
| 82 | * tree. |
| 83 | * |
| 84 | * @param listener listener's object. |
| 85 | * @param ctx context object of the grammar rule. |
| 86 | */ |
| 87 | public static void processLeafListEntry(TreeWalkListener listener, |
| 88 | GeneratedYangParser.LeafListStatementContext ctx) { |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 89 | |
| 90 | // Check for stack to be non empty. |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 91 | checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_LIST_DATA, ctx.IDENTIFIER().getText(), ENTRY); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 92 | |
| 93 | boolean result = validateSubStatementsCardinality(ctx); |
| 94 | if (!result) { |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 95 | throw new ParserException(constructListenerErrorMessage(INVALID_CARDINALITY, yangConstruct, "", ENTRY)); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | YangLeafList leafList = new YangLeafList(); |
| 99 | leafList.setLeafName(ctx.IDENTIFIER().getText()); |
| 100 | |
| 101 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 102 | YangLeavesHolder leaves; |
| 103 | |
| 104 | if (tmpData instanceof YangLeavesHolder) { |
| 105 | leaves = (YangLeavesHolder) tmpData; |
| 106 | leaves.addLeafList(leafList); |
| 107 | } else { |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 108 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAF_LIST_DATA, |
| 109 | ctx.IDENTIFIER().getText(), ENTRY)); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 110 | } |
| 111 | listener.getParsedDataStack().push(leafList); |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * It is called when parser exits from grammar rule (leaf-list), it performs |
| 116 | * validation and updates the data model tree. |
| 117 | * |
| 118 | * @param listener listener's object. |
| 119 | * @param ctx context object of the grammar rule. |
| 120 | */ |
| 121 | public static void processLeafListExit(TreeWalkListener listener, |
| 122 | GeneratedYangParser.LeafListStatementContext ctx) { |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 123 | |
| 124 | // Check for stack to be non empty. |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 125 | checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_LIST_DATA, ctx.IDENTIFIER().getText(), EXIT); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 126 | |
| 127 | if (listener.getParsedDataStack().peek() instanceof YangLeafList) { |
| 128 | listener.getParsedDataStack().pop(); |
| 129 | } else { |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 130 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LEAF_LIST_DATA, |
| 131 | ctx.IDENTIFIER().getText(), EXIT)); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Validates the cardinality of leaf-list sub-statements as per grammar. |
| 137 | * |
| 138 | * @param ctx context object of the grammar rule. |
| 139 | * @return true/false validation success or failure. |
| 140 | */ |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 141 | private static boolean validateSubStatementsCardinality(GeneratedYangParser |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 142 | .LeafListStatementContext ctx) { |
| 143 | |
| 144 | if (ctx.typeStatement().isEmpty() |
| 145 | || (ctx.typeStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 146 | yangConstruct = ParsableDataType.TYPE_DATA; |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | if ((!ctx.unitsStatement().isEmpty()) |
| 151 | && (ctx.unitsStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 152 | yangConstruct = ParsableDataType.UNITS_DATA; |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | if ((!ctx.configStatement().isEmpty()) |
| 157 | && (ctx.configStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 158 | yangConstruct = ParsableDataType.CONFIG_DATA; |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | if ((!ctx.maxElementsStatement().isEmpty()) |
| 163 | && (ctx.maxElementsStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 164 | yangConstruct = ParsableDataType.MAX_ELEMENT_DATA; |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | if ((!ctx.minElementsStatement().isEmpty()) |
| 169 | && (ctx.minElementsStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 170 | yangConstruct = ParsableDataType.MIN_ELEMENT_DATA; |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | if ((!ctx.descriptionStatement().isEmpty()) |
| 175 | && (ctx.descriptionStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 176 | yangConstruct = ParsableDataType.DESCRIPTION_DATA; |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | if ((!ctx.referenceStatement().isEmpty()) |
| 181 | && (ctx.referenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 182 | yangConstruct = ParsableDataType.REFERENCE_DATA; |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | if ((!ctx.statusStatement().isEmpty()) |
| 187 | && (ctx.statusStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) { |
| 188 | yangConstruct = ParsableDataType.STATUS_DATA; |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | return true; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 193 | } |
| 194 | } |