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; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 18 | |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangContainer; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangList; |
| 21 | import org.onosproject.yangutils.datamodel.YangModule; |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangNode; |
| 23 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 24 | import org.onosproject.yangutils.parser.Parsable; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 36 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation; |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 38 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | 8e8770a | 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.utils.YangConstructType.CONFIG_DATA; |
| 41 | import static org.onosproject.yangutils.utils.YangConstructType.CONTAINER_DATA; |
| 42 | import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA; |
| 43 | import static org.onosproject.yangutils.utils.YangConstructType.PRESENCE_DATA; |
| 44 | import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA; |
| 45 | import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA; |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 49 | * |
| 50 | * ABNF grammar as per RFC6020 |
| 51 | * container-stmt = container-keyword sep identifier-arg-str optsep |
| 52 | * (";" / |
| 53 | * "{" stmtsep |
| 54 | * ;; these stmts can appear in any order |
| 55 | * [when-stmt stmtsep] |
| 56 | * *(if-feature-stmt stmtsep) |
| 57 | * *(must-stmt stmtsep) |
| 58 | * [presence-stmt stmtsep] |
| 59 | * [config-stmt stmtsep] |
| 60 | * [status-stmt stmtsep] |
| 61 | * [description-stmt stmtsep] |
| 62 | * [reference-stmt stmtsep] |
| 63 | * *((typedef-stmt / |
| 64 | * grouping-stmt) stmtsep) |
| 65 | * *(data-def-stmt stmtsep) |
| 66 | * "}") |
| 67 | * |
| 68 | * ANTLR grammar rule |
| 69 | * containerStatement : CONTAINER_KEYWORD IDENTIFIER |
| 70 | * (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement | |
| 71 | * presenceStatement | configStatement | statusStatement | descriptionStatement | |
| 72 | * referenceStatement | typedefStatement | groupingStatement |
| 73 | * | dataDefStatement)* RIGHT_CURLY_BRACE); |
| 74 | */ |
| 75 | |
| 76 | /** |
| 77 | * Implements listener based call back function corresponding to the "container" |
| 78 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 79 | */ |
| 80 | public final class ContainerListener { |
| 81 | |
| 82 | /** |
| 83 | * Creates a new container listener. |
| 84 | */ |
| 85 | private ContainerListener() { |
| 86 | } |
| 87 | |
| 88 | /** |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 89 | * It is called when parser receives an input matching the grammar rule |
| 90 | * (container), performs validation and updates the data model tree. |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 91 | * |
Gaurav Agrawal | 8e8770a | 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 | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 94 | */ |
| 95 | public static void processContainerEntry(TreeWalkListener listener, |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 96 | GeneratedYangParser.ContainerStatementContext ctx) { |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 97 | |
| 98 | // Check for stack to be non empty. |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 99 | checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.IDENTIFIER().getText(), ENTRY); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 100 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 101 | // Validate sub statement cardinality. |
| 102 | validateSubStatementsCardinality(ctx); |
| 103 | |
| 104 | // Check for identifier collision |
| 105 | int line = ctx.IDENTIFIER().getSymbol().getLine(); |
| 106 | int charPositionInLine = ctx.IDENTIFIER().getSymbol().getCharPositionInLine(); |
| 107 | String identifierName = ctx.IDENTIFIER().getText(); |
| 108 | detectCollidingChildUtil(listener, line, charPositionInLine, identifierName, CONTAINER_DATA); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 109 | |
| 110 | YangContainer container = new YangContainer(); |
| 111 | container.setName(ctx.IDENTIFIER().getText()); |
| 112 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 113 | /* |
| 114 | * If "config" is not specified, the default is the same as the parent |
| 115 | * schema node's "config" value. |
| 116 | */ |
Vidyashree Rama | f4c617c | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 117 | if (ctx.configStatement().isEmpty()) { |
| 118 | boolean parentConfig = ListenerValidation.getParentNodeConfig(listener); |
| 119 | container.setConfig(parentConfig); |
| 120 | } |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 121 | |
Vidyashree Rama | f4c617c | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 122 | Parsable curData = listener.getParsedDataStack().peek(); |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 123 | if ((curData instanceof YangModule) || (curData instanceof YangContainer) |
| 124 | || (curData instanceof YangList)) { |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 125 | YangNode curNode = (YangNode) curData; |
| 126 | try { |
| 127 | curNode.addChild(container); |
| 128 | } catch (DataModelException e) { |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 129 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 130 | CONTAINER_DATA, ctx.IDENTIFIER().getText(), ENTRY, e.getMessage())); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 131 | } |
| 132 | listener.getParsedDataStack().push(container); |
| 133 | } else { |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 134 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONTAINER_DATA, |
| 135 | ctx.IDENTIFIER().getText(), ENTRY)); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 136 | } |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
| 140 | * It is called when parser exits from grammar rule (container), it perform |
| 141 | * validations and updates the data model tree. |
| 142 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 143 | * @param listener listener's object |
| 144 | * @param ctx context object of the grammar rule |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 145 | */ |
| 146 | public static void processContainerExit(TreeWalkListener listener, |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 147 | GeneratedYangParser.ContainerStatementContext ctx) { |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 148 | |
| 149 | // Check for stack to be non empty. |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 150 | checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.IDENTIFIER().getText(), EXIT); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 151 | |
| 152 | if (listener.getParsedDataStack().peek() instanceof YangContainer) { |
Vidyashree Rama | f4c617c | 2016-02-24 12:28:22 +0530 | [diff] [blame] | 153 | YangContainer yangContainer = (YangContainer) listener.getParsedDataStack().peek(); |
| 154 | try { |
| 155 | yangContainer.validateDataOnExit(); |
| 156 | } catch (DataModelException e) { |
| 157 | throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, |
| 158 | CONTAINER_DATA, ctx.IDENTIFIER().getText(), EXIT, e.getMessage())); |
| 159 | } |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 160 | listener.getParsedDataStack().pop(); |
| 161 | } else { |
Vidyashree Rama | 59071f3 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 162 | throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CONTAINER_DATA, |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 163 | ctx.IDENTIFIER().getText(), EXIT)); |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Validates the cardinality of container sub-statements as per grammar. |
| 169 | * |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 170 | * @param ctx context object of the grammar rule |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 171 | */ |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 172 | private static void validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) { |
Vidyashree Rama | 4f1f08b | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 173 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 174 | validateCardinality(ctx.presenceStatement(), PRESENCE_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText()); |
| 175 | validateCardinality(ctx.configStatement(), CONFIG_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText()); |
| 176 | validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText()); |
| 177 | validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText()); |
| 178 | validateCardinality(ctx.statusStatement(), STATUS_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText()); |
| 179 | // TODO when, grouping, typedef. |
Vidyashree Rama | 92fc556 | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 180 | } |
| 181 | } |