Gaurav Agrawal
Committed by Gerrit Code Review

[ONOS-3880, 3881] Yang Listener for Module and Sub-Module

Change-Id: Iee75c3e04af9b66ebc38acb3396aa4c54af5a268
Showing 99 changed files with 3309 additions and 211 deletions
...@@ -130,7 +130,186 @@ public enum ParsableDataType { ...@@ -130,7 +130,186 @@ public enum ParsableDataType {
130 REVISION_DATA, 130 REVISION_DATA,
131 131
132 /** 132 /**
133 + * Identifies the YANG revision date parsed data.
134 + */
135 + REVISION_DATE_DATA,
136 +
137 + /**
133 * Identifies the YANG namespace parsed data. 138 * Identifies the YANG namespace parsed data.
134 */ 139 */
135 - NAMESPACE_DATA 140 + NAMESPACE_DATA,
136 -} 141 +
142 + /**
143 + * Identifies the YANG contact parsed data.
144 + */
145 + CONTACT_DATA,
146 +
147 + /**
148 + * Identifies the YANG config parsed data.
149 + */
150 + CONFIG_DATA,
151 +
152 + /**
153 + * Identifies the YANG description parsed data.
154 + */
155 + DESCRIPTION_DATA,
156 +
157 + /**
158 + * Identifies the YANG key parsed data.
159 + */
160 + KEY_DATA,
161 +
162 + /**
163 + * Identifies the YANG mandatory parsed data.
164 + */
165 + MANDATORY_DATA,
166 +
167 + /**
168 + * Identifies the YANG max element parsed data.
169 + */
170 + MAX_ELEMENT_DATA,
171 +
172 + /**
173 + * Identifies the YANG min element parsed data.
174 + */
175 + MIN_ELEMENT_DATA,
176 +
177 + /**
178 + * Identifies the YANG presence element parsed data.
179 + */
180 + PRESENCE_DATA,
181 +
182 + /**
183 + * Identifies the YANG reference element parsed data.
184 + */
185 + REFERENCE_DATA,
186 +
187 + /**
188 + * Identifies the YANG status element parsed data.
189 + */
190 + STATUS_DATA,
191 +
192 + /**
193 + * Identifies the YANG units element parsed data.
194 + */
195 + UNITS_DATA,
196 +
197 + /**
198 + * Identifies the YANG version element parsed data.
199 + */
200 + VERSION_DATA,
201 +
202 + /**
203 + * Identifies the YANG base element parsed data.
204 + */
205 + YANGBASE_DATA,
206 +
207 + /**
208 + * Identifies the YANG prefix element parsed data.
209 + */
210 + PREFIX_DATA,
211 +
212 + /**
213 + * Identifies the YANG default element parsed data.
214 + */
215 + DEFAULT_DATA,
216 +
217 + /**
218 + * Identifies the YANG organization parsed data.
219 + */
220 + ORGANIZATION_DATA;
221 +
222 + /**
223 + * Returns the YANG construct keyword corresponding to enum values.
224 + *
225 + * @param parsableDataType enum value for parsable data type.
226 + * @return YANG construct keyword.
227 + */
228 + public static String getParsableDataType(ParsableDataType parsableDataType) {
229 +
230 + switch (parsableDataType) {
231 + case MODULE_DATA:
232 + return "module";
233 + case SUB_MODULE_DATA:
234 + return "submodule";
235 + case TYPEDEF_DATA:
236 + return "typedef";
237 + case TYPE_DATA:
238 + return "type";
239 + case CHOICE_DATA:
240 + return "choice";
241 + case CASE_DATA:
242 + return "case";
243 + case ENUMERATION_DATA:
244 + return "enumeration";
245 + case GROUPING_DATA:
246 + return "grouping";
247 + case USES_DATA:
248 + return "uses";
249 + case AUGMENT_DATA:
250 + return "augment";
251 + case CONTAINER_DATA:
252 + return "container";
253 + case LIST_DATA:
254 + return "list";
255 + case BELONGS_TO_DATA:
256 + return "belongs-to";
257 + case BIT_DATA:
258 + return "bit";
259 + case BITS_DATA:
260 + return "bits";
261 + case ENUM_DATA:
262 + return "enum";
263 + case IMPORT_DATA:
264 + return "import";
265 + case INCLUDE_DATA:
266 + return "include";
267 + case LEAF_DATA:
268 + return "leaf";
269 + case LEAF_LIST_DATA:
270 + return "leaf-list";
271 + case MUST_DATA:
272 + return "must";
273 + case REVISION_DATA:
274 + return "revision";
275 + case REVISION_DATE_DATA:
276 + return "revision-date";
277 + case NAMESPACE_DATA:
278 + return "namespace";
279 + case CONTACT_DATA:
280 + return "contact";
281 + case CONFIG_DATA:
282 + return "config";
283 + case DESCRIPTION_DATA:
284 + return "description";
285 + case KEY_DATA:
286 + return "key";
287 + case MANDATORY_DATA:
288 + return "mandatory";
289 + case MAX_ELEMENT_DATA:
290 + return "max-elements";
291 + case MIN_ELEMENT_DATA:
292 + return "min-elements";
293 + case PRESENCE_DATA:
294 + return "presence";
295 + case REFERENCE_DATA:
296 + return "reference";
297 + case STATUS_DATA:
298 + return "status";
299 + case UNITS_DATA:
300 + return "units";
301 + case VERSION_DATA:
302 + return "version";
303 + case YANGBASE_DATA:
304 + return "yangbase";
305 + case PREFIX_DATA:
306 + return "prefix";
307 + case ORGANIZATION_DATA:
308 + return "organization";
309 + case DEFAULT_DATA:
310 + return "default";
311 + default:
312 + return "yang";
313 + }
314 + }
315 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -19,13 +19,12 @@ package org.onosproject.yangutils.parser.exceptions; ...@@ -19,13 +19,12 @@ package org.onosproject.yangutils.parser.exceptions;
19 /** 19 /**
20 * Base class for exceptions in parser operations. 20 * Base class for exceptions in parser operations.
21 */ 21 */
22 -public class ParserException extends Exception { 22 +public class ParserException extends RuntimeException {
23 23
24 private static final long serialVersionUID = 20160211L; 24 private static final long serialVersionUID = 20160211L;
25 private int lineNumber; 25 private int lineNumber;
26 private int charPositionInLine; 26 private int charPositionInLine;
27 private String fileName; 27 private String fileName;
28 - private String msg;
29 28
30 /** 29 /**
31 * Create a new parser exception. 30 * Create a new parser exception.
...@@ -90,15 +89,6 @@ public class ParserException extends Exception { ...@@ -90,15 +89,6 @@ public class ParserException extends Exception {
90 } 89 }
91 90
92 /** 91 /**
93 - * Returns msg detail of exception in string.
94 - *
95 - * @return msg detail of exception in string
96 - */
97 - public String getMsg() {
98 - return this.msg;
99 - }
100 -
101 - /**
102 * Sets line number of YANG file. 92 * Sets line number of YANG file.
103 * 93 *
104 * @param line line number of YANG file 94 * @param line line number of YANG file
...@@ -117,15 +107,6 @@ public class ParserException extends Exception { ...@@ -117,15 +107,6 @@ public class ParserException extends Exception {
117 } 107 }
118 108
119 /** 109 /**
120 - * Sets the detail of exception in string.
121 - *
122 - * @param msg the detail of exception in string
123 - */
124 - public void setMsg(String msg) {
125 - this.msg = msg;
126 - }
127 -
128 - /**
129 * Sets file name in parser exception. 110 * Sets file name in parser exception.
130 * 111 *
131 * @param fileName YANG file name 112 * @param fileName YANG file name
......
...@@ -69,16 +69,12 @@ public class YangUtilsParserManager implements YangUtilsParser { ...@@ -69,16 +69,12 @@ public class YangUtilsParserManager implements YangUtilsParser {
69 // Add customized error listener to catch errors during parsing. 69 // Add customized error listener to catch errors during parsing.
70 parser.addErrorListener(parseTreeErrorListener); 70 parser.addErrorListener(parseTreeErrorListener);
71 71
72 - // Begin parsing YANG file and generate parse tree. 72 + ParseTree tree;
73 - ParseTree tree = parser.yangfile();
74 73
75 - /** 74 + try {
76 - * Throws an parser Exception if exception flag is set i.e. exception has 75 + // Begin parsing YANG file and generate parse tree.
77 - * occurred during parsing. 76 + tree = parser.yangfile();
78 - */ 77 + } catch (ParserException parserException) {
79 - if (parseTreeErrorListener.isExceptionFlag()) {
80 - // Get the exception occurred during parsing.
81 - ParserException parserException = parseTreeErrorListener.getParserException();
82 parserException.setFileName(yangFile); 78 parserException.setFileName(yangFile);
83 throw parserException; 79 throw parserException;
84 } 80 }
...@@ -93,18 +89,17 @@ public class YangUtilsParserManager implements YangUtilsParser { ...@@ -93,18 +89,17 @@ public class YangUtilsParserManager implements YangUtilsParser {
93 * Walk parse tree, provide call backs to methods in listener and 89 * Walk parse tree, provide call backs to methods in listener and
94 * build data model tree. 90 * build data model tree.
95 */ 91 */
96 - walker.walk(treeWalker, tree); 92 + try {
97 - 93 + walker.walk(treeWalker, tree);
98 - // Throws an parser exception which has occurred during listener walk. 94 + } catch (ParserException listenerException) {
99 - if (treeWalker.getErrorInformation().isErrorFlag()) { 95 + // TODO free incomplete data model tree.
100 - // Create object of listener exception
101 - ParserException listenerException = new ParserException();
102 - listenerException.setMsg(treeWalker.getErrorInformation().getErrorMsg());
103 listenerException.setFileName(yangFile); 96 listenerException.setFileName(yangFile);
104 throw listenerException; 97 throw listenerException;
98 + } finally {
99 + // TODO free parsable stack
105 } 100 }
106 101
107 // Returns the Root Node of the constructed data model tree. 102 // Returns the Root Node of the constructed data model tree.
108 return treeWalker.getRootNode(); 103 return treeWalker.getRootNode();
109 } 104 }
110 -} 105 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,8 +16,17 @@ ...@@ -16,8 +16,17 @@
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.YangModule;
20 +import org.onosproject.yangutils.datamodel.YangNode;
21 +import org.onosproject.yangutils.datamodel.YangSubModule;
22 +import org.onosproject.yangutils.parser.ParsableDataType;
19 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 23 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
24 +import org.onosproject.yangutils.parser.exceptions.ParserException;
20 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 25 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
26 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
29 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
21 30
22 /* 31 /*
23 * Reference: RFC6020 and YANG ANTLR Grammar 32 * Reference: RFC6020 and YANG ANTLR Grammar
...@@ -48,7 +57,11 @@ public final class BaseFileListener { ...@@ -48,7 +57,11 @@ public final class BaseFileListener {
48 * @param ctx context object of the grammar rule. 57 * @param ctx context object of the grammar rule.
49 */ 58 */
50 public static void processYangFileEntry(TreeWalkListener listener, GeneratedYangParser.YangfileContext ctx) { 59 public static void processYangFileEntry(TreeWalkListener listener, GeneratedYangParser.YangfileContext ctx) {
51 - // TODO method implementation 60 +
61 + // Check if stack is empty.
62 + ListenerValidation.checkStackIsEmpty(listener, ListenerErrorType.INVALID_HOLDER,
63 + ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.ENTRY);
64 +
52 } 65 }
53 66
54 /** 67 /**
...@@ -59,6 +72,25 @@ public final class BaseFileListener { ...@@ -59,6 +72,25 @@ public final class BaseFileListener {
59 * @param ctx context object of the grammar rule. 72 * @param ctx context object of the grammar rule.
60 */ 73 */
61 public static void processYangFileExit(TreeWalkListener listener, GeneratedYangParser.YangfileContext ctx) { 74 public static void processYangFileExit(TreeWalkListener listener, GeneratedYangParser.YangfileContext ctx) {
62 - // TODO method implementation 75 +
76 + // Check for stack to be non empty.
77 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
78 + ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.EXIT);
79 +
80 + // Data Model tree root node is set.
81 + if (listener.getParsedDataStack().peek() instanceof YangModule
82 + | listener.getParsedDataStack().peek() instanceof YangSubModule) {
83 + listener.setRootNode((YangNode) listener.getParsedDataStack().pop());
84 + } else {
85 + throw new ParserException(
86 + ListenerErrorMessageConstruction
87 + .constructListenerErrorMessage(ListenerErrorType.INVALID_CHILD,
88 + ParsableDataType.YANGBASE_DATA, "",
89 + ListenerErrorLocation.EXIT));
90 + }
91 +
92 + // Check if stack is empty.
93 + ListenerValidation.checkStackIsEmpty(listener, ListenerErrorType.INVALID_HOLDER,
94 + ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.EXIT);
63 } 95 }
64 -} 96 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,8 +16,17 @@ ...@@ -16,8 +16,17 @@
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.YangBelongsTo;
20 +import org.onosproject.yangutils.datamodel.YangSubModule;
21 +import org.onosproject.yangutils.parser.Parsable;
22 +import org.onosproject.yangutils.parser.ParsableDataType;
19 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 23 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
24 +import org.onosproject.yangutils.parser.exceptions.ParserException;
20 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 25 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
26 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
29 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
21 30
22 /* 31 /*
23 * Reference: RFC6020 and YANG ANTLR Grammar 32 * Reference: RFC6020 and YANG ANTLR Grammar
...@@ -64,7 +73,18 @@ public final class BelongsToListener { ...@@ -64,7 +73,18 @@ public final class BelongsToListener {
64 */ 73 */
65 public static void processBelongsToEntry(TreeWalkListener listener, 74 public static void processBelongsToEntry(TreeWalkListener listener,
66 GeneratedYangParser.BelongstoStatementContext ctx) { 75 GeneratedYangParser.BelongstoStatementContext ctx) {
67 - // TODO method implementation 76 +
77 + // Check for stack to be non empty.
78 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
79 + ParsableDataType.BELONGS_TO_DATA,
80 + String.valueOf(ctx.IDENTIFIER().getText()),
81 + ListenerErrorLocation.ENTRY);
82 +
83 + YangBelongsTo belongstoNode = new YangBelongsTo();
84 + belongstoNode.setBelongsToModuleName(String.valueOf(ctx.IDENTIFIER().getText()));
85 +
86 + // Push belongsto into the stack.
87 + listener.getParsedDataStack().push(belongstoNode);
68 } 88 }
69 89
70 /** 90 /**
...@@ -76,6 +96,47 @@ public final class BelongsToListener { ...@@ -76,6 +96,47 @@ public final class BelongsToListener {
76 */ 96 */
77 public static void processBelongsToExit(TreeWalkListener listener, 97 public static void processBelongsToExit(TreeWalkListener listener,
78 GeneratedYangParser.BelongstoStatementContext ctx) { 98 GeneratedYangParser.BelongstoStatementContext ctx) {
79 - // TODO method implementation 99 +
100 + // Check for stack to be non empty.
101 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
102 + ParsableDataType.BELONGS_TO_DATA,
103 + String.valueOf(ctx.IDENTIFIER().getText()),
104 + ListenerErrorLocation.EXIT);
105 +
106 + Parsable tmpBelongstoNode = listener.getParsedDataStack().peek();
107 + if (tmpBelongstoNode instanceof YangBelongsTo) {
108 + listener.getParsedDataStack().pop();
109 +
110 + // Check for stack to be empty.
111 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
112 + ParsableDataType.BELONGS_TO_DATA,
113 + String.valueOf(ctx.IDENTIFIER().getText()),
114 + ListenerErrorLocation.EXIT);
115 +
116 + Parsable tmpNode = listener.getParsedDataStack().peek();
117 + switch (tmpNode.getParsableDataType()) {
118 + case SUB_MODULE_DATA: {
119 + YangSubModule subModule = (YangSubModule) tmpNode;
120 + subModule.setBelongsTo((YangBelongsTo) tmpBelongstoNode);
121 + break;
122 + }
123 + default:
124 + throw new ParserException(
125 + ListenerErrorMessageConstruction
126 + .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
127 + ParsableDataType.BELONGS_TO_DATA,
128 + String.valueOf(ctx.IDENTIFIER()
129 + .getText()),
130 + ListenerErrorLocation.EXIT));
131 + }
132 + } else {
133 + throw new ParserException(
134 + ListenerErrorMessageConstruction
135 + .constructListenerErrorMessage(ListenerErrorType.MISSING_CURRENT_HOLDER,
136 + ParsableDataType.BELONGS_TO_DATA,
137 + String.valueOf(ctx.IDENTIFIER()
138 + .getText()),
139 + ListenerErrorLocation.EXIT));
140 + }
80 } 141 }
81 -} 142 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,8 +16,17 @@ ...@@ -16,8 +16,17 @@
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.YangModule;
20 +import org.onosproject.yangutils.datamodel.YangSubModule;
21 +import org.onosproject.yangutils.parser.Parsable;
22 +import org.onosproject.yangutils.parser.ParsableDataType;
19 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 23 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
24 +import org.onosproject.yangutils.parser.exceptions.ParserException;
20 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 25 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
26 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
29 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
21 30
22 /* 31 /*
23 * Reference: RFC6020 and YANG ANTLR Grammar 32 * Reference: RFC6020 and YANG ANTLR Grammar
...@@ -28,8 +37,7 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener; ...@@ -28,8 +37,7 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener;
28 * [contact-stmt stmtsep] 37 * [contact-stmt stmtsep]
29 * [description-stmt stmtsep] 38 * [description-stmt stmtsep]
30 * [reference-stmt stmtsep] 39 * [reference-stmt stmtsep]
31 - * organization-stmt = organization-keyword sep string 40 + * contact-stmt = contact-keyword sep string optsep stmtend
32 - * optsep stmtend
33 * 41 *
34 * ANTLR grammar rule 42 * ANTLR grammar rule
35 * meta_stmts : organization_stmt? contact_stmt? description_stmt? reference_stmt? 43 * meta_stmts : organization_stmt? contact_stmt? description_stmt? reference_stmt?
...@@ -57,7 +65,7 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener; ...@@ -57,7 +65,7 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener;
57 * | description_stmt? organization_stmt? contact_stmt? reference_stmt? 65 * | description_stmt? organization_stmt? contact_stmt? reference_stmt?
58 * | description_stmt? organization_stmt? reference_stmt? contact_stmt? 66 * | description_stmt? organization_stmt? reference_stmt? contact_stmt?
59 * ; 67 * ;
60 - * organization_stmt : ORGANIZATION_KEYWORD string STMTEND; 68 + * contact_stmt : CONTACT_KEYWORD string STMTEND;
61 */ 69 */
62 70
63 /** 71 /**
...@@ -81,6 +89,32 @@ public final class ContactListener { ...@@ -81,6 +89,32 @@ public final class ContactListener {
81 * @param ctx context object of the grammar rule. 89 * @param ctx context object of the grammar rule.
82 */ 90 */
83 public static void processContactEntry(TreeWalkListener listener, GeneratedYangParser.ContactStatementContext ctx) { 91 public static void processContactEntry(TreeWalkListener listener, GeneratedYangParser.ContactStatementContext ctx) {
84 - // TODO method implementation 92 +
93 + // Check for stack to be non empty.
94 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
95 + ParsableDataType.CONTACT_DATA,
96 + String.valueOf(ctx.string().getText()), ListenerErrorLocation.ENTRY);
97 +
98 + // Obtain the node of the stack.
99 + Parsable tmpNode = listener.getParsedDataStack().peek();
100 + switch (tmpNode.getParsableDataType()) {
101 + case MODULE_DATA: {
102 + YangModule module = (YangModule) tmpNode;
103 + module.setContact(String.valueOf(ctx.string().getText()));
104 + break;
105 + }
106 + case SUB_MODULE_DATA: {
107 + YangSubModule subModule = (YangSubModule) tmpNode;
108 + subModule.setContact(String.valueOf(ctx.string().getText()));
109 + break;
110 + }
111 + default:
112 + throw new ParserException(
113 + ListenerErrorMessageConstruction
114 + .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
115 + ParsableDataType.CONTACT_DATA,
116 + String.valueOf(ctx.string().getText()),
117 + ListenerErrorLocation.ENTRY));
118 + }
85 } 119 }
86 -} 120 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,8 +16,18 @@ ...@@ -16,8 +16,18 @@
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.YangImport;
20 +import org.onosproject.yangutils.datamodel.YangModule;
21 +import org.onosproject.yangutils.datamodel.YangSubModule;
22 +import org.onosproject.yangutils.parser.Parsable;
23 +import org.onosproject.yangutils.parser.ParsableDataType;
19 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 24 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
25 +import org.onosproject.yangutils.parser.exceptions.ParserException;
20 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 26 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
27 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
28 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
29 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
30 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
21 31
22 /* 32 /*
23 * Reference: RFC6020 and YANG ANTLR Grammar 33 * Reference: RFC6020 and YANG ANTLR Grammar
...@@ -62,7 +72,18 @@ public final class ImportListener { ...@@ -62,7 +72,18 @@ public final class ImportListener {
62 * @param ctx context object of the grammar rule. 72 * @param ctx context object of the grammar rule.
63 */ 73 */
64 public static void processImportEntry(TreeWalkListener listener, GeneratedYangParser.ImportStatementContext ctx) { 74 public static void processImportEntry(TreeWalkListener listener, GeneratedYangParser.ImportStatementContext ctx) {
65 - // TODO method implementation 75 +
76 + // Check for stack to be non empty.
77 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
78 + ParsableDataType.IMPORT_DATA,
79 + String.valueOf(ctx.IDENTIFIER().getText()),
80 + ListenerErrorLocation.ENTRY);
81 +
82 + YangImport importNode = new YangImport();
83 + importNode.setModuleName(String.valueOf(ctx.IDENTIFIER().getText()));
84 +
85 + // Push import node to the stack.
86 + listener.getParsedDataStack().push(importNode);
66 } 87 }
67 88
68 /** 89 /**
...@@ -73,6 +94,52 @@ public final class ImportListener { ...@@ -73,6 +94,52 @@ public final class ImportListener {
73 * @param ctx context object of the grammar rule. 94 * @param ctx context object of the grammar rule.
74 */ 95 */
75 public static void processImportExit(TreeWalkListener listener, GeneratedYangParser.ImportStatementContext ctx) { 96 public static void processImportExit(TreeWalkListener listener, GeneratedYangParser.ImportStatementContext ctx) {
76 - // TODO method implementation 97 +
98 + // Check for stack to be non empty.
99 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
100 + ParsableDataType.IMPORT_DATA,
101 + String.valueOf(ctx.IDENTIFIER().getText()),
102 + ListenerErrorLocation.EXIT);
103 +
104 + Parsable tmpImportNode = listener.getParsedDataStack().peek();
105 + if (tmpImportNode instanceof YangImport) {
106 + listener.getParsedDataStack().pop();
107 +
108 + // Check for stack to be non empty.
109 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
110 + ParsableDataType.IMPORT_DATA,
111 + String.valueOf(ctx.IDENTIFIER().getText()),
112 + ListenerErrorLocation.EXIT);
113 +
114 + Parsable tmpNode = listener.getParsedDataStack().peek();
115 + switch (tmpNode.getParsableDataType()) {
116 + case MODULE_DATA: {
117 + YangModule module = (YangModule) tmpNode;
118 + module.addImportedInfo((YangImport) tmpImportNode);
119 + break;
120 + }
121 + case SUB_MODULE_DATA: {
122 + YangSubModule subModule = (YangSubModule) tmpNode;
123 + subModule.addImportedInfo((YangImport) tmpImportNode);
124 + break;
125 + }
126 + default:
127 + throw new ParserException(
128 + ListenerErrorMessageConstruction
129 + .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
130 + ParsableDataType.IMPORT_DATA,
131 + String.valueOf(ctx.IDENTIFIER()
132 + .getText()),
133 + ListenerErrorLocation.EXIT));
134 + }
135 + } else {
136 + throw new ParserException(
137 + ListenerErrorMessageConstruction
138 + .constructListenerErrorMessage(ListenerErrorType.MISSING_CURRENT_HOLDER,
139 + ParsableDataType.IMPORT_DATA, String
140 + .valueOf(ctx.IDENTIFIER()
141 + .getText()),
142 + ListenerErrorLocation.EXIT));
143 + }
77 } 144 }
78 -} 145 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,8 +16,18 @@ ...@@ -16,8 +16,18 @@
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.YangInclude;
20 +import org.onosproject.yangutils.datamodel.YangModule;
21 +import org.onosproject.yangutils.datamodel.YangSubModule;
22 +import org.onosproject.yangutils.parser.Parsable;
23 +import org.onosproject.yangutils.parser.ParsableDataType;
19 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 24 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
25 +import org.onosproject.yangutils.parser.exceptions.ParserException;
20 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 26 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
27 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
28 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
29 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
30 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
21 31
22 /* 32 /*
23 * Reference: RFC6020 and YANG ANTLR Grammar 33 * Reference: RFC6020 and YANG ANTLR Grammar
...@@ -37,7 +47,7 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener; ...@@ -37,7 +47,7 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener;
37 * linkage_stmts : (import_stmt 47 * linkage_stmts : (import_stmt
38 * | include_stmt)*; 48 * | include_stmt)*;
39 * include_stmt : INCLUDE_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE 49 * include_stmt : INCLUDE_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE
40 - * revision_date_stmt_body? RIGHT_CURLY_BRACE); 50 + * revision_date_stmt? RIGHT_CURLY_BRACE);
41 */ 51 */
42 52
43 /** 53 /**
...@@ -61,7 +71,17 @@ public final class IncludeListener { ...@@ -61,7 +71,17 @@ public final class IncludeListener {
61 * @param ctx context object of the grammar rule. 71 * @param ctx context object of the grammar rule.
62 */ 72 */
63 public static void processIncludeEntry(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) { 73 public static void processIncludeEntry(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) {
64 - // TODO method implementation 74 +
75 + // Check for stack to be non empty.
76 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
77 + ParsableDataType.INCLUDE_DATA,
78 + String.valueOf(ctx.IDENTIFIER().getText()),
79 + ListenerErrorLocation.ENTRY);
80 +
81 + YangInclude includeNode = new YangInclude();
82 + includeNode.setSubModuleName(String.valueOf(ctx.IDENTIFIER().getText()));
83 +
84 + listener.getParsedDataStack().push(includeNode);
65 } 85 }
66 86
67 /** 87 /**
...@@ -72,6 +92,52 @@ public final class IncludeListener { ...@@ -72,6 +92,52 @@ public final class IncludeListener {
72 * @param ctx context object of the grammar rule. 92 * @param ctx context object of the grammar rule.
73 */ 93 */
74 public static void processIncludeExit(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) { 94 public static void processIncludeExit(TreeWalkListener listener, GeneratedYangParser.IncludeStatementContext ctx) {
75 - // TODO method implementation 95 +
96 + // Check for stack to be non empty.
97 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
98 + ParsableDataType.INCLUDE_DATA,
99 + String.valueOf(ctx.IDENTIFIER().getText()),
100 + ListenerErrorLocation.EXIT);
101 +
102 + Parsable tmpIncludeNode = listener.getParsedDataStack().peek();
103 + if (tmpIncludeNode instanceof YangInclude) {
104 + listener.getParsedDataStack().pop();
105 +
106 + // Check for stack to be non empty.
107 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
108 + ParsableDataType.INCLUDE_DATA,
109 + String.valueOf(ctx.IDENTIFIER().getText()),
110 + ListenerErrorLocation.EXIT);
111 +
112 + Parsable tmpNode = listener.getParsedDataStack().peek();
113 + switch (tmpNode.getParsableDataType()) {
114 + case MODULE_DATA: {
115 + YangModule module = (YangModule) tmpNode;
116 + module.addIncludedInfo((YangInclude) tmpIncludeNode);
117 + break;
118 + }
119 + case SUB_MODULE_DATA: {
120 + YangSubModule subModule = (YangSubModule) tmpNode;
121 + subModule.addIncludedInfo((YangInclude) tmpIncludeNode);
122 + break;
123 + }
124 + default:
125 + throw new ParserException(
126 + ListenerErrorMessageConstruction
127 + .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
128 + ParsableDataType.INCLUDE_DATA,
129 + String.valueOf(ctx.IDENTIFIER()
130 + .getText()),
131 + ListenerErrorLocation.EXIT));
132 + }
133 + } else {
134 + throw new ParserException(
135 + ListenerErrorMessageConstruction
136 + .constructListenerErrorMessage(ListenerErrorType.MISSING_CURRENT_HOLDER,
137 + ParsableDataType.INCLUDE_DATA, String
138 + .valueOf(ctx.IDENTIFIER()
139 + .getText()),
140 + ListenerErrorLocation.EXIT));
141 + }
76 } 142 }
77 -} 143 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,8 +16,15 @@ ...@@ -16,8 +16,15 @@
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.YangModule;
20 +import org.onosproject.yangutils.parser.ParsableDataType;
19 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 21 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
22 +import org.onosproject.yangutils.parser.exceptions.ParserException;
20 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 23 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
24 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
25 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
26 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
27 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
21 28
22 /* 29 /*
23 * Reference: RFC6020 and YANG ANTLR Grammar 30 * Reference: RFC6020 and YANG ANTLR Grammar
...@@ -58,7 +65,16 @@ public final class ModuleListener { ...@@ -58,7 +65,16 @@ public final class ModuleListener {
58 * @param ctx context object of the grammar rule. 65 * @param ctx context object of the grammar rule.
59 */ 66 */
60 public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) { 67 public static void processModuleEntry(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
61 - // TODO method implementation 68 +
69 + // Check if stack is empty.
70 + ListenerValidation
71 + .checkStackIsEmpty(listener, ListenerErrorType.INVALID_HOLDER, ParsableDataType.MODULE_DATA,
72 + String.valueOf(ctx.IDENTIFIER().getText()), ListenerErrorLocation.ENTRY);
73 +
74 + YangModule yangModule = new YangModule();
75 + yangModule.setName(ctx.IDENTIFIER().getText());
76 +
77 + listener.getParsedDataStack().push(yangModule);
62 } 78 }
63 79
64 /** 80 /**
...@@ -69,6 +85,21 @@ public final class ModuleListener { ...@@ -69,6 +85,21 @@ public final class ModuleListener {
69 * @param ctx context object of the grammar rule. 85 * @param ctx context object of the grammar rule.
70 */ 86 */
71 public static void processModuleExit(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) { 87 public static void processModuleExit(TreeWalkListener listener, GeneratedYangParser.ModuleStatementContext ctx) {
72 - // TODO method implementation 88 +
89 + // Check for stack to be non empty.
90 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
91 + ParsableDataType.MODULE_DATA,
92 + String.valueOf(ctx.IDENTIFIER().getText()),
93 + ListenerErrorLocation.EXIT);
94 +
95 + if (!(listener.getParsedDataStack().peek() instanceof YangModule)) {
96 + throw new ParserException(
97 + ListenerErrorMessageConstruction
98 + .constructListenerErrorMessage(ListenerErrorType.MISSING_CURRENT_HOLDER,
99 + ParsableDataType.MODULE_DATA, String
100 + .valueOf(ctx.IDENTIFIER()
101 + .getText()),
102 + ListenerErrorLocation.EXIT));
103 + }
73 } 104 }
74 -} 105 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,8 +16,19 @@ ...@@ -16,8 +16,19 @@
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.YangModule;
20 +import org.onosproject.yangutils.datamodel.YangNameSpace;
21 +import org.onosproject.yangutils.parser.Parsable;
22 +import org.onosproject.yangutils.parser.ParsableDataType;
19 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 23 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
24 +import org.onosproject.yangutils.parser.exceptions.ParserException;
20 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 25 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
26 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
29 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
30 +
31 +import java.net.URI;
21 32
22 /* 33 /*
23 * Reference: RFC6020 and YANG ANTLR Grammar 34 * Reference: RFC6020 and YANG ANTLR Grammar
...@@ -63,6 +74,53 @@ public final class NamespaceListener { ...@@ -63,6 +74,53 @@ public final class NamespaceListener {
63 */ 74 */
64 public static void processNamespaceEntry(TreeWalkListener listener, 75 public static void processNamespaceEntry(TreeWalkListener listener,
65 GeneratedYangParser.NamespaceStatementContext ctx) { 76 GeneratedYangParser.NamespaceStatementContext ctx) {
66 - // TODO method implementation 77 +
78 + // Check for stack to be non empty.
79 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
80 + ParsableDataType.NAMESPACE_DATA,
81 + String.valueOf(ctx.string().getText()), ListenerErrorLocation.ENTRY);
82 +
83 + if (!validateUriValue(String.valueOf(ctx.string().getText()))) {
84 + ParserException parserException = new ParserException("Invalid namespace URI");
85 + parserException.setLine(ctx.string().STRING(0).getSymbol().getLine());
86 + parserException.setCharPosition(ctx.string().STRING(0).getSymbol().getCharPositionInLine());
87 + throw parserException;
88 + }
89 +
90 + // Obtain the node of the stack.
91 + Parsable tmpNode = listener.getParsedDataStack().peek();
92 + switch (tmpNode.getParsableDataType()) {
93 + case MODULE_DATA: {
94 + YangModule module = (YangModule) tmpNode;
95 + YangNameSpace uri = new YangNameSpace();
96 + uri.setUri(String.valueOf(ctx.string().getText()));
97 + module.setNameSpace(uri);
98 + break;
99 + }
100 + default:
101 + throw new ParserException(
102 + ListenerErrorMessageConstruction
103 + .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
104 + ParsableDataType.NAMESPACE_DATA,
105 + String.valueOf(ctx.string().getText()),
106 + ListenerErrorLocation.ENTRY));
107 + }
108 + }
109 +
110 + /**
111 + * Validate input URI.
112 + *
113 + * @param uri input namespace URI
114 + * @return validation result
115 + */
116 + private static boolean validateUriValue(String uri) {
117 + uri = uri.replace("\"", "");
118 + final URI tmpUri;
119 + try {
120 + tmpUri = URI.create(uri);
121 + } catch (Exception e1) {
122 + return false;
123 + }
124 + return true;
67 } 125 }
68 -} 126 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,8 +16,17 @@ ...@@ -16,8 +16,17 @@
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.YangModule;
20 +import org.onosproject.yangutils.datamodel.YangSubModule;
21 +import org.onosproject.yangutils.parser.Parsable;
22 +import org.onosproject.yangutils.parser.ParsableDataType;
19 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 23 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
24 +import org.onosproject.yangutils.parser.exceptions.ParserException;
20 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 25 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
26 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
29 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
21 30
22 /* 31 /*
23 * Reference: RFC6020 and YANG ANTLR Grammar 32 * Reference: RFC6020 and YANG ANTLR Grammar
...@@ -28,7 +37,8 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener; ...@@ -28,7 +37,8 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener;
28 * [contact-stmt stmtsep] 37 * [contact-stmt stmtsep]
29 * [description-stmt stmtsep] 38 * [description-stmt stmtsep]
30 * [reference-stmt stmtsep] 39 * [reference-stmt stmtsep]
31 - * contact-stmt = contact-keyword sep string optsep stmtend 40 + * organization-stmt = organization-keyword sep string
41 + * optsep stmtend
32 * 42 *
33 * ANTLR grammar rule 43 * ANTLR grammar rule
34 * meta_stmts : organization_stmt? contact_stmt? description_stmt? reference_stmt? 44 * meta_stmts : organization_stmt? contact_stmt? description_stmt? reference_stmt?
...@@ -56,7 +66,7 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener; ...@@ -56,7 +66,7 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener;
56 * | description_stmt? organization_stmt? contact_stmt? reference_stmt? 66 * | description_stmt? organization_stmt? contact_stmt? reference_stmt?
57 * | description_stmt? organization_stmt? reference_stmt? contact_stmt? 67 * | description_stmt? organization_stmt? reference_stmt? contact_stmt?
58 * ; 68 * ;
59 - * contact_stmt : CONTACT_KEYWORD string STMTEND; 69 + * organization_stmt : ORGANIZATION_KEYWORD string STMTEND;
60 */ 70 */
61 71
62 /** 72 /**
...@@ -81,6 +91,32 @@ public final class OrganizationListener { ...@@ -81,6 +91,32 @@ public final class OrganizationListener {
81 */ 91 */
82 public static void processOrganizationEntry(TreeWalkListener listener, 92 public static void processOrganizationEntry(TreeWalkListener listener,
83 GeneratedYangParser.OrganizationStatementContext ctx) { 93 GeneratedYangParser.OrganizationStatementContext ctx) {
84 - // TODO method implementation 94 +
95 + // Check for stack to be non empty.
96 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
97 + ParsableDataType.ORGANIZATION_DATA,
98 + String.valueOf(ctx.string().getText()), ListenerErrorLocation.ENTRY);
99 +
100 + // Obtain the node of the stack.
101 + Parsable tmpNode = listener.getParsedDataStack().peek();
102 + switch (tmpNode.getParsableDataType()) {
103 + case MODULE_DATA: {
104 + YangModule module = (YangModule) tmpNode;
105 + module.setOrganization(String.valueOf(ctx.string().getText()));
106 + break;
107 + }
108 + case SUB_MODULE_DATA: {
109 + YangSubModule subModule = (YangSubModule) tmpNode;
110 + subModule.setOrganization(String.valueOf(ctx.string().getText()));
111 + break;
112 + }
113 + default:
114 + throw new ParserException(
115 + ListenerErrorMessageConstruction
116 + .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
117 + ParsableDataType.ORGANIZATION_DATA,
118 + String.valueOf(ctx.string().getText()),
119 + ListenerErrorLocation.ENTRY));
120 + }
85 } 121 }
86 -} 122 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,8 +16,18 @@ ...@@ -16,8 +16,18 @@
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.YangBelongsTo;
20 +import org.onosproject.yangutils.datamodel.YangImport;
21 +import org.onosproject.yangutils.datamodel.YangModule;
22 +import org.onosproject.yangutils.parser.Parsable;
23 +import org.onosproject.yangutils.parser.ParsableDataType;
19 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 24 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
25 +import org.onosproject.yangutils.parser.exceptions.ParserException;
20 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 26 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
27 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
28 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
29 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
30 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
21 31
22 /* 32 /*
23 * Reference: RFC6020 and YANG ANTLR Grammar 33 * Reference: RFC6020 and YANG ANTLR Grammar
...@@ -63,6 +73,39 @@ public final class PrefixListener { ...@@ -63,6 +73,39 @@ public final class PrefixListener {
63 * @param ctx context object of the grammar rule. 73 * @param ctx context object of the grammar rule.
64 */ 74 */
65 public static void processPrefixEntry(TreeWalkListener listener, GeneratedYangParser.PrefixStatementContext ctx) { 75 public static void processPrefixEntry(TreeWalkListener listener, GeneratedYangParser.PrefixStatementContext ctx) {
66 - // TODO method implementation 76 +
77 + // Check for stack to be non empty.
78 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
79 + ParsableDataType.PREFIX_DATA,
80 + String.valueOf(ctx.IDENTIFIER().getText()),
81 + ListenerErrorLocation.ENTRY);
82 +
83 + // Obtain the node of the stack.
84 + Parsable tmpNode = listener.getParsedDataStack().peek();
85 + switch (tmpNode.getParsableDataType()) {
86 + case MODULE_DATA: {
87 + YangModule module = (YangModule) tmpNode;
88 + module.setPrefix(ctx.IDENTIFIER().getText());
89 + break;
90 + }
91 + case IMPORT_DATA: {
92 + YangImport importNode = (YangImport) tmpNode;
93 + importNode.setPrefixId(ctx.IDENTIFIER().getText());
94 + break;
95 + }
96 + case BELONGS_TO_DATA: {
97 + YangBelongsTo belongstoNode = (YangBelongsTo) tmpNode;
98 + belongstoNode.setPrefix(ctx.IDENTIFIER().getText());
99 + break;
100 + }
101 + default:
102 + throw new ParserException(
103 + ListenerErrorMessageConstruction
104 + .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
105 + ParsableDataType.PREFIX_DATA, String
106 + .valueOf(ctx.IDENTIFIER()
107 + .getText()),
108 + ListenerErrorLocation.ENTRY));
109 + }
67 } 110 }
68 -} 111 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,8 +16,17 @@ ...@@ -16,8 +16,17 @@
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.YangImport;
20 +import org.onosproject.yangutils.datamodel.YangInclude;
21 +import org.onosproject.yangutils.parser.Parsable;
22 +import org.onosproject.yangutils.parser.ParsableDataType;
19 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 23 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
24 +import org.onosproject.yangutils.parser.exceptions.ParserException;
20 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 25 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
26 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
29 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
21 30
22 /* 31 /*
23 * Reference: RFC6020 and YANG ANTLR Grammar 32 * Reference: RFC6020 and YANG ANTLR Grammar
...@@ -69,6 +78,34 @@ public final class RevisionDateListener { ...@@ -69,6 +78,34 @@ public final class RevisionDateListener {
69 */ 78 */
70 public static void processRevisionDateEntry(TreeWalkListener listener, 79 public static void processRevisionDateEntry(TreeWalkListener listener,
71 GeneratedYangParser.RevisionDateStatementContext ctx) { 80 GeneratedYangParser.RevisionDateStatementContext ctx) {
72 - // TODO method implementation 81 +
82 + // Check for stack to be non empty.
83 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
84 + ParsableDataType.REVISION_DATE_DATA,
85 + String.valueOf(ctx.DATE_ARG().getText()),
86 + ListenerErrorLocation.ENTRY);
87 +
88 + // Obtain the node of the stack.
89 + Parsable tmpNode = listener.getParsedDataStack().peek();
90 + switch (tmpNode.getParsableDataType()) {
91 + case IMPORT_DATA: {
92 + YangImport importNode = (YangImport) tmpNode;
93 + importNode.setRevision(String.valueOf(ctx.DATE_ARG().getText()));
94 + break;
95 + }
96 + case INCLUDE_DATA: {
97 + YangInclude includeNode = (YangInclude) tmpNode;
98 + includeNode.setRevision(String.valueOf(ctx.DATE_ARG().getText()));
99 + break;
100 + }
101 + default:
102 + throw new ParserException(
103 + ListenerErrorMessageConstruction
104 + .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
105 + ParsableDataType.REVISION_DATE_DATA,
106 + String.valueOf(ctx.DATE_ARG().getText()),
107 + ListenerErrorLocation.ENTRY));
108 + }
73 } 109 }
74 -} 110 + // TODO Implement the DATE_ARG validation as per RFC 6020.
111 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,8 +16,18 @@ ...@@ -16,8 +16,18 @@
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.YangModule;
20 +import org.onosproject.yangutils.datamodel.YangRevision;
21 +import org.onosproject.yangutils.datamodel.YangSubModule;
22 +import org.onosproject.yangutils.parser.Parsable;
23 +import org.onosproject.yangutils.parser.ParsableDataType;
19 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 24 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
25 +import org.onosproject.yangutils.parser.exceptions.ParserException;
20 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 26 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
27 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
28 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
29 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
30 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
21 31
22 /* 32 /*
23 * Reference: RFC6020 and YANG ANTLR Grammar 33 * Reference: RFC6020 and YANG ANTLR Grammar
...@@ -59,28 +69,94 @@ public final class RevisionListener { ...@@ -59,28 +69,94 @@ public final class RevisionListener {
59 private RevisionListener() { 69 private RevisionListener() {
60 } 70 }
61 71
72 + public static void processRevisionEntry(TreeWalkListener listener,
73 + GeneratedYangParser.RevisionStatementContext ctx) {
74 +
75 + // Check for stack to be non empty.
76 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
77 + ParsableDataType.REVISION_DATA,
78 + String.valueOf(ctx.DATE_ARG().getText()),
79 + ListenerErrorLocation.ENTRY);
80 +
81 + // Validate for reverse chronological order of revision & for revision value.
82 + if (!validateRevision(listener, ctx)) {
83 + return;
84 + // TODO to be implemented.
85 + }
86 +
87 + YangRevision revisionNode = new YangRevision();
88 + revisionNode.setRevDate(String.valueOf(ctx.DATE_ARG().getText()));
89 +
90 + listener.getParsedDataStack().push(revisionNode);
91 + }
92 +
62 /** 93 /**
63 - * It is called when parser receives an input matching the grammar 94 + * It is called when parser exits from grammar rule (revision), it perform
64 - * rule (revision), perform validations and update the data model 95 + * validations and update the data model tree.
65 - * tree.
66 * 96 *
67 * @param listener Listener's object. 97 * @param listener Listener's object.
68 * @param ctx context object of the grammar rule. 98 * @param ctx context object of the grammar rule.
69 */ 99 */
70 - public static void processRevisionEntry(TreeWalkListener listener, GeneratedYangParser.RevisionStatementContext 100 + public static void processRevisionExit(TreeWalkListener listener,
71 - ctx) { 101 + GeneratedYangParser.RevisionStatementContext ctx) {
72 - // TODO method implementation 102 +
103 + // Check for stack to be non empty.
104 + ListenerValidation
105 + .checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER, ParsableDataType.REVISION_DATA,
106 + String.valueOf(ctx.DATE_ARG().getText()), ListenerErrorLocation.EXIT);
107 +
108 + Parsable tmpRevisionNode = listener.getParsedDataStack().peek();
109 + if (tmpRevisionNode instanceof YangRevision) {
110 + listener.getParsedDataStack().pop();
111 +
112 + // Check for stack to be non empty.
113 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
114 + ParsableDataType.REVISION_DATA,
115 + String.valueOf(ctx.DATE_ARG().getText()),
116 + ListenerErrorLocation.EXIT);
117 +
118 + Parsable tmpNode = listener.getParsedDataStack().peek();
119 + switch (tmpNode.getParsableDataType()) {
120 + case MODULE_DATA: {
121 + YangModule module = (YangModule) tmpNode;
122 + module.setRevision((YangRevision) tmpRevisionNode);
123 + break;
124 + }
125 + case SUB_MODULE_DATA: {
126 + YangSubModule subModule = (YangSubModule) tmpNode;
127 + subModule.setRevision((YangRevision) tmpRevisionNode);
128 + break;
129 + }
130 + default:
131 + throw new ParserException(
132 + ListenerErrorMessageConstruction
133 + .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
134 + ParsableDataType.REVISION_DATA,
135 + String.valueOf(ctx.DATE_ARG()
136 + .getText()),
137 + ListenerErrorLocation.EXIT));
138 + }
139 + } else {
140 + throw new ParserException(
141 + ListenerErrorMessageConstruction
142 + .constructListenerErrorMessage(ListenerErrorType.MISSING_CURRENT_HOLDER,
143 + ParsableDataType.REVISION_DATA, String
144 + .valueOf(ctx.DATE_ARG()
145 + .getText()),
146 + ListenerErrorLocation.EXIT));
147 + }
73 } 148 }
74 149
75 /** 150 /**
76 - * It is called when parser exits from grammar rule (revision), it perform 151 + * Validate revision.
77 - * validations and update the data model tree.
78 * 152 *
79 * @param listener Listener's object. 153 * @param listener Listener's object.
80 * @param ctx context object of the grammar rule. 154 * @param ctx context object of the grammar rule.
155 + * @return validation result
81 */ 156 */
82 - public static void processRevisionExit(TreeWalkListener listener, GeneratedYangParser.RevisionStatementContext 157 + private static boolean validateRevision(TreeWalkListener listener,
83 - ctx) { 158 + GeneratedYangParser.RevisionStatementContext ctx) {
84 - // TODO method implementation 159 + // TODO to be implemented
160 + return true;
85 } 161 }
86 -} 162 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,8 +16,15 @@ ...@@ -16,8 +16,15 @@
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.YangSubModule;
20 +import org.onosproject.yangutils.parser.ParsableDataType;
19 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 21 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
22 +import org.onosproject.yangutils.parser.exceptions.ParserException;
20 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 23 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
24 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
25 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
26 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
27 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
21 28
22 /* 29 /*
23 * Reference: RFC6020 and YANG ANTLR Grammar 30 * Reference: RFC6020 and YANG ANTLR Grammar
...@@ -60,7 +67,16 @@ public final class SubModuleListener { ...@@ -60,7 +67,16 @@ public final class SubModuleListener {
60 */ 67 */
61 public static void processSubModuleEntry(TreeWalkListener listener, 68 public static void processSubModuleEntry(TreeWalkListener listener,
62 GeneratedYangParser.SubModuleStatementContext ctx) { 69 GeneratedYangParser.SubModuleStatementContext ctx) {
63 - // TODO method implementation 70 +
71 + // Check if stack is empty.
72 + ListenerValidation
73 + .checkStackIsEmpty(listener, ListenerErrorType.INVALID_HOLDER, ParsableDataType.SUB_MODULE_DATA,
74 + String.valueOf(ctx.IDENTIFIER().getText()), ListenerErrorLocation.ENTRY);
75 +
76 + YangSubModule yangSubModule = new YangSubModule();
77 + yangSubModule.setName(ctx.IDENTIFIER().getText());
78 +
79 + listener.getParsedDataStack().push(yangSubModule);
64 } 80 }
65 81
66 /** 82 /**
...@@ -72,6 +88,21 @@ public final class SubModuleListener { ...@@ -72,6 +88,21 @@ public final class SubModuleListener {
72 */ 88 */
73 public static void processSubModuleExit(TreeWalkListener listener, 89 public static void processSubModuleExit(TreeWalkListener listener,
74 GeneratedYangParser.SubModuleStatementContext ctx) { 90 GeneratedYangParser.SubModuleStatementContext ctx) {
75 - // TODO method implementation 91 +
92 + // Check for stack to be non empty.
93 + ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
94 + ParsableDataType.SUB_MODULE_DATA,
95 + String.valueOf(ctx.IDENTIFIER().getText()),
96 + ListenerErrorLocation.EXIT);
97 +
98 + if (!(listener.getParsedDataStack().peek() instanceof YangSubModule)) {
99 + throw new ParserException(
100 + ListenerErrorMessageConstruction
101 + .constructListenerErrorMessage(ListenerErrorType.MISSING_CURRENT_HOLDER,
102 + ParsableDataType.SUB_MODULE_DATA,
103 + String.valueOf(ctx.IDENTIFIER()
104 + .getText()),
105 + ListenerErrorLocation.EXIT));
106 + }
76 } 107 }
77 -} 108 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -16,8 +16,17 @@ ...@@ -16,8 +16,17 @@
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.YangModule;
20 +import org.onosproject.yangutils.datamodel.YangSubModule;
21 +import org.onosproject.yangutils.parser.Parsable;
22 +import org.onosproject.yangutils.parser.ParsableDataType;
19 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 23 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
24 +import org.onosproject.yangutils.parser.exceptions.ParserException;
20 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 25 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
26 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
29 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
21 30
22 /* 31 /*
23 * Reference: RFC6020 and YANG ANTLR Grammar 32 * Reference: RFC6020 and YANG ANTLR Grammar
...@@ -73,6 +82,48 @@ public final class VersionListener { ...@@ -73,6 +82,48 @@ public final class VersionListener {
73 */ 82 */
74 public static void processVersionEntry(TreeWalkListener listener, 83 public static void processVersionEntry(TreeWalkListener listener,
75 GeneratedYangParser.YangVersionStatementContext ctx) { 84 GeneratedYangParser.YangVersionStatementContext ctx) {
76 - // TODO method implementation 85 +
86 + // Check for stack to be non empty.
87 + ListenerValidation
88 + .checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER, ParsableDataType.VERSION_DATA,
89 + String.valueOf(ctx.INTEGER().getText()), ListenerErrorLocation.ENTRY);
90 +
91 + Integer version = Integer.valueOf(ctx.INTEGER().getText());
92 + if (!isVersionValid(version)) {
93 + ParserException parserException = new ParserException("Input version not supported");
94 + parserException.setLine(ctx.INTEGER().getSymbol().getLine());
95 + parserException.setCharPosition(ctx.INTEGER().getSymbol().getCharPositionInLine());
96 + throw parserException;
97 + }
98 +
99 + // Obtain the node of the stack.
100 + Parsable tmpNode = listener.getParsedDataStack().peek();
101 + switch (tmpNode.getParsableDataType()) {
102 + case MODULE_DATA: {
103 + YangModule module = (YangModule) tmpNode;
104 + module.setVersion((byte) 1);
105 + break;
106 + }
107 + case SUB_MODULE_DATA: {
108 + YangSubModule subModule = (YangSubModule) tmpNode;
109 + subModule.setVersion((byte) 1);
110 + break;
111 + }
112 + default:
113 + throw new ParserException(ListenerErrorMessageConstruction.
114 + constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
115 + ParsableDataType.VERSION_DATA, String.valueOf(ctx.INTEGER().getText()),
116 + ListenerErrorLocation.ENTRY));
117 + }
118 + }
119 +
120 + /**
121 + * Validates whether the value of YANG version.
122 + *
123 + * @param version input yang version
124 + * @return validation result
125 + */
126 + private static boolean isVersionValid(Integer version) {
127 + return version == 1;
77 } 128 }
78 -} 129 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -21,12 +21,18 @@ package org.onosproject.yangutils.parser.impl.parserutils; ...@@ -21,12 +21,18 @@ package org.onosproject.yangutils.parser.impl.parserutils;
21 */ 21 */
22 public class ListenerError { 22 public class ListenerError {
23 23
24 - // Maintains the state of Exception. 24 + // Maintains the state of exception.
25 private boolean errorFlag = false; 25 private boolean errorFlag = false;
26 26
27 - // Maintains the reason of Exception. 27 + // Maintains the reason of exception.
28 private String errorMsg; 28 private String errorMsg;
29 29
30 + // Maintains the line number of exception.
31 + private int lineNumber;
32 +
33 + // Maintains the character position in lin of exception.
34 + private int charPositionInLine;
35 +
30 /** 36 /**
31 * Returns error flag. 37 * Returns error flag.
32 * 38 *
...@@ -37,18 +43,36 @@ public class ListenerError { ...@@ -37,18 +43,36 @@ public class ListenerError {
37 } 43 }
38 44
39 /** 45 /**
40 - * Returns error message. 46 + * Returns reason for error.
41 * 47 *
42 - * @return error msg. 48 + * @return error message
43 */ 49 */
44 public String getErrorMsg() { 50 public String getErrorMsg() {
45 return errorMsg; 51 return errorMsg;
46 } 52 }
47 53
48 /** 54 /**
55 + * Returns error line number.
56 + *
57 + * @return error line number.
58 + */
59 + public int getLineNumber() {
60 + return lineNumber;
61 + }
62 +
63 + /**
64 + * Returns error position in line.
65 + *
66 + * @return error character position in line.
67 + */
68 + public int getCharPositionInLine() {
69 + return charPositionInLine;
70 + }
71 +
72 + /**
49 * Set error flag. 73 * Set error flag.
50 * 74 *
51 - * @param errorFlag error existence flag 75 + * @param errorFlag error existence flag.
52 */ 76 */
53 public void setErrorFlag(boolean errorFlag) { 77 public void setErrorFlag(boolean errorFlag) {
54 this.errorFlag = errorFlag; 78 this.errorFlag = errorFlag;
...@@ -62,4 +86,22 @@ public class ListenerError { ...@@ -62,4 +86,22 @@ public class ListenerError {
62 public void setErrorMsg(String errorMsg) { 86 public void setErrorMsg(String errorMsg) {
63 this.errorMsg = errorMsg; 87 this.errorMsg = errorMsg;
64 } 88 }
89 +
90 + /**
91 + * Set error line number.
92 + *
93 + * @param lineNumber line number of error.
94 + */
95 + public void setLineNumber(int lineNumber) {
96 + this.lineNumber = lineNumber;
97 + }
98 +
99 + /**
100 + * Set error character position in line.
101 + *
102 + * @param charPositionInLine error character position in line.
103 + */
104 + public void setCharPositionInLine(int charPositionInLine) {
105 + this.charPositionInLine = charPositionInLine;
106 + }
65 } 107 }
...\ 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.parserutils;
18 +
19 +/**
20 + * Maintains listener error location.
21 + */
22 +public enum ListenerErrorLocation {
23 + /**
24 + * Represents that the error location is before processing.
25 + */
26 + ENTRY(),
27 +
28 + /**
29 + * Represents that the error location is before processing.
30 + */
31 + EXIT();
32 +
33 + /**
34 + * Returns the message corresponding to listener error location.
35 + *
36 + * @param errorLocation enum value for type of error.
37 + * @return message corresponding to listener error location.
38 + */
39 + public static String getErrorLocationMessage(ListenerErrorLocation errorLocation) {
40 +
41 + switch (errorLocation) {
42 + case ENTRY:
43 + return "before";
44 + case EXIT:
45 + return "after";
46 + default:
47 + return "during";
48 + }
49 + }
50 +}
...\ 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.parserutils;
18 +
19 +import org.onosproject.yangutils.parser.ParsableDataType;
20 +
21 +/**
22 + * It's a utility to help construct detailed error message.
23 + */
24 +public final class ListenerErrorMessageConstruction {
25 +
26 + /**
27 + * Private constructor.
28 + */
29 + private ListenerErrorMessageConstruction() {
30 + }
31 +
32 + /**
33 + * Constructs message for error with extended information and returns the same.
34 + *
35 + * @param errorType error type needs to be set in error message.
36 + * @param parsableDataType type of parsable data in which error occurred.
37 + * @param parsableDataTypeName identifier/string of parsable data type in which error occurred.
38 + * @param errorLocation location where error occurred.
39 + * @param extendedErrorInformation extended error information.
40 + * @return constructed error message.
41 + */
42 + public static String constructExtendedListenerErrorMessage(ListenerErrorType errorType,
43 + ParsableDataType parsableDataType,
44 + String parsableDataTypeName,
45 + ListenerErrorLocation errorLocation,
46 + String extendedErrorInformation) {
47 + String newErrorMessage;
48 + newErrorMessage = constructListenerErrorMessage(errorType, parsableDataType, parsableDataTypeName,
49 + errorLocation) + "\n" + "Error Information: " + extendedErrorInformation;
50 + return newErrorMessage;
51 + }
52 +
53 + /**
54 + * Constructs message for error during listener based tree walk and returns the same.
55 + *
56 + * @param errorType error type needs to be set in error message.
57 + * @param parsableDataType type of parsable data in which error occurred.
58 + * @param parsableDataTypeName identifier/string of parsable data type in which error occurred.
59 + * @param errorLocation location where error occurred.
60 + * @return constructed error message.
61 + */
62 + public static String constructListenerErrorMessage(ListenerErrorType errorType,
63 + ParsableDataType parsableDataType,
64 + String parsableDataTypeName,
65 + ListenerErrorLocation errorLocation) {
66 +
67 + String errorMessage;
68 +
69 + errorMessage = "Internal parser error detected: " + ListenerErrorType.getErrorType(errorType) + " "
70 + + ParsableDataType.getParsableDataType(parsableDataType);
71 +
72 +
73 + if (!parsableDataTypeName.isEmpty()) {
74 + errorMessage = errorMessage + " \"" + parsableDataTypeName + "\" ";
75 + } else {
76 + errorMessage = errorMessage + " ";
77 +
78 + }
79 + errorMessage = errorMessage + ListenerErrorLocation.getErrorLocationMessage(errorLocation) + " processing.";
80 + return errorMessage;
81 + }
82 +}
...\ 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.parserutils;
18 +
19 +/**
20 + * Maintains listener error type.
21 + */
22 +public enum ListenerErrorType {
23 + /**
24 + * Represents the parent holder in parsable stack for given YANG construct is invalid.
25 + */
26 + INVALID_HOLDER(),
27 +
28 + /**
29 + * Represents the parent holder in parsable stack for given YANG construct is missing.
30 + */
31 + MISSING_HOLDER(),
32 +
33 + /**
34 + * Represents the current holder in parsable stack for given YANG construct is missing.
35 + */
36 + MISSING_CURRENT_HOLDER(),
37 +
38 + /**
39 + * Represents that the child in parsable stack for given YANG construct is invalid.
40 + */
41 + INVALID_CHILD(),
42 +
43 + /**
44 + * Represents that the cardinality for given YANG construct is invalid.
45 + */
46 + INVALID_CARDINALITY(),
47 +
48 + /**
49 + * Represents that some of earlier parsed data is not handled correctly.
50 + */
51 + UNHANDLED_PARSED_DATA();
52 +
53 + /**
54 + * Returns the message corresponding to listener error type.
55 + *
56 + * @param errorType enum value for type of error.
57 + * @return message corresponding to listener error type.
58 + */
59 + public static String getErrorType(ListenerErrorType errorType) {
60 +
61 + switch (errorType) {
62 + case INVALID_HOLDER:
63 + return "Invalid holder for";
64 + case MISSING_HOLDER:
65 + return "Missing holder at";
66 + case MISSING_CURRENT_HOLDER:
67 + return "Missing";
68 + case INVALID_CHILD:
69 + return "Invalid child in";
70 + case INVALID_CARDINALITY:
71 + return "Invalid cardinality in";
72 + case UNHANDLED_PARSED_DATA:
73 + return "Unhandled parsed data at";
74 + default:
75 + return "Problem in";
76 + }
77 + }
78 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -16,39 +16,66 @@ ...@@ -16,39 +16,66 @@
16 16
17 package org.onosproject.yangutils.parser.impl.parserutils; 17 package org.onosproject.yangutils.parser.impl.parserutils;
18 18
19 +import org.onosproject.yangutils.parser.ParsableDataType;
20 +import org.onosproject.yangutils.parser.exceptions.ParserException;
19 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 21 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
20 22
21 /** 23 /**
22 - * Its a utility to carry out listener validation. 24 + * It's a utility to carry out listener validation.
23 */ 25 */
24 public final class ListenerValidation { 26 public final class ListenerValidation {
25 27
26 /** 28 /**
27 - * Creates a new belongto listener. 29 + * Creates a new listener validation.
28 */ 30 */
29 private ListenerValidation() { 31 private ListenerValidation() {
30 } 32 }
31 33
32 /** 34 /**
33 - * Checks if error is set or parsed data stack is empty. 35 + * Checks parsed data stack is not empty.
34 * 36 *
35 * @param listener Listener's object. 37 * @param listener Listener's object.
36 - * @param errNode parsable node for which validation needs to be done. 38 + * @param errorType error type needs to be set in error message.
37 - * @return validation result. 39 + * @param parsableDataType type of parsable data in which error occurred.
40 + * @param parsableDataTypeName name of parsable data type in which error occurred.
41 + * @param errorLocation location where error occurred.
38 */ 42 */
39 - public static boolean preValidation(TreeWalkListener listener, String errNode) { 43 + public static void checkStackIsNotEmpty(TreeWalkListener listener, ListenerErrorType errorType,
40 - 44 + ParsableDataType parsableDataType, String parsableDataTypeName,
41 - // Check whether error found while walking YANG file, if yes return true. 45 + ListenerErrorLocation errorLocation) {
42 - if (listener.getErrorInformation().isErrorFlag()) { 46 + if (listener.getParsedDataStack().empty()) {
43 - return true; 47 + /*
48 + * If stack is empty it indicates error condition, value of parsableDataTypeName will be null in case there
49 + * is no name attached to parsable data type.
50 + */
51 + String message = ListenerErrorMessageConstruction.constructListenerErrorMessage(errorType, parsableDataType,
52 + parsableDataTypeName, errorLocation);
53 + throw new ParserException(message);
44 } 54 }
55 + }
45 56
46 - // If stack is empty it indicates error condition 57 + /**
47 - if (listener.getParsedDataStack().empty()) { 58 + * Checks parsed data stack is empty.
48 - listener.getErrorInformation().setErrorFlag(true); 59 + *
49 - listener.getErrorInformation().setErrorMsg("Parsable stack empty at" + errNode + "entry"); 60 + * @param listener Listener's object.
50 - return true; 61 + * @param errorType error type needs to be set in error message.
62 + * @param parsableDataType type of parsable data in which error occurred.
63 + * @param parsableDataTypeName name of parsable data type in which error occurred.
64 + * @param errorLocation location where error occurred.
65 + */
66 +
67 + public static void checkStackIsEmpty(TreeWalkListener listener, ListenerErrorType errorType,
68 + ParsableDataType parsableDataType, String parsableDataTypeName,
69 + ListenerErrorLocation errorLocation) {
70 +
71 + if (!listener.getParsedDataStack().empty()) {
72 + /*
73 + * If stack is empty it indicates error condition, value of parsableDataTypeName will be null in case there
74 + * is no name attached to parsable data type.
75 + */
76 + String message = ListenerErrorMessageConstruction.constructListenerErrorMessage(errorType, parsableDataType,
77 + parsableDataTypeName, errorLocation);
78 + throw new ParserException(message);
51 } 79 }
52 - return false;
53 } 80 }
54 } 81 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -29,37 +29,13 @@ import org.onosproject.yangutils.parser.exceptions.ParserException; ...@@ -29,37 +29,13 @@ import org.onosproject.yangutils.parser.exceptions.ParserException;
29 */ 29 */
30 public class ParseTreeErrorListener extends BaseErrorListener { 30 public class ParseTreeErrorListener extends BaseErrorListener {
31 31
32 - // Exception of type parser exceptions are catched during parsing.
33 - private ParserException parserException = new ParserException();
34 -
35 - // Flag to indicate presence of exception.
36 - private boolean exceptionFlag = false;
37 -
38 - /**
39 - * Returns the status of exception flag.
40 - *
41 - * @return flag which contains the status of exception.
42 - */
43 - public boolean isExceptionFlag() {
44 - return exceptionFlag;
45 - }
46 -
47 - /**
48 - * Returns the parser exception object populated with line, character
49 - * position and message.
50 - *
51 - * @return object of parser exception.
52 - */
53 - public ParserException getParserException() {
54 - return parserException;
55 - }
56 -
57 @Override 32 @Override
58 public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, 33 public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine,
59 String msg, RecognitionException e) { 34 String msg, RecognitionException e) {
35 +
36 + ParserException parserException = new ParserException(msg);
60 parserException.setLine(line); 37 parserException.setLine(line);
61 parserException.setCharPosition(charPositionInLine); 38 parserException.setCharPosition(charPositionInLine);
62 - parserException.setMsg(msg); 39 + throw parserException;
63 - exceptionFlag = true;
64 } 40 }
65 } 41 }
...\ 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;
18 +
19 +import org.hamcrest.Description;
20 +import org.hamcrest.TypeSafeMatcher;
21 +import org.onosproject.yangutils.parser.exceptions.ParserException;
22 +
23 +/**
24 + * ExpectedException framework can use the Hamcrest matcher's to test
25 + * custom/extended exceptions. This class extends the type safe matcher to define
26 + * the custom exception matcher.
27 + */
28 +public final class CustomExceptionMatcher extends TypeSafeMatcher<ParserException> {
29 +
30 + /**
31 + * Customized exception matcher to match error location.
32 + *
33 + * @param line error line
34 + * @param charPosition error character position
35 + * @return
36 + */
37 + public static CustomExceptionMatcher errorLocation(int line, int charPosition) {
38 + return new CustomExceptionMatcher(line, charPosition);
39 + }
40 +
41 + private int actualLine;
42 + private final int expectedLine;
43 + private int actualCharPosition;
44 + private final int expectedCharPosition;
45 +
46 + private CustomExceptionMatcher(int expectedLine, int expectedCharPosition) {
47 + this.expectedLine = expectedLine;
48 + this.expectedCharPosition = expectedCharPosition;
49 + }
50 +
51 + @Override
52 + protected boolean matchesSafely(final ParserException exception) {
53 + actualLine = exception.getLineNumber();
54 + actualCharPosition = exception.getCharPositionInLine();
55 + return ((actualLine == expectedLine) && (actualCharPosition == expectedCharPosition));
56 + }
57 +
58 + @Override
59 + public void describeTo(Description description) {
60 + description.appendText(" Error reported location ")
61 + .appendText("Line " + actualLine + ", " + "CharPosition " + actualCharPosition)
62 + .appendText(" instead of expected ")
63 + .appendText("Line " + expectedLine + ", " + "CharPosition " + expectedCharPosition);
64 + }
65 +}
...\ 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 org.junit.Rule;
20 +import org.junit.Test;
21 +import org.junit.rules.ExpectedException;
22 +import org.onosproject.yangutils.datamodel.YangModule;
23 +import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
24 +import org.onosproject.yangutils.parser.exceptions.ParserException;
25 +import org.onosproject.yangutils.parser.impl.TreeWalkListener;
26 +
27 +/**
28 + * Test cases for testing base rule listener functionality.
29 + */
30 +public class BaseFileListenerTest {
31 +
32 + @Rule
33 + public ExpectedException thrown = ExpectedException.none();
34 +
35 + /**
36 + * Checks for exception if stack of parsable data is not empty at the entry
37 + * of yang base rule.
38 + */
39 + @Test
40 + public void processYangFileEntryNonEmptyStack() {
41 + thrown.expect(ParserException.class);
42 + thrown.expectMessage("Internal parser error detected: Invalid holder for yangbase before processing.");
43 +
44 + YangModule tmpModule = new YangModule();
45 + TreeWalkListener listener = new TreeWalkListener();
46 + listener.getParsedDataStack().push(tmpModule);
47 + GeneratedYangParser.YangfileContext ctx = null;
48 + BaseFileListener.processYangFileEntry(listener, ctx);
49 + }
50 +
51 + /**
52 + * Checks that exception shouldn't be generated if stack of parsable data is
53 + * empty at the entry of yang base rule.
54 + */
55 + @Test
56 + public void processYangFileEntryEmptyStack() {
57 +
58 + TreeWalkListener listener = new TreeWalkListener();
59 + GeneratedYangParser.YangfileContext ctx = null;
60 + BaseFileListener.processYangFileEntry(listener, ctx);
61 + }
62 +
63 + /**
64 + * Checks that exception should be generated if stack of parsable data is
65 + * not empty at the exit of yang base rule.
66 + */
67 + @Test
68 + public void processYangFileExitEmptyStack() {
69 + thrown.expect(ParserException.class);
70 + thrown.expectMessage("Internal parser error detected: Missing holder at yangbase after processing.");
71 +
72 + TreeWalkListener listener = new TreeWalkListener();
73 + GeneratedYangParser.YangfileContext ctx = null;
74 + BaseFileListener.processYangFileExit(listener, ctx);
75 + }
76 +
77 + /**
78 + * Checks that exception shouldn't be generated if stack of parsable data is
79 + * empty at the exit of yang base rule.
80 + */
81 + @Test
82 + public void processYangFileExitNonEmptyStack() {
83 +
84 + TreeWalkListener listener = new TreeWalkListener();
85 + GeneratedYangParser.YangfileContext ctx = null;
86 + BaseFileListener.processYangFileEntry(listener, ctx);
87 + }
88 +
89 + /**
90 + * Checks that after popping out the parsable node from stack it should be
91 + * empty.
92 + */
93 + @Test
94 + public void processYangFileExitStackErrorExtraEntryTest() {
95 + thrown.expect(ParserException.class);
96 + thrown.expectMessage("Internal parser error detected: Invalid holder for yangbase after processing.");
97 +
98 + YangModule tmpModule = new YangModule();
99 + YangModule tmpModule2 = new YangModule();
100 + TreeWalkListener listener = new TreeWalkListener();
101 + listener.getParsedDataStack().push(tmpModule);
102 + listener.getParsedDataStack().push(tmpModule2);
103 + GeneratedYangParser.YangfileContext ctx = null;
104 + BaseFileListener.processYangFileExit(listener, ctx);
105 + }
106 +}
...\ 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 org.junit.Rule;
20 +import org.junit.Test;
21 +import org.junit.rules.ExpectedException;
22 +import org.onosproject.yangutils.datamodel.YangNode;
23 +import org.onosproject.yangutils.datamodel.YangSubModule;
24 +import org.onosproject.yangutils.parser.exceptions.ParserException;
25 +import org.onosproject.yangutils.parser.impl.CustomExceptionMatcher;
26 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
27 +
28 +import java.io.IOException;
29 +
30 +import static org.hamcrest.core.Is.is;
31 +import static org.junit.Assert.assertThat;
32 +
33 +/**
34 + * Test cases for testing belongsto listener functionality.
35 + */
36 +public class BelongstoListenerTest {
37 +
38 + @Rule
39 + public ExpectedException thrown = ExpectedException.none();
40 +
41 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
42 +
43 + /**
44 + * Checks if mandatory belongsto parameter "prefix" is not present.
45 + */
46 + @Test
47 + public void processBelongsToWithoutPrefix() throws IOException, ParserException {
48 + thrown.expect(ParserException.class);
49 + thrown.expectMessage("mismatched input '}' expecting 'prefix'");
50 + thrown.expect(CustomExceptionMatcher.errorLocation(4, 0));
51 + YangNode node = manager.getDataModel("src/test/resources/BelongsToWithoutPrefix.yang");
52 + }
53 +
54 + /**
55 + * Checks that prefix must be present only once in belongsto.
56 + */
57 + @Test
58 + public void processBelongsToDualPrefix() throws IOException, ParserException {
59 + thrown.expect(ParserException.class);
60 + thrown.expectMessage("mismatched input 'prefix' expecting '}'");
61 + thrown.expect(CustomExceptionMatcher.errorLocation(5, 0));
62 + YangNode node = manager.getDataModel("src/test/resources/BelongsToDualPrefix.yang");
63 + }
64 +
65 + /**
66 + * Checks if belongsto listener updates the date model tree.
67 + */
68 + @Test
69 + public void processBelongsToWithPrefix() throws IOException, ParserException {
70 +
71 + YangNode node = manager.getDataModel("src/test/resources/BelongsToWithPrefix.yang");
72 + YangSubModule yangNode = (YangSubModule) node;
73 + assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
74 + }
75 +
76 + /**
77 + * Checks if mandatory parameter "belongsto" is present.
78 + */
79 + @Test
80 + public void processSubModuleWithoutBelongsTo() throws IOException, ParserException {
81 + thrown.expect(ParserException.class);
82 + thrown.expectMessage("mismatched input '}' expecting 'belongs-to'");
83 + thrown.expect(CustomExceptionMatcher.errorLocation(3, 0));
84 + YangNode node = manager.getDataModel("src/test/resources/SubModuleWithoutBelongsTo.yang");
85 + }
86 +}
...\ 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 org.junit.Rule;
20 +import org.junit.Test;
21 +import org.junit.rules.ExpectedException;
22 +import org.onosproject.yangutils.datamodel.YangModule;
23 +import org.onosproject.yangutils.datamodel.YangNode;
24 +import org.onosproject.yangutils.parser.exceptions.ParserException;
25 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
26 +
27 +import java.io.IOException;
28 +
29 +import static org.hamcrest.core.Is.is;
30 +import static org.junit.Assert.assertThat;
31 +
32 +/**
33 + * Test cases for testing contact listener functionality.
34 + */
35 +public class ContactListenerTest {
36 +
37 + @Rule
38 + public ExpectedException thrown = ExpectedException.none();
39 +
40 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
41 +
42 + /**
43 + * Checks if contact listener updates the data model tree.
44 + */
45 + @Test
46 + public void processContactValidEntry() throws IOException, ParserException {
47 +
48 + YangNode node = manager.getDataModel("src/test/resources/ContactValidEntry.yang");
49 +
50 + // Checks for the version value in data model tree.
51 + assertThat(((YangModule) node).getContact(), is("\"WG List: <mailto:spring@ietf.org>\nEditor: "
52 + + "Stephane Litkowski\n " + "<mailto:stephane.litkowski@orange.com>\""));
53 + }
54 +
55 + /**
56 + * Checks that contact must be present only once.
57 + */
58 + @Test(expected = ParserException.class)
59 + public void processContactDualEntryTest() throws IOException, ParserException {
60 +
61 + YangNode node = manager.getDataModel("src/test/resources/ContactDualEntryTest.yang");
62 +
63 + }
64 +
65 + /**
66 + * Checks if contact is not empty.
67 + */
68 + @Test(expected = ParserException.class)
69 + public void processContactWithEmptyString() throws IOException, ParserException {
70 +
71 + YangNode node = manager.getDataModel("src/test/resources/ContactWithEmptyString.yang");
72 + }
73 +
74 + /**
75 + * Checks that contact must be present after namespace.
76 + */
77 + @Test(expected = ParserException.class)
78 + public void processContactIncorrectOrder() throws IOException, ParserException {
79 +
80 + YangNode node = manager.getDataModel("src/test/resources/ContactIncorrectOrder.yang");
81 + }
82 +}
...\ 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 org.junit.Test;
20 +import org.onosproject.yangutils.datamodel.YangModule;
21 +import org.onosproject.yangutils.datamodel.YangNode;
22 +import org.onosproject.yangutils.parser.exceptions.ParserException;
23 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
24 +
25 +import java.io.IOException;
26 +
27 +import static org.hamcrest.core.Is.is;
28 +import static org.junit.Assert.assertThat;
29 +
30 +/**
31 + * Test cases for testing import listener functionality.
32 + */
33 +public class ImportListenerTest {
34 +
35 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
36 +
37 + /**
38 + * Checks if mandatory parameter prefix is present in import.
39 + */
40 + @Test(expected = ParserException.class)
41 + public void processImportWithoutPrefix() throws IOException, ParserException {
42 +
43 + YangNode node = manager.getDataModel("src/test/resources/ImportWithoutPrefix.yang");
44 + }
45 +
46 + /**
47 + * Checks that prefix must be present only once in import.
48 + */
49 + @Test(expected = ParserException.class)
50 + public void processImportWithDualPrefix() throws IOException, ParserException {
51 +
52 + YangNode node = manager.getDataModel("src/test/resources/ImportWithDualPrefix.yang");
53 + }
54 +
55 + /**
56 + * Checks for the correct order of prefix in import.
57 + */
58 + @Test(expected = ParserException.class)
59 + public void processImportInvalidOrder() throws IOException, ParserException {
60 +
61 + YangNode node = manager.getDataModel("src/test/resources/ImportInvalidOrder.yang");
62 + }
63 +
64 + /**
65 + * Checks if import listener updates the data model tree.
66 + */
67 + @Test
68 + public void processImportValidEntry() throws IOException, ParserException {
69 +
70 + YangNode node = manager.getDataModel("src/test/resources/ImportValidEntry.yang");
71 +
72 + // Checks for the revision value in data model tree.
73 + assertThat(((YangModule) node).getImportList().get(0).getRevision(), is("2015-02-03"));
74 + // Checks for the prefix id in data model tree.
75 + assertThat(((YangModule) node).getImportList().get(0).getPrefixId(), is("On2"));
76 + // Checks for the module name in data model tree.
77 + assertThat(((YangModule) node).getImportList().get(0).getModuleName(), is("ietf"));
78 + }
79 +
80 + /**
81 + * Checks if optional parameter revision is not mandatory in import.
82 + */
83 + @Test
84 + public void processImportWithoutRevision() throws IOException, ParserException {
85 +
86 + YangNode node = manager.getDataModel("src/test/resources/ImportWithoutRevision.yang");
87 +
88 + // Checks for the prefix id in data model tree.
89 + assertThat(((YangModule) node).getImportList().get(0).getPrefixId(), is("On2"));
90 + // Checks for the module name in data model tree.
91 + assertThat(((YangModule) node).getImportList().get(0).getModuleName(), is("ietf"));
92 + }
93 +
94 + /**
95 + * Checks if multiple imports are allowed.
96 + */
97 + @Test()
98 + public void processImportMultipleInstance() throws IOException, ParserException {
99 +
100 + YangNode node = manager.getDataModel("src/test/resources/ImportMultipleInstance.yang");
101 +
102 + // Checks for the prefix id in data model tree.
103 + assertThat(((YangModule) node).getImportList().get(0).getPrefixId(), is("On2"));
104 + // Checks for the module name in data model tree.
105 + assertThat(((YangModule) node).getImportList().get(0).getModuleName(), is("ietf"));
106 +
107 + // Checks for the prefix id in data model tree.
108 + assertThat(((YangModule) node).getImportList().get(1).getPrefixId(), is("On3"));
109 + // Checks for the module name in data model tree.
110 + assertThat(((YangModule) node).getImportList().get(1).getModuleName(), is("itut"));
111 + }
112 +}
...\ 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 org.junit.Test;
20 +import org.onosproject.yangutils.datamodel.YangModule;
21 +import org.onosproject.yangutils.datamodel.YangNode;
22 +import org.onosproject.yangutils.parser.exceptions.ParserException;
23 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
24 +
25 +import java.io.IOException;
26 +
27 +import static org.hamcrest.core.Is.is;
28 +import static org.junit.Assert.assertThat;
29 +
30 +/**
31 + * Test cases for testing include listener functionality.
32 + */
33 +public class IncludeListenerTest {
34 +
35 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
36 +
37 + /**
38 + * Checks if include listener with ; is valid and updates the data
39 + * model tree.
40 + */
41 + @Test
42 + public void processIncludeWithStmtend() throws IOException, ParserException {
43 +
44 + YangNode node = manager.getDataModel("src/test/resources/IncludeWithStmtend.yang");
45 +
46 + // Checks for the sub module name in data model tree.
47 + assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
48 + }
49 +
50 + /**
51 + * Checks if include listener with braces and without revision date is valid
52 + * and updates the data model tree.
53 + */
54 + @Test
55 + public void processIncludeWithEmptyBody() throws IOException, ParserException {
56 +
57 + YangNode node = manager.getDataModel("src/test/resources/IncludeWithEmptyBody.yang");
58 +
59 + // Checks for the sub module name in data model tree.
60 + assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
61 + }
62 +
63 + /**
64 + * Checks if include listener with braces and with revision date is valid
65 + * and updates the data model tree.
66 + */
67 + @Test
68 + public void processIncludeWithDate() throws IOException, ParserException {
69 +
70 + YangNode node = manager.getDataModel("src/test/resources/IncludeWithDate.yang");
71 +
72 + // Checks for the sub module name in data model tree.
73 + assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
74 + assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is("2016-02-03"));
75 + }
76 +
77 + /**
78 + * Checks if include has more than one occurrence.
79 + */
80 + @Test
81 + public void processIncludeMultiInstance() throws IOException, ParserException {
82 +
83 + YangNode node = manager.getDataModel("src/test/resources/IncludeMultiInstance.yang");
84 +
85 + // Checks for the sub module name in data model tree.
86 + assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
87 + assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is("2016-02-03"));
88 + assertThat(((YangModule) node).getIncludeList().get(1).getSubModuleName(), is("sdn"));
89 + assertThat(((YangModule) node).getIncludeList().get(1).getRevision(), is("2014-02-03"));
90 + }
91 +
92 + /**
93 + * Checks if include and import can come in any order.
94 + */
95 + @Test
96 + public void processIncludeImportAnyOrder() throws IOException, ParserException {
97 +
98 + YangNode node = manager.getDataModel("src/test/resources/IncludeImportAnyOrder.yang");
99 +
100 + // Checks for the sub module name in data model tree.
101 + assertThat(((YangModule) node).getIncludeList().get(0).getSubModuleName(), is("itut"));
102 + assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is("2016-02-03"));
103 + assertThat(((YangModule) node).getIncludeList().get(1).getSubModuleName(), is("sdn"));
104 + assertThat(((YangModule) node).getIncludeList().get(1).getRevision(), is("2014-02-03"));
105 + }
106 +
107 + /**
108 + * Checks if syntax of Include is not correct.
109 + */
110 + @Test(expected = ParserException.class)
111 + public void processIncludeInvalidSyntax() throws IOException, ParserException {
112 +
113 + YangNode node = manager.getDataModel("src/test/resources/IncludeInvalidSyntax.yang");
114 + }
115 +
116 + /**
117 + * Checks if syntax of revision date in Include is not correct.
118 + */
119 + @Test(expected = ParserException.class)
120 + public void processIncludeInvalidDateSyntax() throws IOException, ParserException {
121 +
122 + YangNode node = manager.getDataModel("src/test/resources/IncludeInvalidDateSyntax.yang");
123 + }
124 +}
...\ 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 org.junit.Test;
20 +import org.onosproject.yangutils.datamodel.YangModule;
21 +import org.onosproject.yangutils.datamodel.YangNode;
22 +import org.onosproject.yangutils.datamodel.YangNodeType;
23 +import org.onosproject.yangutils.parser.exceptions.ParserException;
24 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
25 +
26 +import java.io.IOException;
27 +
28 +import static org.hamcrest.core.Is.is;
29 +import static org.junit.Assert.assertThat;
30 +
31 +/**
32 + * Test cases for testing module listener functionality.
33 + */
34 +public class ModuleListenerTest {
35 +
36 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
37 +
38 + /**
39 + * Checks if module listener updates the data model root node.
40 + */
41 + @Test
42 + public void processModuleValidEntry() throws IOException, ParserException {
43 +
44 + YangNode node = manager.getDataModel("src/test/resources/ModuleValidEntry.yang");
45 +
46 + // Check whether the data model tree returned is of type module.
47 + assertThat((node instanceof YangModule), is(true));
48 +
49 + // Check whether the node type is set properly to module.
50 + assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
51 +
52 + // Check whether the module name is set correctly.
53 + YangModule yangNode = (YangModule) node;
54 + assertThat(yangNode.getName(), is("Test"));
55 + }
56 +
57 + /**
58 + * Checks if module name is set correctly.
59 + */
60 + @Test(expected = ParserException.class)
61 + public void processModuleInvalidEntryTest() throws IOException, ParserException {
62 +
63 + YangNode node = manager.getDataModel("src/test/resources/ModuleWithInvalidIdentifier.yang");
64 + }
65 +}
...\ 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 org.junit.Test;
20 +import org.onosproject.yangutils.datamodel.YangModule;
21 +import org.onosproject.yangutils.datamodel.YangNode;
22 +import org.onosproject.yangutils.parser.exceptions.ParserException;
23 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
24 +
25 +import java.io.IOException;
26 +
27 +import static org.hamcrest.core.Is.is;
28 +import static org.junit.Assert.assertThat;
29 +
30 +/**
31 + * Test cases for testing namespace listener functionality.
32 + */
33 +public class NamespaceListenerTest {
34 +
35 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
36 +
37 + /**
38 + * Checks that value of namespace shouldn't have invalid spaces.
39 + */
40 + @Test(expected = ParserException.class)
41 + public void processNamespaceWithInvalidSpaces() throws IOException, ParserException {
42 +
43 + YangNode node = manager.getDataModel("src/test/resources/NamespaceWithInvalidSpaces.yang");
44 + }
45 +
46 + /**
47 + * Checks if namespace with double quotes is allowed.
48 + */
49 + @Test()
50 + public void processNamespaceInDoubleQuotes() throws IOException, ParserException {
51 +
52 + YangNode node = manager.getDataModel("src/test/resources/NamespaceInDoubleQuotes.yang");
53 +
54 + // Checks for the version value in data model tree.
55 + assertThat(((YangModule) node).getNameSpace().getUri(), is("\"urn:ietf:params:xml:ns:yang:ietf-ospf\""));
56 + }
57 +
58 + /**
59 + * Checks if namespace without double quotes is allowed.
60 + */
61 + @Test()
62 + public void processNamespaceWithoutQuotes() throws IOException, ParserException {
63 +
64 + YangNode node = manager.getDataModel("src/test/resources/NamespaceWithoutQuotes.yang");
65 +
66 + // Checks for the version value in data model tree.
67 + assertThat(((YangModule) node).getNameSpace().getUri(), is("urn:ietf:params:xml:ns:yang:ietf-ospf"));
68 + }
69 +
70 + /**
71 + * Checks if namespace is present only once.
72 + */
73 + @Test(expected = ParserException.class)
74 + public void processNamespaceDualEntry() throws IOException, ParserException {
75 +
76 + YangNode node = manager.getDataModel("src/test/resources/NamespaceDualEntry.yang");
77 + }
78 +
79 + /**
80 + * Checks if mandatory parameter namespace is present.
81 + */
82 + @Test(expected = ParserException.class)
83 + public void processNamespaceNoEntryTest() throws IOException, ParserException {
84 +
85 + YangNode node = manager.getDataModel("src/test/resources/NamespaceNoEntryTest.yang");
86 + }
87 +}
...\ 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 org.junit.Test;
20 +import org.onosproject.yangutils.datamodel.YangModule;
21 +import org.onosproject.yangutils.datamodel.YangNode;
22 +import org.onosproject.yangutils.parser.exceptions.ParserException;
23 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
24 +
25 +import java.io.IOException;
26 +
27 +import static org.hamcrest.core.Is.is;
28 +import static org.junit.Assert.assertThat;
29 +
30 +/**
31 + * Test cases for testing organization listener functionality.
32 + */
33 +public class OrganizationListenerTest {
34 +
35 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
36 +
37 + /**
38 + * Checks if organization listener updates the data model tree.
39 + */
40 + @Test
41 + public void processOrganizationValidEntry() throws IOException, ParserException {
42 +
43 + YangNode node = manager.getDataModel("src/test/resources/OrganizationValidEntry.yang");
44 +
45 + // Checks for the version value in data model tree.
46 + assertThat(((YangModule) node).getOrganization(), is("\"IETF SPRING Working Group\""));
47 + }
48 +
49 + /**
50 + * Checks that organization must be present only once.
51 + */
52 + @Test(expected = ParserException.class)
53 + public void processOrganizationDualEntry() throws IOException, ParserException {
54 +
55 + YangNode node = manager.getDataModel("src/test/resources/OrganizationDualEntry.yang");
56 + }
57 +
58 + /**
59 + * Checks if organization entry syntax is correct.
60 + */
61 + @Test(expected = ParserException.class)
62 + public void processOrganizationMissingValue() throws IOException, ParserException {
63 +
64 + YangNode node = manager.getDataModel("src/test/resources/OrganizationMissingValue.yang");
65 + }
66 +
67 + /**
68 + * Checks if organization and namespace is present in correct order.
69 + */
70 + @Test(expected = ParserException.class)
71 + public void processOrganizationInvalidOrder() throws IOException, ParserException {
72 +
73 + YangNode node = manager.getDataModel("src/test/resources/OrganizationInvalidOrder.yang");
74 + }
75 +}
...\ 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 org.junit.Test;
20 +import org.onosproject.yangutils.datamodel.YangModule;
21 +import org.onosproject.yangutils.datamodel.YangNode;
22 +import org.onosproject.yangutils.parser.exceptions.ParserException;
23 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
24 +
25 +import java.io.IOException;
26 +
27 +import static org.hamcrest.core.Is.is;
28 +import static org.junit.Assert.assertThat;
29 +
30 +/**
31 + * Test cases for testing prefix listener functionality.
32 + */
33 +public class PrefixListenerTest {
34 +
35 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
36 +
37 + /**
38 + * Checks if value of prefix is correct.
39 + */
40 + @Test(expected = ParserException.class)
41 + public void processPrefixInvalidValue() throws IOException, ParserException {
42 +
43 + YangNode node = manager.getDataModel("src/test/resources/PrefixInvalidValue.yang");
44 + }
45 +
46 + /**
47 + * Checks if prefix listener updates the data model tree.
48 + */
49 + @Test
50 + public void processPrefixValidEntry() throws IOException, ParserException {
51 +
52 + YangNode node = manager.getDataModel("src/test/resources/PrefixValidEntry.yang");
53 +
54 + // Checks for the version value in data model tree.
55 + assertThat(((YangModule) node).getPrefix(), is("On"));
56 + }
57 +
58 + /**
59 + * Checks that prefix should be present just once.
60 + */
61 + @Test(expected = ParserException.class)
62 + public void processPrefixDualEntry() throws IOException, ParserException {
63 +
64 + YangNode node = manager.getDataModel("src/test/resources/PrefixDualEntry.yang");
65 + }
66 +
67 + /**
68 + * Checks if prefix syntax is followed.
69 + */
70 + @Test(expected = ParserException.class)
71 + public void processPrefixMissingValue() throws IOException, ParserException {
72 +
73 + YangNode node = manager.getDataModel("src/test/resources/PrefixMissingValue.yang");
74 + }
75 +
76 + /**
77 + * Checks that exception should be reported if prefix is missing.
78 + */
79 + @Test(expected = ParserException.class)
80 + public void processPrefixOrder() throws IOException, ParserException {
81 +
82 + YangNode node = manager.getDataModel("src/test/resources/PrefixOrder.yang");
83 + }
84 +}
...\ 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 org.junit.Test;
20 +import org.onosproject.yangutils.datamodel.YangModule;
21 +import org.onosproject.yangutils.datamodel.YangNode;
22 +import org.onosproject.yangutils.parser.exceptions.ParserException;
23 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
24 +
25 +import java.io.IOException;
26 +
27 +import static org.hamcrest.core.Is.is;
28 +import static org.junit.Assert.assertThat;
29 +
30 +/**
31 + * Test cases for testing revision date listener functionality.
32 + */
33 +public class RevisionDateListenerTest {
34 +
35 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
36 +
37 + /**
38 + * Checks if revision date syntax is correct in include.
39 + */
40 + @Test(expected = ParserException.class)
41 + public void processRevisionDateInvalidSyntaxAtInclude() throws IOException, ParserException {
42 +
43 + YangNode node = manager.getDataModel("src/test/resources/RevisionDateInvalidSyntaxAtInclude.yang");
44 + }
45 +
46 + /**
47 + * Checks if revision date syntax is correct in import.
48 + */
49 + @Test(expected = ParserException.class)
50 + public void processRevisionDateInvalidSyntaxAtImport() throws IOException, ParserException {
51 +
52 + YangNode node = manager.getDataModel("src/test/resources/RevisionDateInvalidSyntaxAtImport.yang");
53 + }
54 +
55 + /**
56 + * Checks revision date should not be in quotes inside include.
57 + */
58 + @Test(expected = ParserException.class)
59 + public void processRevisionDateInQuotesAtInclude() throws IOException, ParserException {
60 +
61 + YangNode node = manager.getDataModel("src/test/resources/RevisionDateInQuotesAtInclude.yang");
62 + }
63 +
64 + /**
65 + * Checks revision date should not be in quotes inside import.
66 + */
67 + @Test(expected = ParserException.class)
68 + public void processRevisionDateInQuotesAtImport() throws IOException, ParserException {
69 +
70 + YangNode node = manager.getDataModel("src/test/resources/RevisionDateInQuotesAtImport.yang");
71 + }
72 +
73 + /**
74 + * Checks if revision date follows YYYY-MM-DD format.
75 + */
76 + @Test(expected = ParserException.class)
77 + public void processRevisionDateInvalidFormat() throws IOException, ParserException {
78 +
79 + YangNode node = manager.getDataModel("src/test/resources/RevisionDateInvalidFormat.yang");
80 + }
81 +
82 + /**
83 + * Checks if revision date listener updates the data model tree.
84 + */
85 + @Test
86 + public void processRevisionDateValidEntry() throws IOException, ParserException {
87 +
88 + YangNode node = manager.getDataModel("src/test/resources/RevisionDateValidEntry.yang");
89 +
90 + // Checks for the version value in data model tree.
91 + assertThat(((YangModule) node).getImportList().get(0).getRevision(), is("2015-02-03"));
92 + assertThat(((YangModule) node).getIncludeList().get(0).getRevision(), is("2016-02-03"));
93 + assertThat(((YangModule) node).getIncludeList().get(1).getRevision(), is("2014-02-03"));
94 + }
95 +}
...\ 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 org.junit.Test;
20 +import org.onosproject.yangutils.datamodel.YangModule;
21 +import org.onosproject.yangutils.datamodel.YangNode;
22 +import org.onosproject.yangutils.parser.exceptions.ParserException;
23 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
24 +
25 +import java.io.IOException;
26 +
27 +import static org.hamcrest.core.Is.is;
28 +import static org.junit.Assert.assertThat;
29 +
30 +/**
31 + * Test cases for testing revision listener functionality.
32 + */
33 +public class RevisionListenerTest {
34 +
35 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
36 +
37 + /**
38 + * Checks if revision doesn't have optional parameters "revision and
39 + * description".
40 + */
41 + @Test
42 + public void processRevisionNoOptionalParameter() throws IOException, ParserException {
43 +
44 + YangNode node = manager.getDataModel("src/test/resources/RevisionNoOptionalParameter.yang");
45 +
46 + // Checks for the version value in data model tree.
47 + assertThat(((YangModule) node).getRevision().getRevDate(), is("2016-02-03"));
48 + }
49 +
50 + /**
51 + * Checks if the syntax of revision is correct.
52 + */
53 + @Test(expected = ParserException.class)
54 + public void processRevisionInValidSyntax() throws IOException, ParserException {
55 +
56 + YangNode node = manager.getDataModel("src/test/resources/RevisionInValidSyntax.yang");
57 + }
58 +
59 + /**
60 + * Checks if the correct order is followed.
61 + */
62 + @Test(expected = ParserException.class)
63 + public void processRevisionInValidOrder() throws IOException, ParserException {
64 +
65 + YangNode node = manager.getDataModel("src/test/resources/RevisionInValidOrder.yang");
66 + }
67 +}
...\ 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 org.junit.Test;
20 +import org.onosproject.yangutils.datamodel.YangNode;
21 +import org.onosproject.yangutils.datamodel.YangNodeType;
22 +import org.onosproject.yangutils.datamodel.YangSubModule;
23 +import org.onosproject.yangutils.parser.exceptions.ParserException;
24 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
25 +
26 +import java.io.IOException;
27 +
28 +import static org.hamcrest.core.Is.is;
29 +import static org.junit.Assert.assertThat;
30 +
31 +/**
32 + * Test cases for testing submodule listener functionality.
33 + */
34 +public class SubModuleListenerTest {
35 +
36 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
37 +
38 + /**
39 + * Checks if the sub module listeners updates the data model tree.
40 + */
41 + @Test
42 + public void processSubModuleValidEntry() throws IOException, ParserException {
43 +
44 + YangNode node = manager.getDataModel("src/test/resources/SubModuleValidEntry.yang");
45 +
46 + // Check whether the data model tree returned is of type module.
47 + assertThat((node instanceof YangSubModule), is(true));
48 +
49 + // Check whether the node type is set properly to module.
50 + assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE));
51 +
52 + YangSubModule yangNode = (YangSubModule) node;
53 + // Check whether the module name is set correctly.
54 + assertThat(yangNode.getName(), is("Test"));
55 + // Checks for the version value in data model tree.
56 + assertThat(yangNode.getVersion(), is((byte) 1));
57 + // Checks identifier of belongsto in data model tree.
58 + assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
59 + // Checks for the version value in data model tree.
60 + assertThat(yangNode.getBelongsTo().getPrefix(), is("On1"));
61 + }
62 +
63 + /**
64 + * Checks if the yang version and belongs to can come in any order in sub
65 + * module.
66 + */
67 + @Test
68 + public void processSubModuleOrder() throws IOException, ParserException {
69 +
70 + YangNode node = manager.getDataModel("src/test/resources/SubModuleOrder.yang");
71 +
72 + // Check whether the data model tree returned is of type module.
73 + assertThat((node instanceof YangSubModule), is(true));
74 +
75 + // Check whether the node type is set properly to module.
76 + assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE));
77 +
78 + YangSubModule yangNode = (YangSubModule) node;
79 + // Check whether the module name is set correctly.
80 + assertThat(yangNode.getName(), is("Test"));
81 + // Checks for the version value in data model tree.
82 + assertThat(yangNode.getVersion(), is((byte) 1));
83 + // Checks identifier of belongsto in data model tree.
84 + assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
85 + // Checks for the version value in data model tree.
86 + assertThat(yangNode.getBelongsTo().getPrefix(), is("On1"));
87 + }
88 +
89 + /**
90 + * Checks if yang version is optional.
91 + */
92 + @Test
93 + public void processSubModuleWithoutVersion() throws IOException, ParserException {
94 +
95 + YangNode node = manager.getDataModel("src/test/resources/SubModuleWithoutVersion.yang");
96 +
97 + // Check whether the data model tree returned is of type module.
98 + assertThat((node instanceof YangSubModule), is(true));
99 +
100 + // Check whether the node type is set properly to module.
101 + assertThat(node.getNodeType(), is(YangNodeType.SUB_MODULE_NODE));
102 +
103 + YangSubModule yangNode = (YangSubModule) node;
104 + // Check whether the module name is set correctly.
105 + assertThat(yangNode.getName(), is("Test"));
106 + // Checks identifier of belongsto in data model tree.
107 + assertThat(yangNode.getBelongsTo().getBelongsToModuleName(), is("ONOS"));
108 + // Checks for the version value in data model tree.
109 + assertThat(yangNode.getBelongsTo().getPrefix(), is("On1"));
110 + }
111 +
112 + /**
113 + * Checks if sub module name is correct.
114 + */
115 + @Test(expected = ParserException.class)
116 + public void processSubModuleInvalidName() throws IOException, ParserException {
117 +
118 + YangNode node = manager.getDataModel("src/test/resources/SubModuleInvalidName.yang");
119 + }
120 +
121 + /**
122 + * Checks if sub module has invalid modules construct eg namespace.
123 + */
124 + @Test(expected = ParserException.class)
125 + public void processSubModuleWithNamespace() throws IOException, ParserException {
126 +
127 + YangNode node = manager.getDataModel("src/test/resources/SubModuleWithNamespace.yang");
128 + }
129 +}
...\ 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 org.junit.Test;
20 +import org.onosproject.yangutils.datamodel.YangModule;
21 +import org.onosproject.yangutils.datamodel.YangNode;
22 +import org.onosproject.yangutils.parser.exceptions.ParserException;
23 +import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
24 +
25 +import java.io.IOException;
26 +
27 +import static org.hamcrest.core.Is.is;
28 +import static org.junit.Assert.assertThat;
29 +
30 +/**
31 + * Test cases for testing version listener functionality.
32 + */
33 +public class VersionListenerTest {
34 +
35 + private final YangUtilsParserManager manager = new YangUtilsParserManager();
36 +
37 + /**
38 + * Checks if value of version is correct.
39 + */
40 + @Test(expected = ParserException.class)
41 + public void processVersionInvalidValue() throws IOException, ParserException {
42 +
43 + YangNode node = manager.getDataModel("src/test/resources/VersionInvalidValue.yang");
44 + }
45 +
46 + /**
47 + * Checks if version listener updates the data model tree.
48 + */
49 + @Test
50 + public void processVersionValidEntry() throws IOException, ParserException {
51 +
52 + YangNode node = manager.getDataModel("src/test/resources/VersionValidEntry.yang");
53 +
54 + // Checks for the version value in data model tree.
55 + assertThat(((YangModule) node).getVersion(), is((byte) 1));
56 + }
57 +
58 + /**
59 + * Checks if version which is optional paramater is not present.
60 + */
61 + @Test
62 + public void processVersionNotPresent() throws IOException, ParserException {
63 +
64 + YangNode node = manager.getDataModel("src/test/resources/VersionNotPresent.yang");
65 +
66 + // Checks for the version value in data model tree.
67 + assertThat(((YangModule) node).getVersion(), is((byte) 0));
68 + }
69 +
70 + /**
71 + * Checks that version should be present only once.
72 + */
73 + @Test(expected = ParserException.class)
74 + public void processVersionDualEntry() throws IOException, ParserException {
75 +
76 + YangNode node = manager.getDataModel("src/test/resources/VersionDualEntry.yang");
77 + }
78 +
79 + /**
80 + * Checks if version can appear in any order in module header.
81 + */
82 + @Test
83 + public void processVersionOrder() throws IOException, ParserException {
84 +
85 + YangNode node = manager.getDataModel("src/test/resources/VersionOrder.yang");
86 +
87 + // Checks for the version value in data model tree.
88 + assertThat(((YangModule) node).getVersion(), is((byte) 1));
89 + }
90 +
91 + /**
92 + * Checks if sytax of version entry is not correct.
93 + */
94 + @Test(expected = ParserException.class)
95 + public void processVersionInvalidSyntax() throws IOException, ParserException {
96 +
97 + YangNode node = manager.getDataModel("src/test/resources/VersionInvalidSyntax.yang");
98 + }
99 +}
...\ 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.parseutils;
18 +
19 +import org.junit.Test;
20 +import org.onosproject.yangutils.parser.ParsableDataType;
21 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
22 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
23 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
24 +
25 +import static org.hamcrest.core.Is.is;
26 +import static org.junit.Assert.assertThat;
27 +
28 +/**
29 + * Test case for testing listener error message construction util.
30 + */
31 +public class ListenerErrorMessageConstructionTest {
32 +
33 + /**
34 + * Checks for error message construction with parsable data type name.
35 + */
36 + @Test
37 + public void checkErrorMsgConstructionWithName() {
38 +
39 + // Create an test error message
40 + String testErrorMessage = ListenerErrorMessageConstruction
41 + .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER, ParsableDataType.CONTACT_DATA,
42 + "Test Instance", ListenerErrorLocation.ENTRY);
43 +
44 + // Check message.
45 + assertThat(testErrorMessage, is("Internal parser error detected: Invalid holder for contact "
46 + + "\"Test Instance\" before processing."));
47 + }
48 +
49 + /**
50 + * Checks for error message construction without parsable data type name.
51 + */
52 + @Test
53 + public void checkErrorMsgConstructionWithoutName() {
54 +
55 + // Create an test error message
56 + String testErrorMessage = ListenerErrorMessageConstruction
57 + .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER, ParsableDataType.CONTACT_DATA,
58 + "Test Instance", ListenerErrorLocation.ENTRY);
59 +
60 + // Check message.
61 + assertThat(testErrorMessage,
62 + is("Internal parser error detected: Invalid holder for contact \"Test Instance\""
63 + + " before processing."));
64 + }
65 +
66 + /**
67 + * Checks for extended error message construction with parsable data type name.
68 + */
69 + @Test
70 + public void checkExtendedErrorMsgConstructionWithName() {
71 +
72 + // Create an test error message
73 + String testErrorMessage = ListenerErrorMessageConstruction
74 + .constructExtendedListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
75 + ParsableDataType.CONTACT_DATA, "Test Instance",
76 + ListenerErrorLocation.ENTRY, "Extended Information");
77 +
78 + // Check message.
79 + assertThat(testErrorMessage,
80 + is("Internal parser error detected: Invalid holder for contact \"Test Instance\""
81 + + " before processing.\n" + "Error Information: Extended Information"));
82 + }
83 +
84 + /**
85 + * Checks for extended error message construction without parsable data type name.
86 + */
87 + @Test
88 + public void checkExtendedErrorMsgConstructionWithoutName() {
89 +
90 + // Create an test error message
91 + String testErrorMessage = ListenerErrorMessageConstruction
92 + .constructExtendedListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
93 + ParsableDataType.CONTACT_DATA, "",
94 + ListenerErrorLocation.ENTRY, "Extended Information");
95 +
96 + // Check message.
97 + assertThat(testErrorMessage, is("Internal parser error detected: Invalid holder for contact"
98 + + " before processing.\n" + "Error Information: Extended Information"));
99 + }
100 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -16,74 +16,80 @@ ...@@ -16,74 +16,80 @@
16 16
17 package org.onosproject.yangutils.parser.parseutils; 17 package org.onosproject.yangutils.parser.parseutils;
18 18
19 +import org.junit.Rule;
19 import org.junit.Test; 20 import org.junit.Test;
21 +import org.junit.rules.ExpectedException;
20 import org.onosproject.yangutils.datamodel.YangRevision; 22 import org.onosproject.yangutils.datamodel.YangRevision;
23 +import org.onosproject.yangutils.parser.ParsableDataType;
24 +import org.onosproject.yangutils.parser.exceptions.ParserException;
21 import org.onosproject.yangutils.parser.impl.TreeWalkListener; 25 import org.onosproject.yangutils.parser.impl.TreeWalkListener;
22 -import org.onosproject.yangutils.parser.impl.parserutils.ListenerError; 26 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28 +import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
23 import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation; 29 import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
24 30
25 -import static org.hamcrest.core.Is.is;
26 -import static org.junit.Assert.assertThat;
27 -
28 /** 31 /**
29 * Test case for testing listener validation util. 32 * Test case for testing listener validation util.
30 */ 33 */
31 public class ListenerValidationTest { 34 public class ListenerValidationTest {
32 35
36 + @Rule
37 + public ExpectedException thrown = ExpectedException.none();
38 +
33 /** 39 /**
34 - * This test case checks in case error pre-exists, listener validate 40 + * Checks for exception in case parsable stack is empty while validating for
35 - * function returns true. 41 + * not empty scenario.
36 */ 42 */
37 @Test 43 @Test
38 - public void listenerValidationErrorExists() { 44 + public void validateStackIsNotEmptyForEmptyStack() {
45 +
46 + String expectedError = ListenerErrorMessageConstruction
47 + .constructListenerErrorMessage(ListenerErrorType.MISSING_HOLDER, ParsableDataType.YANGBASE_DATA, "",
48 + ListenerErrorLocation.EXIT);
39 49
40 - // Create an test error. 50 + // Get the exception occurred during parsing.
41 - ListenerError testError = new ListenerError(); 51 + thrown.expect(ParserException.class);
42 - testError.setErrorFlag(true); 52 + thrown.expectMessage(expectedError);
43 - testError.setErrorMsg("Test Error");
44 53
45 // Create test walker and assign test error to it. 54 // Create test walker and assign test error to it.
46 TreeWalkListener testWalker = new TreeWalkListener(); 55 TreeWalkListener testWalker = new TreeWalkListener();
47 - testWalker.setErrorInformation(testError);
48 56
49 - // Create a temporary node of parsable. 57 + ListenerValidation.checkStackIsNotEmpty(testWalker, ListenerErrorType.MISSING_HOLDER,
50 - YangRevision tmpNode = new YangRevision(); 58 + ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.EXIT);
51 - testWalker.getParsedDataStack().push(tmpNode);
52 -
53 - boolean errorFlag = ListenerValidation.preValidation(testWalker, "ErrorTest");
54 -
55 - /**
56 - * Check for the values set in syntax error function. If not set properly
57 - * report an assert.
58 - */
59 - assertThat(errorFlag, is(true));
60 } 59 }
61 60
62 /** 61 /**
63 - * This test case checks in case parsable stack is empty, listener validate 62 + * Checks if there is no exception in case parsable stack is not empty while validating
64 - * function returns true. 63 + * for not empty scenario.
65 */ 64 */
66 @Test 65 @Test
67 - public void listenerValidationEmptyStack() { 66 + public void validateStackIsNotEmptyForNonEmptyStack() {
68 67
69 // Create test walker and assign test error to it. 68 // Create test walker and assign test error to it.
70 TreeWalkListener testWalker = new TreeWalkListener(); 69 TreeWalkListener testWalker = new TreeWalkListener();
71 70
72 - boolean errorFlag = ListenerValidation.preValidation(testWalker, "ErrorTest"); 71 + // Create a temporary node of parsable.
72 + YangRevision tmpNode = new YangRevision();
73 + testWalker.getParsedDataStack().push(tmpNode);
73 74
74 - /** 75 + ListenerValidation.checkStackIsNotEmpty(testWalker, ListenerErrorType.MISSING_HOLDER,
75 - * Check for the values set in syntax error function. If not set properly 76 + ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.EXIT);
76 - * report an assert.
77 - */
78 - assertThat(errorFlag, is(true));
79 } 77 }
80 78
81 /** 79 /**
82 - * This test case checks in case of error doesn't pre-exists and stack is, 80 + * Checks for exception in case parsable stack is not empty while validating
83 - * non empty, listener validate function returns false. 81 + * for empty scenario.
84 */ 82 */
85 @Test 83 @Test
86 - public void listenerValidationNoErrorNotExists() { 84 + public void validateStackIsEmptyForNonEmptyStack() {
85 +
86 + String expectedError = ListenerErrorMessageConstruction
87 + .constructListenerErrorMessage(ListenerErrorType.MISSING_HOLDER, ParsableDataType.YANGBASE_DATA, "",
88 + ListenerErrorLocation.EXIT);
89 +
90 + // Get the exception occurred during parsing.
91 + thrown.expect(ParserException.class);
92 + thrown.expectMessage(expectedError);
87 93
88 // Create test walker and assign test error to it. 94 // Create test walker and assign test error to it.
89 TreeWalkListener testWalker = new TreeWalkListener(); 95 TreeWalkListener testWalker = new TreeWalkListener();
...@@ -92,12 +98,21 @@ public class ListenerValidationTest { ...@@ -92,12 +98,21 @@ public class ListenerValidationTest {
92 YangRevision tmpNode = new YangRevision(); 98 YangRevision tmpNode = new YangRevision();
93 testWalker.getParsedDataStack().push(tmpNode); 99 testWalker.getParsedDataStack().push(tmpNode);
94 100
95 - boolean errorFlag = ListenerValidation.preValidation(testWalker, "ErrorTest"); 101 + ListenerValidation.checkStackIsEmpty(testWalker, ListenerErrorType.MISSING_HOLDER,
102 + ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.EXIT);
103 + }
104 +
105 + /**
106 + * Checks if there is no exception in case parsable stack is empty while validating
107 + * for empty scenario.
108 + */
109 + @Test
110 + public void validateStackIsEmptyForEmptyStack() {
111 +
112 + // Create test walker and assign test error to it.
113 + TreeWalkListener testWalker = new TreeWalkListener();
96 114
97 - /** 115 + ListenerValidation.checkStackIsEmpty(testWalker, ListenerErrorType.MISSING_HOLDER,
98 - * Check for the values set in syntax error function. If not set properly 116 + ParsableDataType.YANGBASE_DATA, "", ListenerErrorLocation.EXIT);
99 - * report an assert.
100 - */
101 - assertThat(errorFlag, is(false));
102 } 117 }
103 } 118 }
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -14,29 +14,26 @@ ...@@ -14,29 +14,26 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.yangutils.parser.impl.parseutils; 17 +package org.onosproject.yangutils.parser.parseutils;
18 18
19 import org.antlr.v4.runtime.ANTLRFileStream; 19 import org.antlr.v4.runtime.ANTLRFileStream;
20 import org.antlr.v4.runtime.ANTLRInputStream; 20 import org.antlr.v4.runtime.ANTLRInputStream;
21 import org.antlr.v4.runtime.CommonTokenStream; 21 import org.antlr.v4.runtime.CommonTokenStream;
22 import org.antlr.v4.runtime.tree.ParseTree; 22 import org.antlr.v4.runtime.tree.ParseTree;
23 -import org.junit.After; 23 +import org.junit.Rule;
24 -import org.junit.Before;
25 import org.junit.Test; 24 import org.junit.Test;
25 +import org.junit.rules.ExpectedException;
26 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangLexer; 26 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangLexer;
27 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; 27 import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
28 import org.onosproject.yangutils.parser.exceptions.ParserException; 28 import org.onosproject.yangutils.parser.exceptions.ParserException;
29 +import org.onosproject.yangutils.parser.impl.CustomExceptionMatcher;
29 import org.onosproject.yangutils.parser.impl.YangUtilsParserManager; 30 import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
30 import org.onosproject.yangutils.parser.impl.parserutils.ParseTreeErrorListener; 31 import org.onosproject.yangutils.parser.impl.parserutils.ParseTreeErrorListener;
31 32
32 import java.io.BufferedWriter; 33 import java.io.BufferedWriter;
33 import java.io.File; 34 import java.io.File;
34 -import java.io.FileWriter;
35 import java.io.IOException; 35 import java.io.IOException;
36 36
37 -import static org.hamcrest.core.Is.is;
38 -import static org.junit.Assert.assertThat;
39 -
40 /** 37 /**
41 * Test case for testing parse tree error listener. 38 * Test case for testing parse tree error listener.
42 */ 39 */
...@@ -46,31 +43,16 @@ public class ParseTreeErrorListenerTest { ...@@ -46,31 +43,16 @@ public class ParseTreeErrorListenerTest {
46 File file; 43 File file;
47 BufferedWriter out; 44 BufferedWriter out;
48 45
49 - @Before 46 + @Rule
50 - public void setUp() throws Exception { 47 + public ExpectedException thrown = ExpectedException.none();
51 - file = new File("demo.yang");
52 - out = new BufferedWriter(new FileWriter(file));
53 - }
54 - @After
55 - public void tearDown() throws Exception {
56 - file.delete();
57 - }
58 48
59 /** 49 /**
60 - * This test case checks whether the error received from parser is correctly 50 + * Checks that no exception is generated for YANG file with valid syntax.
61 - * handled.
62 */ 51 */
63 @Test 52 @Test
64 - public void syntaxErrorValidationTest() throws IOException { 53 + public void checkValidYangFileForNoSyntaxError() throws IOException {
65 -
66 - out.write("module ONOS {\n");
67 - out.write("yang-version 1\n");
68 - out.write("namespace urn:ietf:params:xml:ns:yang:ietf-ospf;\n");
69 - out.write("prefix On;\n");
70 - out.write("}\n");
71 - out.close();
72 54
73 - ANTLRInputStream input = new ANTLRFileStream("demo.yang"); 55 + ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithoutSyntaxError.yang");
74 56
75 // Create a lexer that feeds off of input char stream. 57 // Create a lexer that feeds off of input char stream.
76 GeneratedYangLexer lexer = new GeneratedYangLexer(input); 58 GeneratedYangLexer lexer = new GeneratedYangLexer(input);
...@@ -86,15 +68,34 @@ public class ParseTreeErrorListenerTest { ...@@ -86,15 +68,34 @@ public class ParseTreeErrorListenerTest {
86 parser.addErrorListener(parseTreeErrorListener); 68 parser.addErrorListener(parseTreeErrorListener);
87 // Begin parsing YANG file and generate parse tree. 69 // Begin parsing YANG file and generate parse tree.
88 ParseTree tree = parser.yangfile(); 70 ParseTree tree = parser.yangfile();
71 + }
72 +
73 + /**
74 + * Checks that exception is generated for YANG file with invalid syntax.
75 + */
76 + @Test
77 + public void checkInvalidYangFileForSyntaxError() throws IOException {
78 +
89 // Get the exception occurred during parsing. 79 // Get the exception occurred during parsing.
90 - ParserException parserException = parseTreeErrorListener.getParserException(); 80 + thrown.expect(ParserException.class);
81 + thrown.expect(CustomExceptionMatcher.errorLocation(3, 0));
82 + thrown.expectMessage("no viable alternative at input 'yang-version 1\\nnamespace'");
91 83
92 - /** 84 + ANTLRInputStream input = new ANTLRFileStream("src/test/resources/YangFileWithSyntaxError.yang");
93 - * Check for the values set in syntax error function. If not set properly 85 +
94 - * report an assert. 86 + // Create a lexer that feeds off of input char stream.
95 - */ 87 + GeneratedYangLexer lexer = new GeneratedYangLexer(input);
96 - assertThat(parseTreeErrorListener.isExceptionFlag(), is(true)); 88 + // Create a buffer of tokens pulled from the lexer.
97 - assertThat(parserException.getLineNumber(), is(3)); 89 + CommonTokenStream tokens = new CommonTokenStream(lexer);
98 - assertThat(parserException.getCharPositionInLine(), is(0)); 90 + // Create a parser that feeds off the tokens buffer.
91 + GeneratedYangParser parser = new GeneratedYangParser(tokens);
92 + // Remove console error listener.
93 + parser.removeErrorListeners();
94 + // Create instance of customized error listener.
95 + ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener();
96 + // Add customized error listener to catch errors during parsing.
97 + parser.addErrorListener(parseTreeErrorListener);
98 + // Begin parsing YANG file and generate parse tree.
99 + ParseTree tree = parser.yangfile();
99 } 100 }
100 } 101 }
...\ No newline at end of file ...\ No newline at end of file
......
1 +submodule Test {
2 +yang-version 1;
3 +belongs-to ONOS {
4 +prefix On1;
5 +prefix On2;
6 +}
7 +}
8 +
1 +submodule Test {
2 +yang-version 1;
3 +belongs-to ONOS {
4 +prefix On1;
5 +}
6 +}
1 +submodule Test {
2 +yang-version 1;
3 +belongs-to ONOS {
4 +}
5 +}
6 +
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +organization "IETF SPRING Working Group";
6 +contact "WG List";
7 +contact "Invalid";
8 +}
9 +
1 +module Test {
2 +yang-version 1;
3 +contact "Test";
4 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
5 +prefix On;
6 +organization "IETF SPRING Working Group";
7 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +organization "IETF SPRING Working Group";
6 +contact "WG List: <mailto:spring@ietf.org>
7 +Editor: Stephane Litkowski
8 + <mailto:stephane.litkowski@orange.com>";
9 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +organization "IETF SPRING Working Group";
6 +contact;
7 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +revision-date 2015-02-03;
7 +prefix On1;
8 +}
9 +contact "Test";
10 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +import itut {
10 +prefix On3;
11 +revision-date 2016-02-03;
12 +}
13 +contact "Test";
14 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +contact "Test";
10 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On1;
7 +prefix On2;
8 +revision-date 2015-02-03;
9 +}
10 +contact "Test";
11 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +revision-date 2015-02-03;
7 +}
8 +contact "Test";
9 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On2;
7 +}
8 +contact "Test";
9 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +include itut {
6 +revision-date 2016-02-03;
7 +}
8 +import ietf {
9 +prefix On2;
10 +revision-date 2015-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut {
10 +revision-date 16-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut; {
10 +revision-date 2016-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut {
10 +revision-date 2016-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut {
10 +revision-date 2016-02-03;
11 +}
12 +contact "Test";
13 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut {
10 +}
11 +contact "Test";
12 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut;
10 +contact "Test";
11 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +}
1 +module Test:Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +namespace urn:ietf:params:xml:ns:yang:ietf-segment-routing;
5 +prefix On;
6 +}
1 +module Test {
2 +yang-version 1;
3 +namespace "urn:ietf:params:xml:ns:yang:ietf-ospf";
4 +prefix On;
5 +}
1 +module Test {
2 +yang-version 1;
3 +prefix On;
4 +}
1 +module Test {
2 +yang-version 1;
3 +namespace "urn:ietf:params:xml:ns:"
4 + + "yang:ietf-segment-routing";
5 +prefix On;
6 +}
1 +module Test {
2 +yang-version 1;
3 +namespace "urn:ietf:params:xml :ns:yang:ietf-ospf";
4 +prefix On;
5 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut {
10 +revision-date 2016-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +organization "IETF SPRING Working Group";
17 +organization "ITUT SPRING Working Group";
18 +}
1 +module Test {
2 +yang-version 1;
3 +organization "ONOS";
4 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
5 +prefix On;
6 +import ietf {
7 +prefix On2;
8 +revision-date 2015-02-03;
9 +}
10 +include itut; {
11 +revision-date 2016-02-03;
12 +}
13 +include sdn {
14 +revision-date 2014-02-03;
15 +}
16 +contact "Test";
17 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut {
10 +revision-date 2016-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +organization ;
17 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut {
10 +revision-date 2016-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +organization "IETF SPRING Working Group";
17 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On2;
7 +prefix On3;
8 +revision-date 2015-02-03;
9 +}
10 +include itut {
11 +revision-date 2016-02-03;
12 +}
13 +include sdn {
14 +revision-date 2014-02-03;
15 +}
16 +contact "Test";
17 +organization "ONOS";
18 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix -On;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut {
10 +revision-date 2016-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +organization "ONOS";
17 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix ;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut {
10 +revision-date 2016-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +organization "ONOS";
17 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +import ietf {
5 +prefix On2;
6 +revision-date 2015-02-03;
7 +}
8 +prefix test;
9 +include itut {
10 +revision-date 2016-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +organization "ONOS";
17 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut {
10 +revision-date 2016-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +organization "ONOS";
17 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix test;
5 +import ietf {
6 +prefix On2;
7 +revision-date "2015-02-03";
8 +}
9 +include itut {
10 +revision-date 2016-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +organization "ONOS";
17 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix test;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut {
10 +revision-date "2016-02-03";
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +organization "ONOS";
17 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix test;
5 +import ietf {
6 +prefix On2;
7 +revision-date 15-02-03;
8 +}
9 +include itut {
10 +revision-date 2016-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +organization "ONOS";
17 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix test;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015/02/03;
8 +}
9 +include itut {
10 +revision-date 2016-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +organization "ONOS";
17 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix test;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut {
10 +revision-date 2016/02/03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +organization "ONOS";
17 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix test;
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut {
10 +revision-date 2016-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +organization "ONOS";
17 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix test;
5 +revision 2016-02-03;
6 +include itut {
7 +revision-date 2016-02-03;
8 +}
9 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix test;
5 +contact "Test";
6 +organization "ONOS";
7 +revision;
8 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix test;
5 +contact "Test";
6 +organization "ONOS";
7 +revision 2016-02-03;
8 +}
1 +submodule Test:Test {
2 +belongs-to ONOS {
3 +prefix On1;
4 +}
5 +yang-version 1;
6 +import ietf {
7 +prefix On2;
8 +revision-date 2015-02-03;
9 +}
10 +include itut {
11 +revision-date 2016-02-03;
12 +}
13 +include sdn {
14 +revision-date 2014-02-03;
15 +}
16 +contact "Test";
17 +organization "ONOS";
18 +}
1 +submodule Test {
2 +belongs-to ONOS {
3 +prefix On1;
4 +}
5 +yang-version 1;
6 +import ietf {
7 +prefix On2;
8 +revision-date 2015-02-03;
9 +}
10 +include itut {
11 +revision-date 2016-02-03;
12 +}
13 +include sdn {
14 +revision-date 2014-02-03;
15 +}
16 +contact "Test";
17 +organization "ONOS";
18 +}
1 +submodule Test {
2 +yang-version 1;
3 +belongs-to ONOS {
4 +prefix On1;
5 +}
6 +import ietf {
7 +prefix On2;
8 +revision-date 2015-02-03;
9 +}
10 +include itut {
11 +revision-date 2016-02-03;
12 +}
13 +include sdn {
14 +revision-date 2014-02-03;
15 +}
16 +contact "Test";
17 +organization "ONOS";
18 +}
1 +submodule Test {
2 +belongs-to ONOS {
3 +prefix On1;
4 +}
5 +yang-version 1;
6 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
7 +import ietf {
8 +prefix On2;
9 +revision-date 2015-02-03;
10 +}
11 +include itut {
12 +revision-date 2016-02-03;
13 +}
14 +include sdn {
15 +revision-date 2014-02-03;
16 +}
17 +contact "Test";
18 +organization "ONOS";
19 +}
1 +submodule Test {
2 +belongs-to ONOS {
3 +prefix On1;
4 +}
5 +import ietf {
6 +prefix On2;
7 +revision-date 2015-02-03;
8 +}
9 +include itut {
10 +revision-date 2016-02-03;
11 +}
12 +include sdn {
13 +revision-date 2014-02-03;
14 +}
15 +contact "Test";
16 +organization "ONOS";
17 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +yang-version 1;
6 +}
1 +module Test {
2 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
3 +prefix On;
4 +yang-version ;
5 +}
1 +module Test {
2 +yang-version 2;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +}
1 +module Test {
2 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
3 +prefix On;
4 +}
1 +module Test {
2 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
3 +prefix On;
4 +yang-version 1;
5 +}
1 +module Test {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix On;
5 +}
1 +module Antlrtest {
2 +yang-version 1
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix Ant;
5 +}
6 +
1 +module Antlrtest {
2 +yang-version 1;
3 +namespace urn:ietf:params:xml:ns:yang:ietf-ospf;
4 +prefix Ant;
5 +}
6 +