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.*;
13 import java.text.DateFormat;
14 import java.text.ParseException;
15 import java.text.SimpleDateFormat;
16 import java.util.Date;
17 import java.util.List;
18 import java.util.Stack;
20 import org.antlr.v4.runtime.tree.ParseTree;
21 import org.opendaylight.yangtools.antlrv4.code.gen.*;
22 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Argument_stmtContext;
23 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Base_stmtContext;
24 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Contact_stmtContext;
25 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Container_stmtContext;
26 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Default_stmtContext;
27 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Description_stmtContext;
28 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_add_stmtContext;
29 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_delete_stmtContext;
30 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_not_supported_stmtContext;
31 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_replace_stmtContext;
32 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Import_stmtContext;
33 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Key_stmtContext;
34 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Leaf_list_stmtContext;
35 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Leaf_stmtContext;
36 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.List_stmtContext;
37 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Module_header_stmtsContext;
38 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Namespace_stmtContext;
39 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Ordered_by_stmtContext;
40 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Organization_stmtContext;
41 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Prefix_stmtContext;
42 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Presence_stmtContext;
43 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Reference_stmtContext;
44 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_date_stmtContext;
45 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtContext;
46 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtsContext;
47 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Status_stmtContext;
48 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Type_body_stmtsContext;
49 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Units_stmtContext;
50 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.When_stmtContext;
51 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Yang_version_stmtContext;
52 import org.opendaylight.yangtools.yang.common.QName;
53 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
54 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
55 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
56 import org.opendaylight.yangtools.yang.model.util.YangTypesConverter;
57 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
58 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
59 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
60 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
61 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
62 import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder;
63 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
64 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
65 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
66 import org.opendaylight.yangtools.yang.parser.builder.impl.DeviationBuilder;
67 import org.opendaylight.yangtools.yang.parser.builder.impl.ExtensionBuilder;
68 import org.opendaylight.yangtools.yang.parser.builder.impl.FeatureBuilder;
69 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
70 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
71 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
72 import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder;
73 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
74 import org.opendaylight.yangtools.yang.parser.builder.impl.NotificationBuilder;
75 import org.opendaylight.yangtools.yang.parser.builder.impl.RpcDefinitionBuilder;
76 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
77 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
78 import org.opendaylight.yangtools.yang.parser.util.RefineHolder;
79 import org.slf4j.Logger;
80 import org.slf4j.LoggerFactory;
82 public final class YangParserListenerImpl extends YangParserBaseListener {
83 private static final Logger logger = LoggerFactory.getLogger(YangParserListenerImpl.class);
85 private ModuleBuilder moduleBuilder;
86 private String moduleName;
87 private URI namespace;
88 private String yangModelPrefix;
89 private Date revision = new Date(0L);
91 public final static DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
92 private final Stack<Stack<QName>> actualPath = new Stack<>();
94 private void addNodeToPath(QName name) {
95 actualPath.peek().push(name);
98 private QName removeNodeFromPath() {
99 return actualPath.peek().pop();
103 public void enterModule_stmt(YangParser.Module_stmtContext ctx) {
104 moduleName = stringFromNode(ctx);
105 logger.debug("entering module " + moduleName);
106 enterLog("module", moduleName, 0);
107 actualPath.push(new Stack<QName>());
109 moduleBuilder = new ModuleBuilder(moduleName);
111 String description = null;
112 String reference = null;
113 for (int i = 0; i < ctx.getChildCount(); i++) {
114 ParseTree child = ctx.getChild(i);
115 if (child instanceof Description_stmtContext) {
116 description = stringFromNode(child);
117 } else if (child instanceof Reference_stmtContext) {
118 reference = stringFromNode(child);
120 if (description != null && reference != null) {
125 moduleBuilder.setDescription(description);
126 moduleBuilder.setReference(reference);
130 public void exitModule_stmt(YangParser.Module_stmtContext ctx) {
131 exitLog("module", "");
136 public void enterModule_header_stmts(Module_header_stmtsContext ctx) {
137 enterLog("module_header", "", ctx.getStart().getLine());
138 String yangVersion = null;
139 for (int i = 0; i < ctx.getChildCount(); ++i) {
140 final ParseTree treeNode = ctx.getChild(i);
141 if (treeNode instanceof Namespace_stmtContext) {
142 final String namespaceStr = stringFromNode(treeNode);
143 namespace = URI.create(namespaceStr);
144 moduleBuilder.setNamespace(namespace);
145 setLog("namespace", namespaceStr);
146 } else if (treeNode instanceof Prefix_stmtContext) {
147 yangModelPrefix = stringFromNode(treeNode);
148 moduleBuilder.setPrefix(yangModelPrefix);
149 setLog("prefix", yangModelPrefix);
150 } else if (treeNode instanceof Yang_version_stmtContext) {
151 yangVersion = stringFromNode(treeNode);
152 setLog("yang-version", yangVersion);
156 if (yangVersion == null) {
159 moduleBuilder.setYangVersion(yangVersion);
163 public void exitModule_header_stmts(Module_header_stmtsContext ctx) {
164 exitLog("module_header", "");
168 public void enterMeta_stmts(YangParser.Meta_stmtsContext ctx) {
169 enterLog("meta_stmt", "", ctx.getStart().getLine());
170 for (int i = 0; i < ctx.getChildCount(); i++) {
171 ParseTree child = ctx.getChild(i);
172 if (child instanceof Organization_stmtContext) {
173 final String organization = stringFromNode(child);
174 moduleBuilder.setOrganization(organization);
175 setLog("organization", organization);
176 } else if (child instanceof Contact_stmtContext) {
177 final String contact = stringFromNode(child);
178 moduleBuilder.setContact(contact);
179 setLog("contact", contact);
180 } else if (child instanceof Description_stmtContext) {
181 final String description = stringFromNode(child);
182 moduleBuilder.setDescription(description);
183 setLog("description", description);
184 } else if (child instanceof Reference_stmtContext) {
185 final String reference = stringFromNode(child);
186 moduleBuilder.setReference(reference);
187 setLog("reference", reference);
193 public void exitMeta_stmts(YangParser.Meta_stmtsContext ctx) {
194 exitLog("meta_stmt", "");
198 public void enterRevision_stmts(Revision_stmtsContext ctx) {
199 enterLog("revisions", "", ctx.getStart().getLine());
200 for (int i = 0; i < ctx.getChildCount(); ++i) {
201 final ParseTree treeNode = ctx.getChild(i);
202 if (treeNode instanceof Revision_stmtContext) {
203 updateRevisionForRevisionStatement(treeNode);
209 public void exitRevision_stmts(Revision_stmtsContext ctx) {
210 exitLog("revisions", "");
213 private void updateRevisionForRevisionStatement(final ParseTree treeNode) {
214 final String revisionDateStr = stringFromNode(treeNode);
216 final Date revision = simpleDateFormat.parse(revisionDateStr);
217 if ((revision != null) && (this.revision.compareTo(revision) < 0)) {
218 this.revision = revision;
219 moduleBuilder.setRevision(this.revision);
220 setLog("revision", this.revision.toString());
221 for (int i = 0; i < treeNode.getChildCount(); ++i) {
222 ParseTree child = treeNode.getChild(i);
223 if (child instanceof Reference_stmtContext) {
224 moduleBuilder.setReference(stringFromNode(child));
228 } catch (ParseException e) {
229 final String message = "Failed to parse revision string: " + revisionDateStr;
230 logger.warn(message);
235 public void enterImport_stmt(Import_stmtContext ctx) {
236 final int line = ctx.getStart().getLine();
237 final String importName = stringFromNode(ctx);
238 enterLog("import", importName, line);
240 String importPrefix = null;
241 Date importRevision = null;
243 for (int i = 0; i < ctx.getChildCount(); ++i) {
244 final ParseTree treeNode = ctx.getChild(i);
245 if (treeNode instanceof Prefix_stmtContext) {
246 importPrefix = stringFromNode(treeNode);
248 if (treeNode instanceof Revision_date_stmtContext) {
249 String importRevisionStr = stringFromNode(treeNode);
251 importRevision = simpleDateFormat.parse(importRevisionStr);
252 } catch (ParseException e) {
253 logger.warn("Failed to parse import revision-date at line " + line + ": " + importRevisionStr);
257 moduleBuilder.addModuleImport(importName, importRevision, importPrefix);
258 setLog("import", "(" + importName + "; " + importRevision + "; " + importPrefix + ")");
262 public void exitImport_stmt(Import_stmtContext ctx) {
263 exitLog("import", "");
267 public void enterAugment_stmt(YangParser.Augment_stmtContext ctx) {
268 final int line = ctx.getStart().getLine();
269 final String augmentPath = stringFromNode(ctx);
270 enterLog("augment", augmentPath, line);
271 actualPath.push(new Stack<QName>());
273 AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath);
275 for (int i = 0; i < ctx.getChildCount(); i++) {
276 ParseTree child = ctx.getChild(i);
277 if (child instanceof Description_stmtContext) {
278 builder.setDescription(stringFromNode(child));
279 } else if (child instanceof Reference_stmtContext) {
280 builder.setReference(stringFromNode(child));
281 } else if (child instanceof Status_stmtContext) {
282 builder.setStatus(parseStatus((Status_stmtContext) child));
283 } else if (child instanceof When_stmtContext) {
284 builder.addWhenCondition(stringFromNode(child));
288 moduleBuilder.enterNode(builder);
292 public void exitAugment_stmt(YangParser.Augment_stmtContext ctx) {
293 moduleBuilder.exitNode();
294 exitLog("augment", "");
299 public void enterExtension_stmt(YangParser.Extension_stmtContext ctx) {
300 final int line = ctx.getStart().getLine();
301 final String extName = stringFromNode(ctx);
302 enterLog("extension", extName, line);
303 QName qname = new QName(namespace, revision, yangModelPrefix, extName);
304 addNodeToPath(qname);
305 SchemaPath path = createActualSchemaPath(actualPath.peek());
307 ExtensionBuilder builder = moduleBuilder.addExtension(qname, line);
308 builder.setPath(path);
309 parseSchemaNodeArgs(ctx, builder);
311 String argument = null;
313 for (int i = 0; i < ctx.getChildCount(); i++) {
314 ParseTree child = ctx.getChild(i);
315 if (child instanceof Argument_stmtContext) {
316 argument = stringFromNode(child);
317 yin = parseYinValue((Argument_stmtContext) child);
321 builder.setArgument(argument);
322 builder.setYinElement(yin);
324 moduleBuilder.enterNode(builder);
328 public void exitExtension_stmt(YangParser.Extension_stmtContext ctx) {
329 moduleBuilder.exitNode();
330 exitLog("extension", removeNodeFromPath());
334 public void enterTypedef_stmt(YangParser.Typedef_stmtContext ctx) {
335 final int line = ctx.getStart().getLine();
336 final String typedefName = stringFromNode(ctx);
337 enterLog("typedef", typedefName, line);
338 QName typedefQName = new QName(namespace, revision, yangModelPrefix, typedefName);
339 addNodeToPath(typedefQName);
340 SchemaPath path = createActualSchemaPath(actualPath.peek());
342 TypeDefinitionBuilder builder = moduleBuilder.addTypedef(line, typedefQName);
343 builder.setPath(path);
344 parseSchemaNodeArgs(ctx, builder);
345 builder.setUnits(parseUnits(ctx));
346 builder.setDefaultValue(parseDefault(ctx));
348 moduleBuilder.enterNode(builder);
352 public void exitTypedef_stmt(YangParser.Typedef_stmtContext ctx) {
353 moduleBuilder.exitNode();
354 exitLog("typedef", removeNodeFromPath());
358 public void enterType_stmt(YangParser.Type_stmtContext ctx) {
359 final int line = ctx.getStart().getLine();
360 final String typeName = stringFromNode(ctx);
361 enterLog("type", typeName, line);
363 final QName typeQName = parseQName(typeName);
365 TypeDefinition<?> type = null;
366 Type_body_stmtsContext typeBody = null;
367 for (int i = 0; i < ctx.getChildCount(); i++) {
368 if (ctx.getChild(i) instanceof Type_body_stmtsContext) {
369 typeBody = (Type_body_stmtsContext) ctx.getChild(i);
374 // if this is base yang type...
375 if (YangTypesConverter.isBaseYangType(typeName)) {
376 if (typeBody == null) {
377 // check for types which must have body
378 checkMissingBody(typeName, moduleName, line);
379 // if there are no constraints, just grab default base yang type
380 type = YangTypesConverter.javaTypeForBaseYangType(typeName);
381 addNodeToPath(type.getQName());
382 moduleBuilder.setType(type);
384 if ("union".equals(typeName)) {
385 QName qname = BaseTypes.constructQName("union");
386 addNodeToPath(qname);
387 UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, namespace, revision);
388 Builder parent = moduleBuilder.getActualNode();
389 unionBuilder.setParent(parent);
390 moduleBuilder.enterNode(unionBuilder);
391 } else if ("identityref".equals(typeName)) {
392 QName qname = BaseTypes.constructQName("identityref");
393 addNodeToPath(qname);
394 SchemaPath path = createActualSchemaPath(actualPath.peek());
395 moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody));
397 type = parseTypeWithBody(typeName, typeBody, actualPath.peek(), namespace, revision, yangModelPrefix, moduleBuilder.getActualNode());
398 moduleBuilder.setType(type);
399 addNodeToPath(type.getQName());
403 type = parseUnknownTypeWithBody(typeQName, typeBody, actualPath.peek(), namespace, revision, yangModelPrefix,
404 moduleBuilder.getActualNode());
405 // add parent node of this type statement to dirty nodes
406 moduleBuilder.markActualNodeDirty();
407 moduleBuilder.setType(type);
408 addNodeToPath(type.getQName());
413 private QName parseQName(String typeName) {
415 if (typeName.contains(":")) {
416 String[] splittedName = typeName.split(":");
417 String prefix = splittedName[0];
418 String name = splittedName[1];
419 if (prefix.equals(yangModelPrefix)) {
420 typeQName = new QName(namespace, revision, prefix, name);
422 typeQName = new QName(null, null, prefix, name);
425 typeQName = new QName(namespace, revision, yangModelPrefix, typeName);
431 public void exitType_stmt(YangParser.Type_stmtContext ctx) {
432 final String typeName = stringFromNode(ctx);
433 if ("union".equals(typeName)) {
434 moduleBuilder.exitNode();
436 exitLog("type", removeNodeFromPath());
440 public void enterGrouping_stmt(YangParser.Grouping_stmtContext ctx) {
441 final int line = ctx.getStart().getLine();
442 final String groupName = stringFromNode(ctx);
443 enterLog("grouping", groupName, line);
444 QName groupQName = new QName(namespace, revision, yangModelPrefix, groupName);
445 addNodeToPath(groupQName);
446 SchemaPath path = createActualSchemaPath(actualPath.peek());
448 GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName);
449 builder.setPath(path);
450 parseSchemaNodeArgs(ctx, builder);
452 moduleBuilder.enterNode(builder);
456 public void exitGrouping_stmt(YangParser.Grouping_stmtContext ctx) {
457 moduleBuilder.exitNode();
458 exitLog("grouping", removeNodeFromPath());
462 public void enterContainer_stmt(Container_stmtContext ctx) {
463 final int line = ctx.getStart().getLine();
464 final String containerName = stringFromNode(ctx);
465 enterLog("container", containerName, line);
467 QName containerQName = new QName(namespace, revision, yangModelPrefix, containerName);
468 addNodeToPath(containerQName);
469 SchemaPath path = createActualSchemaPath(actualPath.peek());
471 ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(line, containerQName, path);
472 parseSchemaNodeArgs(ctx, builder);
473 parseConstraints(ctx, builder.getConstraints());
474 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
476 for (int i = 0; i < ctx.getChildCount(); ++i) {
477 final ParseTree childNode = ctx.getChild(i);
478 if (childNode instanceof Presence_stmtContext) {
479 builder.setPresence(true);
484 moduleBuilder.enterNode(builder);
488 public void exitContainer_stmt(Container_stmtContext ctx) {
489 moduleBuilder.exitNode();
490 exitLog("container", removeNodeFromPath());
494 public void enterLeaf_stmt(Leaf_stmtContext ctx) {
495 final int line = ctx.getStart().getLine();
496 final String leafName = stringFromNode(ctx);
497 enterLog("leaf", leafName, line);
499 QName leafQName = new QName(namespace, revision, yangModelPrefix, leafName);
500 addNodeToPath(leafQName);
501 SchemaPath path = createActualSchemaPath(actualPath.peek());
503 LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, path);
504 parseSchemaNodeArgs(ctx, builder);
505 parseConstraints(ctx, builder.getConstraints());
506 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
508 String defaultStr = null;
509 String unitsStr = null;
510 for (int i = 0; i < ctx.getChildCount(); i++) {
511 ParseTree child = ctx.getChild(i);
512 if (child instanceof Default_stmtContext) {
513 defaultStr = stringFromNode(child);
514 } else if (child instanceof Units_stmtContext) {
515 unitsStr = stringFromNode(child);
518 builder.setDefaultStr(defaultStr);
519 builder.setUnits(unitsStr);
521 moduleBuilder.enterNode(builder);
525 public void exitLeaf_stmt(YangParser.Leaf_stmtContext ctx) {
526 moduleBuilder.exitNode();
527 exitLog("leaf", removeNodeFromPath());
531 public void enterUses_stmt(YangParser.Uses_stmtContext ctx) {
532 final int line = ctx.getStart().getLine();
533 final String groupingPathStr = stringFromNode(ctx);
534 enterLog("uses", groupingPathStr, line);
536 UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPathStr);
538 moduleBuilder.enterNode(builder);
542 public void exitUses_stmt(YangParser.Uses_stmtContext ctx) {
543 moduleBuilder.exitNode();
548 public void enterUses_augment_stmt(YangParser.Uses_augment_stmtContext ctx) {
549 actualPath.push(new Stack<QName>());
550 final int line = ctx.getStart().getLine();
551 final String augmentPath = stringFromNode(ctx);
552 enterLog("augment", augmentPath, line);
554 AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath);
556 for (int i = 0; i < ctx.getChildCount(); i++) {
557 ParseTree child = ctx.getChild(i);
558 if (child instanceof Description_stmtContext) {
559 builder.setDescription(stringFromNode(child));
560 } else if (child instanceof Reference_stmtContext) {
561 builder.setReference(stringFromNode(child));
562 } else if (child instanceof Status_stmtContext) {
563 builder.setStatus(parseStatus((Status_stmtContext) child));
564 } else if (child instanceof When_stmtContext) {
565 builder.addWhenCondition(stringFromNode(child));
569 moduleBuilder.enterNode(builder);
573 public void exitUses_augment_stmt(YangParser.Uses_augment_stmtContext ctx) {
574 moduleBuilder.exitNode();
575 exitLog("augment", "");
580 public void enterRefine_stmt(YangParser.Refine_stmtContext ctx) {
581 final String refineString = stringFromNode(ctx);
582 enterLog("refine", refineString, ctx.getStart().getLine());
584 RefineHolder refine = parseRefine(ctx, moduleName);
585 moduleBuilder.addRefine(refine);
586 moduleBuilder.enterNode(refine);
590 public void exitRefine_stmt(YangParser.Refine_stmtContext ctx) {
591 moduleBuilder.exitNode();
592 exitLog("refine", "");
596 public void enterLeaf_list_stmt(Leaf_list_stmtContext ctx) {
597 final int line = ctx.getStart().getLine();
598 final String leafListName = stringFromNode(ctx);
599 enterLog("leaf-list", leafListName, line);
600 QName leafListQName = new QName(namespace, revision, yangModelPrefix, leafListName);
601 addNodeToPath(leafListQName);
602 SchemaPath path = createActualSchemaPath(actualPath.peek());
604 LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, path);
605 moduleBuilder.enterNode(builder);
607 parseSchemaNodeArgs(ctx, builder);
608 parseConstraints(ctx, builder.getConstraints());
609 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, ctx.getStart().getLine()));
611 for (int i = 0; i < ctx.getChildCount(); ++i) {
612 final ParseTree childNode = ctx.getChild(i);
613 if (childNode instanceof Ordered_by_stmtContext) {
614 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
615 final boolean userOrdered = parseUserOrdered(orderedBy);
616 builder.setUserOrdered(userOrdered);
623 public void exitLeaf_list_stmt(YangParser.Leaf_list_stmtContext ctx) {
624 moduleBuilder.exitNode();
625 exitLog("leaf-list", removeNodeFromPath());
629 public void enterList_stmt(List_stmtContext ctx) {
630 final int line = ctx.getStart().getLine();
631 final String listName = stringFromNode(ctx);
632 enterLog("list", listName, line);
634 QName listQName = new QName(namespace, revision, yangModelPrefix, listName);
635 addNodeToPath(listQName);
636 SchemaPath path = createActualSchemaPath(actualPath.peek());
638 ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, path);
639 moduleBuilder.enterNode(builder);
641 parseSchemaNodeArgs(ctx, builder);
642 parseConstraints(ctx, builder.getConstraints());
643 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
645 String keyDefinition = "";
646 for (int i = 0; i < ctx.getChildCount(); ++i) {
647 ParseTree childNode = ctx.getChild(i);
648 if (childNode instanceof Ordered_by_stmtContext) {
649 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
650 final boolean userOrdered = parseUserOrdered(orderedBy);
651 builder.setUserOrdered(userOrdered);
652 } else if (childNode instanceof Key_stmtContext) {
653 keyDefinition = stringFromNode(childNode);
654 List<QName> key = createListKey(keyDefinition, namespace, revision, yangModelPrefix);
655 builder.setKeyDefinition(key);
661 public void exitList_stmt(List_stmtContext ctx) {
662 moduleBuilder.exitNode();
663 exitLog("list", removeNodeFromPath());
667 public void enterAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) {
668 final int line = ctx.getStart().getLine();
669 final String anyXmlName = stringFromNode(ctx);
670 enterLog("anyxml", anyXmlName, line);
672 QName anyXmlQName = new QName(namespace, revision, yangModelPrefix, anyXmlName);
673 addNodeToPath(anyXmlQName);
674 SchemaPath path = createActualSchemaPath(actualPath.peek());
676 AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, path);
677 moduleBuilder.enterNode(builder);
679 parseSchemaNodeArgs(ctx, builder);
680 parseConstraints(ctx, builder.getConstraints());
681 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
685 public void exitAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) {
686 moduleBuilder.exitNode();
687 exitLog("anyxml", removeNodeFromPath());
691 public void enterChoice_stmt(YangParser.Choice_stmtContext ctx) {
692 final int line = ctx.getStart().getLine();
693 final String choiceName = stringFromNode(ctx);
694 enterLog("choice", choiceName, line);
696 QName choiceQName = new QName(namespace, revision, yangModelPrefix, choiceName);
697 addNodeToPath(choiceQName);
698 SchemaPath path = createActualSchemaPath(actualPath.peek());
700 ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName);
701 builder.setPath(path);
702 moduleBuilder.enterNode(builder);
704 parseSchemaNodeArgs(ctx, builder);
705 parseConstraints(ctx, builder.getConstraints());
706 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
708 // set 'default' case
709 for (int i = 0; i < ctx.getChildCount(); i++) {
710 ParseTree child = ctx.getChild(i);
711 if (child instanceof Default_stmtContext) {
712 String defaultCase = stringFromNode(child);
713 builder.setDefaultCase(defaultCase);
720 public void exitChoice_stmt(YangParser.Choice_stmtContext ctx) {
721 moduleBuilder.exitNode();
722 exitLog("choice", removeNodeFromPath());
726 public void enterCase_stmt(YangParser.Case_stmtContext ctx) {
727 final int line = ctx.getStart().getLine();
728 final String caseName = stringFromNode(ctx);
729 enterLog("case", caseName, line);
731 QName caseQName = new QName(namespace, revision, yangModelPrefix, caseName);
732 addNodeToPath(caseQName);
733 SchemaPath path = createActualSchemaPath(actualPath.peek());
735 ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName);
736 builder.setPath(path);
737 moduleBuilder.enterNode(builder);
739 parseSchemaNodeArgs(ctx, builder);
740 parseConstraints(ctx, builder.getConstraints());
744 public void exitCase_stmt(YangParser.Case_stmtContext ctx) {
745 moduleBuilder.exitNode();
746 exitLog("case", removeNodeFromPath());
750 public void enterNotification_stmt(YangParser.Notification_stmtContext ctx) {
751 final int line = ctx.getStart().getLine();
752 final String notificationName = stringFromNode(ctx);
753 enterLog("notification", notificationName, line);
755 QName notificationQName = new QName(namespace, revision, yangModelPrefix, notificationName);
756 addNodeToPath(notificationQName);
757 SchemaPath path = createActualSchemaPath(actualPath.peek());
759 NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName);
760 builder.setPath(path);
761 moduleBuilder.enterNode(builder);
763 parseSchemaNodeArgs(ctx, builder);
767 public void exitNotification_stmt(YangParser.Notification_stmtContext ctx) {
768 moduleBuilder.exitNode();
769 exitLog("notification", removeNodeFromPath());
774 public void enterIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
775 final int line = ctx.getStart().getLine();
776 final String nodeParameter = stringFromNode(ctx);
777 enterLog("unknown-node", nodeParameter, line);
779 QName nodeType = null;
781 final String nodeTypeStr = ctx.getChild(0).getText();
782 final String[] splittedElement = nodeTypeStr.split(":");
783 if (splittedElement.length == 1) {
784 nodeType = new QName(namespace, revision, yangModelPrefix, splittedElement[0]);
786 nodeType = new QName(namespace, revision, splittedElement[0], splittedElement[1]);
790 if (nodeParameter != null) {
791 String[] splittedName = nodeParameter.split(":");
792 if (splittedName.length == 2) {
793 qname = new QName(null, null, splittedName[0], splittedName[1]);
795 qname = new QName(namespace, revision, yangModelPrefix, splittedName[0]);
798 qname = new QName(namespace, revision, yangModelPrefix, nodeParameter);
801 UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(line, qname);
802 builder.setNodeType(nodeType);
803 builder.setNodeParameter(nodeParameter);
804 addNodeToPath(new QName(namespace, revision, yangModelPrefix, nodeParameter));
806 SchemaPath path = createActualSchemaPath(actualPath.peek());
807 builder.setPath(path);
809 parseSchemaNodeArgs(ctx, builder);
810 moduleBuilder.enterNode(builder);
814 public void exitIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
815 moduleBuilder.exitNode();
816 exitLog("unknown-node", removeNodeFromPath());
820 public void enterRpc_stmt(YangParser.Rpc_stmtContext ctx) {
821 final int line = ctx.getStart().getLine();
822 final String rpcName = stringFromNode(ctx);
823 enterLog("rpc", rpcName, line);
825 QName rpcQName = new QName(namespace, revision, yangModelPrefix, rpcName);
826 RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName);
827 moduleBuilder.enterNode(rpcBuilder);
828 addNodeToPath(rpcQName);
830 SchemaPath path = createActualSchemaPath(actualPath.peek());
831 rpcBuilder.setPath(path);
833 parseSchemaNodeArgs(ctx, rpcBuilder);
837 public void exitRpc_stmt(YangParser.Rpc_stmtContext ctx) {
838 moduleBuilder.exitNode();
839 exitLog("rpc", removeNodeFromPath());
843 public void enterInput_stmt(YangParser.Input_stmtContext ctx) {
844 final int line = ctx.getStart().getLine();
845 final String input = "input";
846 enterLog(input, input, line);
848 QName rpcQName = new QName(namespace, revision, yangModelPrefix, input);
849 addNodeToPath(rpcQName);
850 SchemaPath path = createActualSchemaPath(actualPath.peek());
852 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(line, rpcQName, path);
853 moduleBuilder.enterNode(builder);
855 parseSchemaNodeArgs(ctx, builder);
856 parseConstraints(ctx, builder.getConstraints());
860 public void exitInput_stmt(YangParser.Input_stmtContext ctx) {
861 moduleBuilder.exitNode();
862 exitLog("input", removeNodeFromPath());
866 public void enterOutput_stmt(YangParser.Output_stmtContext ctx) {
867 final int line = ctx.getStart().getLine();
868 final String output = "output";
869 enterLog(output, output, line);
871 QName rpcQName = new QName(namespace, revision, yangModelPrefix, output);
872 addNodeToPath(rpcQName);
873 SchemaPath path = createActualSchemaPath(actualPath.peek());
875 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line);
876 moduleBuilder.enterNode(builder);
878 parseSchemaNodeArgs(ctx, builder);
879 parseConstraints(ctx, builder.getConstraints());
883 public void exitOutput_stmt(YangParser.Output_stmtContext ctx) {
884 moduleBuilder.exitNode();
885 exitLog("output", removeNodeFromPath());
889 public void enterFeature_stmt(YangParser.Feature_stmtContext ctx) {
890 final int line = ctx.getStart().getLine();
891 final String featureName = stringFromNode(ctx);
892 enterLog("feature", featureName, line);
894 QName featureQName = new QName(namespace, revision, yangModelPrefix, featureName);
895 FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName);
896 moduleBuilder.enterNode(featureBuilder);
897 addNodeToPath(featureQName);
899 SchemaPath path = createActualSchemaPath(actualPath.peek());
900 featureBuilder.setPath(path);
901 parseSchemaNodeArgs(ctx, featureBuilder);
905 public void exitFeature_stmt(YangParser.Feature_stmtContext ctx) {
906 moduleBuilder.exitNode();
907 exitLog("feature", removeNodeFromPath());
911 public void enterDeviation_stmt(YangParser.Deviation_stmtContext ctx) {
912 final int line = ctx.getStart().getLine();
913 final String targetPath = stringFromNode(ctx);
914 enterLog("deviation", targetPath, line);
916 String reference = null;
917 String deviate = null;
918 DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath);
919 moduleBuilder.enterNode(builder);
921 for (int i = 0; i < ctx.getChildCount(); i++) {
922 ParseTree child = ctx.getChild(i);
923 if (child instanceof Reference_stmtContext) {
924 reference = stringFromNode(child);
925 } else if (child instanceof Deviate_not_supported_stmtContext) {
926 deviate = stringFromNode(child);
927 } else if (child instanceof Deviate_add_stmtContext) {
928 deviate = stringFromNode(child);
929 } else if (child instanceof Deviate_replace_stmtContext) {
930 deviate = stringFromNode(child);
931 } else if (child instanceof Deviate_delete_stmtContext) {
932 deviate = stringFromNode(child);
935 builder.setReference(reference);
936 builder.setDeviate(deviate);
940 public void exitDeviation_stmt(YangParser.Deviation_stmtContext ctx) {
941 moduleBuilder.exitNode();
942 exitLog("deviation", "");
946 public void enterIdentity_stmt(YangParser.Identity_stmtContext ctx) {
947 final int line = ctx.getStart().getLine();
948 final String identityName = stringFromNode(ctx);
949 enterLog("identity", identityName, line);
951 final QName identityQName = new QName(namespace, revision, yangModelPrefix, identityName);
952 IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line);
953 moduleBuilder.enterNode(builder);
954 addNodeToPath(identityQName);
956 SchemaPath path = createActualSchemaPath(actualPath.peek());
957 builder.setPath(path);
959 parseSchemaNodeArgs(ctx, builder);
961 for (int i = 0; i < ctx.getChildCount(); i++) {
962 ParseTree child = ctx.getChild(i);
963 if (child instanceof Base_stmtContext) {
964 String baseIdentityName = stringFromNode(child);
965 builder.setBaseIdentityName(baseIdentityName);
971 public void exitIdentity_stmt(YangParser.Identity_stmtContext ctx) {
972 moduleBuilder.exitNode();
973 exitLog("identity", removeNodeFromPath());
976 public ModuleBuilder getModuleBuilder() {
977 return moduleBuilder;
980 private void enterLog(String p1, String p2, int line) {
981 logger.trace("entering {} {} ({})", p1, p2, line);
984 private void exitLog(String p1, String p2) {
985 logger.trace("exiting {} {}", p1, p2);
988 private void exitLog(String p1, QName p2) {
989 logger.trace("exiting {} {}", p1, p2.getLocalName());
992 private void setLog(String p1, String p2) {
993 logger.trace("setting {} {}", p1, p2);