blob: 42326b7d81e683ada57adf1061f763cc0d57ca20 [file] [log] [blame]
Vidyashree Rama92fc5562016-02-12 18:44:12 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Vidyashree Rama92fc5562016-02-12 18:44:12 +05303 *
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
Bharat saraswald9822e92016-04-05 15:13:44 +053019import org.onosproject.yangutils.datamodel.YangAugment;
Gaurav Agrawalbd804472016-03-25 11:25:36 +053020import org.onosproject.yangutils.datamodel.YangCase;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053021import org.onosproject.yangutils.datamodel.YangContainer;
janani b4e53f9b2016-04-26 18:49:20 +053022import org.onosproject.yangutils.datamodel.YangGrouping;
Bharat saraswald9822e92016-04-05 15:13:44 +053023import org.onosproject.yangutils.datamodel.YangInput;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053024import org.onosproject.yangutils.datamodel.YangList;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053025import org.onosproject.yangutils.datamodel.YangModule;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053026import org.onosproject.yangutils.datamodel.YangNode;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053027import org.onosproject.yangutils.datamodel.YangNotification;
Vidyashree Rama506cbe12016-03-28 11:59:27 +053028import org.onosproject.yangutils.datamodel.YangOutput;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053029import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
30import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053031import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053032import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053033import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053034import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
35
Vinod Kumar S38046502016-03-23 15:30:27 +053036import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
37import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangListNode;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053043import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053044import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
45import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053046import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Vinod Kumar S38046502016-03-23 15:30:27 +053047import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053048import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal78f72402016-03-11 00:30:12 +053049import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
50import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityNonZero;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053051import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
52import static org.onosproject.yangutils.utils.YangConstructType.DATA_DEF_DATA;
53import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
54import static org.onosproject.yangutils.utils.YangConstructType.KEY_DATA;
55import static org.onosproject.yangutils.utils.YangConstructType.LIST_DATA;
56import static org.onosproject.yangutils.utils.YangConstructType.MAX_ELEMENT_DATA;
57import static org.onosproject.yangutils.utils.YangConstructType.MIN_ELEMENT_DATA;
58import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
59import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053060
Vidyashree Rama92fc5562016-02-12 18:44:12 +053061/*
62 * Reference: RFC6020 and YANG ANTLR Grammar
63 *
64 * ABNF grammar as per RFC6020
65 * list-stmt = list-keyword sep identifier-arg-str optsep
66 * "{" stmtsep
67 * ;; these stmts can appear in any order
68 * [when-stmt stmtsep]
69 * *(if-feature-stmt stmtsep)
70 * *(must-stmt stmtsep)
71 * [key-stmt stmtsep]
72 * *(unique-stmt stmtsep)
73 * [config-stmt stmtsep]
74 * [min-elements-stmt stmtsep]
75 * [max-elements-stmt stmtsep]
76 * [ordered-by-stmt stmtsep]
77 * [status-stmt stmtsep]
78 * [description-stmt stmtsep]
79 * [reference-stmt stmtsep]
80 * *((typedef-stmt /
81 * grouping-stmt) stmtsep)
82 * 1*(data-def-stmt stmtsep)
83 * "}"
84 *
85 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053086 * listStatement : LIST_KEYWORD identifier LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
Vidyashree Rama92fc5562016-02-12 18:44:12 +053087 * keyStatement | uniqueStatement | configStatement | minElementsStatement | maxElementsStatement |
88 * orderedByStatement | statusStatement | descriptionStatement | referenceStatement | typedefStatement |
89 * groupingStatement| dataDefStatement)* RIGHT_CURLY_BRACE;
90 */
91
92/**
Bharat saraswald9822e92016-04-05 15:13:44 +053093 * Represents listener based call back function corresponding to the "list" rule
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053094 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053095 */
96public final class ListListener {
97
98 /**
99 * Creates a new list listener.
100 */
101 private ListListener() {
102 }
103
104 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530105 * It is called when parser receives an input matching the grammar rule
106 * (list), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530107 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530108 * @param listener listener's object
109 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530110 */
111 public static void processListEntry(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530112 GeneratedYangParser.ListStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530113
114 YangNode curNode;
115
Vidyashree Rama468f8282016-03-04 19:08:35 +0530116 checkStackIsNotEmpty(listener, MISSING_HOLDER, LIST_DATA, ctx.identifier().getText(), ENTRY);
117
118 String identifier = getValidIdentifier(ctx.identifier().getText(), LIST_DATA, ctx);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530119
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530120 // Validate sub statement cardinality.
121 validateSubStatementsCardinality(ctx);
122
123 // Check for identifier collision
Vidyashree Rama468f8282016-03-04 19:08:35 +0530124 int line = ctx.getStart().getLine();
125 int charPositionInLine = ctx.getStart().getCharPositionInLine();
126 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, LIST_DATA);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530127
Vinod Kumar S38046502016-03-23 15:30:27 +0530128 YangList yangList = getYangListNode(JAVA_GENERATION);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530129 yangList.setName(identifier);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530130
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530131 /*
132 * If "config" is not specified, the default is the same as the parent
133 * schema node's "config" value.
134 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530135 if (ctx.configStatement().isEmpty()) {
136 boolean parentConfig = ListenerValidation.getParentNodeConfig(listener);
137 yangList.setConfig(parentConfig);
138 }
139
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530140 Parsable curData = listener.getParsedDataStack().peek();
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530141 if (curData instanceof YangModule || curData instanceof YangContainer
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530142 || curData instanceof YangList || curData instanceof YangCase
143 || curData instanceof YangNotification || curData instanceof YangInput
janani b4e53f9b2016-04-26 18:49:20 +0530144 || curData instanceof YangOutput || curData instanceof YangAugment || curData instanceof YangGrouping) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530145 curNode = (YangNode) curData;
146 try {
147 curNode.addChild(yangList);
148 } catch (DataModelException e) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530149 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530150 LIST_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530151 }
152 listener.getParsedDataStack().push(yangList);
153 } else {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530154 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LIST_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530155 ctx.identifier().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530156 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530157 }
158
159 /**
160 * It is called when parser exits from grammar rule (list), it performs
161 * validation and updates the data model tree.
162 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530163 * @param listener listener's object
164 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530165 */
166 public static void processListExit(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530167 GeneratedYangParser.ListStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530168
Vidyashree Rama468f8282016-03-04 19:08:35 +0530169 checkStackIsNotEmpty(listener, MISSING_HOLDER, LIST_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530170
171 if (listener.getParsedDataStack().peek() instanceof YangList) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530172 YangList yangList = (YangList) listener.getParsedDataStack().peek();
173 try {
174 yangList.validateDataOnExit();
175 } catch (DataModelException e) {
176 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530177 LIST_DATA, ctx.identifier().getText(), EXIT, e.getMessage()));
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530178 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530179 listener.getParsedDataStack().pop();
180 } else {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530181 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LIST_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530182 ctx.identifier().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530183 }
184 }
185
186 /**
187 * Validates the cardinality of list sub-statements as per grammar.
188 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530189 * @param ctx context object of the grammar rule
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530190 */
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530191 private static void validateSubStatementsCardinality(GeneratedYangParser.ListStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530192
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530193 validateCardinalityMaxOne(ctx.keyStatement(), KEY_DATA, LIST_DATA, ctx.identifier().getText());
194 validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, LIST_DATA, ctx.identifier().getText());
195 validateCardinalityMaxOne(ctx.maxElementsStatement(), MAX_ELEMENT_DATA, LIST_DATA, ctx.identifier().getText());
196 validateCardinalityMaxOne(ctx.minElementsStatement(), MIN_ELEMENT_DATA, LIST_DATA, ctx.identifier().getText());
197 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, LIST_DATA, ctx.identifier().getText());
198 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, LIST_DATA, ctx.identifier().getText());
199 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, LIST_DATA, ctx.identifier().getText());
200 validateCardinalityNonZero(ctx.dataDefStatement(), DATA_DEF_DATA, LIST_DATA, ctx.identifier().getText(), ctx);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530201 //TODO when, typedef, grouping, unique
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530202 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530203}