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