blob: 5a89cc3634f689aa409929a4b8d6cda95e739c78 [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;
Vidyashree Rama59071f32016-02-20 19:27:56 +053018
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 Rama4f1f08b2016-02-13 21:47:58 +053021import org.onosproject.yangutils.datamodel.YangContainer;
Vinod Kumar S427d2932016-04-20 13:02:58 +053022import org.onosproject.yangutils.datamodel.YangGrouping;
Bharat saraswald9822e92016-04-05 15:13:44 +053023import org.onosproject.yangutils.datamodel.YangInput;
Vidyashree Rama59071f32016-02-20 19:27:56 +053024import org.onosproject.yangutils.datamodel.YangList;
25import 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;
Vidyashree Rama468f8282016-03-04 19:08:35 +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.CONTAINER_DATA;
40import static org.onosproject.yangutils.datamodel.utils.YangConstructType.DESCRIPTION_DATA;
41import static org.onosproject.yangutils.datamodel.utils.YangConstructType.PRESENCE_DATA;
42import static org.onosproject.yangutils.datamodel.utils.YangConstructType.REFERENCE_DATA;
43import static org.onosproject.yangutils.datamodel.utils.YangConstructType.STATUS_DATA;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053044import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Rama59071f32016-02-20 19:27:56 +053045import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
46import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vinod Kumar S427d2932016-04-20 13:02:58 +053047import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
48 .constructExtendedListenerErrorMessage;
49import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction
50 .constructListenerErrorMessage;
Vidyashree Rama59071f32016-02-20 19:27:56 +053051import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053052import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Vidyashree Rama59071f32016-02-20 19:27:56 +053053import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
54import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Vinod Kumar S38046502016-03-23 15:30:27 +053055import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Vidyashree Rama59071f32016-02-20 19:27:56 +053056import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal78f72402016-03-11 00:30:12 +053057import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
Bharat saraswal96dfef02016-06-16 00:29:12 +053058import static org.onosproject.yangutils.translator.tojava.YangDataModelFactory.getYangContainerNode;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053059
60/*
61 * Reference: RFC6020 and YANG ANTLR Grammar
62 *
63 * ABNF grammar as per RFC6020
64 * container-stmt = container-keyword sep identifier-arg-str optsep
65 * (";" /
66 * "{" stmtsep
67 * ;; these stmts can appear in any order
68 * [when-stmt stmtsep]
69 * *(if-feature-stmt stmtsep)
70 * *(must-stmt stmtsep)
71 * [presence-stmt stmtsep]
72 * [config-stmt stmtsep]
73 * [status-stmt stmtsep]
74 * [description-stmt stmtsep]
75 * [reference-stmt stmtsep]
76 * *((typedef-stmt /
77 * grouping-stmt) stmtsep)
78 * *(data-def-stmt stmtsep)
79 * "}")
80 *
81 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053082 * containerStatement : CONTAINER_KEYWORD identifier
Vidyashree Rama92fc5562016-02-12 18:44:12 +053083 * (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
84 * presenceStatement | configStatement | statusStatement | descriptionStatement |
85 * referenceStatement | typedefStatement | groupingStatement
86 * | dataDefStatement)* RIGHT_CURLY_BRACE);
87 */
88
89/**
Bharat saraswald9822e92016-04-05 15:13:44 +053090 * Represents listener based call back function corresponding to the "container"
Vidyashree Rama92fc5562016-02-12 18:44:12 +053091 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
92 */
93public final class ContainerListener {
94
95 /**
96 * Creates a new container listener.
97 */
98 private ContainerListener() {
99 }
100
101 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530102 * It is called when parser receives an input matching the grammar rule
103 * (container), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530104 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530105 * @param listener listener's object
106 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530107 */
108 public static void processContainerEntry(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530109 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530110
111 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530112 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.identifier().getText(), ENTRY);
113
114 String identifier = getValidIdentifier(ctx.identifier().getText(), CONTAINER_DATA, ctx);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530115
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530116 // Validate sub statement cardinality.
117 validateSubStatementsCardinality(ctx);
118
119 // Check for identifier collision
Vidyashree Rama468f8282016-03-04 19:08:35 +0530120 int line = ctx.getStart().getLine();
121 int charPositionInLine = ctx.getStart().getCharPositionInLine();
122 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, CONTAINER_DATA);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530123
Vinod Kumar S38046502016-03-23 15:30:27 +0530124 YangContainer container = getYangContainerNode(JAVA_GENERATION);
Vidyashree Rama468f8282016-03-04 19:08:35 +0530125 container.setName(identifier);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530126
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530127 /*
128 * If "config" is not specified, the default is the same as the parent
129 * schema node's "config" value.
130 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530131 if (ctx.configStatement().isEmpty()) {
132 boolean parentConfig = ListenerValidation.getParentNodeConfig(listener);
133 container.setConfig(parentConfig);
134 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530135
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530136 Parsable curData = listener.getParsedDataStack().peek();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530137 if (curData instanceof YangModule || curData instanceof YangSubModule
138 || curData instanceof YangContainer || curData instanceof YangList
139 || curData instanceof YangCase || curData instanceof YangNotification
Vidyashree Rama506cbe12016-03-28 11:59:27 +0530140 || curData instanceof YangInput || curData instanceof YangOutput
Vidyashree Rama1db15562016-05-17 16:16:15 +0530141 || curData instanceof YangAugment || curData instanceof YangGrouping) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530142 YangNode curNode = (YangNode) curData;
143 try {
144 curNode.addChild(container);
145 } catch (DataModelException e) {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530146 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530147 CONTAINER_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530148 }
149 listener.getParsedDataStack().push(container);
150 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530151 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONTAINER_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530152 ctx.identifier().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530153 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530154 }
155
156 /**
157 * It is called when parser exits from grammar rule (container), it perform
158 * validations and updates the data model tree.
159 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530160 * @param listener listener's object
161 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530162 */
163 public static void processContainerExit(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530164 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530165
166 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530167 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530168
169 if (listener.getParsedDataStack().peek() instanceof YangContainer) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530170 YangContainer yangContainer = (YangContainer) listener.getParsedDataStack().peek();
171 try {
172 yangContainer.validateDataOnExit();
173 } catch (DataModelException e) {
Vidyashree Rama36f2fab2016-07-15 14:06:56 +0530174 ParserException parserException = new ParserException(constructExtendedListenerErrorMessage(
175 UNHANDLED_PARSED_DATA, CONTAINER_DATA, ctx.identifier().getText(), EXIT, e.getMessage()));
176 parserException.setLine(ctx.getStart().getLine());
177 parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
178 throw parserException;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530179 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530180 listener.getParsedDataStack().pop();
181 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530182 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CONTAINER_DATA,
Vinod Kumar S38046502016-03-23 15:30:27 +0530183 ctx.identifier().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530184 }
185 }
186
187 /**
188 * Validates the cardinality of container sub-statements as per grammar.
189 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530190 * @param ctx context object of the grammar rule
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530191 */
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530192 private static void validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530193
Gaurav Agrawal78f72402016-03-11 00:30:12 +0530194 validateCardinalityMaxOne(ctx.presenceStatement(), PRESENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
195 validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, CONTAINER_DATA, ctx.identifier().getText());
196 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, CONTAINER_DATA,
197 ctx.identifier().getText());
198 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
199 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, CONTAINER_DATA, ctx.identifier().getText());
Vinod Kumar S427d2932016-04-20 13:02:58 +0530200 // TODO validate 'when' cardinality
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530201 }
202}