blob: d0e73009c4735ed1ffd1ab10af3870cf2990da25 [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 Rama1db15562016-05-17 16:16:15 +053029import org.onosproject.yangutils.datamodel.YangSubModule;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053030import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053031import org.onosproject.yangutils.datamodel.utils.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053032import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053033import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053034import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053035import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
36
Vinod Kumar S38046502016-03-23 15:30:27 +053037import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
Bharat saraswal96dfef02016-06-16 00:29:12 +053038import static org.onosproject.yangutils.datamodel.utils.YangConstructType.CONFIG_DATA;
39import static org.onosproject.yangutils.datamodel.utils.YangConstructType.DATA_DEF_DATA;
40import static org.onosproject.yangutils.datamodel.utils.YangConstructType.DESCRIPTION_DATA;
41import static org.onosproject.yangutils.datamodel.utils.YangConstructType.KEY_DATA;
42import static org.onosproject.yangutils.datamodel.utils.YangConstructType.LIST_DATA;
43import static org.onosproject.yangutils.datamodel.utils.YangConstructType.MAX_ELEMENT_DATA;
44import static org.onosproject.yangutils.datamodel.utils.YangConstructType.MIN_ELEMENT_DATA;
45import static org.onosproject.yangutils.datamodel.utils.YangConstructType.REFERENCE_DATA;
46import static org.onosproject.yangutils.datamodel.utils.YangConstructType.STATUS_DATA;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053047import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053048import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
49import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
50import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
51import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053052import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053053import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
54import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053055import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Vinod Kumar S38046502016-03-23 15:30:27 +053056import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053057import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal78f72402016-03-11 00:30:12 +053058import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
59import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityNonZero;
Bharat saraswal96dfef02016-06-16 00:29:12 +053060import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangListNode;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053061
Vidyashree Rama92fc5562016-02-12 18:44:12 +053062/*
63 * Reference: RFC6020 and YANG ANTLR Grammar
64 *
65 * ABNF grammar as per RFC6020
66 * list-stmt = list-keyword sep identifier-arg-str optsep
67 * "{" stmtsep
68 * ;; these stmts can appear in any order
69 * [when-stmt stmtsep]
70 * *(if-feature-stmt stmtsep)
71 * *(must-stmt stmtsep)
72 * [key-stmt stmtsep]
73 * *(unique-stmt stmtsep)
74 * [config-stmt stmtsep]
75 * [min-elements-stmt stmtsep]
76 * [max-elements-stmt stmtsep]
77 * [ordered-by-stmt stmtsep]
78 * [status-stmt stmtsep]
79 * [description-stmt stmtsep]
80 * [reference-stmt stmtsep]
81 * *((typedef-stmt /
82 * grouping-stmt) stmtsep)
83 * 1*(data-def-stmt stmtsep)
84 * "}"
85 *
86 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053087 * listStatement : LIST_KEYWORD identifier LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
Vidyashree Rama92fc5562016-02-12 18:44:12 +053088 * keyStatement | uniqueStatement | configStatement | minElementsStatement | maxElementsStatement |
89 * orderedByStatement | statusStatement | descriptionStatement | referenceStatement | typedefStatement |
90 * groupingStatement| dataDefStatement)* RIGHT_CURLY_BRACE;
91 */
92
93/**
Bharat saraswald9822e92016-04-05 15:13:44 +053094 * Represents listener based call back function corresponding to the "list" rule
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053095 * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053096 */
97public final class ListListener {
98
99 /**
100 * Creates a new list listener.
101 */
102 private ListListener() {
103 }
104
105 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530106 * It is called when parser receives an input matching the grammar rule
107 * (list), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530108 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530109 * @param listener listener's object
110 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530111 */
112 public static void processListEntry(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530113 GeneratedYangParser.ListStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530114
115 YangNode curNode;
116
Vidyashree Rama468f8282016-03-04 19:08:35 +0530117 checkStackIsNotEmpty(listener, MISSING_HOLDER, LIST_DATA, ctx.identifier().getText(), ENTRY);
118
119 String identifier = getValidIdentifier(ctx.identifier().getText(), LIST_DATA, ctx);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530120
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530121 // Validate sub statement cardinality.
122 validateSubStatementsCardinality(ctx);
123
124 // Check for identifier collision
Vidyashree Rama468f8282016-03-04 19:08:35 +0530125 int line = ctx.getStart().getLine();
126 int charPositionInLine = ctx.getStart().getCharPositionInLine();
127 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, LIST_DATA);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530128
Vinod Kumar S38046502016-03-23 15:30:27 +0530129 YangList yangList = getYangListNode(JAVA_GENERATION);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530130 yangList.setName(identifier);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530131
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530132 /*
133 * If "config" is not specified, the default is the same as the parent
134 * schema node's "config" value.
135 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530136 if (ctx.configStatement().isEmpty()) {
137 boolean parentConfig = ListenerValidation.getParentNodeConfig(listener);
138 yangList.setConfig(parentConfig);
139 }
140
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530141 Parsable curData = listener.getParsedDataStack().peek();
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530142 if (curData instanceof YangModule || curData instanceof YangContainer
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530143 || curData instanceof YangList || curData instanceof YangCase
144 || curData instanceof YangNotification || curData instanceof YangInput
Vidyashree Rama1db15562016-05-17 16:16:15 +0530145 || curData instanceof YangOutput || curData instanceof YangAugment
146 || curData instanceof YangGrouping || curData instanceof YangSubModule) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530147 curNode = (YangNode) curData;
148 try {
149 curNode.addChild(yangList);
150 } catch (DataModelException e) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530151 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530152 LIST_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530153 }
154 listener.getParsedDataStack().push(yangList);
155 } else {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530156 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LIST_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530157 ctx.identifier().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530158 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530159 }
160
161 /**
162 * It is called when parser exits from grammar rule (list), it performs
163 * validation and updates the data model tree.
164 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530165 * @param listener listener's object
166 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530167 */
168 public static void processListExit(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530169 GeneratedYangParser.ListStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530170
Vidyashree Rama468f8282016-03-04 19:08:35 +0530171 checkStackIsNotEmpty(listener, MISSING_HOLDER, LIST_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530172
173 if (listener.getParsedDataStack().peek() instanceof YangList) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530174 YangList yangList = (YangList) listener.getParsedDataStack().peek();
175 try {
176 yangList.validateDataOnExit();
177 } catch (DataModelException e) {
178 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530179 LIST_DATA, ctx.identifier().getText(), EXIT, e.getMessage()));
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530180 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530181 listener.getParsedDataStack().pop();
182 } else {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530183 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LIST_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530184 ctx.identifier().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530185 }
186 }
187
188 /**
189 * Validates the cardinality of list sub-statements as per grammar.
190 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530191 * @param ctx context object of the grammar rule
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530192 */
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530193 private static void validateSubStatementsCardinality(GeneratedYangParser.ListStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530194
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530195 validateCardinalityMaxOne(ctx.keyStatement(), KEY_DATA, LIST_DATA, ctx.identifier().getText());
196 validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, LIST_DATA, ctx.identifier().getText());
197 validateCardinalityMaxOne(ctx.maxElementsStatement(), MAX_ELEMENT_DATA, LIST_DATA, ctx.identifier().getText());
198 validateCardinalityMaxOne(ctx.minElementsStatement(), MIN_ELEMENT_DATA, LIST_DATA, ctx.identifier().getText());
199 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, LIST_DATA, ctx.identifier().getText());
200 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, LIST_DATA, ctx.identifier().getText());
201 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, LIST_DATA, ctx.identifier().getText());
202 validateCardinalityNonZero(ctx.dataDefStatement(), DATA_DEF_DATA, LIST_DATA, ctx.identifier().getText(), ctx);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530203 //TODO when, typedef, grouping, unique
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530204 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530205}