blob: 06dd0d5f63fce1dd9f2d638be876cd184bc96dc6 [file] [log] [blame]
Vidyashree Ramafe971cb2016-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;
Vidyashree Ramae300f702016-02-20 19:27:56 +053018
Gaurav Agrawalf6ccc972016-03-25 11:25:36 +053019import org.onosproject.yangutils.datamodel.YangCase;
Vidyashree Ramab24ca902016-02-13 21:47:58 +053020import org.onosproject.yangutils.datamodel.YangContainer;
Vidyashree Ramae300f702016-02-20 19:27:56 +053021import org.onosproject.yangutils.datamodel.YangList;
22import org.onosproject.yangutils.datamodel.YangModule;
Vidyashree Ramab24ca902016-02-13 21:47:58 +053023import org.onosproject.yangutils.datamodel.YangNode;
24import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
25import org.onosproject.yangutils.parser.Parsable;
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053026import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Ramab24ca902016-02-13 21:47:58 +053027import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053028import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Ramad3221322016-03-04 19:08:35 +053029import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
30
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053031import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
32import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangContainerNode;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053033import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Ramae300f702016-02-20 19:27:56 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vidyashree Ramae300f702016-02-20 19:27:56 +053036import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Vidyashree Ramae300f702016-02-20 19:27:56 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Vidyashree Ramae300f702016-02-20 19:27:56 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053042import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Vidyashree Ramae300f702016-02-20 19:27:56 +053043import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal0cb696d2016-03-11 00:30:12 +053044import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053045import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
46import static org.onosproject.yangutils.utils.YangConstructType.CONTAINER_DATA;
47import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
48import static org.onosproject.yangutils.utils.YangConstructType.PRESENCE_DATA;
49import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
50import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053051
52/*
53 * Reference: RFC6020 and YANG ANTLR Grammar
54 *
55 * ABNF grammar as per RFC6020
56 * container-stmt = container-keyword sep identifier-arg-str optsep
57 * (";" /
58 * "{" stmtsep
59 * ;; these stmts can appear in any order
60 * [when-stmt stmtsep]
61 * *(if-feature-stmt stmtsep)
62 * *(must-stmt stmtsep)
63 * [presence-stmt stmtsep]
64 * [config-stmt stmtsep]
65 * [status-stmt stmtsep]
66 * [description-stmt stmtsep]
67 * [reference-stmt stmtsep]
68 * *((typedef-stmt /
69 * grouping-stmt) stmtsep)
70 * *(data-def-stmt stmtsep)
71 * "}")
72 *
73 * ANTLR grammar rule
Vidyashree Ramad3221322016-03-04 19:08:35 +053074 * containerStatement : CONTAINER_KEYWORD identifier
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053075 * (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
76 * presenceStatement | configStatement | statusStatement | descriptionStatement |
77 * referenceStatement | typedefStatement | groupingStatement
78 * | dataDefStatement)* RIGHT_CURLY_BRACE);
79 */
80
81/**
82 * Implements listener based call back function corresponding to the "container"
83 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
84 */
85public final class ContainerListener {
86
87 /**
88 * Creates a new container listener.
89 */
90 private ContainerListener() {
91 }
92
93 /**
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053094 * It is called when parser receives an input matching the grammar rule
95 * (container), performs validation and updates the data model tree.
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053096 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +053097 * @param listener listener's object
98 * @param ctx context object of the grammar rule
Vidyashree Ramafe971cb2016-02-12 18:44:12 +053099 */
100 public static void processContainerEntry(TreeWalkListener listener,
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530101 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530102
103 // Check for stack to be non empty.
Vidyashree Ramad3221322016-03-04 19:08:35 +0530104 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.identifier().getText(), ENTRY);
105
106 String identifier = getValidIdentifier(ctx.identifier().getText(), CONTAINER_DATA, ctx);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530107
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530108 // Validate sub statement cardinality.
109 validateSubStatementsCardinality(ctx);
110
111 // Check for identifier collision
Vidyashree Ramad3221322016-03-04 19:08:35 +0530112 int line = ctx.getStart().getLine();
113 int charPositionInLine = ctx.getStart().getCharPositionInLine();
114 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, CONTAINER_DATA);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530115
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530116 YangContainer container = getYangContainerNode(JAVA_GENERATION);
Vidyashree Ramad3221322016-03-04 19:08:35 +0530117 container.setName(identifier);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530118
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530119 /*
120 * If "config" is not specified, the default is the same as the parent
121 * schema node's "config" value.
122 */
Vidyashree Ramaf67c4ba2016-02-24 12:28:22 +0530123 if (ctx.configStatement().isEmpty()) {
124 boolean parentConfig = ListenerValidation.getParentNodeConfig(listener);
125 container.setConfig(parentConfig);
126 }
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530127
Vidyashree Ramaf67c4ba2016-02-24 12:28:22 +0530128 Parsable curData = listener.getParsedDataStack().peek();
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530129 if (curData instanceof YangModule || curData instanceof YangContainer
Gaurav Agrawalf6ccc972016-03-25 11:25:36 +0530130 || curData instanceof YangList || curData instanceof YangCase) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530131 YangNode curNode = (YangNode) curData;
132 try {
133 curNode.addChild(container);
134 } catch (DataModelException e) {
Vidyashree Ramae300f702016-02-20 19:27:56 +0530135 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530136 CONTAINER_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530137 }
138 listener.getParsedDataStack().push(container);
139 } else {
Vidyashree Ramae300f702016-02-20 19:27:56 +0530140 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONTAINER_DATA,
Vidyashree Ramad3221322016-03-04 19:08:35 +0530141 ctx.identifier().getText(), ENTRY));
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530142 }
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530143 }
144
145 /**
146 * It is called when parser exits from grammar rule (container), it perform
147 * validations and updates the data model tree.
148 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530149 * @param listener listener's object
150 * @param ctx context object of the grammar rule
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530151 */
152 public static void processContainerExit(TreeWalkListener listener,
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530153 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530154
155 // Check for stack to be non empty.
Vidyashree Ramad3221322016-03-04 19:08:35 +0530156 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530157
158 if (listener.getParsedDataStack().peek() instanceof YangContainer) {
Vidyashree Ramaf67c4ba2016-02-24 12:28:22 +0530159 YangContainer yangContainer = (YangContainer) listener.getParsedDataStack().peek();
160 try {
161 yangContainer.validateDataOnExit();
162 } catch (DataModelException e) {
163 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vidyashree Ramad3221322016-03-04 19:08:35 +0530164 CONTAINER_DATA, ctx.identifier().getText(), EXIT, e.getMessage()));
Vidyashree Ramaf67c4ba2016-02-24 12:28:22 +0530165 }
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530166 listener.getParsedDataStack().pop();
167 } else {
Vidyashree Ramae300f702016-02-20 19:27:56 +0530168 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CONTAINER_DATA,
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530169 ctx.identifier().getText(), EXIT));
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530170 }
171 }
172
173 /**
174 * Validates the cardinality of container sub-statements as per grammar.
175 *
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530176 * @param ctx context object of the grammar rule
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530177 */
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530178 private static void validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Ramab24ca902016-02-13 21:47:58 +0530179
Gaurav Agrawal0cb696d2016-03-11 00:30:12 +0530180 validateCardinalityMaxOne(ctx.presenceStatement(), PRESENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
181 validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, CONTAINER_DATA, ctx.identifier().getText());
182 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, CONTAINER_DATA,
183 ctx.identifier().getText());
184 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
185 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, CONTAINER_DATA, ctx.identifier().getText());
Gaurav Agrawaldb828bd2016-02-27 03:57:50 +0530186 // TODO when, grouping, typedef.
Vidyashree Ramafe971cb2016-02-12 18:44:12 +0530187 }
188}