2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.impl;
10 import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.*;
16 import org.antlr.v4.runtime.tree.ParseTree;
17 import org.opendaylight.yangtools.antlrv4.code.gen.*;
18 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Argument_stmtContext;
19 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Base_stmtContext;
20 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Contact_stmtContext;
21 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Container_stmtContext;
22 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Default_stmtContext;
23 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Description_stmtContext;
24 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_add_stmtContext;
25 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_delete_stmtContext;
26 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_not_supported_stmtContext;
27 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_replace_stmtContext;
28 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Import_stmtContext;
29 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Key_stmtContext;
30 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Leaf_list_stmtContext;
31 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Leaf_stmtContext;
32 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.List_stmtContext;
33 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Module_header_stmtsContext;
34 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Namespace_stmtContext;
35 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Ordered_by_stmtContext;
36 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Organization_stmtContext;
37 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Prefix_stmtContext;
38 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Presence_stmtContext;
39 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Reference_stmtContext;
40 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_date_stmtContext;
41 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtContext;
42 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtsContext;
43 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Status_stmtContext;
44 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Type_body_stmtsContext;
45 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Units_stmtContext;
46 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.When_stmtContext;
47 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Yang_version_stmtContext;
48 import org.opendaylight.yangtools.yang.common.QName;
49 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
50 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
51 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
52 import org.opendaylight.yangtools.yang.model.util.YangTypesConverter;
53 import org.opendaylight.yangtools.yang.parser.builder.api.*;
54 import org.opendaylight.yangtools.yang.parser.builder.impl.*;
55 import org.opendaylight.yangtools.yang.parser.util.RefineHolder;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 public final class YangParserListenerImpl extends YangParserBaseListener {
60 private static final Logger LOGGER = LoggerFactory.getLogger(YangParserListenerImpl.class);
61 private static final String AUGMENT_STR = "augment";
63 private ModuleBuilder moduleBuilder;
64 private String moduleName;
65 private URI namespace;
66 private String yangModelPrefix;
67 private Date revision = new Date(0L);
69 private final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
70 private final Stack<Stack<QName>> actualPath = new Stack<>();
72 private void addNodeToPath(QName name) {
73 actualPath.peek().push(name);
76 private QName removeNodeFromPath() {
77 return actualPath.peek().pop();
81 public void enterModule_stmt(YangParser.Module_stmtContext ctx) {
82 moduleName = stringFromNode(ctx);
83 LOGGER.debug("entering module " + moduleName);
84 enterLog("module", moduleName, 0);
85 actualPath.push(new Stack<QName>());
87 moduleBuilder = new ModuleBuilder(moduleName);
89 String description = null;
90 String reference = null;
91 for (int i = 0; i < ctx.getChildCount(); i++) {
92 ParseTree child = ctx.getChild(i);
93 if (child instanceof Description_stmtContext) {
94 description = stringFromNode(child);
95 } else if (child instanceof Reference_stmtContext) {
96 reference = stringFromNode(child);
98 if (description != null && reference != null) {
103 moduleBuilder.setDescription(description);
104 moduleBuilder.setReference(reference);
108 public void exitModule_stmt(YangParser.Module_stmtContext ctx) {
109 exitLog("module", "");
113 @Override public void enterSubmodule_stmt(YangParser.Submodule_stmtContext ctx) {
114 moduleName = stringFromNode(ctx);
115 LOGGER.debug("entering submodule " + moduleName);
116 enterLog("submodule", moduleName, 0);
117 actualPath.push(new Stack<QName>());
119 moduleBuilder = new ModuleBuilder(moduleName, true);
121 String description = null;
122 String reference = null;
123 for (int i = 0; i < ctx.getChildCount(); i++) {
124 ParseTree child = ctx.getChild(i);
125 if (child instanceof Description_stmtContext) {
126 description = stringFromNode(child);
127 } else if (child instanceof Reference_stmtContext) {
128 reference = stringFromNode(child);
130 if (description != null && reference != null) {
135 moduleBuilder.setDescription(description);
136 moduleBuilder.setReference(reference);
139 @Override public void exitSubmodule_stmt(YangParser.Submodule_stmtContext ctx) {
140 exitLog("submodule", "");
144 @Override public void enterBelongs_to_stmt(YangParser.Belongs_to_stmtContext ctx) {
145 moduleBuilder.setBelongsTo(stringFromNode(ctx));
149 public void enterModule_header_stmts(Module_header_stmtsContext ctx) {
150 enterLog("module_header", "", ctx.getStart().getLine());
151 String yangVersion = null;
152 for (int i = 0; i < ctx.getChildCount(); ++i) {
153 final ParseTree treeNode = ctx.getChild(i);
154 if (treeNode instanceof Namespace_stmtContext) {
155 final String namespaceStr = stringFromNode(treeNode);
156 namespace = URI.create(namespaceStr);
157 moduleBuilder.setNamespace(namespace);
158 setLog("namespace", namespaceStr);
159 } else if (treeNode instanceof Prefix_stmtContext) {
160 yangModelPrefix = stringFromNode(treeNode);
161 moduleBuilder.setPrefix(yangModelPrefix);
162 setLog("prefix", yangModelPrefix);
163 } else if (treeNode instanceof Yang_version_stmtContext) {
164 yangVersion = stringFromNode(treeNode);
165 setLog("yang-version", yangVersion);
169 if (yangVersion == null) {
172 moduleBuilder.setYangVersion(yangVersion);
176 public void exitModule_header_stmts(Module_header_stmtsContext ctx) {
177 exitLog("module_header", "");
181 public void enterMeta_stmts(YangParser.Meta_stmtsContext ctx) {
182 enterLog("meta_stmt", "", ctx.getStart().getLine());
183 for (int i = 0; i < ctx.getChildCount(); i++) {
184 ParseTree child = ctx.getChild(i);
185 if (child instanceof Organization_stmtContext) {
186 final String organization = stringFromNode(child);
187 moduleBuilder.setOrganization(organization);
188 setLog("organization", organization);
189 } else if (child instanceof Contact_stmtContext) {
190 final String contact = stringFromNode(child);
191 moduleBuilder.setContact(contact);
192 setLog("contact", contact);
193 } else if (child instanceof Description_stmtContext) {
194 final String description = stringFromNode(child);
195 moduleBuilder.setDescription(description);
196 setLog("description", description);
197 } else if (child instanceof Reference_stmtContext) {
198 final String reference = stringFromNode(child);
199 moduleBuilder.setReference(reference);
200 setLog("reference", reference);
206 public void exitMeta_stmts(YangParser.Meta_stmtsContext ctx) {
207 exitLog("meta_stmt", "");
211 public void enterRevision_stmts(Revision_stmtsContext ctx) {
212 enterLog("revisions", "", ctx.getStart().getLine());
213 for (int i = 0; i < ctx.getChildCount(); ++i) {
214 final ParseTree treeNode = ctx.getChild(i);
215 if (treeNode instanceof Revision_stmtContext) {
216 updateRevisionForRevisionStatement(treeNode);
222 public void exitRevision_stmts(Revision_stmtsContext ctx) {
223 exitLog("revisions", "");
226 private void updateRevisionForRevisionStatement(final ParseTree treeNode) {
227 final String revisionDateStr = stringFromNode(treeNode);
229 final Date revisionDate = SIMPLE_DATE_FORMAT.parse(revisionDateStr);
230 if ((revisionDate != null) && (this.revision.compareTo(revisionDate) < 0)) {
231 this.revision = revisionDate;
232 moduleBuilder.setRevision(this.revision);
233 setLog("revision", this.revision.toString());
234 for (int i = 0; i < treeNode.getChildCount(); ++i) {
235 ParseTree child = treeNode.getChild(i);
236 if (child instanceof Reference_stmtContext) {
237 moduleBuilder.setReference(stringFromNode(child));
241 } catch (ParseException e) {
242 final String message = "Failed to parse revision string: " + revisionDateStr;
243 LOGGER.warn(message);
248 public void enterImport_stmt(Import_stmtContext ctx) {
249 final int line = ctx.getStart().getLine();
250 final String importName = stringFromNode(ctx);
251 enterLog("import", importName, line);
253 String importPrefix = null;
254 Date importRevision = null;
256 for (int i = 0; i < ctx.getChildCount(); ++i) {
257 final ParseTree treeNode = ctx.getChild(i);
258 if (treeNode instanceof Prefix_stmtContext) {
259 importPrefix = stringFromNode(treeNode);
261 if (treeNode instanceof Revision_date_stmtContext) {
262 String importRevisionStr = stringFromNode(treeNode);
264 importRevision = SIMPLE_DATE_FORMAT.parse(importRevisionStr);
265 } catch (ParseException e) {
266 LOGGER.warn("Failed to parse import revision-date at line " + line + ": " + importRevisionStr);
270 moduleBuilder.addModuleImport(importName, importRevision, importPrefix);
271 setLog("import", "(" + importName + "; " + importRevision + "; " + importPrefix + ")");
275 public void exitImport_stmt(Import_stmtContext ctx) {
276 exitLog("import", "");
280 public void enterAugment_stmt(YangParser.Augment_stmtContext ctx) {
281 final int line = ctx.getStart().getLine();
282 final String augmentPath = stringFromNode(ctx);
283 enterLog(AUGMENT_STR, augmentPath, line);
284 actualPath.push(new Stack<QName>());
286 AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath);
288 for (int i = 0; i < ctx.getChildCount(); i++) {
289 ParseTree child = ctx.getChild(i);
290 if (child instanceof Description_stmtContext) {
291 builder.setDescription(stringFromNode(child));
292 } else if (child instanceof Reference_stmtContext) {
293 builder.setReference(stringFromNode(child));
294 } else if (child instanceof Status_stmtContext) {
295 builder.setStatus(parseStatus((Status_stmtContext) child));
296 } else if (child instanceof When_stmtContext) {
297 builder.addWhenCondition(stringFromNode(child));
301 moduleBuilder.enterNode(builder);
305 public void exitAugment_stmt(YangParser.Augment_stmtContext ctx) {
306 moduleBuilder.exitNode();
307 exitLog(AUGMENT_STR, "");
312 public void enterExtension_stmt(YangParser.Extension_stmtContext ctx) {
313 final int line = ctx.getStart().getLine();
314 final String extName = stringFromNode(ctx);
315 enterLog("extension", extName, line);
316 QName qname = new QName(namespace, revision, yangModelPrefix, extName);
317 addNodeToPath(qname);
318 SchemaPath path = createActualSchemaPath(actualPath.peek());
320 ExtensionBuilder builder = moduleBuilder.addExtension(qname, line, path);
321 parseSchemaNodeArgs(ctx, builder);
323 String argument = null;
325 for (int i = 0; i < ctx.getChildCount(); i++) {
326 ParseTree child = ctx.getChild(i);
327 if (child instanceof Argument_stmtContext) {
328 argument = stringFromNode(child);
329 yin = parseYinValue((Argument_stmtContext) child);
333 builder.setArgument(argument);
334 builder.setYinElement(yin);
336 moduleBuilder.enterNode(builder);
340 public void exitExtension_stmt(YangParser.Extension_stmtContext ctx) {
341 moduleBuilder.exitNode();
342 exitLog("extension", removeNodeFromPath());
346 public void enterTypedef_stmt(YangParser.Typedef_stmtContext ctx) {
347 final int line = ctx.getStart().getLine();
348 final String typedefName = stringFromNode(ctx);
349 enterLog("typedef", typedefName, line);
350 QName typedefQName = new QName(namespace, revision, yangModelPrefix, typedefName);
351 addNodeToPath(typedefQName);
352 SchemaPath path = createActualSchemaPath(actualPath.peek());
354 TypeDefinitionBuilder builder = moduleBuilder.addTypedef(line, typedefQName, path);
355 parseSchemaNodeArgs(ctx, builder);
356 builder.setUnits(parseUnits(ctx));
357 builder.setDefaultValue(parseDefault(ctx));
359 moduleBuilder.enterNode(builder);
363 public void exitTypedef_stmt(YangParser.Typedef_stmtContext ctx) {
364 moduleBuilder.exitNode();
365 exitLog("typedef", removeNodeFromPath());
369 public void enterType_stmt(YangParser.Type_stmtContext ctx) {
370 final int line = ctx.getStart().getLine();
371 final String typeName = stringFromNode(ctx);
372 enterLog("type", typeName, line);
374 final QName typeQName = parseQName(typeName);
376 TypeDefinition<?> type = null;
377 Type_body_stmtsContext typeBody = null;
378 for (int i = 0; i < ctx.getChildCount(); i++) {
379 if (ctx.getChild(i) instanceof Type_body_stmtsContext) {
380 typeBody = (Type_body_stmtsContext) ctx.getChild(i);
385 // if this is base yang type...
386 if (YangTypesConverter.isBaseYangType(typeName)) {
387 if (typeBody == null) {
388 // check for types which must have body
389 checkMissingBody(typeName, moduleName, line);
390 // if there are no constraints, just grab default base yang type
391 type = YangTypesConverter.javaTypeForBaseYangType(typeName);
392 addNodeToPath(type.getQName());
393 moduleBuilder.setType(type);
398 qname = BaseTypes.constructQName("union");
399 addNodeToPath(qname);
400 UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, namespace, revision);
401 Builder parent = moduleBuilder.getActualNode();
402 unionBuilder.setParent(parent);
403 moduleBuilder.enterNode(unionBuilder);
406 qname = BaseTypes.constructQName("identityref");
407 addNodeToPath(qname);
408 SchemaPath path = createActualSchemaPath(actualPath.peek());
409 moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody));
412 type = parseTypeWithBody(typeName, typeBody, actualPath.peek(), namespace, revision,
413 yangModelPrefix, moduleBuilder.getActualNode());
414 moduleBuilder.setType(type);
415 addNodeToPath(type.getQName());
419 type = parseUnknownTypeWithBody(typeQName, typeBody, actualPath.peek(), namespace, revision,
420 yangModelPrefix, moduleBuilder.getActualNode());
421 // add parent node of this type statement to dirty nodes
422 moduleBuilder.markActualNodeDirty();
423 moduleBuilder.setType(type);
424 addNodeToPath(type.getQName());
429 private QName parseQName(String typeName) {
431 if (typeName.contains(":")) {
432 String[] splittedName = typeName.split(":");
433 String prefix = splittedName[0];
434 String name = splittedName[1];
435 if (prefix.equals(yangModelPrefix)) {
436 typeQName = new QName(namespace, revision, prefix, name);
438 typeQName = new QName(null, null, prefix, name);
441 typeQName = new QName(namespace, revision, yangModelPrefix, typeName);
447 public void exitType_stmt(YangParser.Type_stmtContext ctx) {
448 final String typeName = stringFromNode(ctx);
449 if ("union".equals(typeName)) {
450 moduleBuilder.exitNode();
452 exitLog("type", removeNodeFromPath());
456 public void enterGrouping_stmt(YangParser.Grouping_stmtContext ctx) {
457 final int line = ctx.getStart().getLine();
458 final String groupName = stringFromNode(ctx);
459 enterLog("grouping", groupName, line);
460 QName groupQName = new QName(namespace, revision, yangModelPrefix, groupName);
461 addNodeToPath(groupQName);
462 SchemaPath path = createActualSchemaPath(actualPath.peek());
464 GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName, path);
465 parseSchemaNodeArgs(ctx, builder);
467 moduleBuilder.enterNode(builder);
471 public void exitGrouping_stmt(YangParser.Grouping_stmtContext ctx) {
472 moduleBuilder.exitNode();
473 exitLog("grouping", removeNodeFromPath());
477 public void enterContainer_stmt(Container_stmtContext ctx) {
478 final int line = ctx.getStart().getLine();
479 final String containerName = stringFromNode(ctx);
480 enterLog("container", containerName, line);
482 QName containerQName = new QName(namespace, revision, yangModelPrefix, containerName);
483 addNodeToPath(containerQName);
484 SchemaPath path = createActualSchemaPath(actualPath.peek());
486 ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(line, containerQName, path);
487 parseSchemaNodeArgs(ctx, builder);
488 parseConstraints(ctx, builder.getConstraints());
489 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
491 for (int i = 0; i < ctx.getChildCount(); ++i) {
492 final ParseTree childNode = ctx.getChild(i);
493 if (childNode instanceof Presence_stmtContext) {
494 builder.setPresence(true);
499 moduleBuilder.enterNode(builder);
503 public void exitContainer_stmt(Container_stmtContext ctx) {
504 moduleBuilder.exitNode();
505 exitLog("container", removeNodeFromPath());
509 public void enterLeaf_stmt(Leaf_stmtContext ctx) {
510 final int line = ctx.getStart().getLine();
511 final String leafName = stringFromNode(ctx);
512 enterLog("leaf", leafName, line);
514 QName leafQName = new QName(namespace, revision, yangModelPrefix, leafName);
515 addNodeToPath(leafQName);
516 SchemaPath path = createActualSchemaPath(actualPath.peek());
518 LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, path);
519 parseSchemaNodeArgs(ctx, builder);
520 parseConstraints(ctx, builder.getConstraints());
521 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
523 String defaultStr = null;
524 String unitsStr = null;
525 for (int i = 0; i < ctx.getChildCount(); i++) {
526 ParseTree child = ctx.getChild(i);
527 if (child instanceof Default_stmtContext) {
528 defaultStr = stringFromNode(child);
529 } else if (child instanceof Units_stmtContext) {
530 unitsStr = stringFromNode(child);
533 builder.setDefaultStr(defaultStr);
534 builder.setUnits(unitsStr);
536 moduleBuilder.enterNode(builder);
540 public void exitLeaf_stmt(YangParser.Leaf_stmtContext ctx) {
541 moduleBuilder.exitNode();
542 exitLog("leaf", removeNodeFromPath());
546 public void enterUses_stmt(YangParser.Uses_stmtContext ctx) {
547 final int line = ctx.getStart().getLine();
548 final String groupingPathStr = stringFromNode(ctx);
549 enterLog("uses", groupingPathStr, line);
551 UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPathStr);
553 moduleBuilder.enterNode(builder);
557 public void exitUses_stmt(YangParser.Uses_stmtContext ctx) {
558 moduleBuilder.exitNode();
563 public void enterUses_augment_stmt(YangParser.Uses_augment_stmtContext ctx) {
564 actualPath.push(new Stack<QName>());
565 final int line = ctx.getStart().getLine();
566 final String augmentPath = stringFromNode(ctx);
567 enterLog(AUGMENT_STR, augmentPath, line);
569 AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath);
571 for (int i = 0; i < ctx.getChildCount(); i++) {
572 ParseTree child = ctx.getChild(i);
573 if (child instanceof Description_stmtContext) {
574 builder.setDescription(stringFromNode(child));
575 } else if (child instanceof Reference_stmtContext) {
576 builder.setReference(stringFromNode(child));
577 } else if (child instanceof Status_stmtContext) {
578 builder.setStatus(parseStatus((Status_stmtContext) child));
579 } else if (child instanceof When_stmtContext) {
580 builder.addWhenCondition(stringFromNode(child));
584 moduleBuilder.enterNode(builder);
588 public void exitUses_augment_stmt(YangParser.Uses_augment_stmtContext ctx) {
589 moduleBuilder.exitNode();
590 exitLog(AUGMENT_STR, "");
595 public void enterRefine_stmt(YangParser.Refine_stmtContext ctx) {
596 final String refineString = stringFromNode(ctx);
597 enterLog("refine", refineString, ctx.getStart().getLine());
599 RefineHolder refine = parseRefine(ctx, moduleName);
600 moduleBuilder.addRefine(refine);
601 moduleBuilder.enterNode(refine);
605 public void exitRefine_stmt(YangParser.Refine_stmtContext ctx) {
606 moduleBuilder.exitNode();
607 exitLog("refine", "");
611 public void enterLeaf_list_stmt(Leaf_list_stmtContext ctx) {
612 final int line = ctx.getStart().getLine();
613 final String leafListName = stringFromNode(ctx);
614 enterLog("leaf-list", leafListName, line);
615 QName leafListQName = new QName(namespace, revision, yangModelPrefix, leafListName);
616 addNodeToPath(leafListQName);
617 SchemaPath path = createActualSchemaPath(actualPath.peek());
619 LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, path);
620 moduleBuilder.enterNode(builder);
622 parseSchemaNodeArgs(ctx, builder);
623 parseConstraints(ctx, builder.getConstraints());
624 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, ctx.getStart().getLine()));
626 for (int i = 0; i < ctx.getChildCount(); ++i) {
627 final ParseTree childNode = ctx.getChild(i);
628 if (childNode instanceof Ordered_by_stmtContext) {
629 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
630 final boolean userOrdered = parseUserOrdered(orderedBy);
631 builder.setUserOrdered(userOrdered);
638 public void exitLeaf_list_stmt(YangParser.Leaf_list_stmtContext ctx) {
639 moduleBuilder.exitNode();
640 exitLog("leaf-list", removeNodeFromPath());
644 public void enterList_stmt(List_stmtContext ctx) {
645 final int line = ctx.getStart().getLine();
646 final String listName = stringFromNode(ctx);
647 enterLog("list", listName, line);
649 QName listQName = new QName(namespace, revision, yangModelPrefix, listName);
650 addNodeToPath(listQName);
651 SchemaPath path = createActualSchemaPath(actualPath.peek());
653 ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, path);
654 moduleBuilder.enterNode(builder);
656 parseSchemaNodeArgs(ctx, builder);
657 parseConstraints(ctx, builder.getConstraints());
658 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
660 for (int i = 0; i < ctx.getChildCount(); ++i) {
661 ParseTree childNode = ctx.getChild(i);
662 if (childNode instanceof Ordered_by_stmtContext) {
663 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
664 final boolean userOrdered = parseUserOrdered(orderedBy);
665 builder.setUserOrdered(userOrdered);
666 } else if (childNode instanceof Key_stmtContext) {
667 List<String> key = createListKey((Key_stmtContext) childNode);
668 builder.setKeys(key);
674 public void exitList_stmt(List_stmtContext ctx) {
675 moduleBuilder.exitNode();
676 exitLog("list", removeNodeFromPath());
680 public void enterAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) {
681 final int line = ctx.getStart().getLine();
682 final String anyXmlName = stringFromNode(ctx);
683 enterLog("anyxml", anyXmlName, line);
685 QName anyXmlQName = new QName(namespace, revision, yangModelPrefix, anyXmlName);
686 addNodeToPath(anyXmlQName);
687 SchemaPath path = createActualSchemaPath(actualPath.peek());
689 AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, path);
690 moduleBuilder.enterNode(builder);
692 parseSchemaNodeArgs(ctx, builder);
693 parseConstraints(ctx, builder.getConstraints());
694 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
698 public void exitAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) {
699 moduleBuilder.exitNode();
700 exitLog("anyxml", removeNodeFromPath());
704 public void enterChoice_stmt(YangParser.Choice_stmtContext ctx) {
705 final int line = ctx.getStart().getLine();
706 final String choiceName = stringFromNode(ctx);
707 enterLog("choice", choiceName, line);
709 QName choiceQName = new QName(namespace, revision, yangModelPrefix, choiceName);
710 addNodeToPath(choiceQName);
711 SchemaPath path = createActualSchemaPath(actualPath.peek());
713 ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName, path);
714 moduleBuilder.enterNode(builder);
716 parseSchemaNodeArgs(ctx, builder);
717 parseConstraints(ctx, builder.getConstraints());
718 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
720 // set 'default' case
721 for (int i = 0; i < ctx.getChildCount(); i++) {
722 ParseTree child = ctx.getChild(i);
723 if (child instanceof Default_stmtContext) {
724 String defaultCase = stringFromNode(child);
725 builder.setDefaultCase(defaultCase);
732 public void exitChoice_stmt(YangParser.Choice_stmtContext ctx) {
733 moduleBuilder.exitNode();
734 exitLog("choice", removeNodeFromPath());
738 public void enterCase_stmt(YangParser.Case_stmtContext ctx) {
739 final int line = ctx.getStart().getLine();
740 final String caseName = stringFromNode(ctx);
741 enterLog("case", caseName, line);
743 QName caseQName = new QName(namespace, revision, yangModelPrefix, caseName);
744 addNodeToPath(caseQName);
745 SchemaPath path = createActualSchemaPath(actualPath.peek());
747 ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName, path);
748 moduleBuilder.enterNode(builder);
750 parseSchemaNodeArgs(ctx, builder);
751 parseConstraints(ctx, builder.getConstraints());
755 public void exitCase_stmt(YangParser.Case_stmtContext ctx) {
756 moduleBuilder.exitNode();
757 exitLog("case", removeNodeFromPath());
761 public void enterNotification_stmt(YangParser.Notification_stmtContext ctx) {
762 final int line = ctx.getStart().getLine();
763 final String notificationName = stringFromNode(ctx);
764 enterLog("notification", notificationName, line);
766 QName notificationQName = new QName(namespace, revision, yangModelPrefix, notificationName);
767 addNodeToPath(notificationQName);
768 SchemaPath path = createActualSchemaPath(actualPath.peek());
770 NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName, path);
771 moduleBuilder.enterNode(builder);
773 parseSchemaNodeArgs(ctx, builder);
777 public void exitNotification_stmt(YangParser.Notification_stmtContext ctx) {
778 moduleBuilder.exitNode();
779 exitLog("notification", removeNodeFromPath());
784 public void enterIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
785 final int line = ctx.getStart().getLine();
786 final String nodeParameter = stringFromNode(ctx);
787 enterLog("unknown-node", nodeParameter, line);
790 final String nodeTypeStr = ctx.getChild(0).getText();
791 final String[] splittedElement = nodeTypeStr.split(":");
792 if (splittedElement.length == 1) {
793 nodeType = new QName(namespace, revision, yangModelPrefix, splittedElement[0]);
795 nodeType = new QName(namespace, revision, splittedElement[0], splittedElement[1]);
799 if (nodeParameter != null) {
800 String[] splittedName = nodeParameter.split(":");
801 if (splittedName.length == 2) {
802 qname = new QName(null, null, splittedName[0], splittedName[1]);
804 qname = new QName(namespace, revision, yangModelPrefix, splittedName[0]);
807 qname = new QName(namespace, revision, yangModelPrefix, nodeParameter);
810 addNodeToPath(new QName(namespace, revision, yangModelPrefix, nodeParameter));
811 SchemaPath path = createActualSchemaPath(actualPath.peek());
813 UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(line, qname, path);
814 builder.setNodeType(nodeType);
815 builder.setNodeParameter(nodeParameter);
818 parseSchemaNodeArgs(ctx, builder);
819 moduleBuilder.enterNode(builder);
823 public void exitIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
824 moduleBuilder.exitNode();
825 exitLog("unknown-node", removeNodeFromPath());
829 public void enterRpc_stmt(YangParser.Rpc_stmtContext ctx) {
830 final int line = ctx.getStart().getLine();
831 final String rpcName = stringFromNode(ctx);
832 enterLog("rpc", rpcName, line);
834 QName rpcQName = new QName(namespace, revision, yangModelPrefix, rpcName);
835 addNodeToPath(rpcQName);
836 SchemaPath path = createActualSchemaPath(actualPath.peek());
838 RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName, path);
839 moduleBuilder.enterNode(rpcBuilder);
842 parseSchemaNodeArgs(ctx, rpcBuilder);
846 public void exitRpc_stmt(YangParser.Rpc_stmtContext ctx) {
847 moduleBuilder.exitNode();
848 exitLog("rpc", removeNodeFromPath());
852 public void enterInput_stmt(YangParser.Input_stmtContext ctx) {
853 final int line = ctx.getStart().getLine();
854 final String input = "input";
855 enterLog(input, input, line);
857 QName rpcQName = new QName(namespace, revision, yangModelPrefix, input);
858 addNodeToPath(rpcQName);
859 SchemaPath path = createActualSchemaPath(actualPath.peek());
861 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(line, rpcQName, path);
862 moduleBuilder.enterNode(builder);
864 parseSchemaNodeArgs(ctx, builder);
865 parseConstraints(ctx, builder.getConstraints());
869 public void exitInput_stmt(YangParser.Input_stmtContext ctx) {
870 moduleBuilder.exitNode();
871 exitLog("input", removeNodeFromPath());
875 public void enterOutput_stmt(YangParser.Output_stmtContext ctx) {
876 final int line = ctx.getStart().getLine();
877 final String output = "output";
878 enterLog(output, output, line);
880 QName rpcQName = new QName(namespace, revision, yangModelPrefix, output);
881 addNodeToPath(rpcQName);
882 SchemaPath path = createActualSchemaPath(actualPath.peek());
884 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line);
885 moduleBuilder.enterNode(builder);
887 parseSchemaNodeArgs(ctx, builder);
888 parseConstraints(ctx, builder.getConstraints());
892 public void exitOutput_stmt(YangParser.Output_stmtContext ctx) {
893 moduleBuilder.exitNode();
894 exitLog("output", removeNodeFromPath());
898 public void enterFeature_stmt(YangParser.Feature_stmtContext ctx) {
899 final int line = ctx.getStart().getLine();
900 final String featureName = stringFromNode(ctx);
901 enterLog("feature", featureName, line);
903 QName featureQName = new QName(namespace, revision, yangModelPrefix, featureName);
904 addNodeToPath(featureQName);
905 SchemaPath path = createActualSchemaPath(actualPath.peek());
907 FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName, path);
908 moduleBuilder.enterNode(featureBuilder);
910 parseSchemaNodeArgs(ctx, featureBuilder);
914 public void exitFeature_stmt(YangParser.Feature_stmtContext ctx) {
915 moduleBuilder.exitNode();
916 exitLog("feature", removeNodeFromPath());
920 public void enterDeviation_stmt(YangParser.Deviation_stmtContext ctx) {
921 final int line = ctx.getStart().getLine();
922 final String targetPath = stringFromNode(ctx);
923 enterLog("deviation", targetPath, line);
925 String reference = null;
926 String deviate = null;
927 DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath);
928 moduleBuilder.enterNode(builder);
930 for (int i = 0; i < ctx.getChildCount(); i++) {
931 ParseTree child = ctx.getChild(i);
932 if (child instanceof Reference_stmtContext) {
933 reference = stringFromNode(child);
934 } else if (child instanceof Deviate_not_supported_stmtContext) {
935 deviate = stringFromNode(child);
936 } else if (child instanceof Deviate_add_stmtContext) {
937 deviate = stringFromNode(child);
938 } else if (child instanceof Deviate_replace_stmtContext) {
939 deviate = stringFromNode(child);
940 } else if (child instanceof Deviate_delete_stmtContext) {
941 deviate = stringFromNode(child);
944 builder.setReference(reference);
945 builder.setDeviate(deviate);
949 public void exitDeviation_stmt(YangParser.Deviation_stmtContext ctx) {
950 moduleBuilder.exitNode();
951 exitLog("deviation", "");
955 public void enterIdentity_stmt(YangParser.Identity_stmtContext ctx) {
956 final int line = ctx.getStart().getLine();
957 final String identityName = stringFromNode(ctx);
958 enterLog("identity", identityName, line);
960 final QName identityQName = new QName(namespace, revision, yangModelPrefix, identityName);
961 addNodeToPath(identityQName);
962 SchemaPath path = createActualSchemaPath(actualPath.peek());
964 IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line, path);
965 moduleBuilder.enterNode(builder);
968 parseSchemaNodeArgs(ctx, builder);
970 for (int i = 0; i < ctx.getChildCount(); i++) {
971 ParseTree child = ctx.getChild(i);
972 if (child instanceof Base_stmtContext) {
973 String baseIdentityName = stringFromNode(child);
974 builder.setBaseIdentityName(baseIdentityName);
980 public void exitIdentity_stmt(YangParser.Identity_stmtContext ctx) {
981 moduleBuilder.exitNode();
982 exitLog("identity", removeNodeFromPath());
985 public ModuleBuilder getModuleBuilder() {
986 return moduleBuilder;
989 private void enterLog(String p1, String p2, int line) {
990 LOGGER.trace("entering {} {} ({})", p1, p2, line);
993 private void exitLog(String p1, String p2) {
994 LOGGER.trace("exiting {} {}", p1, p2);
997 private void exitLog(String p1, QName p2) {
998 LOGGER.trace("exiting {} {}", p1, p2.getLocalName());
1001 private void setLog(String p1, String p2) {
1002 LOGGER.trace("setting {} {}", p1, p2);