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 QName qname = BaseTypes.constructQName(typeName);
381 addNodeToPath(qname);
382 SchemaPath path = createActualSchemaPath(actualPath.peek());
383 type = YangTypesConverter.javaTypeForBaseYangType(path, typeName);
384 moduleBuilder.setType(type);
386 if ("union".equals(typeName)) {
387 QName qname = BaseTypes.constructQName("union");
388 addNodeToPath(qname);
389 SchemaPath path = createActualSchemaPath(actualPath.peek());
390 UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, namespace, revision);
391 Builder parent = moduleBuilder.getActualNode();
392 unionBuilder.setParent(parent);
393 moduleBuilder.enterNode(unionBuilder);
394 unionBuilder.setPath(path);
395 } else if ("identityref".equals(typeName)) {
396 QName qname = BaseTypes.constructQName("identityref");
397 addNodeToPath(qname);
398 SchemaPath path = createActualSchemaPath(actualPath.peek());
399 moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody));
401 type = parseTypeWithBody(typeName, typeBody, actualPath.peek(), namespace, revision, yangModelPrefix, moduleBuilder.getActualNode());
402 moduleBuilder.setType(type);
403 addNodeToPath(type.getQName());
407 type = parseUnknownTypeWithBody(typeQName, typeBody, actualPath.peek(), namespace, revision, yangModelPrefix,
408 moduleBuilder.getActualNode());
409 // add parent node of this type statement to dirty nodes
410 moduleBuilder.markActualNodeDirty();
411 moduleBuilder.setType(type);
412 addNodeToPath(type.getQName());
417 private QName parseQName(String typeName) {
419 if (typeName.contains(":")) {
420 String[] splittedName = typeName.split(":");
421 String prefix = splittedName[0];
422 String name = splittedName[1];
423 if (prefix.equals(yangModelPrefix)) {
424 typeQName = new QName(namespace, revision, prefix, name);
426 typeQName = new QName(null, null, prefix, name);
429 typeQName = new QName(namespace, revision, yangModelPrefix, typeName);
435 public void exitType_stmt(YangParser.Type_stmtContext ctx) {
436 final String typeName = stringFromNode(ctx);
437 if ("union".equals(typeName)) {
438 moduleBuilder.exitNode();
440 exitLog("type", removeNodeFromPath());
444 public void enterGrouping_stmt(YangParser.Grouping_stmtContext ctx) {
445 final int line = ctx.getStart().getLine();
446 final String groupName = stringFromNode(ctx);
447 enterLog("grouping", groupName, line);
448 QName groupQName = new QName(namespace, revision, yangModelPrefix, groupName);
449 addNodeToPath(groupQName);
450 SchemaPath path = createActualSchemaPath(actualPath.peek());
452 GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName);
453 builder.setPath(path);
454 parseSchemaNodeArgs(ctx, builder);
456 moduleBuilder.enterNode(builder);
460 public void exitGrouping_stmt(YangParser.Grouping_stmtContext ctx) {
461 moduleBuilder.exitNode();
462 exitLog("grouping", removeNodeFromPath());
466 public void enterContainer_stmt(Container_stmtContext ctx) {
467 final int line = ctx.getStart().getLine();
468 final String containerName = stringFromNode(ctx);
469 enterLog("container", containerName, line);
471 QName containerQName = new QName(namespace, revision, yangModelPrefix, containerName);
472 addNodeToPath(containerQName);
473 SchemaPath path = createActualSchemaPath(actualPath.peek());
475 ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(line, containerQName, path);
476 parseSchemaNodeArgs(ctx, builder);
477 parseConstraints(ctx, builder.getConstraints());
478 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
480 for (int i = 0; i < ctx.getChildCount(); ++i) {
481 final ParseTree childNode = ctx.getChild(i);
482 if (childNode instanceof Presence_stmtContext) {
483 builder.setPresence(true);
488 moduleBuilder.enterNode(builder);
492 public void exitContainer_stmt(Container_stmtContext ctx) {
493 moduleBuilder.exitNode();
494 exitLog("container", removeNodeFromPath());
498 public void enterLeaf_stmt(Leaf_stmtContext ctx) {
499 final int line = ctx.getStart().getLine();
500 final String leafName = stringFromNode(ctx);
501 enterLog("leaf", leafName, line);
503 QName leafQName = new QName(namespace, revision, yangModelPrefix, leafName);
504 addNodeToPath(leafQName);
505 SchemaPath path = createActualSchemaPath(actualPath.peek());
507 LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, path);
508 parseSchemaNodeArgs(ctx, builder);
509 parseConstraints(ctx, builder.getConstraints());
510 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
512 String defaultStr = null;
513 String unitsStr = null;
514 for (int i = 0; i < ctx.getChildCount(); i++) {
515 ParseTree child = ctx.getChild(i);
516 if (child instanceof Default_stmtContext) {
517 defaultStr = stringFromNode(child);
518 } else if (child instanceof Units_stmtContext) {
519 unitsStr = stringFromNode(child);
522 builder.setDefaultStr(defaultStr);
523 builder.setUnits(unitsStr);
525 moduleBuilder.enterNode(builder);
529 public void exitLeaf_stmt(YangParser.Leaf_stmtContext ctx) {
530 moduleBuilder.exitNode();
531 exitLog("leaf", removeNodeFromPath());
535 public void enterUses_stmt(YangParser.Uses_stmtContext ctx) {
536 final int line = ctx.getStart().getLine();
537 final String groupingPathStr = stringFromNode(ctx);
538 enterLog("uses", groupingPathStr, line);
540 UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPathStr);
542 moduleBuilder.enterNode(builder);
546 public void exitUses_stmt(YangParser.Uses_stmtContext ctx) {
547 moduleBuilder.exitNode();
552 public void enterUses_augment_stmt(YangParser.Uses_augment_stmtContext ctx) {
553 actualPath.push(new Stack<QName>());
554 final int line = ctx.getStart().getLine();
555 final String augmentPath = stringFromNode(ctx);
556 enterLog("augment", augmentPath, line);
558 AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath);
560 for (int i = 0; i < ctx.getChildCount(); i++) {
561 ParseTree child = ctx.getChild(i);
562 if (child instanceof Description_stmtContext) {
563 builder.setDescription(stringFromNode(child));
564 } else if (child instanceof Reference_stmtContext) {
565 builder.setReference(stringFromNode(child));
566 } else if (child instanceof Status_stmtContext) {
567 builder.setStatus(parseStatus((Status_stmtContext) child));
568 } else if (child instanceof When_stmtContext) {
569 builder.addWhenCondition(stringFromNode(child));
573 moduleBuilder.enterNode(builder);
577 public void exitUses_augment_stmt(YangParser.Uses_augment_stmtContext ctx) {
578 moduleBuilder.exitNode();
579 exitLog("augment", "");
584 public void enterRefine_stmt(YangParser.Refine_stmtContext ctx) {
585 final String refineString = stringFromNode(ctx);
586 enterLog("refine", refineString, ctx.getStart().getLine());
588 RefineHolder refine = parseRefine(ctx, moduleName);
589 moduleBuilder.addRefine(refine);
590 moduleBuilder.enterNode(refine);
594 public void exitRefine_stmt(YangParser.Refine_stmtContext ctx) {
595 moduleBuilder.exitNode();
596 exitLog("refine", "");
600 public void enterLeaf_list_stmt(Leaf_list_stmtContext ctx) {
601 final int line = ctx.getStart().getLine();
602 final String leafListName = stringFromNode(ctx);
603 enterLog("leaf-list", leafListName, line);
604 QName leafListQName = new QName(namespace, revision, yangModelPrefix, leafListName);
605 addNodeToPath(leafListQName);
606 SchemaPath path = createActualSchemaPath(actualPath.peek());
608 LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, path);
609 moduleBuilder.enterNode(builder);
611 parseSchemaNodeArgs(ctx, builder);
612 parseConstraints(ctx, builder.getConstraints());
613 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, ctx.getStart().getLine()));
615 for (int i = 0; i < ctx.getChildCount(); ++i) {
616 final ParseTree childNode = ctx.getChild(i);
617 if (childNode instanceof Ordered_by_stmtContext) {
618 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
619 final boolean userOrdered = parseUserOrdered(orderedBy);
620 builder.setUserOrdered(userOrdered);
627 public void exitLeaf_list_stmt(YangParser.Leaf_list_stmtContext ctx) {
628 moduleBuilder.exitNode();
629 exitLog("leaf-list", removeNodeFromPath());
633 public void enterList_stmt(List_stmtContext ctx) {
634 final int line = ctx.getStart().getLine();
635 final String listName = stringFromNode(ctx);
636 enterLog("list", listName, line);
638 QName listQName = new QName(namespace, revision, yangModelPrefix, listName);
639 addNodeToPath(listQName);
640 SchemaPath path = createActualSchemaPath(actualPath.peek());
642 ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, path);
643 moduleBuilder.enterNode(builder);
645 parseSchemaNodeArgs(ctx, builder);
646 parseConstraints(ctx, builder.getConstraints());
647 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
649 String keyDefinition = "";
650 for (int i = 0; i < ctx.getChildCount(); ++i) {
651 ParseTree childNode = ctx.getChild(i);
652 if (childNode instanceof Ordered_by_stmtContext) {
653 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
654 final boolean userOrdered = parseUserOrdered(orderedBy);
655 builder.setUserOrdered(userOrdered);
656 } else if (childNode instanceof Key_stmtContext) {
657 keyDefinition = stringFromNode(childNode);
658 List<QName> key = createListKey(keyDefinition, namespace, revision, yangModelPrefix);
659 builder.setKeyDefinition(key);
665 public void exitList_stmt(List_stmtContext ctx) {
666 moduleBuilder.exitNode();
667 exitLog("list", removeNodeFromPath());
671 public void enterAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) {
672 final int line = ctx.getStart().getLine();
673 final String anyXmlName = stringFromNode(ctx);
674 enterLog("anyxml", anyXmlName, line);
676 QName anyXmlQName = new QName(namespace, revision, yangModelPrefix, anyXmlName);
677 addNodeToPath(anyXmlQName);
678 SchemaPath path = createActualSchemaPath(actualPath.peek());
680 AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, path);
681 moduleBuilder.enterNode(builder);
683 parseSchemaNodeArgs(ctx, builder);
684 parseConstraints(ctx, builder.getConstraints());
685 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
689 public void exitAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) {
690 moduleBuilder.exitNode();
691 exitLog("anyxml", removeNodeFromPath());
695 public void enterChoice_stmt(YangParser.Choice_stmtContext ctx) {
696 final int line = ctx.getStart().getLine();
697 final String choiceName = stringFromNode(ctx);
698 enterLog("choice", choiceName, line);
700 QName choiceQName = new QName(namespace, revision, yangModelPrefix, choiceName);
701 addNodeToPath(choiceQName);
702 SchemaPath path = createActualSchemaPath(actualPath.peek());
704 ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName);
705 builder.setPath(path);
706 moduleBuilder.enterNode(builder);
708 parseSchemaNodeArgs(ctx, builder);
709 parseConstraints(ctx, builder.getConstraints());
710 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
712 // set 'default' case
713 for (int i = 0; i < ctx.getChildCount(); i++) {
714 ParseTree child = ctx.getChild(i);
715 if (child instanceof Default_stmtContext) {
716 String defaultCase = stringFromNode(child);
717 builder.setDefaultCase(defaultCase);
724 public void exitChoice_stmt(YangParser.Choice_stmtContext ctx) {
725 moduleBuilder.exitNode();
726 exitLog("choice", removeNodeFromPath());
730 public void enterCase_stmt(YangParser.Case_stmtContext ctx) {
731 final int line = ctx.getStart().getLine();
732 final String caseName = stringFromNode(ctx);
733 enterLog("case", caseName, line);
735 QName caseQName = new QName(namespace, revision, yangModelPrefix, caseName);
736 addNodeToPath(caseQName);
737 SchemaPath path = createActualSchemaPath(actualPath.peek());
739 ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName);
740 builder.setPath(path);
741 moduleBuilder.enterNode(builder);
743 parseSchemaNodeArgs(ctx, builder);
744 parseConstraints(ctx, builder.getConstraints());
748 public void exitCase_stmt(YangParser.Case_stmtContext ctx) {
749 moduleBuilder.exitNode();
750 exitLog("case", removeNodeFromPath());
754 public void enterNotification_stmt(YangParser.Notification_stmtContext ctx) {
755 final int line = ctx.getStart().getLine();
756 final String notificationName = stringFromNode(ctx);
757 enterLog("notification", notificationName, line);
759 QName notificationQName = new QName(namespace, revision, yangModelPrefix, notificationName);
760 addNodeToPath(notificationQName);
761 SchemaPath path = createActualSchemaPath(actualPath.peek());
763 NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName);
764 builder.setPath(path);
765 moduleBuilder.enterNode(builder);
767 parseSchemaNodeArgs(ctx, builder);
771 public void exitNotification_stmt(YangParser.Notification_stmtContext ctx) {
772 moduleBuilder.exitNode();
773 exitLog("notification", removeNodeFromPath());
778 public void enterIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
779 final int line = ctx.getStart().getLine();
780 final String nodeParameter = stringFromNode(ctx);
781 enterLog("unknown-node", nodeParameter, line);
783 QName nodeType = null;
785 final String nodeTypeStr = ctx.getChild(0).getText();
786 final String[] splittedElement = nodeTypeStr.split(":");
787 if (splittedElement.length == 1) {
788 nodeType = new QName(namespace, revision, yangModelPrefix, splittedElement[0]);
790 nodeType = new QName(namespace, revision, splittedElement[0], splittedElement[1]);
794 if (nodeParameter != null) {
795 String[] splittedName = nodeParameter.split(":");
796 if (splittedName.length == 2) {
797 qname = new QName(null, null, splittedName[0], splittedName[1]);
799 qname = new QName(namespace, revision, yangModelPrefix, splittedName[0]);
802 qname = new QName(namespace, revision, yangModelPrefix, nodeParameter);
805 UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(line, qname);
806 builder.setNodeType(nodeType);
807 builder.setNodeParameter(nodeParameter);
808 addNodeToPath(new QName(namespace, revision, yangModelPrefix, nodeParameter));
810 SchemaPath path = createActualSchemaPath(actualPath.peek());
811 builder.setPath(path);
813 parseSchemaNodeArgs(ctx, builder);
814 moduleBuilder.enterNode(builder);
818 public void exitIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
819 moduleBuilder.exitNode();
820 exitLog("unknown-node", removeNodeFromPath());
824 public void enterRpc_stmt(YangParser.Rpc_stmtContext ctx) {
825 final int line = ctx.getStart().getLine();
826 final String rpcName = stringFromNode(ctx);
827 enterLog("rpc", rpcName, line);
829 QName rpcQName = new QName(namespace, revision, yangModelPrefix, rpcName);
830 RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName);
831 moduleBuilder.enterNode(rpcBuilder);
832 addNodeToPath(rpcQName);
834 SchemaPath path = createActualSchemaPath(actualPath.peek());
835 rpcBuilder.setPath(path);
837 parseSchemaNodeArgs(ctx, rpcBuilder);
841 public void exitRpc_stmt(YangParser.Rpc_stmtContext ctx) {
842 moduleBuilder.exitNode();
843 exitLog("rpc", removeNodeFromPath());
847 public void enterInput_stmt(YangParser.Input_stmtContext ctx) {
848 final int line = ctx.getStart().getLine();
849 final String input = "input";
850 enterLog(input, input, line);
852 QName rpcQName = new QName(namespace, revision, yangModelPrefix, input);
853 addNodeToPath(rpcQName);
854 SchemaPath path = createActualSchemaPath(actualPath.peek());
856 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(line, rpcQName, path);
857 moduleBuilder.enterNode(builder);
859 parseSchemaNodeArgs(ctx, builder);
860 parseConstraints(ctx, builder.getConstraints());
864 public void exitInput_stmt(YangParser.Input_stmtContext ctx) {
865 moduleBuilder.exitNode();
866 exitLog("input", removeNodeFromPath());
870 public void enterOutput_stmt(YangParser.Output_stmtContext ctx) {
871 final int line = ctx.getStart().getLine();
872 final String output = "output";
873 enterLog(output, output, line);
875 QName rpcQName = new QName(namespace, revision, yangModelPrefix, output);
876 addNodeToPath(rpcQName);
877 SchemaPath path = createActualSchemaPath(actualPath.peek());
879 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line);
880 moduleBuilder.enterNode(builder);
882 parseSchemaNodeArgs(ctx, builder);
883 parseConstraints(ctx, builder.getConstraints());
887 public void exitOutput_stmt(YangParser.Output_stmtContext ctx) {
888 moduleBuilder.exitNode();
889 exitLog("output", removeNodeFromPath());
893 public void enterFeature_stmt(YangParser.Feature_stmtContext ctx) {
894 final int line = ctx.getStart().getLine();
895 final String featureName = stringFromNode(ctx);
896 enterLog("feature", featureName, line);
898 QName featureQName = new QName(namespace, revision, yangModelPrefix, featureName);
899 FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName);
900 moduleBuilder.enterNode(featureBuilder);
901 addNodeToPath(featureQName);
903 SchemaPath path = createActualSchemaPath(actualPath.peek());
904 featureBuilder.setPath(path);
905 parseSchemaNodeArgs(ctx, featureBuilder);
909 public void exitFeature_stmt(YangParser.Feature_stmtContext ctx) {
910 moduleBuilder.exitNode();
911 exitLog("feature", removeNodeFromPath());
915 public void enterDeviation_stmt(YangParser.Deviation_stmtContext ctx) {
916 final int line = ctx.getStart().getLine();
917 final String targetPath = stringFromNode(ctx);
918 enterLog("deviation", targetPath, line);
920 String reference = null;
921 String deviate = null;
922 DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath);
923 moduleBuilder.enterNode(builder);
925 for (int i = 0; i < ctx.getChildCount(); i++) {
926 ParseTree child = ctx.getChild(i);
927 if (child instanceof Reference_stmtContext) {
928 reference = stringFromNode(child);
929 } else if (child instanceof Deviate_not_supported_stmtContext) {
930 deviate = stringFromNode(child);
931 } else if (child instanceof Deviate_add_stmtContext) {
932 deviate = stringFromNode(child);
933 } else if (child instanceof Deviate_replace_stmtContext) {
934 deviate = stringFromNode(child);
935 } else if (child instanceof Deviate_delete_stmtContext) {
936 deviate = stringFromNode(child);
939 builder.setReference(reference);
940 builder.setDeviate(deviate);
944 public void exitDeviation_stmt(YangParser.Deviation_stmtContext ctx) {
945 moduleBuilder.exitNode();
946 exitLog("deviation", "");
950 public void enterIdentity_stmt(YangParser.Identity_stmtContext ctx) {
951 final int line = ctx.getStart().getLine();
952 final String identityName = stringFromNode(ctx);
953 enterLog("identity", identityName, line);
955 final QName identityQName = new QName(namespace, revision, yangModelPrefix, identityName);
956 IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line);
957 moduleBuilder.enterNode(builder);
958 addNodeToPath(identityQName);
960 SchemaPath path = createActualSchemaPath(actualPath.peek());
961 builder.setPath(path);
963 parseSchemaNodeArgs(ctx, builder);
965 for (int i = 0; i < ctx.getChildCount(); i++) {
966 ParseTree child = ctx.getChild(i);
967 if (child instanceof Base_stmtContext) {
968 String baseIdentityName = stringFromNode(child);
969 builder.setBaseIdentityName(baseIdentityName);
975 public void exitIdentity_stmt(YangParser.Identity_stmtContext ctx) {
976 moduleBuilder.exitNode();
977 exitLog("identity", removeNodeFromPath());
980 public ModuleBuilder getModuleBuilder() {
981 return moduleBuilder;
984 private void enterLog(String p1, String p2, int line) {
985 logger.trace("entering {} {} ({})", p1, p2, line);
988 private void exitLog(String p1, String p2) {
989 logger.trace("exiting {} {}", p1, p2);
992 private void exitLog(String p1, QName p2) {
993 logger.trace("exiting {} {}", p1, p2.getLocalName());
996 private void setLog(String p1, String p2) {
997 logger.trace("setting {} {}", p1, p2);