blob: 6cdf77b6b6af1fc903ec59d661674e689b3fcb69 [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;
Vidyashree Rama59071f32016-02-20 19:27:56 +053018
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053019import org.onosproject.yangutils.datamodel.YangContainer;
Vidyashree Rama59071f32016-02-20 19:27:56 +053020import org.onosproject.yangutils.datamodel.YangList;
21import org.onosproject.yangutils.datamodel.YangModule;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053022import org.onosproject.yangutils.datamodel.YangNode;
23import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
24import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053025import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053026import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053027import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama468f8282016-03-04 19:08:35 +053028import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
29
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053031import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Rama59071f32016-02-20 19:27:56 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vidyashree Rama59071f32016-02-20 19:27:56 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Vidyashree Rama59071f32016-02-20 19:27:56 +053036import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053037import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Vidyashree Rama59071f32016-02-20 19:27:56 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Vidyashree Rama59071f32016-02-20 19:27:56 +053040import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053041import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinality;
42import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
43import static org.onosproject.yangutils.utils.YangConstructType.CONTAINER_DATA;
44import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
45import static org.onosproject.yangutils.utils.YangConstructType.PRESENCE_DATA;
46import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
47import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053048
49/*
50 * Reference: RFC6020 and YANG ANTLR Grammar
51 *
52 * ABNF grammar as per RFC6020
53 * container-stmt = container-keyword sep identifier-arg-str optsep
54 * (";" /
55 * "{" stmtsep
56 * ;; these stmts can appear in any order
57 * [when-stmt stmtsep]
58 * *(if-feature-stmt stmtsep)
59 * *(must-stmt stmtsep)
60 * [presence-stmt stmtsep]
61 * [config-stmt stmtsep]
62 * [status-stmt stmtsep]
63 * [description-stmt stmtsep]
64 * [reference-stmt stmtsep]
65 * *((typedef-stmt /
66 * grouping-stmt) stmtsep)
67 * *(data-def-stmt stmtsep)
68 * "}")
69 *
70 * ANTLR grammar rule
Vidyashree Rama468f8282016-03-04 19:08:35 +053071 * containerStatement : CONTAINER_KEYWORD identifier
Vidyashree Rama92fc5562016-02-12 18:44:12 +053072 * (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
73 * presenceStatement | configStatement | statusStatement | descriptionStatement |
74 * referenceStatement | typedefStatement | groupingStatement
75 * | dataDefStatement)* RIGHT_CURLY_BRACE);
76 */
77
78/**
79 * Implements listener based call back function corresponding to the "container"
80 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
81 */
82public final class ContainerListener {
83
84 /**
85 * Creates a new container listener.
86 */
87 private ContainerListener() {
88 }
89
90 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053091 * It is called when parser receives an input matching the grammar rule
92 * (container), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053093 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053094 * @param listener listener's object
95 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053096 */
97 public static void processContainerEntry(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053098 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053099
100 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530101 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.identifier().getText(), ENTRY);
102
103 String identifier = getValidIdentifier(ctx.identifier().getText(), CONTAINER_DATA, ctx);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530104
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530105 // Validate sub statement cardinality.
106 validateSubStatementsCardinality(ctx);
107
108 // Check for identifier collision
Vidyashree Rama468f8282016-03-04 19:08:35 +0530109 int line = ctx.getStart().getLine();
110 int charPositionInLine = ctx.getStart().getCharPositionInLine();
111 detectCollidingChildUtil(listener, line, charPositionInLine, identifier, CONTAINER_DATA);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530112
113 YangContainer container = new YangContainer();
Vidyashree Rama468f8282016-03-04 19:08:35 +0530114 container.setName(identifier);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530115
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530116 /*
117 * If "config" is not specified, the default is the same as the parent
118 * schema node's "config" value.
119 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530120 if (ctx.configStatement().isEmpty()) {
121 boolean parentConfig = ListenerValidation.getParentNodeConfig(listener);
122 container.setConfig(parentConfig);
123 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530124
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530125 Parsable curData = listener.getParsedDataStack().peek();
Vidyashree Rama59071f32016-02-20 19:27:56 +0530126 if ((curData instanceof YangModule) || (curData instanceof YangContainer)
127 || (curData instanceof YangList)) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530128 YangNode curNode = (YangNode) curData;
129 try {
130 curNode.addChild(container);
131 } catch (DataModelException e) {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530132 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530133 CONTAINER_DATA, ctx.identifier().getText(), ENTRY, e.getMessage()));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530134 }
135 listener.getParsedDataStack().push(container);
136 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530137 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONTAINER_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530138 ctx.identifier().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530139 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530140 }
141
142 /**
143 * It is called when parser exits from grammar rule (container), it perform
144 * validations and updates the data model tree.
145 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530146 * @param listener listener's object
147 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530148 */
149 public static void processContainerExit(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530150 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530151
152 // Check for stack to be non empty.
Vidyashree Rama468f8282016-03-04 19:08:35 +0530153 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.identifier().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530154
155 if (listener.getParsedDataStack().peek() instanceof YangContainer) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530156 YangContainer yangContainer = (YangContainer) listener.getParsedDataStack().peek();
157 try {
158 yangContainer.validateDataOnExit();
159 } catch (DataModelException e) {
160 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530161 CONTAINER_DATA, ctx.identifier().getText(), EXIT, e.getMessage()));
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530162 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530163 listener.getParsedDataStack().pop();
164 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530165 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CONTAINER_DATA,
Vidyashree Rama468f8282016-03-04 19:08:35 +0530166 ctx.identifier().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530167 }
168 }
169
170 /**
171 * Validates the cardinality of container sub-statements as per grammar.
172 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530173 * @param ctx context object of the grammar rule
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530174 */
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530175 private static void validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530176
Vidyashree Rama468f8282016-03-04 19:08:35 +0530177 validateCardinality(ctx.presenceStatement(), PRESENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
178 validateCardinality(ctx.configStatement(), CONFIG_DATA, CONTAINER_DATA, ctx.identifier().getText());
179 validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, CONTAINER_DATA, ctx.identifier().getText());
180 validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
181 validateCardinality(ctx.statusStatement(), STATUS_DATA, CONTAINER_DATA, ctx.identifier().getText());
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530182 // TODO when, grouping, typedef.
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530183 }
184}