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