blob: b21bd715113e2aad52c9744966bc5dff4463c05f [file] [log] [blame]
Vidyashree Rama92fc5562016-02-12 18:44:12 +05301/*
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
17package org.onosproject.yangutils.parser.impl.listeners;
18
Gaurav Agrawalbd804472016-03-25 11:25:36 +053019import org.onosproject.yangutils.datamodel.YangCase;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053020import org.onosproject.yangutils.datamodel.YangContainer;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053021import org.onosproject.yangutils.datamodel.YangList;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053022import org.onosproject.yangutils.datamodel.YangModule;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053023import org.onosproject.yangutils.datamodel.YangNode;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053024import org.onosproject.yangutils.datamodel.YangNotification;
25import org.onosproject.yangutils.datamodel.YangInput;
26import org.onosproject.yangutils.datamodel.YangOutput;
27import org.onosproject.yangutils.datamodel.YangAugment;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053028import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
29import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053030import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053031import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053032import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053033import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
34
Vinod Kumar S38046502016-03-23 15:30:27 +053035import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
36import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangListNode;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053042import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053043import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
44import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053045import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Vinod Kumar S38046502016-03-23 15:30:27 +053046import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053047import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal78f72402016-03-11 00:30:12 +053048import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
49import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityNonZero;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053050import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
51import static org.onosproject.yangutils.utils.YangConstructType.DATA_DEF_DATA;
52import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
53import static org.onosproject.yangutils.utils.YangConstructType.KEY_DATA;
54import static org.onosproject.yangutils.utils.YangConstructType.LIST_DATA;
55import static org.onosproject.yangutils.utils.YangConstructType.MAX_ELEMENT_DATA;
56import static org.onosproject.yangutils.utils.YangConstructType.MIN_ELEMENT_DATA;
57import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
58import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053059
Vidyashree Rama92fc5562016-02-12 18:44:12 +053060/*
61 * Reference: RFC6020 and YANG ANTLR Grammar
62 *
63 * ABNF grammar as per RFC6020
64 * list-stmt = list-keyword sep identifier-arg-str optsep
65 * "{" stmtsep
66 * ;; these stmts can appear in any order
67 * [when-stmt stmtsep]
68 * *(if-feature-stmt stmtsep)
69 * *(must-stmt stmtsep)
70 * [key-stmt stmtsep]
71 * *(unique-stmt stmtsep)
72 * [config-stmt stmtsep]
73 * [min-elements-stmt stmtsep]
74 * [max-elements-stmt stmtsep]
75 * [ordered-by-stmt stmtsep]
76 * [status-stmt stmtsep]
77 * [description-stmt stmtsep]
78 * [reference-stmt stmtsep]
79 * *((typedef-stmt /
80 * grouping-stmt) stmtsep)
81 * 1*(data-def-stmt stmtsep)
82 * "}"
83 *
84 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053085 * listStatement : LIST_KEYWORD identifier LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
Vidyashree Rama92fc5562016-02-12 18:44:12 +053086 * keyStatement | uniqueStatement | configStatement | minElementsStatement | maxElementsStatement |
87 * orderedByStatement | statusStatement | descriptionStatement | referenceStatement | typedefStatement |
88 * groupingStatement| dataDefStatement)* RIGHT_CURLY_BRACE;
89 */
90
91/**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053092 * Implements listener based call back function corresponding to the "list" rule
93 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053094 */
95public final class ListListener {
96
97 /**
98 * Creates a new list listener.
99 */
100 private ListListener() {
101 }
102
103 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530104 * It is called when parser receives an input matching the grammar rule
105 * (list), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530106 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530107 * @param listener listener's object
108 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530109 */
110 public static void processListEntry(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530111 GeneratedYangParser.ListStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530112
113 YangNode curNode;
114
Vidyashree Rama468f8282016-03-04 19:08:35 +0530115 checkStackIsNotEmpty(listener, MISSING_HOLDER, LIST_DATA, ctx.identifier().getText(), ENTRY);
116
117 String identifier = getValidIdentifier(ctx.identifier().getText(), LIST_DATA, ctx);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530118
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530119 // Validate sub statement cardinality.
120 validateSubStatementsCardinality(ctx);
121
122 // Check for identifier collision
Vidyashree Rama468f8282016-03-04 19:08:35 +0530123 int line = ctx.getStart().getLine();
124 int charPositionInLine = ctx.getStart().getCharPositionInLine();
125 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, LIST_DATA);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530126
Vinod Kumar S38046502016-03-23 15:30:27 +0530127 YangList yangList = getYangListNode(JAVA_GENERATION);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530128 yangList.setName(identifier);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530129
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530130 /*
131 * If "config" is not specified, the default is the same as the parent
132 * schema node's "config" value.
133 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530134 if (ctx.configStatement().isEmpty()) {
135 boolean parentConfig = ListenerValidation.getParentNodeConfig(listener);
136 yangList.setConfig(parentConfig);
137 }
138
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530139 Parsable curData = listener.getParsedDataStack().peek();
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530140 if (curData instanceof YangModule || curData instanceof YangContainer
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530141 || curData instanceof YangList || curData instanceof YangCase
142 || curData instanceof YangNotification || curData instanceof YangInput
143 || curData instanceof YangOutput || curData instanceof YangAugment) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530144 curNode = (YangNode) curData;
145 try {
146 curNode.addChild(yangList);
147 } catch (DataModelException e) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530148 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530149 LIST_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530150 }
151 listener.getParsedDataStack().push(yangList);
152 } else {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530153 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LIST_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530154 ctx.identifier().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530155 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530156 }
157
158 /**
159 * It is called when parser exits from grammar rule (list), it performs
160 * validation and updates the data model tree.
161 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530162 * @param listener listener's object
163 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530164 */
165 public static void processListExit(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530166 GeneratedYangParser.ListStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530167
Vidyashree Rama468f8282016-03-04 19:08:35 +0530168 checkStackIsNotEmpty(listener, MISSING_HOLDER, LIST_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530169
170 if (listener.getParsedDataStack().peek() instanceof YangList) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530171 YangList yangList = (YangList) listener.getParsedDataStack().peek();
172 try {
173 yangList.validateDataOnExit();
174 } catch (DataModelException e) {
175 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530176 LIST_DATA, ctx.identifier().getText(), EXIT, e.getMessage()));
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530177 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530178 listener.getParsedDataStack().pop();
179 } else {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530180 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LIST_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530181 ctx.identifier().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530182 }
183 }
184
185 /**
186 * Validates the cardinality of list sub-statements as per grammar.
187 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530188 * @param ctx context object of the grammar rule
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530189 */
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530190 private static void validateSubStatementsCardinality(GeneratedYangParser.ListStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530191
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530192 validateCardinalityMaxOne(ctx.keyStatement(), KEY_DATA, LIST_DATA, ctx.identifier().getText());
193 validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, LIST_DATA, ctx.identifier().getText());
194 validateCardinalityMaxOne(ctx.maxElementsStatement(), MAX_ELEMENT_DATA, LIST_DATA, ctx.identifier().getText());
195 validateCardinalityMaxOne(ctx.minElementsStatement(), MIN_ELEMENT_DATA, LIST_DATA, ctx.identifier().getText());
196 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, LIST_DATA, ctx.identifier().getText());
197 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, LIST_DATA, ctx.identifier().getText());
198 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, LIST_DATA, ctx.identifier().getText());
199 validateCardinalityNonZero(ctx.dataDefStatement(), DATA_DEF_DATA, LIST_DATA, ctx.identifier().getText(), ctx);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530200 //TODO when, typedef, grouping, unique
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530201 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530202}