blob: 2325be353f72cb46def30be25598e3d789d08b33 [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 Rama4f1f08b2016-02-13 21:47:58 +053025import org.onosproject.yangutils.parser.ParsableDataType;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053026import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053027import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053028import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053029import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053030import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
Vidyashree Rama59071f32016-02-20 19:27:56 +053031
32import static org.onosproject.yangutils.parser.ParsableDataType.CONTAINER_DATA;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CARDINALITY;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
42import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053043
44/*
45 * Reference: RFC6020 and YANG ANTLR Grammar
46 *
47 * ABNF grammar as per RFC6020
48 * container-stmt = container-keyword sep identifier-arg-str optsep
49 * (";" /
50 * "{" stmtsep
51 * ;; these stmts can appear in any order
52 * [when-stmt stmtsep]
53 * *(if-feature-stmt stmtsep)
54 * *(must-stmt stmtsep)
55 * [presence-stmt stmtsep]
56 * [config-stmt stmtsep]
57 * [status-stmt stmtsep]
58 * [description-stmt stmtsep]
59 * [reference-stmt stmtsep]
60 * *((typedef-stmt /
61 * grouping-stmt) stmtsep)
62 * *(data-def-stmt stmtsep)
63 * "}")
64 *
65 * ANTLR grammar rule
66 * containerStatement : CONTAINER_KEYWORD IDENTIFIER
67 * (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
68 * presenceStatement | configStatement | statusStatement | descriptionStatement |
69 * referenceStatement | typedefStatement | groupingStatement
70 * | dataDefStatement)* RIGHT_CURLY_BRACE);
71 */
72
73/**
74 * Implements listener based call back function corresponding to the "container"
75 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
76 */
77public final class ContainerListener {
78
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053079 private static ParsableDataType yangConstruct;
80
Vidyashree Rama92fc5562016-02-12 18:44:12 +053081 /**
82 * Creates a new container listener.
83 */
84 private ContainerListener() {
85 }
86
87 /**
88 * It is called when parser receives an input matching the grammar
89 * rule (container), performs validation and updates the data model
90 * tree.
91 *
92 * @param listener listener's object.
93 * @param ctx context object of the grammar rule.
94 */
95 public static void processContainerEntry(TreeWalkListener listener,
96 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053097
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +053098 YangNode parentNode;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053099 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +0530100 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.IDENTIFIER().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530101
102 boolean result = validateSubStatementsCardinality(ctx);
103 if (!result) {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530104 throw new ParserException(constructListenerErrorMessage(INVALID_CARDINALITY, yangConstruct, "", ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530105 }
106
107 YangContainer container = new YangContainer();
108 container.setName(ctx.IDENTIFIER().getText());
109
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530110 /* If "config" is not specified, the default is the same as the parent
111 schema node's "config" value. */
112 if (ctx.configStatement().isEmpty()) {
113 boolean parentConfig = ListenerValidation.getParentNodeConfig(listener);
114 container.setConfig(parentConfig);
115 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530116
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530117 Parsable curData = listener.getParsedDataStack().peek();
Vidyashree Rama59071f32016-02-20 19:27:56 +0530118 if ((curData instanceof YangModule) || (curData instanceof YangContainer)
119 || (curData instanceof YangList)) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530120 YangNode curNode = (YangNode) curData;
121 try {
122 curNode.addChild(container);
123 } catch (DataModelException e) {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530124 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
125 CONTAINER_DATA, ctx.IDENTIFIER().getText(), ENTRY, e.getMessage()));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530126 }
127 listener.getParsedDataStack().push(container);
128 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530129 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONTAINER_DATA,
130 ctx.IDENTIFIER().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530131 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530132 }
133
134 /**
135 * It is called when parser exits from grammar rule (container), it perform
136 * validations and updates the data model tree.
137 *
138 * @param listener listener's object.
139 * @param ctx context object of the grammar rule.
140 */
141 public static void processContainerExit(TreeWalkListener listener,
142 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530143
144 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +0530145 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.IDENTIFIER().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530146
147 if (listener.getParsedDataStack().peek() instanceof YangContainer) {
Vidyashree Ramaf4c617c2016-02-24 12:28:22 +0530148 YangContainer yangContainer = (YangContainer) listener.getParsedDataStack().peek();
149 try {
150 yangContainer.validateDataOnExit();
151 } catch (DataModelException e) {
152 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
153 CONTAINER_DATA, ctx.IDENTIFIER().getText(), EXIT, e.getMessage()));
154 }
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530155 listener.getParsedDataStack().pop();
156 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530157 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CONTAINER_DATA,
158 ctx.IDENTIFIER().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530159 }
160 }
161
162 /**
163 * Validates the cardinality of container sub-statements as per grammar.
164 *
165 * @param ctx context object of the grammar rule.
166 * @return true/false validation success or failure.
167 */
Vidyashree Rama59071f32016-02-20 19:27:56 +0530168 private static boolean validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530169
170 if ((!ctx.presenceStatement().isEmpty())
171 && (ctx.presenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
172 yangConstruct = ParsableDataType.PRESENCE_DATA;
173 return false;
174 }
175
176 if ((!ctx.configStatement().isEmpty())
177 && (ctx.configStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
178 yangConstruct = ParsableDataType.CONFIG_DATA;
179 return false;
180 }
181
182 if ((!ctx.descriptionStatement().isEmpty())
183 && (ctx.descriptionStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
184 yangConstruct = ParsableDataType.DESCRIPTION_DATA;
185 return false;
186 }
187
188 if ((!ctx.referenceStatement().isEmpty())
189 && (ctx.referenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
190 yangConstruct = ParsableDataType.REFERENCE_DATA;
191 return false;
192 }
193
194 if ((!ctx.statusStatement().isEmpty())
195 && (ctx.statusStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
196 yangConstruct = ParsableDataType.STATUS_DATA;
197 return false;
198 }
199
200 return true;
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530201 }
202}