Gaurav Agrawal
Committed by Gerrit Code Review

YANG: Line number and character position addition for cardinality failure + Incr…

…ease readability of Error message and function name

Change-Id: I467a536a9d2a6cbb29873491fdb0a91f211deefd
...@@ -38,7 +38,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp ...@@ -38,7 +38,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
38 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; 38 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
39 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; 39 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
40 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; 40 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
41 -import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinality; 41 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
42 import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA; 42 import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
43 import static org.onosproject.yangutils.utils.YangConstructType.CONTAINER_DATA; 43 import static org.onosproject.yangutils.utils.YangConstructType.CONTAINER_DATA;
44 import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA; 44 import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
...@@ -174,11 +174,12 @@ public final class ContainerListener { ...@@ -174,11 +174,12 @@ public final class ContainerListener {
174 */ 174 */
175 private static void validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) { 175 private static void validateSubStatementsCardinality(GeneratedYangParser.ContainerStatementContext ctx) {
176 176
177 - validateCardinality(ctx.presenceStatement(), PRESENCE_DATA, CONTAINER_DATA, ctx.identifier().getText()); 177 + validateCardinalityMaxOne(ctx.presenceStatement(), PRESENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
178 - validateCardinality(ctx.configStatement(), CONFIG_DATA, CONTAINER_DATA, ctx.identifier().getText()); 178 + validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, CONTAINER_DATA, ctx.identifier().getText());
179 - validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, CONTAINER_DATA, ctx.identifier().getText()); 179 + validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, CONTAINER_DATA,
180 - validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, CONTAINER_DATA, ctx.identifier().getText()); 180 + ctx.identifier().getText());
181 - validateCardinality(ctx.statusStatement(), STATUS_DATA, CONTAINER_DATA, ctx.identifier().getText()); 181 + validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, CONTAINER_DATA, ctx.identifier().getText());
182 + validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, CONTAINER_DATA, ctx.identifier().getText());
182 // TODO when, grouping, typedef. 183 // TODO when, grouping, typedef.
183 } 184 }
184 } 185 }
......
...@@ -32,8 +32,8 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp ...@@ -32,8 +32,8 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
32 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; 32 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
33 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; 33 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
34 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; 34 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
35 -import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinality;
36 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne; 35 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne;
36 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
37 import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA; 37 import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
38 import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA; 38 import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
39 import static org.onosproject.yangutils.utils.YangConstructType.LEAF_LIST_DATA; 39 import static org.onosproject.yangutils.utils.YangConstructType.LEAF_LIST_DATA;
...@@ -150,14 +150,17 @@ public final class LeafListListener { ...@@ -150,14 +150,17 @@ public final class LeafListListener {
150 */ 150 */
151 private static void validateSubStatementsCardinality(GeneratedYangParser.LeafListStatementContext ctx) { 151 private static void validateSubStatementsCardinality(GeneratedYangParser.LeafListStatementContext ctx) {
152 152
153 - validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_LIST_DATA, ctx.identifier().getText()); 153 + validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_LIST_DATA, ctx.identifier().getText(), ctx);
154 - validateCardinality(ctx.unitsStatement(), UNITS_DATA, LEAF_LIST_DATA, ctx.identifier().getText()); 154 + validateCardinalityMaxOne(ctx.unitsStatement(), UNITS_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
155 - validateCardinality(ctx.configStatement(), CONFIG_DATA, LEAF_LIST_DATA, ctx.identifier().getText()); 155 + validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
156 - validateCardinality(ctx.maxElementsStatement(), MAX_ELEMENT_DATA, LEAF_LIST_DATA, ctx.identifier().getText()); 156 + validateCardinalityMaxOne(ctx.maxElementsStatement(), MAX_ELEMENT_DATA, LEAF_LIST_DATA,
157 - validateCardinality(ctx.minElementsStatement(), MIN_ELEMENT_DATA, LEAF_LIST_DATA, ctx.identifier().getText()); 157 + ctx.identifier().getText());
158 - validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_LIST_DATA, ctx.identifier().getText()); 158 + validateCardinalityMaxOne(ctx.minElementsStatement(), MIN_ELEMENT_DATA, LEAF_LIST_DATA,
159 - validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, LEAF_LIST_DATA, ctx.identifier().getText()); 159 + ctx.identifier().getText());
160 - validateCardinality(ctx.statusStatement(), STATUS_DATA, LEAF_LIST_DATA, ctx.identifier().getText()); 160 + validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_LIST_DATA,
161 + ctx.identifier().getText());
162 + validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
163 + validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, LEAF_LIST_DATA, ctx.identifier().getText());
161 //TODO ordered by 164 //TODO ordered by
162 } 165 }
163 } 166 }
......
...@@ -36,8 +36,8 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp ...@@ -36,8 +36,8 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
36 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; 36 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
37 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; 37 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
38 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; 38 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
39 -import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinality;
40 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne; 39 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne;
40 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
41 import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA; 41 import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
42 import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA; 42 import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
43 import static org.onosproject.yangutils.utils.YangConstructType.LEAF_DATA; 43 import static org.onosproject.yangutils.utils.YangConstructType.LEAF_DATA;
...@@ -153,13 +153,13 @@ public final class LeafListener { ...@@ -153,13 +153,13 @@ public final class LeafListener {
153 */ 153 */
154 private static void validateSubStatementsCardinality(GeneratedYangParser.LeafStatementContext ctx) { 154 private static void validateSubStatementsCardinality(GeneratedYangParser.LeafStatementContext ctx) {
155 155
156 - validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_DATA, ctx.identifier().getText()); 156 + validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, LEAF_DATA, ctx.identifier().getText(), ctx);
157 - validateCardinality(ctx.unitsStatement(), UNITS_DATA, LEAF_DATA, ctx.identifier().getText()); 157 + validateCardinalityMaxOne(ctx.unitsStatement(), UNITS_DATA, LEAF_DATA, ctx.identifier().getText());
158 - validateCardinality(ctx.configStatement(), CONFIG_DATA, LEAF_DATA, ctx.identifier().getText()); 158 + validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, LEAF_DATA, ctx.identifier().getText());
159 - validateCardinality(ctx.mandatoryStatement(), MANDATORY_DATA, LEAF_DATA, ctx.identifier().getText()); 159 + validateCardinalityMaxOne(ctx.mandatoryStatement(), MANDATORY_DATA, LEAF_DATA, ctx.identifier().getText());
160 - validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_DATA, ctx.identifier().getText()); 160 + validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, LEAF_DATA, ctx.identifier().getText());
161 - validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, LEAF_DATA, ctx.identifier().getText()); 161 + validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, LEAF_DATA, ctx.identifier().getText());
162 - validateCardinality(ctx.statusStatement(), STATUS_DATA, LEAF_DATA, ctx.identifier().getText()); 162 + validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, LEAF_DATA, ctx.identifier().getText());
163 //TODO when. 163 //TODO when.
164 } 164 }
165 } 165 }
......
...@@ -38,8 +38,8 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp ...@@ -38,8 +38,8 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
38 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; 38 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
39 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; 39 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
40 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; 40 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
41 -import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinality; 41 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
42 -import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityNonNull; 42 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityNonZero;
43 import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA; 43 import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
44 import static org.onosproject.yangutils.utils.YangConstructType.DATA_DEF_DATA; 44 import static org.onosproject.yangutils.utils.YangConstructType.DATA_DEF_DATA;
45 import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA; 45 import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
...@@ -180,14 +180,14 @@ public final class ListListener { ...@@ -180,14 +180,14 @@ public final class ListListener {
180 */ 180 */
181 private static void validateSubStatementsCardinality(GeneratedYangParser.ListStatementContext ctx) { 181 private static void validateSubStatementsCardinality(GeneratedYangParser.ListStatementContext ctx) {
182 182
183 - validateCardinality(ctx.keyStatement(), KEY_DATA, LIST_DATA, ctx.identifier().getText()); 183 + validateCardinalityMaxOne(ctx.keyStatement(), KEY_DATA, LIST_DATA, ctx.identifier().getText());
184 - validateCardinality(ctx.configStatement(), CONFIG_DATA, LIST_DATA, ctx.identifier().getText()); 184 + validateCardinalityMaxOne(ctx.configStatement(), CONFIG_DATA, LIST_DATA, ctx.identifier().getText());
185 - validateCardinality(ctx.maxElementsStatement(), MAX_ELEMENT_DATA, LIST_DATA, ctx.identifier().getText()); 185 + validateCardinalityMaxOne(ctx.maxElementsStatement(), MAX_ELEMENT_DATA, LIST_DATA, ctx.identifier().getText());
186 - validateCardinality(ctx.minElementsStatement(), MIN_ELEMENT_DATA, LIST_DATA, ctx.identifier().getText()); 186 + validateCardinalityMaxOne(ctx.minElementsStatement(), MIN_ELEMENT_DATA, LIST_DATA, ctx.identifier().getText());
187 - validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, LIST_DATA, ctx.identifier().getText()); 187 + validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, LIST_DATA, ctx.identifier().getText());
188 - validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, LIST_DATA, ctx.identifier().getText()); 188 + validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, LIST_DATA, ctx.identifier().getText());
189 - validateCardinality(ctx.statusStatement(), STATUS_DATA, LIST_DATA, ctx.identifier().getText()); 189 + validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, LIST_DATA, ctx.identifier().getText());
190 - validateCardinalityNonNull(ctx.dataDefStatement(), DATA_DEF_DATA, LIST_DATA, ctx.identifier().getText()); 190 + validateCardinalityNonZero(ctx.dataDefStatement(), DATA_DEF_DATA, LIST_DATA, ctx.identifier().getText(), ctx);
191 //TODO when, typedef, grouping, unique 191 //TODO when, typedef, grouping, unique
192 } 192 }
193 } 193 }
......
...@@ -16,37 +16,6 @@ ...@@ -16,37 +16,6 @@
16 16
17 package org.onosproject.yangutils.parser.impl.listeners; 17 package org.onosproject.yangutils.parser.impl.listeners;
18 18
19 -/*
20 - * Reference: RFC6020 and YANG ANTLR Grammar
21 - *
22 - * body-stmts = *((extension-stmt /
23 - * feature-stmt /
24 - * identity-stmt /
25 - * typedef-stmt /
26 - * grouping-stmt /
27 - * data-def-stmt /
28 - * augment-stmt /
29 - * rpc-stmt /
30 - * notification-stmt /
31 - * deviation-stmt) stmtsep)
32 - *
33 - * typedef-stmt = typedef-keyword sep identifier-arg-str optsep
34 - * "{" stmtsep
35 - * ;; these stmts can appear in any order
36 - * type-stmt stmtsep
37 - * [units-stmt stmtsep]
38 - * [default-stmt stmtsep]
39 - * [status-stmt stmtsep]
40 - * [description-stmt stmtsep]
41 - * [reference-stmt stmtsep]
42 - * "}"
43 - *
44 - * ANTLR grammar rule
45 - * typedefStatement : TYPEDEF_KEYWORD identifier LEFT_CURLY_BRACE
46 - * (typeStatement | unitsStatement | defaultStatement | statusStatement
47 - * | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
48 - */
49 -
50 import org.onosproject.yangutils.datamodel.YangContainer; 19 import org.onosproject.yangutils.datamodel.YangContainer;
51 import org.onosproject.yangutils.datamodel.YangDataTypes; 20 import org.onosproject.yangutils.datamodel.YangDataTypes;
52 import org.onosproject.yangutils.datamodel.YangDerivedType; 21 import org.onosproject.yangutils.datamodel.YangDerivedType;
...@@ -73,8 +42,8 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp ...@@ -73,8 +42,8 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
73 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; 42 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
74 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; 43 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
75 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; 44 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
76 -import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinality;
77 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne; 45 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne;
46 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
78 import static org.onosproject.yangutils.utils.YangConstructType.DEFAULT_DATA; 47 import static org.onosproject.yangutils.utils.YangConstructType.DEFAULT_DATA;
79 import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA; 48 import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA;
80 import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA; 49 import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA;
...@@ -83,6 +52,37 @@ import static org.onosproject.yangutils.utils.YangConstructType.TYPEDEF_DATA; ...@@ -83,6 +52,37 @@ import static org.onosproject.yangutils.utils.YangConstructType.TYPEDEF_DATA;
83 import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA; 52 import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA;
84 import static org.onosproject.yangutils.utils.YangConstructType.UNITS_DATA; 53 import static org.onosproject.yangutils.utils.YangConstructType.UNITS_DATA;
85 54
55 +/*
56 + * Reference: RFC6020 and YANG ANTLR Grammar
57 + *
58 + * body-stmts = *((extension-stmt /
59 + * feature-stmt /
60 + * identity-stmt /
61 + * typedef-stmt /
62 + * grouping-stmt /
63 + * data-def-stmt /
64 + * augment-stmt /
65 + * rpc-stmt /
66 + * notification-stmt /
67 + * deviation-stmt) stmtsep)
68 + *
69 + * typedef-stmt = typedef-keyword sep identifier-arg-str optsep
70 + * "{" stmtsep
71 + * ;; these stmts can appear in any order
72 + * type-stmt stmtsep
73 + * [units-stmt stmtsep]
74 + * [default-stmt stmtsep]
75 + * [status-stmt stmtsep]
76 + * [description-stmt stmtsep]
77 + * [reference-stmt stmtsep]
78 + * "}"
79 + *
80 + * ANTLR grammar rule
81 + * typedefStatement : TYPEDEF_KEYWORD identifier LEFT_CURLY_BRACE
82 + * (typeStatement | unitsStatement | defaultStatement | statusStatement
83 + * | descriptionStatement | referenceStatement)* RIGHT_CURLY_BRACE;
84 + */
85 +
86 /** 86 /**
87 * Implements listener based call back function corresponding to the "typedef" 87 * Implements listener based call back function corresponding to the "typedef"
88 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. 88 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
...@@ -181,11 +181,12 @@ public final class TypeDefListener { ...@@ -181,11 +181,12 @@ public final class TypeDefListener {
181 */ 181 */
182 private static void validateSubStatementsCardinality(GeneratedYangParser.TypedefStatementContext ctx) { 182 private static void validateSubStatementsCardinality(GeneratedYangParser.TypedefStatementContext ctx) {
183 183
184 - validateCardinality(ctx.unitsStatement(), UNITS_DATA, TYPEDEF_DATA, ctx.identifier().getText()); 184 + validateCardinalityMaxOne(ctx.unitsStatement(), UNITS_DATA, TYPEDEF_DATA, ctx.identifier().getText());
185 - validateCardinality(ctx.defaultStatement(), DEFAULT_DATA, TYPEDEF_DATA, ctx.identifier().getText()); 185 + validateCardinalityMaxOne(ctx.defaultStatement(), DEFAULT_DATA, TYPEDEF_DATA, ctx.identifier().getText());
186 - validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, TYPEDEF_DATA, ctx.identifier().getText()); 186 + validateCardinalityEqualsOne(ctx.typeStatement(), TYPE_DATA, TYPEDEF_DATA, ctx.identifier().getText(), ctx);
187 - validateCardinality(ctx.descriptionStatement(), DESCRIPTION_DATA, TYPEDEF_DATA, ctx.identifier().getText()); 187 + validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, TYPEDEF_DATA,
188 - validateCardinality(ctx.referenceStatement(), REFERENCE_DATA, TYPEDEF_DATA, ctx.identifier().getText()); 188 + ctx.identifier().getText());
189 - validateCardinality(ctx.statusStatement(), STATUS_DATA, TYPEDEF_DATA, ctx.identifier().getText()); 189 + validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, TYPEDEF_DATA, ctx.identifier().getText());
190 + validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, TYPEDEF_DATA, ctx.identifier().getText());
190 } 191 }
191 } 192 }
......
...@@ -16,19 +16,20 @@ ...@@ -16,19 +16,20 @@
16 16
17 package org.onosproject.yangutils.parser.impl.parserutils; 17 package org.onosproject.yangutils.parser.impl.parserutils;
18 18
19 -import java.util.List; 19 +import org.antlr.v4.runtime.ParserRuleContext;
20 -
21 import org.onosproject.yangutils.datamodel.YangContainer; 20 import org.onosproject.yangutils.datamodel.YangContainer;
22 import org.onosproject.yangutils.datamodel.YangList; 21 import org.onosproject.yangutils.datamodel.YangList;
23 import org.onosproject.yangutils.datamodel.YangNode; 22 import org.onosproject.yangutils.datamodel.YangNode;
24 import org.onosproject.yangutils.parser.Parsable; 23 import org.onosproject.yangutils.parser.Parsable;
25 import org.onosproject.yangutils.parser.exceptions.ParserException; 24 import org.onosproject.yangutils.parser.exceptions.ParserException;
26 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 25 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
27 -import org.onosproject.yangutils.utils.YangConstructType;
28 -
29 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; 26 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
27 +import org.onosproject.yangutils.utils.YangConstructType;
30 import static org.onosproject.yangutils.utils.YangConstructType.getYangConstructType; 28 import static org.onosproject.yangutils.utils.YangConstructType.getYangConstructType;
31 29
30 +import java.util.Iterator;
31 +import java.util.List;
32 +
32 /** 33 /**
33 * It's a utility to carry out listener validation. 34 * It's a utility to carry out listener validation.
34 */ 35 */
...@@ -123,14 +124,18 @@ public final class ListenerValidation { ...@@ -123,14 +124,18 @@ public final class ListenerValidation {
123 * @param parentName parent name 124 * @param parentName parent name
124 * @throws ParserException exception if cardinality check fails 125 * @throws ParserException exception if cardinality check fails
125 */ 126 */
126 - public static void validateCardinality(List<?> childContext, YangConstructType yangChildConstruct, 127 + public static void validateCardinalityMaxOne(List<?> childContext, YangConstructType yangChildConstruct,
127 - YangConstructType yangParentConstruct, String parentName) 128 + YangConstructType yangParentConstruct, String parentName)
128 - throws ParserException { 129 + throws ParserException {
129 130
130 if (!childContext.isEmpty() && childContext.size() != 1) { 131 if (!childContext.isEmpty() && childContext.size() != 1) {
131 - ParserException parserException = new ParserException("YANG file error: Invalid cardinality of " 132 + ParserException parserException = new ParserException("YANG file error: \""
132 - + getYangConstructType(yangChildConstruct) + " in " + getYangConstructType(yangParentConstruct) 133 + + getYangConstructType(yangChildConstruct) + "\" is defined more than once in \""
133 - + " \"" + parentName + "\"."); 134 + + getYangConstructType(yangParentConstruct) + " " + parentName + "\".");
135 +
136 + Iterator<?> context = childContext.iterator();
137 + parserException.setLine(((ParserRuleContext) context.next()).getStart().getLine());
138 + parserException.setCharPosition(((ParserRuleContext) context.next()).getStart().getCharPositionInLine());
134 throw parserException; 139 throw parserException;
135 } 140 }
136 } 141 }
...@@ -140,18 +145,32 @@ public final class ListenerValidation { ...@@ -140,18 +145,32 @@ public final class ListenerValidation {
140 * 145 *
141 * @param childContext child's context 146 * @param childContext child's context
142 * @param yangChildConstruct child construct for whom cardinality is to be 147 * @param yangChildConstruct child construct for whom cardinality is to be
143 - * validated 148 + * validated
144 * @param yangParentConstruct parent construct 149 * @param yangParentConstruct parent construct
145 * @param parentName parent name 150 * @param parentName parent name
151 + * @param parentContext parents's context
146 * @throws ParserException exception if cardinality check fails 152 * @throws ParserException exception if cardinality check fails
147 */ 153 */
148 public static void validateCardinalityEqualsOne(List<?> childContext, YangConstructType yangChildConstruct, 154 public static void validateCardinalityEqualsOne(List<?> childContext, YangConstructType yangChildConstruct,
149 - YangConstructType yangParentConstruct, String parentName) throws ParserException { 155 + YangConstructType yangParentConstruct, String parentName,
156 + ParserRuleContext parentContext)
157 + throws ParserException {
150 158
151 - if (childContext.isEmpty() || childContext.size() != 1) { 159 + if (childContext.isEmpty()) {
152 - ParserException parserException = new ParserException("YANG file error: Invalid cardinality of " 160 + ParserException parserException = new ParserException("YANG file error: Missing \""
153 - + getYangConstructType(yangChildConstruct) + " in " + getYangConstructType(yangParentConstruct) 161 + + getYangConstructType(yangChildConstruct) + "\" in \"" + getYangConstructType(yangParentConstruct)
154 - + " \"" + parentName + "\"."); 162 + + " " + parentName + "\".");
163 + parserException.setLine(parentContext.getStart().getLine());
164 + parserException.setCharPosition(parentContext.getStart().getCharPositionInLine());
165 + throw parserException;
166 + } else if (!childContext.isEmpty() && childContext.size() != 1) {
167 + Iterator<?> childcontext = childContext.iterator();
168 + ParserException parserException = new ParserException("YANG file error: \""
169 + + getYangConstructType(yangChildConstruct) + "\" is present more than once in \""
170 + + getYangConstructType(yangParentConstruct) + " " + parentName + "\".");
171 + parserException.setLine(((ParserRuleContext) childcontext.next()).getStart().getLine());
172 + parserException.setCharPosition(((ParserRuleContext) childcontext.next()).getStart()
173 + .getCharPositionInLine());
155 throw parserException; 174 throw parserException;
156 } 175 }
157 } 176 }
...@@ -161,18 +180,54 @@ public final class ListenerValidation { ...@@ -161,18 +180,54 @@ public final class ListenerValidation {
161 * 180 *
162 * @param childContext child's context 181 * @param childContext child's context
163 * @param yangChildConstruct child construct for whom cardinality is to be 182 * @param yangChildConstruct child construct for whom cardinality is to be
164 - * validated 183 + * validated
165 * @param yangParentConstruct parent construct 184 * @param yangParentConstruct parent construct
166 * @param parentName parent name 185 * @param parentName parent name
186 + * @param parentContext parents's context
167 * @throws ParserException exception if cardinality check fails 187 * @throws ParserException exception if cardinality check fails
168 */ 188 */
169 - public static void validateCardinalityNonNull(List<?> childContext, YangConstructType yangChildConstruct, 189 + public static void validateCardinalityNonZero(List<?> childContext, YangConstructType yangChildConstruct,
170 - YangConstructType yangParentConstruct, String parentName) throws ParserException { 190 + YangConstructType yangParentConstruct, String parentName,
191 + ParserRuleContext parentContext)
192 + throws ParserException {
171 193
172 if (childContext.isEmpty()) { 194 if (childContext.isEmpty()) {
173 - ParserException parserException = new ParserException("YANG file error: Invalid cardinality of " 195 + ParserException parserException = new ParserException("YANG file error: Missing \""
174 - + getYangConstructType(yangChildConstruct) + " in " + getYangConstructType(yangParentConstruct) 196 + + getYangConstructType(yangChildConstruct) + "\" in \"" + getYangConstructType(yangParentConstruct)
175 - + " \"" + parentName + "\"."); 197 + + " " + parentName + "\".");
198 +
199 + parserException.setLine(parentContext.getStart().getLine());
200 + parserException.setCharPosition(parentContext.getStart().getCharPositionInLine());
201 + throw parserException;
202 + }
203 + }
204 +
205 + /**
206 + * Checks if a either of one construct occurrence.
207 + *
208 + * @param child1Context first optional child's context
209 + * @param yangChild1Construct first child construct for whom cardinality is
210 + * to be validated
211 + * @param child2Context second optional child's context
212 + * @param yangChild2Construct second child construct for whom cardinality is
213 + * to be validated
214 + * @param yangParentConstruct parent construct
215 + * @param parentName parent name
216 + * @throws ParserException exception if cardinality check fails
217 + */
218 + public static void validateMutuallyExclusiveChilds(List<?> child1Context, YangConstructType yangChild1Construct,
219 + List<?> child2Context, YangConstructType yangChild2Construct,
220 + YangConstructType yangParentConstruct, String parentName)
221 + throws ParserException {
222 +
223 + if (!child1Context.isEmpty() && !child2Context.isEmpty()) {
224 + ParserException parserException = new ParserException("YANG file error: \""
225 + + getYangConstructType(yangChild1Construct) + "\" & \"" + getYangConstructType(yangChild2Construct)
226 + + "\" should be mutually exclusive in \"" + getYangConstructType(yangParentConstruct) + " "
227 + + parentName + "\".");
228 +
229 + parserException.setLine(((ParserRuleContext) child2Context).getStart().getLine());
230 + parserException.setCharPosition(((ParserRuleContext) child2Context).getStart().getCharPositionInLine());
176 throw parserException; 231 throw parserException;
177 } 232 }
178 } 233 }
......
...@@ -206,7 +206,7 @@ public class ContainerListenerTest { ...@@ -206,7 +206,7 @@ public class ContainerListenerTest {
206 @Test 206 @Test
207 public void processContainerSubStatementCardinality() throws IOException, ParserException { 207 public void processContainerSubStatementCardinality() throws IOException, ParserException {
208 thrown.expect(ParserException.class); 208 thrown.expect(ParserException.class);
209 - thrown.expectMessage("YANG file error: Invalid cardinality of reference in container \"valid\"."); 209 + thrown.expectMessage("YANG file error: \"reference\" is defined more than once in \"container valid\".");
210 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementCardinality.yang"); 210 YangNode node = manager.getDataModel("src/test/resources/ContainerSubStatementCardinality.yang");
211 } 211 }
212 212
......
...@@ -120,7 +120,8 @@ public class LeafListListenerTest { ...@@ -120,7 +120,8 @@ public class LeafListListenerTest {
120 @Test 120 @Test
121 public void processLeafListConfigInvalidCardinality() throws IOException, ParserException { 121 public void processLeafListConfigInvalidCardinality() throws IOException, ParserException {
122 thrown.expect(ParserException.class); 122 thrown.expect(ParserException.class);
123 - thrown.expectMessage("YANG file error: Invalid cardinality of config in leaf-list \"invalid-interval\"."); 123 + thrown.expectMessage("YANG file error: \"config\" is defined more than once in \"leaf-list " +
124 + "invalid-interval\".");
124 YangNode node = manager.getDataModel("src/test/resources/LeafListConfigInvalidCardinality.yang"); 125 YangNode node = manager.getDataModel("src/test/resources/LeafListConfigInvalidCardinality.yang");
125 } 126 }
126 127
...@@ -131,7 +132,7 @@ public class LeafListListenerTest { ...@@ -131,7 +132,7 @@ public class LeafListListenerTest {
131 @Test 132 @Test
132 public void processLeafListUnitsInvalidCardinality() throws IOException, ParserException { 133 public void processLeafListUnitsInvalidCardinality() throws IOException, ParserException {
133 thrown.expect(ParserException.class); 134 thrown.expect(ParserException.class);
134 - thrown.expectMessage("YANG file error: Invalid cardinality of units in leaf-list \"invalid-interval\"."); 135 + thrown.expectMessage("YANG file error: \"units\" is defined more than once in \"leaf-list invalid-interval\"");
135 YangNode node = manager.getDataModel("src/test/resources/LeafListUnitsInvalidCardinality.yang"); 136 YangNode node = manager.getDataModel("src/test/resources/LeafListUnitsInvalidCardinality.yang");
136 } 137 }
137 138
......
...@@ -120,7 +120,7 @@ public class LeafListenerTest { ...@@ -120,7 +120,7 @@ public class LeafListenerTest {
120 @Test 120 @Test
121 public void processLeafConfigInvalidCardinality() throws IOException, ParserException { 121 public void processLeafConfigInvalidCardinality() throws IOException, ParserException {
122 thrown.expect(ParserException.class); 122 thrown.expect(ParserException.class);
123 - thrown.expectMessage("YANG file error: Invalid cardinality of config in leaf \"invalid-interval\"."); 123 + thrown.expectMessage("YANG file error: \"config\" is defined more than once in \"leaf invalid-interval\".");
124 YangNode node = manager.getDataModel("src/test/resources/LeafConfigInvalidCardinality.yang"); 124 YangNode node = manager.getDataModel("src/test/resources/LeafConfigInvalidCardinality.yang");
125 } 125 }
126 126
...@@ -131,7 +131,7 @@ public class LeafListenerTest { ...@@ -131,7 +131,7 @@ public class LeafListenerTest {
131 @Test 131 @Test
132 public void processLeafMandatoryInvalidCardinality() throws IOException, ParserException { 132 public void processLeafMandatoryInvalidCardinality() throws IOException, ParserException {
133 thrown.expect(ParserException.class); 133 thrown.expect(ParserException.class);
134 - thrown.expectMessage("YANG file error: Invalid cardinality of mandatory in leaf \"invalid-interval\"."); 134 + thrown.expectMessage("YANG file error: \"mandatory\" is defined more than once in \"leaf invalid-interval\".");
135 YangNode node = manager.getDataModel("src/test/resources/LeafMandatoryInvalidCardinality.yang"); 135 YangNode node = manager.getDataModel("src/test/resources/LeafMandatoryInvalidCardinality.yang");
136 } 136 }
137 137
......
...@@ -175,7 +175,7 @@ public class ListListenerTest { ...@@ -175,7 +175,7 @@ public class ListListenerTest {
175 @Test 175 @Test
176 public void processListSubStatementsCardinality() throws IOException, ParserException { 176 public void processListSubStatementsCardinality() throws IOException, ParserException {
177 thrown.expect(ParserException.class); 177 thrown.expect(ParserException.class);
178 - thrown.expectMessage("YANG file error: Invalid cardinality of reference in list \"valid\"."); 178 + thrown.expectMessage("YANG file error: \"reference\" is defined more than once in \"list valid\".");
179 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementsCardinality.yang"); 179 YangNode node = manager.getDataModel("src/test/resources/ListSubStatementsCardinality.yang");
180 } 180 }
181 181
...@@ -185,7 +185,7 @@ public class ListListenerTest { ...@@ -185,7 +185,7 @@ public class ListListenerTest {
185 @Test 185 @Test
186 public void processListStatementWithoutChild() throws IOException, ParserException { 186 public void processListStatementWithoutChild() throws IOException, ParserException {
187 thrown.expect(ParserException.class); 187 thrown.expect(ParserException.class);
188 - thrown.expectMessage("YANG file error: Invalid cardinality of data-def-substatements in list \"valid\"."); 188 + thrown.expectMessage("YANG file error: Missing \"data-def-substatements\" in \"list valid\".");
189 YangNode node = manager.getDataModel("src/test/resources/ListStatementWithoutChild.yang"); 189 YangNode node = manager.getDataModel("src/test/resources/ListStatementWithoutChild.yang");
190 } 190 }
191 191
......
...@@ -122,7 +122,8 @@ public class MaxElementsListenerTest { ...@@ -122,7 +122,8 @@ public class MaxElementsListenerTest {
122 @Test 122 @Test
123 public void processMaxElementsCardinality() throws IOException, ParserException { 123 public void processMaxElementsCardinality() throws IOException, ParserException {
124 thrown.expect(ParserException.class); 124 thrown.expect(ParserException.class);
125 - thrown.expectMessage("YANG file error: Invalid cardinality of max-elements in leaf-list \"invalid-interval\"."); 125 + thrown.expectMessage("YANG file error: \"max-elements\" is defined more than once in \"leaf-list " +
126 + "invalid-interval\".");
126 YangNode node = manager.getDataModel("src/test/resources/MaxElementsCardinality.yang"); 127 YangNode node = manager.getDataModel("src/test/resources/MaxElementsCardinality.yang");
127 } 128 }
128 129
......
...@@ -133,7 +133,8 @@ public class MinElementsListenerTest { ...@@ -133,7 +133,8 @@ public class MinElementsListenerTest {
133 @Test 133 @Test
134 public void processMinElementsInvalidCardinality() throws IOException, ParserException { 134 public void processMinElementsInvalidCardinality() throws IOException, ParserException {
135 thrown.expect(ParserException.class); 135 thrown.expect(ParserException.class);
136 - thrown.expectMessage("YANG file error: Invalid cardinality of min-elements in leaf-list \"invalid-interval\"."); 136 + thrown.expectMessage("YANG file error: \"min-elements\" is defined more than once in \"leaf-list " +
137 + "invalid-interval\".");
137 YangNode node = manager.getDataModel("src/test/resources/MinElementsInvalidCardinality.yang"); 138 YangNode node = manager.getDataModel("src/test/resources/MinElementsInvalidCardinality.yang");
138 } 139 }
139 140
......
...@@ -157,7 +157,7 @@ public class UnitsListenerTest { ...@@ -157,7 +157,7 @@ public class UnitsListenerTest {
157 @Test 157 @Test
158 public void processUnitsStatementCardinality() throws IOException, ParserException { 158 public void processUnitsStatementCardinality() throws IOException, ParserException {
159 thrown.expect(ParserException.class); 159 thrown.expect(ParserException.class);
160 - thrown.expectMessage("YANG file error: Invalid cardinality of units in leaf \"invalid-interval\"."); 160 + thrown.expectMessage("YANG file error: \"units\" is defined more than once in \"leaf invalid-interval\".");
161 YangNode node = manager.getDataModel("src/test/resources/UnitsStatementCardinality.yang"); 161 YangNode node = manager.getDataModel("src/test/resources/UnitsStatementCardinality.yang");
162 } 162 }
163 163
......