blob: 08be582f05febc763c6d2aecdbd3e00cc4192706 [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 Rama59071f32016-02-20 19:27:56 +053030
31import static org.onosproject.yangutils.parser.ParsableDataType.CONTAINER_DATA;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
37import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
38import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
39import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CARDINALITY;
40import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
41import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053042
43/*
44 * Reference: RFC6020 and YANG ANTLR Grammar
45 *
46 * ABNF grammar as per RFC6020
47 * container-stmt = container-keyword sep identifier-arg-str optsep
48 * (";" /
49 * "{" stmtsep
50 * ;; these stmts can appear in any order
51 * [when-stmt stmtsep]
52 * *(if-feature-stmt stmtsep)
53 * *(must-stmt stmtsep)
54 * [presence-stmt stmtsep]
55 * [config-stmt stmtsep]
56 * [status-stmt stmtsep]
57 * [description-stmt stmtsep]
58 * [reference-stmt stmtsep]
59 * *((typedef-stmt /
60 * grouping-stmt) stmtsep)
61 * *(data-def-stmt stmtsep)
62 * "}")
63 *
64 * ANTLR grammar rule
65 * containerStatement : CONTAINER_KEYWORD IDENTIFIER
66 * (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | mustStatement |
67 * presenceStatement | configStatement | statusStatement | descriptionStatement |
68 * referenceStatement | typedefStatement | groupingStatement
69 * | dataDefStatement)* RIGHT_CURLY_BRACE);
70 */
71
72/**
73 * Implements listener based call back function corresponding to the "container"
74 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
75 */
76public final class ContainerListener {
77
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053078 private static ParsableDataType yangConstruct;
79
Vidyashree Rama92fc5562016-02-12 18:44:12 +053080 /**
81 * Creates a new container listener.
82 */
83 private ContainerListener() {
84 }
85
86 /**
87 * It is called when parser receives an input matching the grammar
88 * rule (container), performs validation and updates the data model
89 * tree.
90 *
91 * @param listener listener's object.
92 * @param ctx context object of the grammar rule.
93 */
94 public static void processContainerEntry(TreeWalkListener listener,
95 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053096
97 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +053098 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.IDENTIFIER().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053099
100 boolean result = validateSubStatementsCardinality(ctx);
101 if (!result) {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530102 throw new ParserException(constructListenerErrorMessage(INVALID_CARDINALITY, yangConstruct, "", ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530103 }
104
105 YangContainer container = new YangContainer();
106 container.setName(ctx.IDENTIFIER().getText());
107
108 Parsable curData = listener.getParsedDataStack().peek();
109
Vidyashree Rama59071f32016-02-20 19:27:56 +0530110 if ((curData instanceof YangModule) || (curData instanceof YangContainer)
111 || (curData instanceof YangList)) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530112 YangNode curNode = (YangNode) curData;
113 try {
114 curNode.addChild(container);
115 } catch (DataModelException e) {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530116 throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
117 CONTAINER_DATA, ctx.IDENTIFIER().getText(), ENTRY, e.getMessage()));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530118 }
119 listener.getParsedDataStack().push(container);
120 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530121 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, CONTAINER_DATA,
122 ctx.IDENTIFIER().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530123 }
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530124 }
125
126 /**
127 * It is called when parser exits from grammar rule (container), it perform
128 * validations and updates the data model tree.
129 *
130 * @param listener listener's object.
131 * @param ctx context object of the grammar rule.
132 */
133 public static void processContainerExit(TreeWalkListener listener,
134 GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530135
136 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +0530137 checkStackIsNotEmpty(listener, MISSING_HOLDER, CONTAINER_DATA, ctx.IDENTIFIER().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530138
139 if (listener.getParsedDataStack().peek() instanceof YangContainer) {
140 listener.getParsedDataStack().pop();
141 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530142 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, CONTAINER_DATA,
143 ctx.IDENTIFIER().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530144 }
145 }
146
147 /**
148 * Validates the cardinality of container sub-statements as per grammar.
149 *
150 * @param ctx context object of the grammar rule.
151 * @return true/false validation success or failure.
152 */
Vidyashree Rama59071f32016-02-20 19:27:56 +0530153 private static boolean validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530154
155 if ((!ctx.presenceStatement().isEmpty())
156 && (ctx.presenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
157 yangConstruct = ParsableDataType.PRESENCE_DATA;
158 return false;
159 }
160
161 if ((!ctx.configStatement().isEmpty())
162 && (ctx.configStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
163 yangConstruct = ParsableDataType.CONFIG_DATA;
164 return false;
165 }
166
167 if ((!ctx.descriptionStatement().isEmpty())
168 && (ctx.descriptionStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
169 yangConstruct = ParsableDataType.DESCRIPTION_DATA;
170 return false;
171 }
172
173 if ((!ctx.referenceStatement().isEmpty())
174 && (ctx.referenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
175 yangConstruct = ParsableDataType.REFERENCE_DATA;
176 return false;
177 }
178
179 if ((!ctx.statusStatement().isEmpty())
180 && (ctx.statusStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
181 yangConstruct = ParsableDataType.STATUS_DATA;
182 return false;
183 }
184
185 return true;
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530186 }
187}