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