blob: 1f234499cd88ee2a897b4f7a3f98d313a5796684 [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;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053028import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
Vidyashree Rama59071f32016-02-20 19:27:56 +053029import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
Vidyashree Rama59071f32016-02-20 19:27:56 +053031import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053032import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Vidyashree Rama59071f32016-02-20 19:27:56 +053033import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053034import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
Vidyashree Rama59071f32016-02-20 19:27:56 +053035import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053037import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Vidyashree Rama59071f32016-02-20 19:27:56 +053038import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053039import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinality;
40import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
41import static org.onosproject.yangutils.utils.YangConstructType.CONTAINER_DATA;
42import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
43import static org.onosproject.yangutils.utils.YangConstructType.PRESENCE_DATA;
44import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
45import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053046
47/*
48 * Reference: RFC6020 and YANG ANTLR Grammar
49 *
50 * ABNF grammar as per RFC6020
51 * container-stmt = container-keyword sep identifier-arg-str optsep
52 * (";" /
53 * "{" stmtsep
54 * ;; these stmts can appear in any order
55 * [when-stmt stmtsep]
56 * *(if-feature-stmt stmtsep)
57 * *(must-stmt stmtsep)
58 * [presence-stmt stmtsep]
59 * [config-stmt stmtsep]
60 * [status-stmt stmtsep]
61 * [description-stmt stmtsep]
62 * [reference-stmt stmtsep]
63 * *((typedef-stmt /
64 * grouping-stmt) stmtsep)
65 * *(data-def-stmt stmtsep)
66 * "}")
67 *
68 * ANTLR grammar rule
69 * containerStatement : CONTAINER_KEYWORD IDENTIFIER
70 * (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
71 * presenceStatement | configStatement | statusStatement | descriptionStatement |
72 * referenceStatement | typedefStatement | groupingStatement
73 * | dataDefStatement)* RIGHT_CURLY_BRACE);
74 */
75
76/**
77 * Implements listener based call back function corresponding to the "container"
78 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
79 */
80public final class ContainerListener {
81
82 /**
83 * Creates a new container listener.
84 */
85 private ContainerListener() {
86 }
87
88 /**
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053089 * It is called when parser receives an input matching the grammar rule
90 * (container), performs validation and updates the data model tree.
Vidyashree Rama92fc5562016-02-12 18:44:12 +053091 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053092 * @param listener listener's object
93 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +053094 */
95 public static void processContainerEntry(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053096 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053097
98 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +053099 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.IDENTIFIER().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530100
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530101 // Validate sub statement cardinality.
102 validateSubStatementsCardinality(ctx);
103
104 // Check for identifier collision
105 int line = ctx.IDENTIFIER().getSymbol().getLine();
106 int charPositionInLine = ctx.IDENTIFIER().getSymbol().getCharPositionInLine();
107 String identifierName = ctx.IDENTIFIER().getText();
108 detectCollidingChildUtil(listener, line, charPositionInLine, identifierName, CONTAINER_DATA);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530109
110 YangContainer container = new YangContainer();
111 container.setName(ctx.IDENTIFIER().getText());
112
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530113 /*
114 * If "config" is not specified, the default is the same as the parent
115 * schema node's "config" value.
116 */
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530117 if (ctx.configStatement().isEmpty()) {
118 boolean parentConfig = ListenerValidation.getParentNodeConfig(listener);
119 container.setConfig(parentConfig);
120 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530121
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530122 Parsable curData = listener.getParsedDataStack().peek();
Vidyashree Rama59071f32016-02-20 19:27:56 +0530123 if ((curData instanceof YangModule) || (curData instanceof YangContainer)
124 || (curData instanceof YangList)) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530125 YangNode curNode = (YangNode) curData;
126 try {
127 curNode.addChild(container);
128 } catch (DataModelException e) {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530129 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530130 CONTAINER_DATA, ctx.IDENTIFIER().getText(), ENTRY, e.getMessage()));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530131 }
132 listener.getParsedDataStack().push(container);
133 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530134 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONTAINER_DATA,
135 ctx.IDENTIFIER().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530136 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530137 }
138
139 /**
140 * It is called when parser exits from grammar rule (container), it perform
141 * validations and updates the data model tree.
142 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530143 * @param listener listener's object
144 * @param ctx context object of the grammar rule
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530145 */
146 public static void processContainerExit(TreeWalkListener listener,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530147 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530148
149 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +0530150 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.IDENTIFIER().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530151
152 if (listener.getParsedDataStack().peek() instanceof YangContainer) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530153 YangContainer yangContainer = (YangContainer) listener.getParsedDataStack().peek();
154 try {
155 yangContainer.validateDataOnExit();
156 } catch (DataModelException e) {
157 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
158 CONTAINER_DATA, ctx.IDENTIFIER().getText(), EXIT, e.getMessage()));
159 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530160 listener.getParsedDataStack().pop();
161 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530162 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CONTAINER_DATA,
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530163 ctx.IDENTIFIER().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530164 }
165 }
166
167 /**
168 * Validates the cardinality of container sub-statements as per grammar.
169 *
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530170 * @param ctx context object of the grammar rule
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530171 */
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530172 private static void validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530173
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530174 validateCardinality(ctx.presenceStatement(), PRESENCE_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText());
175 validateCardinality(ctx.configStatement(), CONFIG_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText());
176 validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText());
177 validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText());
178 validateCardinality(ctx.statusStatement(), STATUS_DATA, CONTAINER_DATA, ctx.IDENTIFIER().getText());
179 // TODO when, grouping, typedef.
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530180 }
181}