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 String keyDefinition = "";
661 for (int i = 0; i < ctx.getChildCount(); ++i) {
662 ParseTree childNode = ctx.getChild(i);
663 if (childNode instanceof Ordered_by_stmtContext) {
664 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
665 final boolean userOrdered = parseUserOrdered(orderedBy);
666 builder.setUserOrdered(userOrdered);
667 } else if (childNode instanceof Key_stmtContext) {
668 keyDefinition = stringFromNode(childNode);
669 List<QName> key = createListKey(keyDefinition, namespace, revision, yangModelPrefix);
670 builder.setKeyDefinition(key);
676 public void exitList_stmt(List_stmtContext ctx) {
677 moduleBuilder.exitNode();
678 exitLog("list", removeNodeFromPath());
682 public void enterAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) {
683 final int line = ctx.getStart().getLine();
684 final String anyXmlName = stringFromNode(ctx);
685 enterLog("anyxml", anyXmlName, line);
687 QName anyXmlQName = new QName(namespace, revision, yangModelPrefix, anyXmlName);
688 addNodeToPath(anyXmlQName);
689 SchemaPath path = createActualSchemaPath(actualPath.peek());
691 AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, path);
692 moduleBuilder.enterNode(builder);
694 parseSchemaNodeArgs(ctx, builder);
695 parseConstraints(ctx, builder.getConstraints());
696 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
700 public void exitAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) {
701 moduleBuilder.exitNode();
702 exitLog("anyxml", removeNodeFromPath());
706 public void enterChoice_stmt(YangParser.Choice_stmtContext ctx) {
707 final int line = ctx.getStart().getLine();
708 final String choiceName = stringFromNode(ctx);
709 enterLog("choice", choiceName, line);
711 QName choiceQName = new QName(namespace, revision, yangModelPrefix, choiceName);
712 addNodeToPath(choiceQName);
713 SchemaPath path = createActualSchemaPath(actualPath.peek());
715 ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName, path);
716 moduleBuilder.enterNode(builder);
718 parseSchemaNodeArgs(ctx, builder);
719 parseConstraints(ctx, builder.getConstraints());
720 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
722 // set 'default' case
723 for (int i = 0; i < ctx.getChildCount(); i++) {
724 ParseTree child = ctx.getChild(i);
725 if (child instanceof Default_stmtContext) {
726 String defaultCase = stringFromNode(child);
727 builder.setDefaultCase(defaultCase);
734 public void exitChoice_stmt(YangParser.Choice_stmtContext ctx) {
735 moduleBuilder.exitNode();
736 exitLog("choice", removeNodeFromPath());
740 public void enterCase_stmt(YangParser.Case_stmtContext ctx) {
741 final int line = ctx.getStart().getLine();
742 final String caseName = stringFromNode(ctx);
743 enterLog("case", caseName, line);
745 QName caseQName = new QName(namespace, revision, yangModelPrefix, caseName);
746 addNodeToPath(caseQName);
747 SchemaPath path = createActualSchemaPath(actualPath.peek());
749 ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName, path);
750 moduleBuilder.enterNode(builder);
752 parseSchemaNodeArgs(ctx, builder);
753 parseConstraints(ctx, builder.getConstraints());
757 public void exitCase_stmt(YangParser.Case_stmtContext ctx) {
758 moduleBuilder.exitNode();
759 exitLog("case", removeNodeFromPath());
763 public void enterNotification_stmt(YangParser.Notification_stmtContext ctx) {
764 final int line = ctx.getStart().getLine();
765 final String notificationName = stringFromNode(ctx);
766 enterLog("notification", notificationName, line);
768 QName notificationQName = new QName(namespace, revision, yangModelPrefix, notificationName);
769 addNodeToPath(notificationQName);
770 SchemaPath path = createActualSchemaPath(actualPath.peek());
772 NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName, path);
773 moduleBuilder.enterNode(builder);
775 parseSchemaNodeArgs(ctx, builder);
779 public void exitNotification_stmt(YangParser.Notification_stmtContext ctx) {
780 moduleBuilder.exitNode();
781 exitLog("notification", removeNodeFromPath());
786 public void enterIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
787 final int line = ctx.getStart().getLine();
788 final String nodeParameter = stringFromNode(ctx);
789 enterLog("unknown-node", nodeParameter, line);
792 final String nodeTypeStr = ctx.getChild(0).getText();
793 final String[] splittedElement = nodeTypeStr.split(":");
794 if (splittedElement.length == 1) {
795 nodeType = new QName(namespace, revision, yangModelPrefix, splittedElement[0]);
797 nodeType = new QName(namespace, revision, splittedElement[0], splittedElement[1]);
801 if (nodeParameter != null) {
802 String[] splittedName = nodeParameter.split(":");
803 if (splittedName.length == 2) {
804 qname = new QName(null, null, splittedName[0], splittedName[1]);
806 qname = new QName(namespace, revision, yangModelPrefix, splittedName[0]);
809 qname = new QName(namespace, revision, yangModelPrefix, nodeParameter);
812 addNodeToPath(new QName(namespace, revision, yangModelPrefix, nodeParameter));
813 SchemaPath path = createActualSchemaPath(actualPath.peek());
815 UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(line, qname, path);
816 builder.setNodeType(nodeType);
817 builder.setNodeParameter(nodeParameter);
820 parseSchemaNodeArgs(ctx, builder);
821 moduleBuilder.enterNode(builder);
825 public void exitIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
826 moduleBuilder.exitNode();
827 exitLog("unknown-node", removeNodeFromPath());
831 public void enterRpc_stmt(YangParser.Rpc_stmtContext ctx) {
832 final int line = ctx.getStart().getLine();
833 final String rpcName = stringFromNode(ctx);
834 enterLog("rpc", rpcName, line);
836 QName rpcQName = new QName(namespace, revision, yangModelPrefix, rpcName);
837 addNodeToPath(rpcQName);
838 SchemaPath path = createActualSchemaPath(actualPath.peek());
840 RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName, path);
841 moduleBuilder.enterNode(rpcBuilder);
844 parseSchemaNodeArgs(ctx, rpcBuilder);
848 public void exitRpc_stmt(YangParser.Rpc_stmtContext ctx) {
849 moduleBuilder.exitNode();
850 exitLog("rpc", removeNodeFromPath());
854 public void enterInput_stmt(YangParser.Input_stmtContext ctx) {
855 final int line = ctx.getStart().getLine();
856 final String input = "input";
857 enterLog(input, input, line);
859 QName rpcQName = new QName(namespace, revision, yangModelPrefix, input);
860 addNodeToPath(rpcQName);
861 SchemaPath path = createActualSchemaPath(actualPath.peek());
863 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(line, rpcQName, path);
864 moduleBuilder.enterNode(builder);
866 parseSchemaNodeArgs(ctx, builder);
867 parseConstraints(ctx, builder.getConstraints());
871 public void exitInput_stmt(YangParser.Input_stmtContext ctx) {
872 moduleBuilder.exitNode();
873 exitLog("input", removeNodeFromPath());
877 public void enterOutput_stmt(YangParser.Output_stmtContext ctx) {
878 final int line = ctx.getStart().getLine();
879 final String output = "output";
880 enterLog(output, output, line);
882 QName rpcQName = new QName(namespace, revision, yangModelPrefix, output);
883 addNodeToPath(rpcQName);
884 SchemaPath path = createActualSchemaPath(actualPath.peek());
886 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line);
887 moduleBuilder.enterNode(builder);
889 parseSchemaNodeArgs(ctx, builder);
890 parseConstraints(ctx, builder.getConstraints());
894 public void exitOutput_stmt(YangParser.Output_stmtContext ctx) {
895 moduleBuilder.exitNode();
896 exitLog("output", removeNodeFromPath());
900 public void enterFeature_stmt(YangParser.Feature_stmtContext ctx) {
901 final int line = ctx.getStart().getLine();
902 final String featureName = stringFromNode(ctx);
903 enterLog("feature", featureName, line);
905 QName featureQName = new QName(namespace, revision, yangModelPrefix, featureName);
906 addNodeToPath(featureQName);
907 SchemaPath path = createActualSchemaPath(actualPath.peek());
909 FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName, path);
910 moduleBuilder.enterNode(featureBuilder);
912 parseSchemaNodeArgs(ctx, featureBuilder);
916 public void exitFeature_stmt(YangParser.Feature_stmtContext ctx) {
917 moduleBuilder.exitNode();
918 exitLog("feature", removeNodeFromPath());
922 public void enterDeviation_stmt(YangParser.Deviation_stmtContext ctx) {
923 final int line = ctx.getStart().getLine();
924 final String targetPath = stringFromNode(ctx);
925 enterLog("deviation", targetPath, line);
927 String reference = null;
928 String deviate = null;
929 DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath);
930 moduleBuilder.enterNode(builder);
932 for (int i = 0; i < ctx.getChildCount(); i++) {
933 ParseTree child = ctx.getChild(i);
934 if (child instanceof Reference_stmtContext) {
935 reference = stringFromNode(child);
936 } else if (child instanceof Deviate_not_supported_stmtContext) {
937 deviate = stringFromNode(child);
938 } else if (child instanceof Deviate_add_stmtContext) {
939 deviate = stringFromNode(child);
940 } else if (child instanceof Deviate_replace_stmtContext) {
941 deviate = stringFromNode(child);
942 } else if (child instanceof Deviate_delete_stmtContext) {
943 deviate = stringFromNode(child);
946 builder.setReference(reference);
947 builder.setDeviate(deviate);
951 public void exitDeviation_stmt(YangParser.Deviation_stmtContext ctx) {
952 moduleBuilder.exitNode();
953 exitLog("deviation", "");
957 public void enterIdentity_stmt(YangParser.Identity_stmtContext ctx) {
958 final int line = ctx.getStart().getLine();
959 final String identityName = stringFromNode(ctx);
960 enterLog("identity", identityName, line);
962 final QName identityQName = new QName(namespace, revision, yangModelPrefix, identityName);
963 addNodeToPath(identityQName);
964 SchemaPath path = createActualSchemaPath(actualPath.peek());
966 IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line, path);
967 moduleBuilder.enterNode(builder);
970 parseSchemaNodeArgs(ctx, builder);
972 for (int i = 0; i < ctx.getChildCount(); i++) {
973 ParseTree child = ctx.getChild(i);
974 if (child instanceof Base_stmtContext) {
975 String baseIdentityName = stringFromNode(child);
976 builder.setBaseIdentityName(baseIdentityName);
982 public void exitIdentity_stmt(YangParser.Identity_stmtContext ctx) {
983 moduleBuilder.exitNode();
984 exitLog("identity", removeNodeFromPath());
987 public ModuleBuilder getModuleBuilder() {
988 return moduleBuilder;
991 private void enterLog(String p1, String p2, int line) {
992 LOGGER.trace("entering {} {} ({})", p1, p2, line);
995 private void exitLog(String p1, String p2) {
996 LOGGER.trace("exiting {} {}", p1, p2);
999 private void exitLog(String p1, QName p2) {
1000 LOGGER.trace("exiting {} {}", p1, p2.getLocalName());
1003 private void setLog(String p1, String p2) {
1004 LOGGER.trace("setting {} {}", p1, p2);