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