Gaurav Agrawal
Committed by Gerrit Code Review

Yang Listener for BITS Data Type

Change-Id: I81e7f7e8de38ce20addd63548414a486b7e2ffb8
Showing 20 changed files with 908 additions and 20 deletions
...@@ -20,6 +20,8 @@ import org.onosproject.yangutils.datamodel.exceptions.DataModelException; ...@@ -20,6 +20,8 @@ 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.parser.ParsableDataType; 21 import org.onosproject.yangutils.parser.ParsableDataType;
22 22
23 +import java.util.Objects;
24 +
23 /*- 25 /*-
24 * The "bit" statement, which is a sub-statement to the "type" statement, 26 * The "bit" statement, which is a sub-statement to the "type" statement,
25 * MUST be present if the type is "bits". It is repeatedly used to 27 * MUST be present if the type is "bits". It is repeatedly used to
...@@ -57,7 +59,7 @@ public class YangBit implements YangCommonInfo, Parsable { ...@@ -57,7 +59,7 @@ public class YangBit implements YangCommonInfo, Parsable {
57 private String description; 59 private String description;
58 60
59 /** 61 /**
60 - * reference info of the bit field. 62 + * Reference info of the bit field.
61 */ 63 */
62 private String reference; 64 private String reference;
63 65
...@@ -67,7 +69,7 @@ public class YangBit implements YangCommonInfo, Parsable { ...@@ -67,7 +69,7 @@ public class YangBit implements YangCommonInfo, Parsable {
67 private YangStatusType status; 69 private YangStatusType status;
68 70
69 /** 71 /**
70 - * position of the bit whose name bit is described. 72 + * Position of the bit whose name bit is described.
71 */ 73 */
72 private int position; 74 private int position;
73 75
...@@ -79,7 +81,7 @@ public class YangBit implements YangCommonInfo, Parsable { ...@@ -79,7 +81,7 @@ public class YangBit implements YangCommonInfo, Parsable {
79 } 81 }
80 82
81 /** 83 /**
82 - * Get the bit name. 84 + * Returns bit name.
83 * 85 *
84 * @return the bit name. 86 * @return the bit name.
85 */ 87 */
...@@ -97,7 +99,7 @@ public class YangBit implements YangCommonInfo, Parsable { ...@@ -97,7 +99,7 @@ public class YangBit implements YangCommonInfo, Parsable {
97 } 99 }
98 100
99 /** 101 /**
100 - * Get the description. 102 + * Returns description.
101 * 103 *
102 * @return the description. 104 * @return the description.
103 */ 105 */
...@@ -117,7 +119,7 @@ public class YangBit implements YangCommonInfo, Parsable { ...@@ -117,7 +119,7 @@ public class YangBit implements YangCommonInfo, Parsable {
117 } 119 }
118 120
119 /** 121 /**
120 - * Get the textual reference. 122 + * Returns textual reference.
121 * 123 *
122 * @return the reference. 124 * @return the reference.
123 */ 125 */
...@@ -137,7 +139,7 @@ public class YangBit implements YangCommonInfo, Parsable { ...@@ -137,7 +139,7 @@ public class YangBit implements YangCommonInfo, Parsable {
137 } 139 }
138 140
139 /** 141 /**
140 - * Get the status. 142 + * Returns status.
141 * 143 *
142 * @return the status. 144 * @return the status.
143 */ 145 */
...@@ -157,7 +159,7 @@ public class YangBit implements YangCommonInfo, Parsable { ...@@ -157,7 +159,7 @@ public class YangBit implements YangCommonInfo, Parsable {
157 } 159 }
158 160
159 /** 161 /**
160 - * Get the bit position. 162 + * Returns bit position.
161 * 163 *
162 * @return the position 164 * @return the position
163 */ 165 */
...@@ -184,6 +186,23 @@ public class YangBit implements YangCommonInfo, Parsable { ...@@ -184,6 +186,23 @@ public class YangBit implements YangCommonInfo, Parsable {
184 return ParsableDataType.BIT_DATA; 186 return ParsableDataType.BIT_DATA;
185 } 187 }
186 188
189 + @Override
190 + public boolean equals(Object obj) {
191 + if (this == obj) {
192 + return true;
193 + }
194 + if (obj instanceof YangBit) {
195 + final YangBit other = (YangBit) obj;
196 + return Objects.equals(this.bitName, other.bitName);
197 + }
198 + return false;
199 + }
200 +
201 + @Override
202 + public int hashCode() {
203 + return Objects.hashCode(this.bitName);
204 + }
205 +
187 /** 206 /**
188 * Validate the data on entering the corresponding parse tree node. 207 * Validate the data on entering the corresponding parse tree node.
189 * 208 *
......
...@@ -36,8 +36,12 @@ import org.onosproject.yangutils.parser.ParsableDataType; ...@@ -36,8 +36,12 @@ import org.onosproject.yangutils.parser.ParsableDataType;
36 */ 36 */
37 public class YangBits implements Parsable { 37 public class YangBits implements Parsable {
38 38
39 + // Bits information set.
39 private Set<YangBit> bitSet; 40 private Set<YangBit> bitSet;
40 41
42 + // BITS name.
43 + private String bitsName;
44 +
41 /** 45 /**
42 * Create a YANG bits type object. 46 * Create a YANG bits type object.
43 */ 47 */
...@@ -46,7 +50,7 @@ public class YangBits implements Parsable { ...@@ -46,7 +50,7 @@ public class YangBits implements Parsable {
46 } 50 }
47 51
48 /** 52 /**
49 - * Get the bit set. 53 + * Returns the bit set.
50 * 54 *
51 * @return the bit set 55 * @return the bit set
52 */ 56 */
...@@ -55,7 +59,7 @@ public class YangBits implements Parsable { ...@@ -55,7 +59,7 @@ public class YangBits implements Parsable {
55 } 59 }
56 60
57 /** 61 /**
58 - * set the bit set. 62 + * Set the bit set.
59 * 63 *
60 * @param bitSet the bit set 64 * @param bitSet the bit set
61 */ 65 */
...@@ -66,10 +70,13 @@ public class YangBits implements Parsable { ...@@ -66,10 +70,13 @@ public class YangBits implements Parsable {
66 /** 70 /**
67 * Add bit info. 71 * Add bit info.
68 * 72 *
69 - * @param bitInfo the bit Info to add. 73 + * @param bitInfo the bit information to be added.
74 + * @throws DataModelException due to violation in data model rules.
70 */ 75 */
71 - public void addBitInfo(YangBit bitInfo) { 76 + public void addBitInfo(YangBit bitInfo) throws DataModelException {
72 - getBitSet().add(bitInfo); 77 + if (!getBitSet().add(bitInfo)) {
78 + throw new DataModelException("YANG Bit already exists");
79 + }
73 } 80 }
74 81
75 /** 82 /**
...@@ -83,6 +90,24 @@ public class YangBits implements Parsable { ...@@ -83,6 +90,24 @@ public class YangBits implements Parsable {
83 } 90 }
84 91
85 /** 92 /**
93 + * Returns the bits name.
94 + *
95 + * @return name of the bits.
96 + */
97 + public String getBitsName() {
98 + return bitsName;
99 + }
100 +
101 + /**
102 + * Set bits name.
103 + *
104 + * @param bitsName bit name to be set.
105 + */
106 + public void setBitsName(String bitsName) {
107 + this.bitsName = bitsName;
108 + }
109 +
110 + /**
86 * Validate the data on entering the corresponding parse tree node. 111 * Validate the data on entering the corresponding parse tree node.
87 * 112 *
88 * @throws DataModelException a violation of data model rules. 113 * @throws DataModelException a violation of data model rules.
......
...@@ -225,6 +225,11 @@ public enum ParsableDataType { ...@@ -225,6 +225,11 @@ public enum ParsableDataType {
225 ORGANIZATION_DATA, 225 ORGANIZATION_DATA,
226 226
227 /** 227 /**
228 + * Identifies the YANG position element parsed data.
229 + */
230 + POSITION_DATA,
231 +
232 + /**
228 * Identifies the derived data type. 233 * Identifies the derived data type.
229 */ 234 */
230 DERIVED; 235 DERIVED;
...@@ -318,6 +323,8 @@ public enum ParsableDataType { ...@@ -318,6 +323,8 @@ public enum ParsableDataType {
318 return "organization"; 323 return "organization";
319 case VALUE_DATA: 324 case VALUE_DATA:
320 return "value"; 325 return "value";
326 + case POSITION_DATA:
327 + return "position";
321 case DEFAULT_DATA: 328 case DEFAULT_DATA:
322 return "default"; 329 return "default";
323 case DERIVED: 330 case DERIVED:
......
...@@ -27,6 +27,8 @@ import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangListener; ...@@ -27,6 +27,8 @@ import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangListener;
27 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 27 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
28 import org.onosproject.yangutils.parser.impl.listeners.BaseFileListener; 28 import org.onosproject.yangutils.parser.impl.listeners.BaseFileListener;
29 import org.onosproject.yangutils.parser.impl.listeners.BelongsToListener; 29 import org.onosproject.yangutils.parser.impl.listeners.BelongsToListener;
30 +import org.onosproject.yangutils.parser.impl.listeners.BitListener;
31 +import org.onosproject.yangutils.parser.impl.listeners.BitsListener;
30 import org.onosproject.yangutils.parser.impl.listeners.ConfigListener; 32 import org.onosproject.yangutils.parser.impl.listeners.ConfigListener;
31 import org.onosproject.yangutils.parser.impl.listeners.ContactListener; 33 import org.onosproject.yangutils.parser.impl.listeners.ContactListener;
32 import org.onosproject.yangutils.parser.impl.listeners.ContainerListener; 34 import org.onosproject.yangutils.parser.impl.listeners.ContainerListener;
...@@ -46,6 +48,7 @@ import org.onosproject.yangutils.parser.impl.listeners.MinElementsListener; ...@@ -46,6 +48,7 @@ import org.onosproject.yangutils.parser.impl.listeners.MinElementsListener;
46 import org.onosproject.yangutils.parser.impl.listeners.ModuleListener; 48 import org.onosproject.yangutils.parser.impl.listeners.ModuleListener;
47 import org.onosproject.yangutils.parser.impl.listeners.NamespaceListener; 49 import org.onosproject.yangutils.parser.impl.listeners.NamespaceListener;
48 import org.onosproject.yangutils.parser.impl.listeners.OrganizationListener; 50 import org.onosproject.yangutils.parser.impl.listeners.OrganizationListener;
51 +import org.onosproject.yangutils.parser.impl.listeners.PositionListener;
49 import org.onosproject.yangutils.parser.impl.listeners.PrefixListener; 52 import org.onosproject.yangutils.parser.impl.listeners.PrefixListener;
50 import org.onosproject.yangutils.parser.impl.listeners.PresenceListener; 53 import org.onosproject.yangutils.parser.impl.listeners.PresenceListener;
51 import org.onosproject.yangutils.parser.impl.listeners.ReferenceListener; 54 import org.onosproject.yangutils.parser.impl.listeners.ReferenceListener;
...@@ -691,22 +694,22 @@ public class TreeWalkListener implements GeneratedYangListener { ...@@ -691,22 +694,22 @@ public class TreeWalkListener implements GeneratedYangListener {
691 694
692 @Override 695 @Override
693 public void enterBitsSpecification(GeneratedYangParser.BitsSpecificationContext ctx) { 696 public void enterBitsSpecification(GeneratedYangParser.BitsSpecificationContext ctx) {
694 - // TODO: implement the method. 697 + BitsListener.processBitsEntry(this, ctx);
695 } 698 }
696 699
697 @Override 700 @Override
698 public void exitBitsSpecification(GeneratedYangParser.BitsSpecificationContext ctx) { 701 public void exitBitsSpecification(GeneratedYangParser.BitsSpecificationContext ctx) {
699 - // TODO: implement the method. 702 + BitsListener.processBitsExit(this, ctx);
700 } 703 }
701 704
702 @Override 705 @Override
703 public void enterBitStatement(GeneratedYangParser.BitStatementContext ctx) { 706 public void enterBitStatement(GeneratedYangParser.BitStatementContext ctx) {
704 - // TODO: implement the method. 707 + BitListener.processBitEntry(this, ctx);
705 } 708 }
706 709
707 @Override 710 @Override
708 public void exitBitStatement(GeneratedYangParser.BitStatementContext ctx) { 711 public void exitBitStatement(GeneratedYangParser.BitStatementContext ctx) {
709 - // TODO: implement the method. 712 + BitListener.processBitExit(this, ctx);
710 } 713 }
711 714
712 @Override 715 @Override
...@@ -721,7 +724,7 @@ public class TreeWalkListener implements GeneratedYangListener { ...@@ -721,7 +724,7 @@ public class TreeWalkListener implements GeneratedYangListener {
721 724
722 @Override 725 @Override
723 public void enterPositionStatement(GeneratedYangParser.PositionStatementContext ctx) { 726 public void enterPositionStatement(GeneratedYangParser.PositionStatementContext ctx) {
724 - // TODO: implement the method. 727 + PositionListener.processPositionEntry(this, ctx);
725 } 728 }
726 729
727 @Override 730 @Override
......
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 +/*
20 + * Reference: RFC6020 and YANG ANTLR Grammar
21 + *
22 + * ABNF grammar as per RFC6020
23 + * bit-stmt = bit-keyword sep identifier-arg-str optsep
24 + * (";" /
25 + * "{" stmtsep
26 + * ;; these stmts can appear in any order
27 + * [position-stmt stmtsep]
28 + * [status-stmt stmtsep]
29 + * [description-stmt stmtsep]
30 + * [reference-stmt stmtsep]
31 + * "}"
32 + * "}")
33 + *
34 + * ANTLR grammar rule
35 + * bitStatement : BIT_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE bitBodyStatement RIGHT_CURLY_BRACE);
36 + *
37 + * bitBodyStatement : positionStatement? statusStatement? descriptionStatement? referenceStatement?
38 + * | positionStatement? statusStatement? referenceStatement? descriptionStatement?
39 + * | positionStatement? descriptionStatement? statusStatement? referenceStatement?
40 + * | positionStatement? descriptionStatement? referenceStatement? statusStatement?
41 + * | positionStatement? referenceStatement? statusStatement? descriptionStatement?
42 + * | positionStatement? referenceStatement? descriptionStatement? statusStatement?
43 + * | statusStatement? positionStatement? descriptionStatement? referenceStatement?
44 + * | statusStatement? positionStatement? referenceStatement? descriptionStatement?
45 + * | statusStatement? descriptionStatement? descriptionStatement? positionStatement?
46 + * | statusStatement? descriptionStatement? positionStatement? descriptionStatement?
47 + * | statusStatement? referenceStatement? positionStatement? descriptionStatement?
48 + * | statusStatement? referenceStatement? descriptionStatement? positionStatement?
49 + * | descriptionStatement? positionStatement? statusStatement? referenceStatement?
50 + * | descriptionStatement? positionStatement? referenceStatement? statusStatement?
51 + * | descriptionStatement? statusStatement? positionStatement? referenceStatement?
52 + * | descriptionStatement? statusStatement? referenceStatement? positionStatement?
53 + * | descriptionStatement? referenceStatement? positionStatement? statusStatement?
54 + * | descriptionStatement? referenceStatement? statusStatement? positionStatement?
55 + * | referenceStatement? positionStatement? descriptionStatement? statusStatement?
56 + * | referenceStatement? positionStatement? statusStatement? descriptionStatement?
57 + * | referenceStatement? statusStatement? descriptionStatement? positionStatement?
58 + * | referenceStatement? statusStatement? positionStatement? descriptionStatement?
59 + * | referenceStatement? descriptionStatement? positionStatement? statusStatement?
60 + * | referenceStatement? descriptionStatement? statusStatement? positionStatement?
61 + * ;
62 + */
63 +
64 +import org.onosproject.yangutils.datamodel.YangBit;
65 +import org.onosproject.yangutils.datamodel.YangBits;
66 +import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
67 +import org.onosproject.yangutils.parser.Parsable;
68 +import static org.onosproject.yangutils.parser.ParsableDataType.BIT_DATA;
69 +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
70 +import org.onosproject.yangutils.parser.exceptions.ParserException;
71 +import org.onosproject.yangutils.parser.impl.TreeWalkListener;
72 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
73 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
74 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
75 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
76 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_CONTENT;
77 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
78 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
79 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
80 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
81 +
82 +/**
83 + * Implements listener based call back function corresponding to the "bit"
84 + * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
85 + */
86 +public final class BitListener {
87 +
88 + /**
89 + * Creates a new bit listener.
90 + */
91 + private BitListener() {
92 + }
93 +
94 + /**
95 + * It is called when parser enters grammar rule (bit), it perform
96 + * validations and updates the data model tree.
97 + *
98 + * @param listener listener's object.
99 + * @param ctx context object of the grammar rule.
100 + */
101 + public static void processBitEntry(TreeWalkListener listener,
102 + GeneratedYangParser.BitStatementContext ctx) {
103 +
104 + // Check for stack to be non empty.
105 + checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), ENTRY);
106 +
107 + YangBit bitNode = new YangBit();
108 + bitNode.setBitName(ctx.IDENTIFIER().getText());
109 + listener.getParsedDataStack().push(bitNode);
110 + }
111 +
112 + /**
113 + * It is called when parser exits from grammar rule (bit), it perform
114 + * validations and update the data model tree.
115 + *
116 + * @param listener Listener's object.
117 + * @param ctx context object of the grammar rule.
118 + */
119 + public static void processBitExit(TreeWalkListener listener,
120 + GeneratedYangParser.BitStatementContext ctx) {
121 +
122 + // Check for stack to be non empty.
123 + checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT);
124 +
125 + Parsable tmpBitNode = listener.getParsedDataStack().peek();
126 + if (tmpBitNode instanceof YangBit) {
127 + listener.getParsedDataStack().pop();
128 +
129 + // Check for stack to be non empty.
130 + checkStackIsNotEmpty(listener, MISSING_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT);
131 +
132 + Parsable tmpNode = listener.getParsedDataStack().peek();
133 + switch (tmpNode.getParsableDataType()) {
134 + case BITS_DATA: {
135 + YangBits yangBits = (YangBits) tmpNode;
136 + if ((ctx.bitBodyStatement() == null) || (ctx.bitBodyStatement().positionStatement() == null)) {
137 + int maxPosition = 0;
138 + boolean isPositionPresent = false;
139 +
140 + for (YangBit curBit : yangBits.getBitSet()) {
141 + if (maxPosition <= curBit.getPosition()) {
142 + maxPosition = curBit.getPosition();
143 + isPositionPresent = true;
144 + }
145 + }
146 + if (isPositionPresent) {
147 + maxPosition++;
148 + }
149 + ((YangBit) tmpBitNode).setPosition(maxPosition);
150 + }
151 + try {
152 + yangBits.addBitInfo((YangBit) tmpBitNode);
153 + } catch (DataModelException e) {
154 + ParserException parserException = new ParserException(constructExtendedListenerErrorMessage(
155 + INVALID_CONTENT, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT, e.getMessage()));
156 + parserException.setLine(ctx.IDENTIFIER().getSymbol().getLine());
157 + parserException.setCharPosition(ctx.IDENTIFIER().getSymbol().getCharPositionInLine());
158 + throw parserException;
159 + }
160 + break;
161 + }
162 + default:
163 + throw new ParserException(
164 + constructListenerErrorMessage(INVALID_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT));
165 + }
166 + } else {
167 + throw new ParserException(
168 + constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BIT_DATA, ctx.IDENTIFIER().getText(), EXIT));
169 + }
170 + }
171 +}
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 +/*
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 + * bits-specification = 1*(bit-stmt stmtsep)
34 + *
35 + * ANTLR grammar rule
36 + *
37 + * typeBodyStatements : numericalRestrictions | stringRestrictions | enumSpecification
38 + * | leafrefSpecification | identityrefSpecification | instanceIdentifierSpecification
39 + * | bitsSpecification | unionSpecification;
40 + *
41 + * bitsSpecification : bitStatement+;
42 + */
43 +
44 +import org.onosproject.yangutils.datamodel.YangBits;
45 +import org.onosproject.yangutils.datamodel.YangLeaf;
46 +import org.onosproject.yangutils.datamodel.YangLeafList;
47 +import org.onosproject.yangutils.datamodel.YangType;
48 +import org.onosproject.yangutils.parser.Parsable;
49 +import static org.onosproject.yangutils.parser.ParsableDataType.BITS_DATA;
50 +import static org.onosproject.yangutils.parser.ParsableDataType.TYPE_DATA;
51 +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
52 +import org.onosproject.yangutils.parser.exceptions.ParserException;
53 +import org.onosproject.yangutils.parser.impl.TreeWalkListener;
54 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
55 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
56 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
57 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
58 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
59 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
60 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
61 +
62 +/**
63 + * Implements listener based call back function corresponding to the "bits"
64 + * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
65 + */
66 +public final class BitsListener {
67 +
68 + /**
69 + * Creates a new bits listener.
70 + */
71 + private BitsListener() {
72 + }
73 +
74 + /**
75 + * It is called when parser enters grammar rule (bits), it perform
76 + * validations and updates the data model tree.
77 + *
78 + * @param listener listener's object.
79 + * @param ctx context object of the grammar rule.
80 + */
81 + public static void processBitsEntry(TreeWalkListener listener,
82 + GeneratedYangParser.BitsSpecificationContext ctx) {
83 +
84 + // Check for stack to be non empty.
85 + checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", ENTRY);
86 +
87 + if (listener.getParsedDataStack().peek() instanceof YangType) {
88 + YangBits bitsNode = new YangBits();
89 + Parsable typeData = listener.getParsedDataStack().pop();
90 +
91 + // Check for stack to be non empty.
92 + checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", ENTRY);
93 +
94 + Parsable tmpData = listener.getParsedDataStack().peek();
95 +
96 + switch (tmpData.getParsableDataType()) {
97 + case LEAF_DATA:
98 + bitsNode.setBitsName(((YangLeaf) tmpData).getLeafName());
99 + break;
100 + case LEAF_LIST_DATA:
101 + bitsNode.setBitsName(((YangLeafList) tmpData).getLeafName());
102 + break;
103 + // TODO typedef, union, deviate.
104 + default:
105 + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, TYPE_DATA,
106 + ((YangType) typeData).getDataTypeName(), ENTRY));
107 + }
108 + listener.getParsedDataStack().push(typeData);
109 + listener.getParsedDataStack().push(bitsNode);
110 + } else {
111 + throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, BITS_DATA, "", ENTRY));
112 + }
113 + }
114 +
115 + /**
116 + * It is called when parser exits from grammar rule (bits), it perform
117 + * validations and update the data model tree.
118 + *
119 + * @param listener Listener's object.
120 + * @param ctx context object of the grammar rule.
121 + */
122 + public static void processBitsExit(TreeWalkListener listener,
123 + GeneratedYangParser.BitsSpecificationContext ctx) {
124 +
125 + // Check for stack to be non empty.
126 + checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", EXIT);
127 +
128 + Parsable tmpBitsNode = listener.getParsedDataStack().peek();
129 + if (tmpBitsNode instanceof YangBits) {
130 + listener.getParsedDataStack().pop();
131 +
132 + // Check for stack to be non empty.
133 + checkStackIsNotEmpty(listener, MISSING_HOLDER, BITS_DATA, "", EXIT);
134 +
135 + Parsable tmpNode = listener.getParsedDataStack().peek();
136 + switch (tmpNode.getParsableDataType()) {
137 + case TYPE_DATA: {
138 + YangType typeNode = (YangType) tmpNode;
139 + typeNode.setDataTypeExtendedInfo((YangBits) tmpBitsNode);
140 + break;
141 + }
142 + default:
143 + throw new ParserException(
144 + constructListenerErrorMessage(INVALID_HOLDER, BITS_DATA, "", EXIT));
145 + }
146 + } else {
147 + throw new ParserException(
148 + constructListenerErrorMessage(MISSING_CURRENT_HOLDER, BITS_DATA, "", EXIT));
149 + }
150 + }
151 +}
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 +/*
20 + * Reference: RFC6020 and YANG ANTLR Grammar
21 + *
22 + * ABNF grammar as per RFC6020
23 + * position-stmt = position-keyword sep
24 + * position-value-arg-str stmtend
25 + * position-value-arg-str = < a string that matches the rule
26 + * position-value-arg >
27 + * position-value-arg = non-negative-integer-value
28 + * non-negative-integer-value = "0" / positive-integer-value
29 + * positive-integer-value = (non-zero-digit *DIGIT)
30 + * zero-integer-value = 1*DIGIT
31 + *
32 + * ANTLR grammar rule
33 + * positionStatement : POSITION_KEYWORD INTEGER STMTEND;
34 + */
35 +
36 +import org.onosproject.yangutils.datamodel.YangBit;
37 +import org.onosproject.yangutils.datamodel.YangBits;
38 +import org.onosproject.yangutils.parser.Parsable;
39 +import static org.onosproject.yangutils.parser.ParsableDataType.POSITION_DATA;
40 +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
41 +import org.onosproject.yangutils.parser.exceptions.ParserException;
42 +import org.onosproject.yangutils.parser.impl.TreeWalkListener;
43 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
44 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
45 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
46 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
47 +import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
48 +
49 +/**
50 + * Implements listener based call back function corresponding to the "position"
51 + * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
52 + */
53 +public final class PositionListener {
54 +
55 + // Exact message in case position is invalid.
56 + private static String errMsg;
57 +
58 + /**
59 + * Creates a new position listener.
60 + */
61 + private PositionListener() {
62 + }
63 +
64 + /**
65 + * It is called when parser receives an input matching the grammar rule
66 + * (position), perform validations and update the data model tree.
67 + *
68 + * @param listener Listener's object.
69 + * @param ctx context object of the grammar rule.
70 + */
71 + public static void processPositionEntry(TreeWalkListener listener,
72 + GeneratedYangParser.PositionStatementContext ctx) {
73 +
74 + // Check for stack to be non empty.
75 + checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY);
76 +
77 + // Obtain the node of the stack.
78 + Parsable tmpNode = listener.getParsedDataStack().peek();
79 + switch (tmpNode.getParsableDataType()) {
80 + case BIT_DATA: {
81 + YangBit bitNode = (YangBit) tmpNode;
82 + if (!isBitPositionValid(listener, ctx)) {
83 + ParserException parserException = new ParserException(errMsg);
84 + parserException.setLine(ctx.INTEGER().getSymbol().getLine());
85 + parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine());
86 + throw parserException;
87 + }
88 + bitNode.setPosition(Integer.valueOf(ctx.INTEGER().getText()));
89 + break;
90 + }
91 + default:
92 + throw new ParserException(
93 + constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY));
94 + }
95 + }
96 +
97 + /**
98 + * Validates BITS position value correctness and uniqueness.
99 + *
100 + * @param listener Listener's object.
101 + * @param ctx context object of the grammar rule.
102 + * @return validation result
103 + */
104 + private static boolean isBitPositionValid(TreeWalkListener listener,
105 + GeneratedYangParser.PositionStatementContext ctx) {
106 + Parsable bitNode = listener.getParsedDataStack().pop();
107 +
108 + // Check for stack to be non empty.
109 + checkStackIsNotEmpty(listener, MISSING_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY);
110 +
111 + if (Integer.valueOf(ctx.INTEGER().getText()) < 0) {
112 + errMsg = "Negative value of position is invalid";
113 + listener.getParsedDataStack().push(bitNode);
114 + return false;
115 + }
116 +
117 + Parsable tmpNode = listener.getParsedDataStack().peek();
118 + switch (tmpNode.getParsableDataType()) {
119 + case BITS_DATA: {
120 + YangBits yangBits = (YangBits) tmpNode;
121 + for (YangBit curBit : yangBits.getBitSet()) {
122 + if (Integer.valueOf(ctx.INTEGER().getText()) == curBit.getPosition()) {
123 + errMsg = "Duplicate value of position is invalid";
124 + listener.getParsedDataStack().push(bitNode);
125 + return false;
126 + }
127 + }
128 + listener.getParsedDataStack().push(bitNode);
129 + return true;
130 + }
131 + default:
132 + listener.getParsedDataStack().push(bitNode);
133 + throw new ParserException(
134 + constructListenerErrorMessage(INVALID_HOLDER, POSITION_DATA, ctx.INTEGER().getText(), ENTRY));
135 + }
136 + }
137 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -34,7 +34,6 @@ import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; ...@@ -34,7 +34,6 @@ import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
34 import org.onosproject.yangutils.parser.exceptions.ParserException; 34 import org.onosproject.yangutils.parser.exceptions.ParserException;
35 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 35 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
36 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; 36 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
37 -import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
38 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; 37 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
39 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; 38 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
40 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; 39 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
...@@ -70,7 +69,7 @@ public final class ValueListener { ...@@ -70,7 +69,7 @@ public final class ValueListener {
70 case ENUM_DATA: { 69 case ENUM_DATA: {
71 YangEnum enumNode = (YangEnum) tmpNode; 70 YangEnum enumNode = (YangEnum) tmpNode;
72 if (!isEnumValueValid(listener, ctx)) { 71 if (!isEnumValueValid(listener, ctx)) {
73 - ParserException parserException = new ParserException("Input version not supported"); 72 + ParserException parserException = new ParserException("Duplicate Value Entry");
74 parserException.setLine(ctx.INTEGER().getSymbol().getLine()); 73 parserException.setLine(ctx.INTEGER().getSymbol().getLine());
75 parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine()); 74 parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine());
76 throw parserException; 75 throw parserException;
...@@ -93,6 +92,10 @@ public final class ValueListener { ...@@ -93,6 +92,10 @@ public final class ValueListener {
93 */ 92 */
94 private static boolean isEnumValueValid(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) { 93 private static boolean isEnumValueValid(TreeWalkListener listener, GeneratedYangParser.ValueStatementContext ctx) {
95 Parsable enumNode = listener.getParsedDataStack().pop(); 94 Parsable enumNode = listener.getParsedDataStack().pop();
95 +
96 + // Check for stack to be non empty.
97 + checkStackIsNotEmpty(listener, MISSING_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY);
98 +
96 Parsable tmpNode = listener.getParsedDataStack().peek(); 99 Parsable tmpNode = listener.getParsedDataStack().peek();
97 switch (tmpNode.getParsableDataType()) { 100 switch (tmpNode.getParsableDataType()) {
98 case ENUMERATION_DATA: { 101 case ENUMERATION_DATA: {
...@@ -109,7 +112,7 @@ public final class ValueListener { ...@@ -109,7 +112,7 @@ public final class ValueListener {
109 default: 112 default:
110 listener.getParsedDataStack().push(enumNode); 113 listener.getParsedDataStack().push(enumNode);
111 throw new ParserException( 114 throw new ParserException(
112 - constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), EXIT)); 115 + constructListenerErrorMessage(INVALID_HOLDER, VALUE_DATA, ctx.INTEGER().getText(), ENTRY));
113 } 116 }
114 } 117 }
115 } 118 }
......
...@@ -55,6 +55,11 @@ public enum ListenerErrorType { ...@@ -55,6 +55,11 @@ public enum ListenerErrorType {
55 DUPLICATE_ENTRY(), 55 DUPLICATE_ENTRY(),
56 56
57 /** 57 /**
58 + * Represents that the content is invalid.
59 + */
60 + INVALID_CONTENT(),
61 +
62 + /**
58 * Represents that some of earlier parsed data is not handled correctly. 63 * Represents that some of earlier parsed data is not handled correctly.
59 */ 64 */
60 UNHANDLED_PARSED_DATA(); 65 UNHANDLED_PARSED_DATA();
...@@ -80,6 +85,8 @@ public enum ListenerErrorType { ...@@ -80,6 +85,8 @@ public enum ListenerErrorType {
80 return "Invalid cardinality in"; 85 return "Invalid cardinality in";
81 case DUPLICATE_ENTRY: 86 case DUPLICATE_ENTRY:
82 return "Duplicate"; 87 return "Duplicate";
88 + case INVALID_CONTENT:
89 + return "Invalid content in";
83 case UNHANDLED_PARSED_DATA: 90 case UNHANDLED_PARSED_DATA:
84 return "Unhandled parsed data at"; 91 return "Unhandled parsed data at";
85 default: 92 default:
......
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.YangBit;
23 +import org.onosproject.yangutils.datamodel.YangBits;
24 +import org.onosproject.yangutils.datamodel.YangDataTypes;
25 +import org.onosproject.yangutils.datamodel.YangLeaf;
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 +import java.util.ListIterator;
34 +import java.util.Set;
35 +
36 +/**
37 + * Test cases for bit listener.
38 + */
39 +public class BitListenerTest {
40 +
41 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
42 +
43 + /**
44 + * Checks bit statement without position.
45 + */
46 + @Test
47 + public void processBitTypeStatement() throws IOException, ParserException {
48 +
49 + YangNode node = manager.getDataModel("src/test/resources/BitTypeStatement.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 + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
62 + YangLeaf leafInfo = leafIterator.next();
63 +
64 + assertThat(leafInfo.getLeafName(), is("mybits"));
65 + assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
66 + assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
67 + assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
68 + is("mybits"));
69 +
70 + Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
71 + for (YangBit tmp : bitSet) {
72 + if (tmp.getBitName().equals("disable-nagle")) {
73 + assertThat(tmp.getPosition(), is(0));
74 + } else if (tmp.getBitName().equals("auto-sense-speed")) {
75 + assertThat(tmp.getPosition(), is(1));
76 + } else if (tmp.getBitName().equals("Ten-Mb-only")) {
77 + assertThat(tmp.getPosition(), is(2));
78 + }
79 + }
80 + }
81 +
82 + /**
83 + * Checks if enum with same name is not allowed.
84 + */
85 + @Test(expected = ParserException.class)
86 + public void processBitWithDuplicateName() throws IOException, ParserException {
87 +
88 + YangNode node = manager.getDataModel("src/test/resources/BitWithDuplicateName.yang");
89 + }
90 +}
...@@ -78,4 +78,13 @@ public class EnumListenerTest { ...@@ -78,4 +78,13 @@ public class EnumListenerTest {
78 } 78 }
79 } 79 }
80 } 80 }
81 +
82 + /**
83 + * Checks if enum with same name is not allowed.
84 + */
85 + @Test(expected = ParserException.class)
86 + public void processEnumWithDuplicateName() throws IOException, ParserException {
87 +
88 + YangNode node = manager.getDataModel("src/test/resources/EnumWithDuplicateName.yang");
89 + }
81 } 90 }
......
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.YangBit;
23 +import org.onosproject.yangutils.datamodel.YangBits;
24 +import org.onosproject.yangutils.datamodel.YangDataTypes;
25 +import org.onosproject.yangutils.datamodel.YangLeaf;
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 +import java.util.ListIterator;
34 +import java.util.Set;
35 +
36 +/**
37 + * Test cases for position listener.
38 + */
39 +public class PositionListenerTest {
40 +
41 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
42 +
43 + /**
44 + * Checks explicitly configured value.
45 + */
46 + @Test
47 + public void processPositionStatement() throws IOException, ParserException {
48 +
49 + YangNode node = manager.getDataModel("src/test/resources/PositionStatement.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 + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
62 + YangLeaf leafInfo = leafIterator.next();
63 +
64 + assertThat(leafInfo.getLeafName(), is("mybits"));
65 + assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
66 + assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
67 + assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
68 + is("mybits"));
69 +
70 + Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
71 + for (YangBit tmp : bitSet) {
72 + if (tmp.getBitName().equals("disable-nagle")) {
73 + assertThat(tmp.getPosition(), is(0));
74 + } else if (tmp.getBitName().equals("auto-sense-speed")) {
75 + assertThat(tmp.getPosition(), is(1));
76 + } else if (tmp.getBitName().equals("Ten-Mb-only")) {
77 + assertThat(tmp.getPosition(), is(2));
78 + }
79 + }
80 + }
81 +
82 + /**
83 + * Checks explicit value and auto generated value.
84 + */
85 + @Test
86 + public void processPositionImplicitAndExplicit() throws IOException, ParserException {
87 +
88 + YangNode node = manager.getDataModel("src/test/resources/PositionImplicitAndExplicit.yang");
89 +
90 + // Check whether the data model tree returned is of type module.
91 + assertThat((node instanceof YangModule), is(true));
92 +
93 + // Check whether the node type is set properly to module.
94 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
95 +
96 + // Check whether the module name is set correctly.
97 + YangModule yangNode = (YangModule) node;
98 + assertThat(yangNode.getName(), is("Test"));
99 +
100 + ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
101 + YangLeaf leafInfo = leafIterator.next();
102 +
103 + assertThat(leafInfo.getLeafName(), is("mybits"));
104 + assertThat(leafInfo.getDataType().getDataTypeName(), is("bits"));
105 + assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.BITS));
106 + assertThat(((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitsName(),
107 + is("mybits"));
108 +
109 + Set<YangBit> bitSet = ((YangBits) leafInfo.getDataType().getDataTypeExtendedInfo()).getBitSet();
110 + for (YangBit tmp : bitSet) {
111 + if (tmp.getBitName().equals("disable-nagle")) {
112 + assertThat(tmp.getPosition(), is(0));
113 + } else if (tmp.getBitName().equals("auto-sense-speed")) {
114 + assertThat(tmp.getPosition(), is(1));
115 + } else if (tmp.getBitName().equals("Ten-Mb-only")) {
116 + assertThat(tmp.getPosition(), is(2));
117 + }
118 + }
119 + }
120 +
121 + /**
122 + * Checks explicit value should not be repeated.
123 + */
124 + @Test(expected = ParserException.class)
125 + public void processPositionDuplication() throws IOException, ParserException {
126 +
127 + YangNode node = manager.getDataModel("src/test/resources/PositionDuplication.yang");
128 + }
129 +
130 + /**
131 + * Checks explicit or auto generated value should not be repeated.
132 + */
133 + @Test(expected = ParserException.class)
134 + public void processPositionImplicitAndExplicitDuplication() throws IOException, ParserException {
135 +
136 + YangNode node = manager.getDataModel("src/test/resources/PositionImplicitAndExplicitDuplication.yang");
137 + }
138 +
139 + /**
140 + * Checks if negative value of position is not allowed.
141 + */
142 + @Test(expected = ParserException.class)
143 + public void processPositionNegativeValue() throws IOException, ParserException {
144 +
145 + YangNode node = manager.getDataModel("src/test/resources/PositionNegativeValue.yang");
146 + }
147 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf mybits {
6 + type bits {
7 + bit disable-nagle;
8 + bit auto-sense-speed;
9 + bit Ten-Mb-only;
10 + }
11 + }
12 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf mybits {
6 + type bits {
7 + bit disable-nagle;
8 + bit disable-nagle;
9 + bit Ten-Mb-only;
10 + }
11 + }
12 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf speed {
6 + type enumeration {
7 + enum 10m;
8 + enum 100m;
9 + enum 10m {
10 + value 11;
11 + }
12 + }
13 + }
14 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf mybits {
6 + type bits {
7 + bit disable-nagle {
8 + position 0;
9 + }
10 + bit auto-sense-speed {
11 + position 1;
12 + }
13 + bit Ten-Mb-only {
14 + position 1;
15 + }
16 + }
17 + }
18 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf mybits {
6 + type bits {
7 + bit disable-nagle;
8 + bit auto-sense-speed {
9 + position 1;
10 + }
11 + bit Ten-Mb-only;
12 + }
13 + }
14 + }
15 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf mybits {
6 + type bits {
7 + bit disable-nagle;
8 + bit auto-sense-speed {
9 + position 0;
10 + }
11 + bit Ten-Mb-only;
12 + }
13 + }
14 + }
15 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf mybits {
6 + type bits {
7 + bit disable-nagle;
8 + bit auto-sense-speed {
9 + position -2;
10 + }
11 + bit Ten-Mb-only;
12 + }
13 + }
14 + }
15 +}
1 +module Test {
2 + yang-version 1;
3 + namespace http://huawei.com;
4 + prefix Ant;
5 + leaf mybits {
6 + type bits {
7 + bit disable-nagle {
8 + position 0;
9 + }
10 + bit auto-sense-speed {
11 + position 1;
12 + }
13 + bit Ten-Mb-only {
14 + position 2;
15 + }
16 + }
17 + }
18 +}