Committed by
Gerrit Code Review
[ONOS-3894, 4071] YANG Advanced Construct Union & Grouping
Change-Id: I0f828adb5884c2d7b6e4120f9843c416608ae5e7
Showing
39 changed files
with
1494 additions
and
43 deletions
... | @@ -17,11 +17,12 @@ package org.onosproject.yangutils.datamodel; | ... | @@ -17,11 +17,12 @@ package org.onosproject.yangutils.datamodel; |
17 | 17 | ||
18 | import java.util.LinkedList; | 18 | import java.util.LinkedList; |
19 | import java.util.List; | 19 | import java.util.List; |
20 | - | ||
21 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | 20 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
22 | import org.onosproject.yangutils.parser.Parsable; | 21 | import org.onosproject.yangutils.parser.Parsable; |
23 | import org.onosproject.yangutils.utils.YangConstructType; | 22 | import org.onosproject.yangutils.utils.YangConstructType; |
24 | 23 | ||
24 | +import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil; | ||
25 | + | ||
25 | /*- | 26 | /*- |
26 | * Reference RFC 6020. | 27 | * Reference RFC 6020. |
27 | * | 28 | * |
... | @@ -76,7 +77,7 @@ import org.onosproject.yangutils.utils.YangConstructType; | ... | @@ -76,7 +77,7 @@ import org.onosproject.yangutils.utils.YangConstructType; |
76 | * Data model node to maintain information defined in YANG grouping. | 77 | * Data model node to maintain information defined in YANG grouping. |
77 | */ | 78 | */ |
78 | public class YangGrouping extends YangNode | 79 | public class YangGrouping extends YangNode |
79 | - implements YangLeavesHolder, YangCommonInfo, Parsable { | 80 | + implements YangLeavesHolder, YangCommonInfo, Parsable, CollisionDetector { |
80 | 81 | ||
81 | /** | 82 | /** |
82 | * Name of the grouping. | 83 | * Name of the grouping. |
... | @@ -113,6 +114,8 @@ public class YangGrouping extends YangNode | ... | @@ -113,6 +114,8 @@ public class YangGrouping extends YangNode |
113 | */ | 114 | */ |
114 | public YangGrouping() { | 115 | public YangGrouping() { |
115 | super(YangNodeType.GROUPING_NODE); | 116 | super(YangNodeType.GROUPING_NODE); |
117 | + listOfLeaf = new LinkedList<YangLeaf>(); | ||
118 | + listOfLeafList = new LinkedList<YangLeafList>(); | ||
116 | } | 119 | } |
117 | 120 | ||
118 | /** | 121 | /** |
... | @@ -181,10 +184,6 @@ public class YangGrouping extends YangNode | ... | @@ -181,10 +184,6 @@ public class YangGrouping extends YangNode |
181 | */ | 184 | */ |
182 | @Override | 185 | @Override |
183 | public void addLeaf(YangLeaf leaf) { | 186 | public void addLeaf(YangLeaf leaf) { |
184 | - if (getListOfLeaf() == null) { | ||
185 | - setListOfLeaf(new LinkedList<YangLeaf>()); | ||
186 | - } | ||
187 | - | ||
188 | getListOfLeaf().add(leaf); | 187 | getListOfLeaf().add(leaf); |
189 | } | 188 | } |
190 | 189 | ||
... | @@ -214,10 +213,6 @@ public class YangGrouping extends YangNode | ... | @@ -214,10 +213,6 @@ public class YangGrouping extends YangNode |
214 | */ | 213 | */ |
215 | @Override | 214 | @Override |
216 | public void addLeafList(YangLeafList leafList) { | 215 | public void addLeafList(YangLeafList leafList) { |
217 | - if (getListOfLeafList() == null) { | ||
218 | - setListOfLeafList(new LinkedList<YangLeafList>()); | ||
219 | - } | ||
220 | - | ||
221 | getListOfLeafList().add(leafList); | 216 | getListOfLeafList().add(leafList); |
222 | } | 217 | } |
223 | 218 | ||
... | @@ -290,4 +285,31 @@ public class YangGrouping extends YangNode | ... | @@ -290,4 +285,31 @@ public class YangGrouping extends YangNode |
290 | public void validateDataOnExit() throws DataModelException { | 285 | public void validateDataOnExit() throws DataModelException { |
291 | // TODO auto-generated method stub, to be implemented by parser | 286 | // TODO auto-generated method stub, to be implemented by parser |
292 | } | 287 | } |
288 | + | ||
289 | + /* | ||
290 | + * Reference RFC6020 | ||
291 | + * | ||
292 | + * Once a grouping is defined, it can be referenced in a "uses" | ||
293 | + * statement (see Section 7.12). A grouping MUST NOT reference itself, | ||
294 | + * neither directly nor indirectly through a chain of other groupings. | ||
295 | + * | ||
296 | + * If the grouping is defined at the top level of a YANG module or | ||
297 | + * submodule, the grouping's identifier MUST be unique within the | ||
298 | + * module. | ||
299 | + */ | ||
300 | + @Override | ||
301 | + public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException { | ||
302 | + | ||
303 | + // Asks helper to detect colliding child. | ||
304 | + detectCollidingChildUtil(identifierName, dataType, this); | ||
305 | + } | ||
306 | + | ||
307 | + @Override | ||
308 | + public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException { | ||
309 | + if (getName().equals(identifierName)) { | ||
310 | + throw new DataModelException("YANG file error: Duplicate input identifier detected, same as grouping \"" + | ||
311 | + getName() + "\""); | ||
312 | + } | ||
313 | + } | ||
314 | + // TODO A grouping MUST NOT reference itself, neither directly nor indirectly through a chain of other groupings. | ||
293 | } | 315 | } | ... | ... |
utils/yangutils/src/main/java/org/onosproject/yangutils/datamodel/YangNodeIdentifier.java
0 → 100644
1 | +/* | ||
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 | + | ||
17 | +package org.onosproject.yangutils.datamodel; | ||
18 | + | ||
19 | +/** | ||
20 | + * YANG node identifier which is a combination of prefix and name. | ||
21 | + */ | ||
22 | +public class YangNodeIdentifier { | ||
23 | + | ||
24 | + // Name of the node. | ||
25 | + String name; | ||
26 | + | ||
27 | + // Prefix of the node. | ||
28 | + String prefix; | ||
29 | + | ||
30 | + /** | ||
31 | + * Creates an instance of YANG node identifier. | ||
32 | + */ | ||
33 | + public YangNodeIdentifier() { | ||
34 | + } | ||
35 | + | ||
36 | + /** | ||
37 | + * Returns name of the node identifier. | ||
38 | + * | ||
39 | + * @return name of the node identifier | ||
40 | + */ | ||
41 | + public String getName() { | ||
42 | + return name; | ||
43 | + } | ||
44 | + | ||
45 | + /** | ||
46 | + * Set name of the node identifier. | ||
47 | + * | ||
48 | + * @param name node identifier name | ||
49 | + */ | ||
50 | + public void setName(String name) { | ||
51 | + this.name = name; | ||
52 | + } | ||
53 | + | ||
54 | + /** | ||
55 | + * Returns prefix of the node identifier. | ||
56 | + * | ||
57 | + * @return prefix of the node identifier | ||
58 | + */ | ||
59 | + public String getPrefix() { | ||
60 | + return prefix; | ||
61 | + } | ||
62 | + | ||
63 | + /** | ||
64 | + * Set prefix of the node identifier. | ||
65 | + * | ||
66 | + * @param prefix prefix of the node identifier | ||
67 | + */ | ||
68 | + public void setPrefix(String prefix) { | ||
69 | + this.prefix = prefix; | ||
70 | + } | ||
71 | +} |
... | @@ -20,6 +20,7 @@ import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | ... | @@ -20,6 +20,7 @@ import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
20 | import org.onosproject.yangutils.parser.Parsable; | 20 | import org.onosproject.yangutils.parser.Parsable; |
21 | import org.onosproject.yangutils.utils.YangConstructType; | 21 | import org.onosproject.yangutils.utils.YangConstructType; |
22 | 22 | ||
23 | +import java.util.LinkedList; | ||
23 | import java.util.List; | 24 | import java.util.List; |
24 | 25 | ||
25 | /* | 26 | /* |
... | @@ -52,13 +53,14 @@ public class YangUnion implements Parsable { | ... | @@ -52,13 +53,14 @@ public class YangUnion implements Parsable { |
52 | // List of YANG type. | 53 | // List of YANG type. |
53 | private List<YangType<?>> typeList; | 54 | private List<YangType<?>> typeList; |
54 | 55 | ||
55 | - // Name of the union. | 56 | + // Name of union. |
56 | private String unionName; | 57 | private String unionName; |
57 | 58 | ||
58 | /** | 59 | /** |
59 | * Create a YANG union node. | 60 | * Create a YANG union node. |
60 | */ | 61 | */ |
61 | public YangUnion() { | 62 | public YangUnion() { |
63 | + typeList = new LinkedList<>(); | ||
62 | } | 64 | } |
63 | 65 | ||
64 | /** | 66 | /** |
... | @@ -89,6 +91,21 @@ public class YangUnion implements Parsable { | ... | @@ -89,6 +91,21 @@ public class YangUnion implements Parsable { |
89 | } | 91 | } |
90 | 92 | ||
91 | /** | 93 | /** |
94 | + * Add YANG type to type list. | ||
95 | + * | ||
96 | + * @param yangType YANG type to be added to list | ||
97 | + * @throws DataModelException union member type must not be one of the | ||
98 | + * built-in types "empty" or "leafref" | ||
99 | + */ | ||
100 | + public void addToTypeList(YangType<?> yangType) throws DataModelException { | ||
101 | + if (yangType.getDataType() == YangDataTypes.EMPTY || yangType.getDataType() == YangDataTypes.LEAFREF) { | ||
102 | + throw new DataModelException("Union member type must not be one of the built-in types \"empty\" or " + | ||
103 | + "\"leafref\""); | ||
104 | + } | ||
105 | + getTypeList().add(yangType); | ||
106 | + } | ||
107 | + | ||
108 | + /** | ||
92 | * Set the union name. | 109 | * Set the union name. |
93 | * | 110 | * |
94 | * @param unionName name of the union. | 111 | * @param unionName name of the union. | ... | ... |
... | @@ -55,17 +55,17 @@ import org.onosproject.yangutils.utils.YangConstructType; | ... | @@ -55,17 +55,17 @@ import org.onosproject.yangutils.utils.YangConstructType; |
55 | public class YangUses extends YangNode implements YangCommonInfo, Parsable { | 55 | public class YangUses extends YangNode implements YangCommonInfo, Parsable { |
56 | 56 | ||
57 | /** | 57 | /** |
58 | - * Name. | 58 | + * Name of YANG uses. |
59 | */ | 59 | */ |
60 | private String name; | 60 | private String name; |
61 | 61 | ||
62 | /** | 62 | /** |
63 | - * referred group. | 63 | + * Referred group. |
64 | */ | 64 | */ |
65 | private YangGrouping refGroup; | 65 | private YangGrouping refGroup; |
66 | 66 | ||
67 | /** | 67 | /** |
68 | - * description. | 68 | + * Description of YANG uses. |
69 | */ | 69 | */ |
70 | private String description; | 70 | private String description; |
71 | 71 | ||
... | @@ -75,7 +75,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable { | ... | @@ -75,7 +75,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable { |
75 | private String reference; | 75 | private String reference; |
76 | 76 | ||
77 | /** | 77 | /** |
78 | - * Status. | 78 | + * Status of YANG uses. |
79 | */ | 79 | */ |
80 | private YangStatusType status; | 80 | private YangStatusType status; |
81 | 81 | ||
... | @@ -87,7 +87,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable { | ... | @@ -87,7 +87,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable { |
87 | } | 87 | } |
88 | 88 | ||
89 | /** | 89 | /** |
90 | - * Get the name. | 90 | + * Returns the name. |
91 | * | 91 | * |
92 | * @return the name | 92 | * @return the name |
93 | */ | 93 | */ |
... | @@ -105,7 +105,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable { | ... | @@ -105,7 +105,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable { |
105 | } | 105 | } |
106 | 106 | ||
107 | /** | 107 | /** |
108 | - * Get the referred group. | 108 | + * Returns the referred group. |
109 | * | 109 | * |
110 | * @return the referred group | 110 | * @return the referred group |
111 | */ | 111 | */ |
... | @@ -123,7 +123,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable { | ... | @@ -123,7 +123,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable { |
123 | } | 123 | } |
124 | 124 | ||
125 | /** | 125 | /** |
126 | - * Get the description. | 126 | + * Returns the description. |
127 | * | 127 | * |
128 | * @return the description | 128 | * @return the description |
129 | */ | 129 | */ |
... | @@ -143,7 +143,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable { | ... | @@ -143,7 +143,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable { |
143 | } | 143 | } |
144 | 144 | ||
145 | /** | 145 | /** |
146 | - * Get the textual reference. | 146 | + * Returns the textual reference. |
147 | * | 147 | * |
148 | * @return the reference | 148 | * @return the reference |
149 | */ | 149 | */ |
... | @@ -163,7 +163,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable { | ... | @@ -163,7 +163,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable { |
163 | } | 163 | } |
164 | 164 | ||
165 | /** | 165 | /** |
166 | - * Get the status. | 166 | + * Returns the status. |
167 | * | 167 | * |
168 | * @return the status | 168 | * @return the status |
169 | */ | 169 | */ |
... | @@ -212,26 +212,14 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable { | ... | @@ -212,26 +212,14 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable { |
212 | // TODO auto-generated method stub, to be implemented by parser | 212 | // TODO auto-generated method stub, to be implemented by parser |
213 | } | 213 | } |
214 | 214 | ||
215 | - /** | ||
216 | - * Get uses name. | ||
217 | - * | ||
218 | - * @return uses name | ||
219 | - */ | ||
220 | @Override | 215 | @Override |
221 | public String getName() { | 216 | public String getName() { |
222 | - // TODO Auto-generated method stub | 217 | + return name; |
223 | - return null; | ||
224 | } | 218 | } |
225 | 219 | ||
226 | - /** | ||
227 | - * Set uses name. | ||
228 | - * | ||
229 | - * @param name uses name | ||
230 | - */ | ||
231 | @Override | 220 | @Override |
232 | public void setName(String name) { | 221 | public void setName(String name) { |
233 | - // TODO Auto-generated method stub | 222 | + this.name = name; |
234 | - | ||
235 | } | 223 | } |
236 | 224 | ||
237 | } | 225 | } | ... | ... |
... | @@ -38,6 +38,7 @@ import org.onosproject.yangutils.parser.impl.listeners.DefaultListener; | ... | @@ -38,6 +38,7 @@ import org.onosproject.yangutils.parser.impl.listeners.DefaultListener; |
38 | import org.onosproject.yangutils.parser.impl.listeners.DescriptionListener; | 38 | import org.onosproject.yangutils.parser.impl.listeners.DescriptionListener; |
39 | import org.onosproject.yangutils.parser.impl.listeners.EnumListener; | 39 | import org.onosproject.yangutils.parser.impl.listeners.EnumListener; |
40 | import org.onosproject.yangutils.parser.impl.listeners.EnumerationListener; | 40 | import org.onosproject.yangutils.parser.impl.listeners.EnumerationListener; |
41 | +import org.onosproject.yangutils.parser.impl.listeners.GroupingListener; | ||
41 | import org.onosproject.yangutils.parser.impl.listeners.ImportListener; | 42 | import org.onosproject.yangutils.parser.impl.listeners.ImportListener; |
42 | import org.onosproject.yangutils.parser.impl.listeners.IncludeListener; | 43 | import org.onosproject.yangutils.parser.impl.listeners.IncludeListener; |
43 | import org.onosproject.yangutils.parser.impl.listeners.KeyListener; | 44 | import org.onosproject.yangutils.parser.impl.listeners.KeyListener; |
... | @@ -61,7 +62,9 @@ import org.onosproject.yangutils.parser.impl.listeners.StatusListener; | ... | @@ -61,7 +62,9 @@ import org.onosproject.yangutils.parser.impl.listeners.StatusListener; |
61 | import org.onosproject.yangutils.parser.impl.listeners.SubModuleListener; | 62 | import org.onosproject.yangutils.parser.impl.listeners.SubModuleListener; |
62 | import org.onosproject.yangutils.parser.impl.listeners.TypeDefListener; | 63 | import org.onosproject.yangutils.parser.impl.listeners.TypeDefListener; |
63 | import org.onosproject.yangutils.parser.impl.listeners.TypeListener; | 64 | import org.onosproject.yangutils.parser.impl.listeners.TypeListener; |
65 | +import org.onosproject.yangutils.parser.impl.listeners.UnionListener; | ||
64 | import org.onosproject.yangutils.parser.impl.listeners.UnitsListener; | 66 | import org.onosproject.yangutils.parser.impl.listeners.UnitsListener; |
67 | +import org.onosproject.yangutils.parser.impl.listeners.UsesListener; | ||
65 | import org.onosproject.yangutils.parser.impl.listeners.ValueListener; | 68 | import org.onosproject.yangutils.parser.impl.listeners.ValueListener; |
66 | import org.onosproject.yangutils.parser.impl.listeners.VersionListener; | 69 | import org.onosproject.yangutils.parser.impl.listeners.VersionListener; |
67 | 70 | ||
... | @@ -687,12 +690,12 @@ public class TreeWalkListener implements GeneratedYangListener { | ... | @@ -687,12 +690,12 @@ public class TreeWalkListener implements GeneratedYangListener { |
687 | 690 | ||
688 | @Override | 691 | @Override |
689 | public void enterUnionSpecification(GeneratedYangParser.UnionSpecificationContext ctx) { | 692 | public void enterUnionSpecification(GeneratedYangParser.UnionSpecificationContext ctx) { |
690 | - // TODO: implement the method. | 693 | + UnionListener.processUnionEntry(this, ctx); |
691 | } | 694 | } |
692 | 695 | ||
693 | @Override | 696 | @Override |
694 | public void exitUnionSpecification(GeneratedYangParser.UnionSpecificationContext ctx) { | 697 | public void exitUnionSpecification(GeneratedYangParser.UnionSpecificationContext ctx) { |
695 | - // TODO: implement the method. | 698 | + UnionListener.processUnionExit(this, ctx); |
696 | } | 699 | } |
697 | 700 | ||
698 | @Override | 701 | @Override |
... | @@ -847,12 +850,12 @@ public class TreeWalkListener implements GeneratedYangListener { | ... | @@ -847,12 +850,12 @@ public class TreeWalkListener implements GeneratedYangListener { |
847 | 850 | ||
848 | @Override | 851 | @Override |
849 | public void enterGroupingStatement(GeneratedYangParser.GroupingStatementContext ctx) { | 852 | public void enterGroupingStatement(GeneratedYangParser.GroupingStatementContext ctx) { |
850 | - // TODO: implement the method. | 853 | + GroupingListener.processGroupingEntry(this, ctx); |
851 | } | 854 | } |
852 | 855 | ||
853 | @Override | 856 | @Override |
854 | public void exitGroupingStatement(GeneratedYangParser.GroupingStatementContext ctx) { | 857 | public void exitGroupingStatement(GeneratedYangParser.GroupingStatementContext ctx) { |
855 | - // TODO: implement the method. | 858 | + GroupingListener.processGroupingExit(this, ctx); |
856 | } | 859 | } |
857 | 860 | ||
858 | @Override | 861 | @Override |
... | @@ -947,12 +950,12 @@ public class TreeWalkListener implements GeneratedYangListener { | ... | @@ -947,12 +950,12 @@ public class TreeWalkListener implements GeneratedYangListener { |
947 | 950 | ||
948 | @Override | 951 | @Override |
949 | public void enterUsesStatement(GeneratedYangParser.UsesStatementContext ctx) { | 952 | public void enterUsesStatement(GeneratedYangParser.UsesStatementContext ctx) { |
950 | - // TODO: implement the method. | 953 | + UsesListener.processUsesEntry(this, ctx); |
951 | } | 954 | } |
952 | 955 | ||
953 | @Override | 956 | @Override |
954 | public void exitUsesStatement(GeneratedYangParser.UsesStatementContext ctx) { | 957 | public void exitUsesStatement(GeneratedYangParser.UsesStatementContext ctx) { |
955 | - // TODO: implement the method. | 958 | + UsesListener.processUsesExit(this, ctx); |
956 | } | 959 | } |
957 | 960 | ||
958 | @Override | 961 | @Override | ... | ... |
utils/yangutils/src/main/java/org/onosproject/yangutils/parser/impl/listeners/ContainerListener.java
... | @@ -16,6 +16,7 @@ | ... | @@ -16,6 +16,7 @@ |
16 | 16 | ||
17 | package org.onosproject.yangutils.parser.impl.listeners; | 17 | package org.onosproject.yangutils.parser.impl.listeners; |
18 | 18 | ||
19 | +import org.onosproject.yangutils.datamodel.YangCase; | ||
19 | import org.onosproject.yangutils.datamodel.YangContainer; | 20 | import org.onosproject.yangutils.datamodel.YangContainer; |
20 | import org.onosproject.yangutils.datamodel.YangList; | 21 | import org.onosproject.yangutils.datamodel.YangList; |
21 | import org.onosproject.yangutils.datamodel.YangModule; | 22 | import org.onosproject.yangutils.datamodel.YangModule; |
... | @@ -126,7 +127,7 @@ public final class ContainerListener { | ... | @@ -126,7 +127,7 @@ public final class ContainerListener { |
126 | 127 | ||
127 | Parsable curData = listener.getParsedDataStack().peek(); | 128 | Parsable curData = listener.getParsedDataStack().peek(); |
128 | if (curData instanceof YangModule || curData instanceof YangContainer | 129 | if (curData instanceof YangModule || curData instanceof YangContainer |
129 | - || curData instanceof YangList) { | 130 | + || curData instanceof YangList || curData instanceof YangCase) { |
130 | YangNode curNode = (YangNode) curData; | 131 | YangNode curNode = (YangNode) curData; |
131 | try { | 132 | try { |
132 | curNode.addChild(container); | 133 | curNode.addChild(container); | ... | ... |
... | @@ -45,6 +45,7 @@ import org.onosproject.yangutils.datamodel.YangEnumeration; | ... | @@ -45,6 +45,7 @@ import org.onosproject.yangutils.datamodel.YangEnumeration; |
45 | import org.onosproject.yangutils.datamodel.YangLeaf; | 45 | import org.onosproject.yangutils.datamodel.YangLeaf; |
46 | import org.onosproject.yangutils.datamodel.YangLeafList; | 46 | import org.onosproject.yangutils.datamodel.YangLeafList; |
47 | import org.onosproject.yangutils.datamodel.YangType; | 47 | import org.onosproject.yangutils.datamodel.YangType; |
48 | +import org.onosproject.yangutils.datamodel.YangUnion; | ||
48 | import org.onosproject.yangutils.parser.Parsable; | 49 | import org.onosproject.yangutils.parser.Parsable; |
49 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | 50 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
50 | import org.onosproject.yangutils.parser.exceptions.ParserException; | 51 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
... | @@ -102,7 +103,10 @@ public final class EnumerationListener { | ... | @@ -102,7 +103,10 @@ public final class EnumerationListener { |
102 | case LEAF_LIST_DATA: | 103 | case LEAF_LIST_DATA: |
103 | enumerationNode.setEnumerationName(((YangLeafList) tmpData).getLeafName()); | 104 | enumerationNode.setEnumerationName(((YangLeafList) tmpData).getLeafName()); |
104 | break; | 105 | break; |
105 | - // TODO typedef, union, deviate. | 106 | + case UNION_DATA: |
107 | + enumerationNode.setEnumerationName(((YangUnion) tmpData).getUnionName()); | ||
108 | + break; | ||
109 | + // TODO typedef, deviate. | ||
106 | default: | 110 | default: |
107 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, | 111 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, |
108 | ((YangType<?>) typeData).getDataTypeName(), ENTRY)); | 112 | ((YangType<?>) typeData).getDataTypeName(), ENTRY)); | ... | ... |
utils/yangutils/src/main/java/org/onosproject/yangutils/parser/impl/listeners/GroupingListener.java
0 → 100644
1 | +/* | ||
2 | + * Copyright 2014-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 | + | ||
17 | +package org.onosproject.yangutils.parser.impl.listeners; | ||
18 | + | ||
19 | +import org.onosproject.yangutils.datamodel.YangContainer; | ||
20 | +import org.onosproject.yangutils.datamodel.YangGrouping; | ||
21 | +import org.onosproject.yangutils.datamodel.YangInput; | ||
22 | +import org.onosproject.yangutils.datamodel.YangList; | ||
23 | +import org.onosproject.yangutils.datamodel.YangModule; | ||
24 | +import org.onosproject.yangutils.datamodel.YangNode; | ||
25 | +import org.onosproject.yangutils.datamodel.YangNotification; | ||
26 | +import org.onosproject.yangutils.datamodel.YangOutput; | ||
27 | +import org.onosproject.yangutils.datamodel.YangRpc; | ||
28 | +import org.onosproject.yangutils.datamodel.YangSubModule; | ||
29 | +import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | ||
30 | +import org.onosproject.yangutils.parser.Parsable; | ||
31 | +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | ||
32 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
33 | +import org.onosproject.yangutils.parser.impl.TreeWalkListener; | ||
34 | + | ||
35 | +import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; | ||
36 | +import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangGroupingNode; | ||
37 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil; | ||
38 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; | ||
39 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; | ||
40 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; | ||
41 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; | ||
42 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; | ||
43 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; | ||
44 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; | ||
45 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; | ||
46 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier; | ||
47 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; | ||
48 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne; | ||
49 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds; | ||
50 | +import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA; | ||
51 | +import static org.onosproject.yangutils.utils.YangConstructType.GROUPING_DATA; | ||
52 | +import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA; | ||
53 | +import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA; | ||
54 | +import static org.onosproject.yangutils.utils.YangConstructType.TYPEDEF_DATA; | ||
55 | + | ||
56 | + | ||
57 | +/* | ||
58 | + * Reference: RFC6020 and YANG ANTLR Grammar | ||
59 | + * | ||
60 | + * ABNF grammar as per RFC6020 | ||
61 | + * grouping-stmt = grouping-keyword sep identifier-arg-str optsep | ||
62 | + * (";" / | ||
63 | + * "{" stmtsep | ||
64 | + * ;; these stmts can appear in any order | ||
65 | + * [status-stmt stmtsep] | ||
66 | + * [description-stmt stmtsep] | ||
67 | + * [reference-stmt stmtsep] | ||
68 | + * *((typedef-stmt / | ||
69 | + * grouping-stmt) stmtsep) | ||
70 | + * *(data-def-stmt stmtsep) | ||
71 | + * "}") | ||
72 | + * | ||
73 | + * ANTLR grammar rule | ||
74 | + * groupingStatement : GROUPING_KEYWORD identifier (STMTEND | LEFT_CURLY_BRACE | ||
75 | + * (statusStatement | descriptionStatement | referenceStatement | typedefStatement | groupingStatement | ||
76 | + * | dataDefStatement)* RIGHT_CURLY_BRACE); | ||
77 | + */ | ||
78 | + | ||
79 | +/** | ||
80 | + * Implements listener based call back function corresponding to the "grouping" | ||
81 | + * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. | ||
82 | + */ | ||
83 | +public final class GroupingListener { | ||
84 | + | ||
85 | + /** | ||
86 | + * Creates a new grouping listener. | ||
87 | + */ | ||
88 | + private GroupingListener() { | ||
89 | + } | ||
90 | + | ||
91 | + /** | ||
92 | + * It is called when parser enters grammar rule (grouping), it perform | ||
93 | + * validations and updates the data model tree. | ||
94 | + * | ||
95 | + * @param listener listener's object | ||
96 | + * @param ctx context object of the grammar rule | ||
97 | + */ | ||
98 | + public static void processGroupingEntry(TreeWalkListener listener, | ||
99 | + GeneratedYangParser.GroupingStatementContext ctx) { | ||
100 | + | ||
101 | + // Check for stack to be non empty. | ||
102 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, GROUPING_DATA, ctx.identifier().getText(), ENTRY); | ||
103 | + | ||
104 | + // Check validity of identifier and remove double quotes. | ||
105 | + String identifier = getValidIdentifier(ctx.identifier().getText(), GROUPING_DATA, ctx); | ||
106 | + | ||
107 | + // Validate sub statement cardinality. | ||
108 | + validateSubStatementsCardinality(ctx); | ||
109 | + | ||
110 | + Parsable curData = listener.getParsedDataStack().peek(); | ||
111 | + | ||
112 | + // Check for identifier collision | ||
113 | + int line = ctx.getStart().getLine(); | ||
114 | + int charPositionInLine = ctx.getStart().getCharPositionInLine(); | ||
115 | + detectCollidingChildUtil(listener, line, charPositionInLine, identifier, GROUPING_DATA); | ||
116 | + | ||
117 | + if (curData instanceof YangModule || curData instanceof YangSubModule | ||
118 | + || curData instanceof YangContainer || curData instanceof YangNotification | ||
119 | + || curData instanceof YangList || curData instanceof YangGrouping | ||
120 | + || curData instanceof YangRpc || curData instanceof YangInput | ||
121 | + || curData instanceof YangOutput) { | ||
122 | + | ||
123 | + YangGrouping groupingNode = getYangGroupingNode(JAVA_GENERATION); | ||
124 | + groupingNode.setName(identifier); | ||
125 | + | ||
126 | + YangNode curNode = (YangNode) curData; | ||
127 | + try { | ||
128 | + curNode.addChild(groupingNode); | ||
129 | + } catch (DataModelException e) { | ||
130 | + throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, | ||
131 | + GROUPING_DATA, ctx.identifier().getText(), ENTRY, e.getMessage())); | ||
132 | + } | ||
133 | + listener.getParsedDataStack().push(groupingNode); | ||
134 | + } else { | ||
135 | + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, | ||
136 | + GROUPING_DATA, ctx.identifier().getText(), ENTRY)); | ||
137 | + } | ||
138 | + } | ||
139 | + | ||
140 | + /** | ||
141 | + * It is called when parser exits from grammar rule (grouping), it perform | ||
142 | + * validations and update the data model tree. | ||
143 | + * | ||
144 | + * @param listener Listener's object | ||
145 | + * @param ctx context object of the grammar rule | ||
146 | + */ | ||
147 | + public static void processGroupingExit(TreeWalkListener listener, | ||
148 | + GeneratedYangParser.GroupingStatementContext ctx) { | ||
149 | + | ||
150 | + // Check for stack to be non empty. | ||
151 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, GROUPING_DATA, ctx.identifier().getText(), EXIT); | ||
152 | + | ||
153 | + if (listener.getParsedDataStack().peek() instanceof YangGrouping) { | ||
154 | + listener.getParsedDataStack().pop(); | ||
155 | + } else { | ||
156 | + throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, GROUPING_DATA, | ||
157 | + ctx.identifier().getText(), EXIT)); | ||
158 | + } | ||
159 | + } | ||
160 | + | ||
161 | + /** | ||
162 | + * Validates the cardinality of case sub-statements as per grammar. | ||
163 | + * | ||
164 | + * @param ctx context object of the grammar rule | ||
165 | + */ | ||
166 | + private static void validateSubStatementsCardinality(GeneratedYangParser.GroupingStatementContext ctx) { | ||
167 | + | ||
168 | + validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, GROUPING_DATA, ctx.identifier().getText()); | ||
169 | + validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, GROUPING_DATA, | ||
170 | + ctx.identifier().getText()); | ||
171 | + validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, GROUPING_DATA, ctx.identifier().getText()); | ||
172 | + validateMutuallyExclusiveChilds(ctx.typedefStatement(), TYPEDEF_DATA, ctx.groupingStatement(), GROUPING_DATA, | ||
173 | + GROUPING_DATA, ctx.identifier().getText()); | ||
174 | + } | ||
175 | +} |
... | @@ -16,6 +16,7 @@ | ... | @@ -16,6 +16,7 @@ |
16 | 16 | ||
17 | package org.onosproject.yangutils.parser.impl.listeners; | 17 | package org.onosproject.yangutils.parser.impl.listeners; |
18 | 18 | ||
19 | +import org.onosproject.yangutils.datamodel.YangCase; | ||
19 | import org.onosproject.yangutils.datamodel.YangContainer; | 20 | import org.onosproject.yangutils.datamodel.YangContainer; |
20 | import org.onosproject.yangutils.datamodel.YangList; | 21 | import org.onosproject.yangutils.datamodel.YangList; |
21 | import org.onosproject.yangutils.datamodel.YangModule; | 22 | import org.onosproject.yangutils.datamodel.YangModule; |
... | @@ -133,7 +134,7 @@ public final class ListListener { | ... | @@ -133,7 +134,7 @@ public final class ListListener { |
133 | 134 | ||
134 | Parsable curData = listener.getParsedDataStack().peek(); | 135 | Parsable curData = listener.getParsedDataStack().peek(); |
135 | if (curData instanceof YangModule || curData instanceof YangContainer | 136 | if (curData instanceof YangModule || curData instanceof YangContainer |
136 | - || curData instanceof YangList) { | 137 | + || curData instanceof YangList || curData instanceof YangCase) { |
137 | curNode = (YangNode) curData; | 138 | curNode = (YangNode) curData; |
138 | try { | 139 | try { |
139 | curNode.addChild(yangList); | 140 | curNode.addChild(yangList); | ... | ... |
... | @@ -22,6 +22,8 @@ import org.onosproject.yangutils.datamodel.YangLeaf; | ... | @@ -22,6 +22,8 @@ import org.onosproject.yangutils.datamodel.YangLeaf; |
22 | import org.onosproject.yangutils.datamodel.YangLeafList; | 22 | import org.onosproject.yangutils.datamodel.YangLeafList; |
23 | import org.onosproject.yangutils.datamodel.YangType; | 23 | import org.onosproject.yangutils.datamodel.YangType; |
24 | import org.onosproject.yangutils.datamodel.YangTypeDef; | 24 | import org.onosproject.yangutils.datamodel.YangTypeDef; |
25 | +import org.onosproject.yangutils.datamodel.YangUnion; | ||
26 | +import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | ||
25 | import org.onosproject.yangutils.parser.Parsable; | 27 | import org.onosproject.yangutils.parser.Parsable; |
26 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | 28 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
27 | import org.onosproject.yangutils.parser.exceptions.ParserException; | 29 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
... | @@ -116,6 +118,17 @@ public final class TypeListener { | ... | @@ -116,6 +118,17 @@ public final class TypeListener { |
116 | YangLeafList leafList = (YangLeafList) tmpData; | 118 | YangLeafList leafList = (YangLeafList) tmpData; |
117 | leafList.setDataType((YangType<?>) type); | 119 | leafList.setDataType((YangType<?>) type); |
118 | break; | 120 | break; |
121 | + case UNION_DATA: | ||
122 | + YangUnion unionNode = (YangUnion) tmpData; | ||
123 | + try { | ||
124 | + unionNode.addToTypeList((YangType<?>) type); | ||
125 | + } catch (DataModelException e) { | ||
126 | + ParserException parserException = new ParserException(e.getMessage()); | ||
127 | + parserException.setLine(ctx.getStart().getLine()); | ||
128 | + parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); | ||
129 | + throw parserException; | ||
130 | + } | ||
131 | + break; | ||
119 | case TYPEDEF_DATA: | 132 | case TYPEDEF_DATA: |
120 | 133 | ||
121 | /* Prepare the base type info and set in derived type */ | 134 | /* Prepare the base type info and set in derived type */ | ... | ... |
utils/yangutils/src/main/java/org/onosproject/yangutils/parser/impl/listeners/UnionListener.java
0 → 100644
1 | +/* | ||
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 | + | ||
17 | +package org.onosproject.yangutils.parser.impl.listeners; | ||
18 | + | ||
19 | +/* | ||
20 | + * Reference: RFC6020 and YANG ANTLR Grammar | ||
21 | + * | ||
22 | + * ABNF grammar as per RFC6020 | ||
23 | + * type-body-stmts = numerical-restrictions / | ||
24 | + * decimal64-specification / | ||
25 | + * string-restrictions / | ||
26 | + * enum-specification / | ||
27 | + * leafref-specification / | ||
28 | + * identityref-specification / | ||
29 | + * instance-identifier-specification / | ||
30 | + * bits-specification / | ||
31 | + * union-specification | ||
32 | + * | ||
33 | + * union-specification = 1*(type-stmt stmtsep) | ||
34 | + * | ||
35 | + * ANTLR grammar rule | ||
36 | + * typeBodyStatements : numericalRestrictions | stringRestrictions | enumSpecification | ||
37 | + * | leafrefSpecification | identityrefSpecification | instanceIdentifierSpecification | ||
38 | + * | bitsSpecification | unionSpecification; | ||
39 | + * | ||
40 | + * unionSpecification : typeStatement+; | ||
41 | + */ | ||
42 | + | ||
43 | +import org.onosproject.yangutils.datamodel.YangLeaf; | ||
44 | +import org.onosproject.yangutils.datamodel.YangLeafList; | ||
45 | +import org.onosproject.yangutils.datamodel.YangType; | ||
46 | +import org.onosproject.yangutils.datamodel.YangUnion; | ||
47 | +import org.onosproject.yangutils.parser.Parsable; | ||
48 | +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | ||
49 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
50 | +import org.onosproject.yangutils.parser.impl.TreeWalkListener; | ||
51 | + | ||
52 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; | ||
53 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; | ||
54 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; | ||
55 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; | ||
56 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; | ||
57 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; | ||
58 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; | ||
59 | +import static org.onosproject.yangutils.utils.YangConstructType.TYPE_DATA; | ||
60 | +import static org.onosproject.yangutils.utils.YangConstructType.UNION_DATA; | ||
61 | + | ||
62 | +/** | ||
63 | + * Implements listener based call back function corresponding to the "union" rule | ||
64 | + * defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. | ||
65 | + */ | ||
66 | +public final class UnionListener { | ||
67 | + /** | ||
68 | + * Creates a new union listener. | ||
69 | + */ | ||
70 | + private UnionListener() { | ||
71 | + } | ||
72 | + | ||
73 | + /** | ||
74 | + * It is called when parser enters grammar rule (union), it perform | ||
75 | + * validations and updates the data model tree. | ||
76 | + * | ||
77 | + * @param listener listener's object | ||
78 | + * @param ctx context object of the grammar rule | ||
79 | + */ | ||
80 | + public static void processUnionEntry(TreeWalkListener listener, | ||
81 | + GeneratedYangParser.UnionSpecificationContext ctx) { | ||
82 | + | ||
83 | + // Check for stack to be non empty. | ||
84 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, UNION_DATA, "", ENTRY); | ||
85 | + | ||
86 | + if (listener.getParsedDataStack().peek() instanceof YangType) { | ||
87 | + YangUnion unionNode = new YangUnion(); | ||
88 | + Parsable typeData = listener.getParsedDataStack().pop(); | ||
89 | + | ||
90 | + // Check for stack to be non empty. | ||
91 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, UNION_DATA, "", ENTRY); | ||
92 | + | ||
93 | + Parsable tmpData = listener.getParsedDataStack().peek(); | ||
94 | + | ||
95 | + switch (tmpData.getYangConstructType()) { | ||
96 | + case LEAF_DATA: | ||
97 | + unionNode.setUnionName(((YangLeaf) tmpData).getLeafName()); | ||
98 | + break; | ||
99 | + case LEAF_LIST_DATA: | ||
100 | + unionNode.setUnionName(((YangLeafList) tmpData).getLeafName()); | ||
101 | + break; | ||
102 | + case UNION_DATA: | ||
103 | + unionNode.setUnionName(((YangUnion) tmpData).getUnionName()); | ||
104 | + break; | ||
105 | + // TODO typedef, deviate. | ||
106 | + default: | ||
107 | + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA, | ||
108 | + ((YangType<?>) typeData).getDataTypeName(), ENTRY)); | ||
109 | + } | ||
110 | + listener.getParsedDataStack().push(typeData); | ||
111 | + listener.getParsedDataStack().push(unionNode); | ||
112 | + } else { | ||
113 | + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, UNION_DATA, "", ENTRY)); | ||
114 | + } | ||
115 | + } | ||
116 | + | ||
117 | + /** | ||
118 | + * It is called when parser exits from grammar rule (union), it perform | ||
119 | + * validations and update the data model tree. | ||
120 | + * | ||
121 | + * @param listener Listener's object | ||
122 | + * @param ctx context object of the grammar rule | ||
123 | + */ | ||
124 | + public static void processUnionExit(TreeWalkListener listener, | ||
125 | + GeneratedYangParser.UnionSpecificationContext ctx) { | ||
126 | + | ||
127 | + // Check for stack to be non empty. | ||
128 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, UNION_DATA, "", EXIT); | ||
129 | + | ||
130 | + Parsable tmpUnionNode = listener.getParsedDataStack().peek(); | ||
131 | + if (tmpUnionNode instanceof YangUnion) { | ||
132 | + YangUnion unionNode = (YangUnion) tmpUnionNode; | ||
133 | + listener.getParsedDataStack().pop(); | ||
134 | + | ||
135 | + // Check for stack to be non empty. | ||
136 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, UNION_DATA, "", EXIT); | ||
137 | + | ||
138 | + Parsable tmpNode = listener.getParsedDataStack().peek(); | ||
139 | + switch (tmpNode.getYangConstructType()) { | ||
140 | + case TYPE_DATA: { | ||
141 | + YangType<YangUnion> typeNode = (YangType<YangUnion>) tmpNode; | ||
142 | + typeNode.setDataTypeExtendedInfo(unionNode); | ||
143 | + break; | ||
144 | + } | ||
145 | + default: | ||
146 | + throw new ParserException( | ||
147 | + constructListenerErrorMessage(INVALID_HOLDER, UNION_DATA, "", EXIT)); | ||
148 | + } | ||
149 | + } else { | ||
150 | + throw new ParserException( | ||
151 | + constructListenerErrorMessage(MISSING_CURRENT_HOLDER, UNION_DATA, "", EXIT)); | ||
152 | + } | ||
153 | + } | ||
154 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
utils/yangutils/src/main/java/org/onosproject/yangutils/parser/impl/listeners/UsesListener.java
0 → 100644
1 | +package org.onosproject.yangutils.parser.impl.listeners; | ||
2 | + | ||
3 | +import org.onosproject.yangutils.datamodel.YangAugment; | ||
4 | +import org.onosproject.yangutils.datamodel.YangCase; | ||
5 | +import org.onosproject.yangutils.datamodel.YangContainer; | ||
6 | +import org.onosproject.yangutils.datamodel.YangGrouping; | ||
7 | +import org.onosproject.yangutils.datamodel.YangInput; | ||
8 | +import org.onosproject.yangutils.datamodel.YangList; | ||
9 | +import org.onosproject.yangutils.datamodel.YangModule; | ||
10 | +import org.onosproject.yangutils.datamodel.YangNode; | ||
11 | +import org.onosproject.yangutils.datamodel.YangNotification; | ||
12 | +import org.onosproject.yangutils.datamodel.YangOutput; | ||
13 | +import org.onosproject.yangutils.datamodel.YangSubModule; | ||
14 | +import org.onosproject.yangutils.datamodel.YangUses; | ||
15 | +import org.onosproject.yangutils.datamodel.exceptions.DataModelException; | ||
16 | +import org.onosproject.yangutils.parser.Parsable; | ||
17 | +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | ||
18 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
19 | +import org.onosproject.yangutils.parser.impl.TreeWalkListener; | ||
20 | + | ||
21 | +import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; | ||
22 | +import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangUsesNode; | ||
23 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; | ||
24 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; | ||
25 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage; | ||
26 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; | ||
27 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; | ||
28 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER; | ||
29 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; | ||
30 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA; | ||
31 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; | ||
32 | +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne; | ||
33 | +import static org.onosproject.yangutils.utils.YangConstructType.DESCRIPTION_DATA; | ||
34 | +import static org.onosproject.yangutils.utils.YangConstructType.REFERENCE_DATA; | ||
35 | +import static org.onosproject.yangutils.utils.YangConstructType.STATUS_DATA; | ||
36 | +import static org.onosproject.yangutils.utils.YangConstructType.USES_DATA; | ||
37 | +import static org.onosproject.yangutils.utils.YangConstructType.WHEN_DATA; | ||
38 | + | ||
39 | +/* | ||
40 | + * Reference: RFC6020 and YANG ANTLR Grammar | ||
41 | + * | ||
42 | + * ABNF grammar as per RFC6020 | ||
43 | + * data-def-stmt = container-stmt / | ||
44 | + * leaf-stmt / | ||
45 | + * leaf-list-stmt / | ||
46 | + * list-stmt / | ||
47 | + * choice-stmt / | ||
48 | + * anyxml-stmt / | ||
49 | + * uses-stmt | ||
50 | + * | ||
51 | + * uses-stmt = uses-keyword sep identifier-ref-arg-str optsep | ||
52 | + * (";" / | ||
53 | + * "{" stmtsep | ||
54 | + * ;; these stmts can appear in any order | ||
55 | + * [when-stmt stmtsep] | ||
56 | + * *(if-feature-stmt stmtsep) | ||
57 | + * [status-stmt stmtsep] | ||
58 | + * [description-stmt stmtsep] | ||
59 | + * [reference-stmt stmtsep] | ||
60 | + * *(refine-stmt stmtsep) | ||
61 | + * *(uses-augment-stmt stmtsep) | ||
62 | + * "}") | ||
63 | + * | ||
64 | + * ANTLR grammar rule | ||
65 | + * dataDefStatement : containerStatement | ||
66 | + * | leafStatement | ||
67 | + * | leafListStatement | ||
68 | + * | listStatement | ||
69 | + * | choiceStatement | ||
70 | + * | usesStatement; | ||
71 | + * | ||
72 | + * usesStatement : USES_KEYWORD string (STMTEND | LEFT_CURLY_BRACE (whenStatement | ifFeatureStatement | ||
73 | + * | statusStatement | descriptionStatement | referenceStatement | refineStatement | ||
74 | + * | usesAugmentStatement)* RIGHT_CURLY_BRACE); | ||
75 | + */ | ||
76 | + | ||
77 | +/** | ||
78 | + * Implements listener based call back function corresponding to the "uses" | ||
79 | + * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. | ||
80 | + */ | ||
81 | +public final class UsesListener { | ||
82 | + | ||
83 | + /** | ||
84 | + * Creates a new uses listener. | ||
85 | + */ | ||
86 | + private UsesListener() { | ||
87 | + } | ||
88 | + | ||
89 | + /** | ||
90 | + * It is called when parser enters grammar rule (uses), it perform | ||
91 | + * validations and updates the data model tree. | ||
92 | + * | ||
93 | + * @param listener listener's object | ||
94 | + * @param ctx context object of the grammar rule | ||
95 | + */ | ||
96 | + public static void processUsesEntry(TreeWalkListener listener, GeneratedYangParser.UsesStatementContext ctx) { | ||
97 | + | ||
98 | + // Check for stack to be non empty. | ||
99 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, USES_DATA, ctx.string().getText(), ENTRY); | ||
100 | + | ||
101 | + // Validate sub statement cardinality. | ||
102 | + validateSubStatementsCardinality(ctx); | ||
103 | + | ||
104 | + Parsable curData = listener.getParsedDataStack().peek(); | ||
105 | + | ||
106 | + if (curData instanceof YangModule || curData instanceof YangSubModule | ||
107 | + || curData instanceof YangContainer || curData instanceof YangList | ||
108 | + || curData instanceof YangUses || curData instanceof YangAugment | ||
109 | + || curData instanceof YangCase || curData instanceof YangGrouping | ||
110 | + || curData instanceof YangInput || curData instanceof YangOutput | ||
111 | + || curData instanceof YangNotification) { | ||
112 | + | ||
113 | + YangUses usesNode = getYangUsesNode(JAVA_GENERATION); | ||
114 | + usesNode.setName(ctx.string().getText()); | ||
115 | + | ||
116 | + YangNode curNode = (YangNode) curData; | ||
117 | + | ||
118 | + try { | ||
119 | + curNode.addChild(usesNode); | ||
120 | + } catch (DataModelException e) { | ||
121 | + throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA, | ||
122 | + USES_DATA, ctx.string().getText(), ENTRY, e.getMessage())); | ||
123 | + } | ||
124 | + listener.getParsedDataStack().push(usesNode); | ||
125 | + } else { | ||
126 | + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, | ||
127 | + USES_DATA, ctx.string().getText(), ENTRY)); | ||
128 | + } | ||
129 | + } | ||
130 | + | ||
131 | + /** | ||
132 | + * It is called when parser exits from grammar rule (uses), it perform | ||
133 | + * validations and update the data model tree. | ||
134 | + * | ||
135 | + * @param listener Listener's object | ||
136 | + * @param ctx context object of the grammar rule | ||
137 | + */ | ||
138 | + public static void processUsesExit(TreeWalkListener listener, | ||
139 | + GeneratedYangParser.UsesStatementContext ctx) { | ||
140 | + | ||
141 | + // Check for stack to be non empty. | ||
142 | + checkStackIsNotEmpty(listener, MISSING_HOLDER, USES_DATA, ctx.string().getText(), EXIT); | ||
143 | + | ||
144 | + if (listener.getParsedDataStack().peek() instanceof YangUses) { | ||
145 | + listener.getParsedDataStack().pop(); | ||
146 | + } else { | ||
147 | + throw new ParserException(constructListenerErrorMessage(MISSING_CURRENT_HOLDER, USES_DATA, | ||
148 | + ctx.string().getText(), EXIT)); | ||
149 | + } | ||
150 | + } | ||
151 | + | ||
152 | + // TODO linker to handle collision scenarios like leaf obtained by uses, conflicts with some existing leaf. | ||
153 | + | ||
154 | + /** | ||
155 | + * Validates the cardinality of case sub-statements as per grammar. | ||
156 | + * | ||
157 | + * @param ctx context object of the grammar rule | ||
158 | + */ | ||
159 | + private static void validateSubStatementsCardinality(GeneratedYangParser.UsesStatementContext ctx) { | ||
160 | + validateCardinalityMaxOne(ctx.whenStatement(), WHEN_DATA, USES_DATA, ctx.string().getText()); | ||
161 | + validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, USES_DATA, ctx.string().getText()); | ||
162 | + validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, USES_DATA, ctx.string().getText()); | ||
163 | + validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, USES_DATA, ctx.string().getText()); } | ||
164 | +} |
... | @@ -23,6 +23,7 @@ import java.text.SimpleDateFormat; | ... | @@ -23,6 +23,7 @@ import java.text.SimpleDateFormat; |
23 | import java.util.Calendar; | 23 | import java.util.Calendar; |
24 | import java.util.regex.Pattern; | 24 | import java.util.regex.Pattern; |
25 | 25 | ||
26 | +import org.onosproject.yangutils.datamodel.YangNodeIdentifier; | ||
26 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; | 27 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
27 | import org.onosproject.yangutils.utils.YangConstructType; | 28 | import org.onosproject.yangutils.utils.YangConstructType; |
28 | import org.onosproject.yangutils.parser.exceptions.ParserException; | 29 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
... | @@ -43,6 +44,7 @@ public final class ListenerUtil { | ... | @@ -43,6 +44,7 @@ public final class ListenerUtil { |
43 | private static final String HYPHEN = "-"; | 44 | private static final String HYPHEN = "-"; |
44 | private static final String SLASH = "/"; | 45 | private static final String SLASH = "/"; |
45 | private static final String SPACE = " "; | 46 | private static final String SPACE = " "; |
47 | + private static final String COLON = ":"; | ||
46 | 48 | ||
47 | /** | 49 | /** |
48 | * Creates a new listener util. | 50 | * Creates a new listener util. |
... | @@ -204,4 +206,35 @@ public final class ListenerUtil { | ... | @@ -204,4 +206,35 @@ public final class ListenerUtil { |
204 | EMPTY_STRING); | 206 | EMPTY_STRING); |
205 | return dateForRevision; | 207 | return dateForRevision; |
206 | } | 208 | } |
209 | + | ||
210 | + /** | ||
211 | + * Checks and return valid node identifier. | ||
212 | + * | ||
213 | + * @param nodeIdentifierString string from yang file | ||
214 | + * @param yangConstruct yang construct for creating error message | ||
215 | + * @param ctx yang construct's context to get the line number and character position | ||
216 | + * @return valid node identifier | ||
217 | + */ | ||
218 | + public static YangNodeIdentifier getValidNodeIdentifier(String nodeIdentifierString, YangConstructType | ||
219 | + yangConstruct, ParserRuleContext ctx) { | ||
220 | + String tmpIdentifierString = removeQuotesAndHandleConcat(nodeIdentifierString); | ||
221 | + String[] tmpData = tmpIdentifierString.split(Pattern.quote(COLON)); | ||
222 | + if (tmpData.length == 1) { | ||
223 | + YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier(); | ||
224 | + nodeIdentifier.setName(getValidIdentifier(tmpData[0], yangConstruct, ctx)); | ||
225 | + return nodeIdentifier; | ||
226 | + } else if (tmpData.length == 2) { | ||
227 | + YangNodeIdentifier nodeIdentifier = new YangNodeIdentifier(); | ||
228 | + nodeIdentifier.setPrefix(getValidIdentifier(tmpData[0], yangConstruct, ctx)); | ||
229 | + nodeIdentifier.setName(getValidIdentifier(tmpData[1], yangConstruct, ctx)); | ||
230 | + return nodeIdentifier; | ||
231 | + } else { | ||
232 | + ParserException parserException = new ParserException("YANG file error : " + | ||
233 | + YangConstructType.getYangConstructType(yangConstruct) + " name " + nodeIdentifierString + | ||
234 | + " is not valid."); | ||
235 | + parserException.setLine(ctx.getStart().getLine()); | ||
236 | + parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); | ||
237 | + throw parserException; | ||
238 | + } | ||
239 | + } | ||
207 | } | 240 | } |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
1 | +/* | ||
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 | + | ||
17 | +package org.onosproject.yangutils.parser.impl.listeners; | ||
18 | + | ||
19 | +import java.io.IOException; | ||
20 | +import java.util.ListIterator; | ||
21 | +import org.junit.Test; | ||
22 | +import org.onosproject.yangutils.datamodel.YangContainer; | ||
23 | +import org.onosproject.yangutils.datamodel.YangGrouping; | ||
24 | +import org.onosproject.yangutils.datamodel.YangLeaf; | ||
25 | +import org.onosproject.yangutils.datamodel.YangList; | ||
26 | +import org.onosproject.yangutils.datamodel.YangModule; | ||
27 | +import org.onosproject.yangutils.datamodel.YangNode; | ||
28 | +import org.onosproject.yangutils.datamodel.YangNodeType; | ||
29 | +import org.onosproject.yangutils.datamodel.YangStatusType; | ||
30 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
31 | +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; | ||
32 | + | ||
33 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
34 | +import static org.hamcrest.core.Is.is; | ||
35 | + | ||
36 | +/** | ||
37 | + * Test cases for testing grouping listener. | ||
38 | + */ | ||
39 | +public class GroupingListenerTest { | ||
40 | + | ||
41 | + private final YangUtilsParserManager manager = new YangUtilsParserManager(); | ||
42 | + | ||
43 | + /** | ||
44 | + * Checks grouping statement inside module. | ||
45 | + */ | ||
46 | + @Test | ||
47 | + public void processGroupingInModule() throws IOException, ParserException { | ||
48 | + | ||
49 | + YangNode node = manager.getDataModel("src/test/resources/GroupingInModule.yang"); | ||
50 | + | ||
51 | + // Check whether the data model tree returned is of type module. | ||
52 | + assertThat((node instanceof YangModule), is(true)); | ||
53 | + | ||
54 | + // Check whether the node type is set properly to module. | ||
55 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
56 | + | ||
57 | + // Check whether the module name is set correctly. | ||
58 | + YangModule yangNode = (YangModule) node; | ||
59 | + assertThat(yangNode.getName(), is("Test")); | ||
60 | + | ||
61 | + YangGrouping yangGrouping = (YangGrouping) yangNode.getChild(); | ||
62 | + assertThat(yangGrouping.getName(), is("endpoint")); | ||
63 | + | ||
64 | + ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator(); | ||
65 | + YangLeaf leafInfo = leafIterator.next(); | ||
66 | + | ||
67 | + assertThat(leafInfo.getLeafName(), is("address")); | ||
68 | + } | ||
69 | + | ||
70 | + /** | ||
71 | + * Checks grouping statement inside container. | ||
72 | + */ | ||
73 | + @Test | ||
74 | + public void processGroupingInContainer() throws IOException, ParserException { | ||
75 | + | ||
76 | + YangNode node = manager.getDataModel("src/test/resources/GroupingInContainer.yang"); | ||
77 | + | ||
78 | + // Check whether the data model tree returned is of type module. | ||
79 | + assertThat((node instanceof YangModule), is(true)); | ||
80 | + | ||
81 | + // Check whether the node type is set properly to module. | ||
82 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
83 | + | ||
84 | + // Check whether the module name is set correctly. | ||
85 | + YangModule yangNode = (YangModule) node; | ||
86 | + assertThat(yangNode.getName(), is("Test")); | ||
87 | + | ||
88 | + YangContainer yangContainer = (YangContainer) yangNode.getChild(); | ||
89 | + assertThat(yangContainer.getName(), is("valid")); | ||
90 | + | ||
91 | + YangGrouping yangGrouping = (YangGrouping) yangContainer.getChild(); | ||
92 | + assertThat(yangGrouping.getName(), is("endpoint")); | ||
93 | + | ||
94 | + ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator(); | ||
95 | + YangLeaf leafInfo = leafIterator.next(); | ||
96 | + | ||
97 | + assertThat(leafInfo.getLeafName(), is("address")); | ||
98 | + } | ||
99 | + | ||
100 | + /** | ||
101 | + * Checks grouping statement inside list. | ||
102 | + */ | ||
103 | + @Test | ||
104 | + public void processGroupingInList() throws IOException, ParserException { | ||
105 | + | ||
106 | + YangNode node = manager.getDataModel("src/test/resources/GroupingInList.yang"); | ||
107 | + | ||
108 | + // Check whether the data model tree returned is of type module. | ||
109 | + assertThat((node instanceof YangModule), is(true)); | ||
110 | + | ||
111 | + // Check whether the node type is set properly to module. | ||
112 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
113 | + | ||
114 | + // Check whether the module name is set correctly. | ||
115 | + YangModule yangNode = (YangModule) node; | ||
116 | + assertThat(yangNode.getName(), is("Test")); | ||
117 | + | ||
118 | + YangList yangList = (YangList) yangNode.getChild(); | ||
119 | + assertThat(yangList.getName(), is("valid")); | ||
120 | + | ||
121 | + YangGrouping yangGrouping = (YangGrouping) yangList.getChild(); | ||
122 | + assertThat(yangGrouping.getName(), is("endpoint")); | ||
123 | + | ||
124 | + ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator(); | ||
125 | + YangLeaf leafInfo = leafIterator.next(); | ||
126 | + | ||
127 | + assertThat(leafInfo.getLeafName(), is("address")); | ||
128 | + } | ||
129 | + | ||
130 | + /** | ||
131 | + * Checks grouping with attributes. | ||
132 | + */ | ||
133 | + @Test | ||
134 | + public void processGroupingAttributes() throws IOException, ParserException { | ||
135 | + | ||
136 | + YangNode node = manager.getDataModel("src/test/resources/GroupingAttributes.yang"); | ||
137 | + | ||
138 | + // Check whether the data model tree returned is of type module. | ||
139 | + assertThat((node instanceof YangModule), is(true)); | ||
140 | + | ||
141 | + // Check whether the node type is set properly to module. | ||
142 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
143 | + | ||
144 | + // Check whether the module name is set correctly. | ||
145 | + YangModule yangNode = (YangModule) node; | ||
146 | + assertThat(yangNode.getName(), is("Test")); | ||
147 | + | ||
148 | + YangList yangList = (YangList) yangNode.getChild(); | ||
149 | + assertThat(yangList.getName(), is("valid")); | ||
150 | + | ||
151 | + YangGrouping yangGrouping = (YangGrouping) yangList.getChild(); | ||
152 | + assertThat(yangGrouping.getName(), is("endpoint")); | ||
153 | + assertThat(yangGrouping.getStatus(), is(YangStatusType.CURRENT)); | ||
154 | + assertThat(yangGrouping.getReference(), is("\"RFC 6020\"")); | ||
155 | + assertThat(yangGrouping.getDescription(), is("\"grouping under test\"")); | ||
156 | + | ||
157 | + ListIterator<YangLeaf> leafIterator = yangGrouping.getListOfLeaf().listIterator(); | ||
158 | + YangLeaf leafInfo = leafIterator.next(); | ||
159 | + | ||
160 | + assertThat(leafInfo.getLeafName(), is("address")); | ||
161 | + } | ||
162 | + | ||
163 | + /** | ||
164 | + * Checks duplicate grouping in list. | ||
165 | + */ | ||
166 | + @Test(expected = ParserException.class) | ||
167 | + public void processDuplicateGroupingInList() throws IOException, ParserException { | ||
168 | + | ||
169 | + YangNode node = manager.getDataModel("src/test/resources/DuplicateGroupingInList.yang"); | ||
170 | + } | ||
171 | + | ||
172 | + /** | ||
173 | + * Checks duplicate grouping in container. | ||
174 | + */ | ||
175 | + @Test (expected = ParserException.class) | ||
176 | + public void processDuplicateGroupingInContainer() throws IOException, ParserException { | ||
177 | + | ||
178 | + YangNode node = manager.getDataModel("src/test/resources/DuplicateGroupingInContainer.yang"); | ||
179 | + } | ||
180 | + | ||
181 | + /** | ||
182 | + * Checks duplicate grouping in module. | ||
183 | + */ | ||
184 | + @Test (expected = ParserException.class) | ||
185 | + public void processDuplicateGroupingInModule() throws IOException, ParserException { | ||
186 | + | ||
187 | + YangNode node = manager.getDataModel("src/test/resources/DuplicateGroupingInModule.yang"); | ||
188 | + } | ||
189 | +} |
1 | +/* | ||
2 | + * Copyright 2014-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 | + | ||
17 | +package org.onosproject.yangutils.parser.impl.listeners; | ||
18 | + | ||
19 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
20 | +import static org.hamcrest.core.Is.is; | ||
21 | +import org.junit.Test; | ||
22 | +import org.onosproject.yangutils.datamodel.YangCase; | ||
23 | +import org.onosproject.yangutils.datamodel.YangChoice; | ||
24 | +import org.onosproject.yangutils.datamodel.YangContainer; | ||
25 | +import org.onosproject.yangutils.datamodel.YangList; | ||
26 | +import org.onosproject.yangutils.datamodel.YangModule; | ||
27 | +import org.onosproject.yangutils.datamodel.YangNode; | ||
28 | +import org.onosproject.yangutils.datamodel.YangNodeType; | ||
29 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
30 | +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; | ||
31 | + | ||
32 | +import java.io.IOException; | ||
33 | + | ||
34 | +/** | ||
35 | + * Test cases for short case listener. | ||
36 | + */ | ||
37 | +public class ShortCaseListenerTest { | ||
38 | + | ||
39 | + private final YangUtilsParserManager manager = new YangUtilsParserManager(); | ||
40 | + | ||
41 | + /** | ||
42 | + * Checks short case listener with container. | ||
43 | + */ | ||
44 | + @Test | ||
45 | + public void processShortCaseListenerWithContainer() throws IOException, ParserException { | ||
46 | + | ||
47 | + YangNode node = manager.getDataModel("src/test/resources/ShortCaseListenerWithContainer.yang"); | ||
48 | + | ||
49 | + // Check whether the data model tree returned is of type module. | ||
50 | + assertThat((node instanceof YangModule), is(true)); | ||
51 | + | ||
52 | + // Check whether the node type is set properly to module. | ||
53 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
54 | + | ||
55 | + // Check whether the module name is set correctly. | ||
56 | + YangModule yangNode = (YangModule) node; | ||
57 | + assertThat(yangNode.getName(), is("Test")); | ||
58 | + | ||
59 | + YangContainer yangContainer = (YangContainer) yangNode.getChild(); | ||
60 | + assertThat(yangContainer.getName(), is("food")); | ||
61 | + | ||
62 | + YangChoice yangChoice = (YangChoice) yangContainer.getChild(); | ||
63 | + assertThat(yangChoice.getName(), is("snack")); | ||
64 | + | ||
65 | + YangCase yangCase = (YangCase) yangChoice.getChild(); | ||
66 | + assertThat(yangCase.getName(), is("sports-arena")); | ||
67 | + | ||
68 | + YangContainer yangContainer1 = (YangContainer) yangCase.getChild(); | ||
69 | + assertThat(yangContainer1.getName(), is("sports-arena")); | ||
70 | + } | ||
71 | + | ||
72 | + /** | ||
73 | + * Checks short case listener with list. | ||
74 | + */ | ||
75 | + @Test | ||
76 | + public void processShortCaseListenerWithList() throws IOException, ParserException { | ||
77 | + | ||
78 | + YangNode node = manager.getDataModel("src/test/resources/ShortCaseListenerWithList.yang"); | ||
79 | + | ||
80 | + // Check whether the data model tree returned is of type module. | ||
81 | + assertThat((node instanceof YangModule), is(true)); | ||
82 | + | ||
83 | + // Check whether the node type is set properly to module. | ||
84 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
85 | + | ||
86 | + // Check whether the module name is set correctly. | ||
87 | + YangModule yangNode = (YangModule) node; | ||
88 | + assertThat(yangNode.getName(), is("Test")); | ||
89 | + | ||
90 | + YangContainer yangContainer = (YangContainer) yangNode.getChild(); | ||
91 | + assertThat(yangContainer.getName(), is("food")); | ||
92 | + | ||
93 | + YangChoice yangChoice = (YangChoice) yangContainer.getChild(); | ||
94 | + assertThat(yangChoice.getName(), is("snack")); | ||
95 | + | ||
96 | + YangCase yangCase = (YangCase) yangChoice.getChild(); | ||
97 | + assertThat(yangCase.getName(), is("sports-arena")); | ||
98 | + | ||
99 | + YangList yangList = (YangList) yangCase.getChild(); | ||
100 | + assertThat(yangList.getName(), is("sports-arena")); | ||
101 | + } | ||
102 | +} |
utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/UnionListenerTest.java
0 → 100644
1 | +/* | ||
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 | + | ||
17 | +package org.onosproject.yangutils.parser.impl.listeners; | ||
18 | + | ||
19 | +import java.io.IOException; | ||
20 | +import java.util.List; | ||
21 | +import java.util.ListIterator; | ||
22 | +import org.junit.Test; | ||
23 | +import org.onosproject.yangutils.datamodel.YangDataTypes; | ||
24 | +import org.onosproject.yangutils.datamodel.YangLeaf; | ||
25 | +import org.onosproject.yangutils.datamodel.YangLeafList; | ||
26 | +import org.onosproject.yangutils.datamodel.YangList; | ||
27 | +import org.onosproject.yangutils.datamodel.YangModule; | ||
28 | +import org.onosproject.yangutils.datamodel.YangNode; | ||
29 | +import org.onosproject.yangutils.datamodel.YangNodeType; | ||
30 | +import org.onosproject.yangutils.datamodel.YangType; | ||
31 | +import org.onosproject.yangutils.datamodel.YangUnion; | ||
32 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
33 | +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; | ||
34 | + | ||
35 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
36 | +import static org.hamcrest.core.Is.is; | ||
37 | + | ||
38 | +/** | ||
39 | + * Test cases for testing union listener. | ||
40 | + */ | ||
41 | +public class UnionListenerTest { | ||
42 | + | ||
43 | + private final YangUtilsParserManager manager = new YangUtilsParserManager(); | ||
44 | + | ||
45 | + /** | ||
46 | + * Checks union when type is in leaf. | ||
47 | + */ | ||
48 | + @Test | ||
49 | + public void processUnionWhenTypeInLeaf() throws IOException, ParserException { | ||
50 | + | ||
51 | + YangNode node = manager.getDataModel("src/test/resources/UnionWhenTypeInLeaf.yang"); | ||
52 | + | ||
53 | + // Check whether the data model tree returned is of type module. | ||
54 | + assertThat((node instanceof YangModule), is(true)); | ||
55 | + | ||
56 | + // Check whether the node type is set properly to module. | ||
57 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
58 | + | ||
59 | + // Check whether the module name is set correctly. | ||
60 | + YangModule yangNode = (YangModule) node; | ||
61 | + assertThat(yangNode.getName(), is("Test")); | ||
62 | + | ||
63 | + YangList yangList = (YangList) yangNode.getChild(); | ||
64 | + assertThat(yangList.getName(), is("valid")); | ||
65 | + | ||
66 | + ListIterator<YangLeaf> leafIterator = yangList.getListOfLeaf().listIterator(); | ||
67 | + YangLeaf leafInfo = leafIterator.next(); | ||
68 | + | ||
69 | + assertThat(leafInfo.getLeafName(), is("invalid-interval")); | ||
70 | + | ||
71 | + assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.UNION)); | ||
72 | + assertThat(leafInfo.getDataType().getDataTypeName(), is("union")); | ||
73 | + | ||
74 | + YangUnion yangUnion = (YangUnion) leafInfo.getDataType().getDataTypeExtendedInfo(); | ||
75 | + | ||
76 | + List<YangType<?>> typeList = yangUnion.getTypeList(); | ||
77 | + ListIterator<YangType<?>> typeListIterator = typeList.listIterator(); | ||
78 | + YangType<?> yangType = typeListIterator.next(); | ||
79 | + | ||
80 | + assertThat(yangType.getDataTypeName(), is("int32")); | ||
81 | + assertThat(yangType.getDataType(), is(YangDataTypes.INT32)); | ||
82 | + | ||
83 | + YangType<?> yangTypeEnum = typeListIterator.next(); | ||
84 | + | ||
85 | + assertThat(yangTypeEnum.getDataTypeName(), is("enumeration")); | ||
86 | + assertThat(yangTypeEnum.getDataType(), is(YangDataTypes.ENUMERATION)); | ||
87 | + } | ||
88 | + | ||
89 | + /** | ||
90 | + * Checks union when type is in leaflist. | ||
91 | + */ | ||
92 | + @Test | ||
93 | + public void processUnionWhenTypeInLeafList() throws IOException, ParserException { | ||
94 | + | ||
95 | + YangNode node = manager.getDataModel("src/test/resources/UnionWhenTypeInLeafList.yang"); | ||
96 | + | ||
97 | + // Check whether the data model tree returned is of type module. | ||
98 | + assertThat((node instanceof YangModule), is(true)); | ||
99 | + | ||
100 | + // Check whether the node type is set properly to module. | ||
101 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
102 | + | ||
103 | + // Check whether the module name is set correctly. | ||
104 | + YangModule yangNode = (YangModule) node; | ||
105 | + assertThat(yangNode.getName(), is("Test")); | ||
106 | + | ||
107 | + YangList yangList = (YangList) yangNode.getChild(); | ||
108 | + assertThat(yangList.getName(), is("valid")); | ||
109 | + | ||
110 | + ListIterator<YangLeafList> leafListIterator = yangList.getListOfLeafList().listIterator(); | ||
111 | + YangLeafList leafListInfo = leafListIterator.next(); | ||
112 | + | ||
113 | + assertThat(leafListInfo.getLeafName(), is("invalid-interval")); | ||
114 | + | ||
115 | + assertThat(leafListInfo.getDataType().getDataType(), is(YangDataTypes.UNION)); | ||
116 | + assertThat(leafListInfo.getDataType().getDataTypeName(), is("union")); | ||
117 | + | ||
118 | + YangUnion yangUnion = (YangUnion) leafListInfo.getDataType().getDataTypeExtendedInfo(); | ||
119 | + | ||
120 | + List<YangType<?>> typeList = yangUnion.getTypeList(); | ||
121 | + ListIterator<YangType<?>> typeListIterator = typeList.listIterator(); | ||
122 | + YangType<?> yangType = typeListIterator.next(); | ||
123 | + | ||
124 | + assertThat(yangType.getDataTypeName(), is("int32")); | ||
125 | + assertThat(yangType.getDataType(), is(YangDataTypes.INT32)); | ||
126 | + | ||
127 | + YangType<?> yangTypeEnum = typeListIterator.next(); | ||
128 | + | ||
129 | + assertThat(yangTypeEnum.getDataTypeName(), is("enumeration")); | ||
130 | + assertThat(yangTypeEnum.getDataType(), is(YangDataTypes.ENUMERATION)); | ||
131 | + } | ||
132 | + | ||
133 | + /** | ||
134 | + * Checks union with empty type. | ||
135 | + */ | ||
136 | + @Test (expected = ParserException.class) | ||
137 | + public void processUnionWithEmptyType() throws IOException, ParserException { | ||
138 | + | ||
139 | + YangNode node = manager.getDataModel("src/test/resources/UnionWithEmptyType.yang"); | ||
140 | + } | ||
141 | +} |
utils/yangutils/src/test/java/org/onosproject/yangutils/parser/impl/listeners/UsesListenerTest.java
0 → 100644
1 | +/* | ||
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 | + | ||
17 | +package org.onosproject.yangutils.parser.impl.listeners; | ||
18 | + | ||
19 | +import java.io.IOException; | ||
20 | +import org.junit.Test; | ||
21 | +import org.onosproject.yangutils.datamodel.YangContainer; | ||
22 | +import org.onosproject.yangutils.datamodel.YangList; | ||
23 | +import org.onosproject.yangutils.datamodel.YangModule; | ||
24 | +import org.onosproject.yangutils.datamodel.YangNode; | ||
25 | +import org.onosproject.yangutils.datamodel.YangNodeType; | ||
26 | +import org.onosproject.yangutils.datamodel.YangStatusType; | ||
27 | +import org.onosproject.yangutils.datamodel.YangUses; | ||
28 | +import org.onosproject.yangutils.parser.exceptions.ParserException; | ||
29 | +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; | ||
30 | + | ||
31 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
32 | +import static org.hamcrest.core.Is.is; | ||
33 | + | ||
34 | +/** | ||
35 | + * Test cases for testing uses listener. | ||
36 | + */ | ||
37 | +public class UsesListenerTest { | ||
38 | + | ||
39 | + private final YangUtilsParserManager manager = new YangUtilsParserManager(); | ||
40 | + | ||
41 | + /** | ||
42 | + * Checks uses statement inside module. | ||
43 | + */ | ||
44 | + @Test | ||
45 | + public void processUsesInModule() throws IOException, ParserException { | ||
46 | + | ||
47 | + YangNode node = manager.getDataModel("src/test/resources/UsesInModule.yang"); | ||
48 | + | ||
49 | + // Check whether the data model tree returned is of type module. | ||
50 | + assertThat((node instanceof YangModule), is(true)); | ||
51 | + | ||
52 | + // Check whether the node type is set properly to module. | ||
53 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
54 | + | ||
55 | + // Check whether the module name is set correctly. | ||
56 | + YangModule yangNode = (YangModule) node; | ||
57 | + assertThat(yangNode.getName(), is("Test")); | ||
58 | + | ||
59 | + YangUses yangUses = (YangUses) yangNode.getChild(); | ||
60 | + assertThat(yangUses.getName(), is("endpoint")); | ||
61 | + } | ||
62 | + | ||
63 | + /** | ||
64 | + * Checks uses statement inside container. | ||
65 | + */ | ||
66 | + @Test | ||
67 | + public void processUsesInContainer() throws IOException, ParserException { | ||
68 | + | ||
69 | + YangNode node = manager.getDataModel("src/test/resources/UsesInContainer.yang"); | ||
70 | + | ||
71 | + // Check whether the data model tree returned is of type module. | ||
72 | + assertThat((node instanceof YangModule), is(true)); | ||
73 | + | ||
74 | + // Check whether the node type is set properly to module. | ||
75 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
76 | + | ||
77 | + // Check whether the module name is set correctly. | ||
78 | + YangModule yangNode = (YangModule) node; | ||
79 | + assertThat(yangNode.getName(), is("Test")); | ||
80 | + | ||
81 | + YangContainer yangContainer = (YangContainer) yangNode.getChild(); | ||
82 | + assertThat(yangContainer.getName(), is("valid")); | ||
83 | + | ||
84 | + YangUses yangUses = (YangUses) yangContainer.getChild(); | ||
85 | + assertThat(yangUses.getName(), is("endpoint")); | ||
86 | + | ||
87 | + // Check attributes associated with uses. | ||
88 | + assertThat(yangUses.getStatus(), is(YangStatusType.CURRENT)); | ||
89 | + assertThat(yangUses.getReference(), is("\"RFC 6020\"")); | ||
90 | + assertThat(yangUses.getDescription(), is("\"grouping under test\"")); | ||
91 | + } | ||
92 | + | ||
93 | + /** | ||
94 | + * Checks uses statement inside list. | ||
95 | + */ | ||
96 | + @Test | ||
97 | + public void processUsesInList() throws IOException, ParserException { | ||
98 | + | ||
99 | + YangNode node = manager.getDataModel("src/test/resources/UsesInList.yang"); | ||
100 | + | ||
101 | + // Check whether the data model tree returned is of type module. | ||
102 | + assertThat((node instanceof YangModule), is(true)); | ||
103 | + | ||
104 | + // Check whether the node type is set properly to module. | ||
105 | + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE)); | ||
106 | + | ||
107 | + // Check whether the module name is set correctly. | ||
108 | + YangModule yangNode = (YangModule) node; | ||
109 | + assertThat(yangNode.getName(), is("Test")); | ||
110 | + | ||
111 | + YangList yangList = (YangList) yangNode.getChild(); | ||
112 | + assertThat(yangList.getName(), is("valid")); | ||
113 | + | ||
114 | + YangUses yangUses = (YangUses) yangList.getChild(); | ||
115 | + assertThat(yangUses.getName(), is("endpoint")); | ||
116 | + | ||
117 | + // Check attributes associated with uses. | ||
118 | + assertThat(yangUses.getStatus(), is(YangStatusType.CURRENT)); | ||
119 | + assertThat(yangUses.getReference(), is("\"RFC 6020\"")); | ||
120 | + assertThat(yangUses.getDescription(), is("\"grouping under test\"")); | ||
121 | + } | ||
122 | +} |
1 | +module Test { | ||
2 | + yang-version 1; | ||
3 | + namespace http://huawei.com; | ||
4 | + prefix Ant; | ||
5 | + container valid { | ||
6 | + grouping endpoint { | ||
7 | + leaf address { | ||
8 | + type ip-address; | ||
9 | + } | ||
10 | + leaf port { | ||
11 | + type port-number; | ||
12 | + } | ||
13 | + } | ||
14 | + grouping endpoint { | ||
15 | + leaf address { | ||
16 | + type ip-address; | ||
17 | + } | ||
18 | + leaf port { | ||
19 | + type port-number; | ||
20 | + } | ||
21 | + } | ||
22 | + } | ||
23 | +} |
1 | +module Test { | ||
2 | + yang-version 1; | ||
3 | + namespace http://huawei.com; | ||
4 | + prefix Ant; | ||
5 | + list valid { | ||
6 | + key address; | ||
7 | + grouping endpoint { | ||
8 | + description "grouping under test"; | ||
9 | + status current; | ||
10 | + reference "RFC 6020"; | ||
11 | + leaf address { | ||
12 | + type ip-address; | ||
13 | + } | ||
14 | + leaf port { | ||
15 | + type port-number; | ||
16 | + } | ||
17 | + } | ||
18 | + leaf address { | ||
19 | + type ip; | ||
20 | + } | ||
21 | + grouping endpoint { | ||
22 | + description "grouping under test"; | ||
23 | + status current; | ||
24 | + reference "RFC 6020"; | ||
25 | + leaf address { | ||
26 | + type ip-address; | ||
27 | + } | ||
28 | + leaf port { | ||
29 | + type port-number; | ||
30 | + } | ||
31 | + } | ||
32 | + } | ||
33 | +} |
1 | +module Test { | ||
2 | + yang-version 1; | ||
3 | + namespace http://huawei.com; | ||
4 | + prefix Ant; | ||
5 | + grouping endpoint { | ||
6 | + leaf address { | ||
7 | + type ip-address; | ||
8 | + } | ||
9 | + leaf port { | ||
10 | + type port-number; | ||
11 | + } | ||
12 | + } | ||
13 | + grouping endpoint { | ||
14 | + leaf address { | ||
15 | + type ip-address; | ||
16 | + } | ||
17 | + leaf port { | ||
18 | + type port-number; | ||
19 | + } | ||
20 | + } | ||
21 | +} |
1 | +module Test { | ||
2 | + yang-version 1; | ||
3 | + namespace http://huawei.com; | ||
4 | + prefix Ant; | ||
5 | + list valid { | ||
6 | + key address; | ||
7 | + leaf address { | ||
8 | + type ip; | ||
9 | + } | ||
10 | + grouping endpoint { | ||
11 | + description "grouping under test"; | ||
12 | + status current; | ||
13 | + reference "RFC 6020"; | ||
14 | + leaf address { | ||
15 | + type ip-address; | ||
16 | + } | ||
17 | + leaf port { | ||
18 | + type port-number; | ||
19 | + } | ||
20 | + } | ||
21 | + } | ||
22 | +} |
1 | +module Test { | ||
2 | + yang-version 1; | ||
3 | + namespace http://huawei.com; | ||
4 | + prefix Ant; | ||
5 | + grouping endpoint { | ||
6 | + leaf address { | ||
7 | + type ip-address; | ||
8 | + } | ||
9 | + leaf port { | ||
10 | + type port-number; | ||
11 | + } | ||
12 | + } | ||
13 | + container valid { | ||
14 | + grouping endpoint { | ||
15 | + leaf address { | ||
16 | + type ip-address; | ||
17 | + } | ||
18 | + leaf port { | ||
19 | + type port-number; | ||
20 | + } | ||
21 | + } | ||
22 | + } | ||
23 | +} |
-
Please register or login to post a comment