blob: 1ada6dd4a2d0224412a37a6690e47eda34e1e5a6 [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;
18
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053019import org.onosproject.yangutils.datamodel.YangLeafList;
20import org.onosproject.yangutils.datamodel.YangLeavesHolder;
21import org.onosproject.yangutils.parser.Parsable;
22import org.onosproject.yangutils.parser.ParsableDataType;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053023import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053024import org.onosproject.yangutils.parser.exceptions.ParserException;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053025import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053026import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
Vidyashree Rama59071f32016-02-20 19:27:56 +053027
28import static org.onosproject.yangutils.parser.ParsableDataType.LEAF_LIST_DATA;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
32import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
33import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
34import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CARDINALITY;
35import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
36import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
Vidyashree Rama92fc5562016-02-12 18:44:12 +053037
38/*
39 * Reference: RFC6020 and YANG ANTLR Grammar
40 *
41 * ABNF grammar as per RFC6020
42 * leaf-list-stmt = leaf-list-keyword sep identifier-arg-str optsep
43 * "{" stmtsep
44 * ;; these stmts can appear in any order
45 * [when-stmt stmtsep]
46 * *(if-feature-stmt stmtsep)
47 * type-stmt stmtsep
48 * [units-stmt stmtsep]
49 * *(must-stmt stmtsep)
50 * [config-stmt stmtsep]
51 * [min-elements-stmt stmtsep]
52 * [max-elements-stmt stmtsep]
53 * [ordered-by-stmt stmtsep]
54 * [status-stmt stmtsep]
55 * [description-stmt stmtsep]
56 * [reference-stmt stmtsep]
57 * "}"
58 *
59 * ANTLR grammar rule
60 * leafListStatement : LEAF_LIST_KEYWORD IDENTIFIER LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement |
61 * typeStatement | unitsStatement | mustStatement | configStatement | minElementsStatement | maxElementsStatement |
62 * orderedByStatement | statusStatement | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
63 */
64
65/**
66 * Implements listener based call back function corresponding to the "leaf-list"
67 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
68 */
69public final class LeafListListener {
70
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053071 private static ParsableDataType yangConstruct;
72
Vidyashree Rama92fc5562016-02-12 18:44:12 +053073 /**
74 * Creates a new leaf list listener.
75 */
76 private LeafListListener() {
77 }
78
79 /**
80 * It is called when parser receives an input matching the grammar
81 * rule (leaf-list), performs validation and updates the data model
82 * tree.
83 *
84 * @param listener listener's object.
85 * @param ctx context object of the grammar rule.
86 */
87 public static void processLeafListEntry(TreeWalkListener listener,
88 GeneratedYangParser.LeafListStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053089
90 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +053091 checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_LIST_DATA, ctx.IDENTIFIER().getText(), ENTRY);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053092
93 boolean result = validateSubStatementsCardinality(ctx);
94 if (!result) {
Vidyashree Rama59071f32016-02-20 19:27:56 +053095 throw new ParserException(constructListenerErrorMessage(INVALID_CARDINALITY, yangConstruct, "", ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053096 }
97
98 YangLeafList leafList = new YangLeafList();
99 leafList.setLeafName(ctx.IDENTIFIER().getText());
100
101 Parsable tmpData = listener.getParsedDataStack().peek();
102 YangLeavesHolder leaves;
103
104 if (tmpData instanceof YangLeavesHolder) {
105 leaves = (YangLeavesHolder) tmpData;
106 leaves.addLeafList(leafList);
107 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530108 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, LEAF_LIST_DATA,
109 ctx.IDENTIFIER().getText(), ENTRY));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530110 }
111 listener.getParsedDataStack().push(leafList);
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530112 }
113
114 /**
115 * It is called when parser exits from grammar rule (leaf-list), it performs
116 * validation and updates the data model tree.
117 *
118 * @param listener listener's object.
119 * @param ctx context object of the grammar rule.
120 */
121 public static void processLeafListExit(TreeWalkListener listener,
122 GeneratedYangParser.LeafListStatementContext ctx) {
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530123
124 // Check for stack to be non empty.
Vidyashree Rama59071f32016-02-20 19:27:56 +0530125 checkStackIsNotEmpty(listener, MISSING_HOLDER, LEAF_LIST_DATA, ctx.IDENTIFIER().getText(), EXIT);
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530126
127 if (listener.getParsedDataStack().peek() instanceof YangLeafList) {
128 listener.getParsedDataStack().pop();
129 } else {
Vidyashree Rama59071f32016-02-20 19:27:56 +0530130 throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, LEAF_LIST_DATA,
131 ctx.IDENTIFIER().getText(), EXIT));
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530132 }
133 }
134
135 /**
136 * Validates the cardinality of leaf-list sub-statements as per grammar.
137 *
138 * @param ctx context object of the grammar rule.
139 * @return true/false validation success or failure.
140 */
Vidyashree Rama59071f32016-02-20 19:27:56 +0530141 private static boolean validateSubStatementsCardinality(GeneratedYangParser
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +0530142 .LeafListStatementContext ctx) {
143
144 if (ctx.typeStatement().isEmpty()
145 || (ctx.typeStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
146 yangConstruct = ParsableDataType.TYPE_DATA;
147 return false;
148 }
149
150 if ((!ctx.unitsStatement().isEmpty())
151 && (ctx.unitsStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
152 yangConstruct = ParsableDataType.UNITS_DATA;
153 return false;
154 }
155
156 if ((!ctx.configStatement().isEmpty())
157 && (ctx.configStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
158 yangConstruct = ParsableDataType.CONFIG_DATA;
159 return false;
160 }
161
162 if ((!ctx.maxElementsStatement().isEmpty())
163 && (ctx.maxElementsStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
164 yangConstruct = ParsableDataType.MAX_ELEMENT_DATA;
165 return false;
166 }
167
168 if ((!ctx.minElementsStatement().isEmpty())
169 && (ctx.minElementsStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
170 yangConstruct = ParsableDataType.MIN_ELEMENT_DATA;
171 return false;
172 }
173
174 if ((!ctx.descriptionStatement().isEmpty())
175 && (ctx.descriptionStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
176 yangConstruct = ParsableDataType.DESCRIPTION_DATA;
177 return false;
178 }
179
180 if ((!ctx.referenceStatement().isEmpty())
181 && (ctx.referenceStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
182 yangConstruct = ParsableDataType.REFERENCE_DATA;
183 return false;
184 }
185
186 if ((!ctx.statusStatement().isEmpty())
187 && (ctx.statusStatement().size() != YangUtilsParserManager.SUB_STATEMENT_CARDINALITY)) {
188 yangConstruct = ParsableDataType.STATUS_DATA;
189 return false;
190 }
191
192 return true;
Vidyashree Rama92fc5562016-02-12 18:44:12 +0530193 }
194}