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.impl.ParserListenerUtils.checkMissingBody;
11 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.createListKey;
12 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.getConfig;
13 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.getIdentityrefBase;
14 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseConstraints;
15 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseDefault;
16 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseRefine;
17 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseSchemaNodeArgs;
18 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseStatus;
19 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseTypeWithBody;
20 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseUnits;
21 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseUnknownTypeWithBody;
22 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseUserOrdered;
23 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseYinValue;
24 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.stringFromNode;
26 import com.google.common.base.Splitter;
27 import com.google.common.base.Strings;
28 import com.google.common.collect.Iterables;
31 import java.text.DateFormat;
32 import java.text.ParseException;
33 import java.text.SimpleDateFormat;
34 import java.util.Date;
35 import java.util.Iterator;
36 import java.util.List;
38 import org.antlr.v4.runtime.tree.ParseTree;
39 import org.antlr.v4.runtime.tree.ParseTreeWalker;
40 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser;
41 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Argument_stmtContext;
42 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Base_stmtContext;
43 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Contact_stmtContext;
44 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Container_stmtContext;
45 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Default_stmtContext;
46 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Description_stmtContext;
47 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_add_stmtContext;
48 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_delete_stmtContext;
49 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_not_supported_stmtContext;
50 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_replace_stmtContext;
51 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Import_stmtContext;
52 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Key_stmtContext;
53 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Leaf_list_stmtContext;
54 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Leaf_stmtContext;
55 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.List_stmtContext;
56 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Module_header_stmtsContext;
57 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Namespace_stmtContext;
58 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Ordered_by_stmtContext;
59 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Organization_stmtContext;
60 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Prefix_stmtContext;
61 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Presence_stmtContext;
62 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Reference_stmtContext;
63 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_date_stmtContext;
64 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtContext;
65 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtsContext;
66 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Status_stmtContext;
67 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Type_body_stmtsContext;
68 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Units_stmtContext;
69 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.When_stmtContext;
70 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Yang_version_stmtContext;
71 import org.opendaylight.yangtools.antlrv4.code.gen.YangParserBaseListener;
72 import org.opendaylight.yangtools.yang.common.QName;
73 import org.opendaylight.yangtools.yang.common.QNameModule;
74 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
75 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
76 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
77 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
78 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
79 import org.opendaylight.yangtools.yang.parser.builder.api.ExtensionBuilder;
80 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
81 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
82 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
83 import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder;
84 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
85 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
86 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
87 import org.opendaylight.yangtools.yang.parser.builder.impl.DeviationBuilder;
88 import org.opendaylight.yangtools.yang.parser.builder.impl.FeatureBuilder;
89 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
90 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
91 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
92 import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder;
93 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
94 import org.opendaylight.yangtools.yang.parser.builder.impl.NotificationBuilder;
95 import org.opendaylight.yangtools.yang.parser.builder.impl.RefineHolderImpl;
96 import org.opendaylight.yangtools.yang.parser.builder.impl.RpcDefinitionBuilder;
97 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
98 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilderImpl;
99 import org.slf4j.Logger;
100 import org.slf4j.LoggerFactory;
103 public final class YangParserListenerImpl extends YangParserBaseListener {
104 private static final Logger LOG = LoggerFactory.getLogger(YangParserListenerImpl.class);
105 private static final Splitter COLON_SPLITTER = Splitter.on(':');
106 private static final String AUGMENT_STR = "augment";
108 private final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
109 private final SchemaPathStack stack = new SchemaPathStack();
110 private final String sourcePath;
111 private QName moduleQName = new QName(null, new Date(0L), null, "dummy");
112 private ModuleBuilder moduleBuilder;
113 private String moduleName;
114 private int augmentOrder;
116 public YangParserListenerImpl(final String sourcePath) {
117 this.sourcePath = sourcePath;
121 * Create a new instance.
123 * FIXME: the resulting type needs to be extracted, such that we can reuse
124 * the "BaseListener" aspect, which need not be exposed to the user.
125 * Maybe factor out a base class into repo.spi?
132 public static YangParserListenerImpl create(final String sourcePath, final ParseTreeWalker walker, final ParseTree tree) {
133 final YangParserListenerImpl ret = new YangParserListenerImpl(sourcePath);
134 walker.walk(ret, tree);
139 public void enterModule_stmt(final YangParser.Module_stmtContext ctx) {
140 moduleName = stringFromNode(ctx);
141 LOG.trace("entering module {}", moduleName);
142 enterLog("module", moduleName, 0);
145 moduleBuilder = new ModuleBuilder(moduleName, sourcePath);
147 String description = null;
148 String reference = null;
149 for (int i = 0; i < ctx.getChildCount(); i++) {
150 ParseTree child = ctx.getChild(i);
151 if (child instanceof Description_stmtContext) {
152 description = stringFromNode(child);
153 } else if (child instanceof Reference_stmtContext) {
154 reference = stringFromNode(child);
156 if (description != null && reference != null) {
161 moduleBuilder.setDescription(description);
162 moduleBuilder.setReference(reference);
166 public void exitModule_stmt(final YangParser.Module_stmtContext ctx) {
172 public void enterSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
173 moduleName = stringFromNode(ctx);
174 LOG.trace("entering submodule {}", moduleName);
175 enterLog("submodule", moduleName, 0);
178 moduleBuilder = new ModuleBuilder(moduleName, true, sourcePath);
180 String description = null;
181 String reference = null;
182 for (int i = 0; i < ctx.getChildCount(); i++) {
183 ParseTree child = ctx.getChild(i);
184 if (child instanceof Description_stmtContext) {
185 description = stringFromNode(child);
186 } else if (child instanceof Reference_stmtContext) {
187 reference = stringFromNode(child);
189 if (description != null && reference != null) {
194 moduleBuilder.setDescription(description);
195 moduleBuilder.setReference(reference);
199 public void exitSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
200 exitLog("submodule");
205 public void enterBelongs_to_stmt(final YangParser.Belongs_to_stmtContext ctx) {
206 moduleBuilder.setBelongsTo(stringFromNode(ctx));
210 public void enterModule_header_stmts(final Module_header_stmtsContext ctx) {
211 enterLog("module_header", "", ctx.getStart().getLine());
212 String yangVersion = null;
213 for (int i = 0; i < ctx.getChildCount(); ++i) {
214 final ParseTree treeNode = ctx.getChild(i);
215 if (treeNode instanceof Namespace_stmtContext) {
216 final String namespaceStr = stringFromNode(treeNode);
217 final URI namespace = URI.create(namespaceStr);
218 this.moduleQName = new QName(namespace, moduleQName.getRevision(), moduleQName.getPrefix(), moduleQName.getLocalName());
219 moduleBuilder.setQNameModule(moduleQName.getModule());
220 setLog("namespace", namespaceStr);
221 } else if (treeNode instanceof Prefix_stmtContext) {
222 final String yangModelPrefix = stringFromNode(treeNode);
223 this.moduleQName = QName.create(moduleQName.getModule(), yangModelPrefix, moduleQName.getLocalName());
224 moduleBuilder.setPrefix(yangModelPrefix);
225 setLog("prefix", yangModelPrefix);
226 } else if (treeNode instanceof Yang_version_stmtContext) {
227 yangVersion = stringFromNode(treeNode);
228 setLog("yang-version", yangVersion);
232 if (yangVersion == null) {
235 moduleBuilder.setYangVersion(yangVersion);
239 public void exitModule_header_stmts(final Module_header_stmtsContext ctx) {
240 exitLog("module_header");
244 public void enterMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
245 enterLog("meta_stmt", "", ctx.getStart().getLine());
246 for (int i = 0; i < ctx.getChildCount(); i++) {
247 ParseTree child = ctx.getChild(i);
248 if (child instanceof Organization_stmtContext) {
249 final String organization = stringFromNode(child);
250 moduleBuilder.setOrganization(organization);
251 setLog("organization", organization);
252 } else if (child instanceof Contact_stmtContext) {
253 final String contact = stringFromNode(child);
254 moduleBuilder.setContact(contact);
255 setLog("contact", contact);
256 } else if (child instanceof Description_stmtContext) {
257 final String description = stringFromNode(child);
258 moduleBuilder.setDescription(description);
259 setLog("description", description);
260 } else if (child instanceof Reference_stmtContext) {
261 final String reference = stringFromNode(child);
262 moduleBuilder.setReference(reference);
263 setLog("reference", reference);
269 public void exitMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
270 exitLog("meta_stmt");
274 public void enterRevision_stmts(final Revision_stmtsContext ctx) {
275 enterLog("revisions", "", ctx.getStart().getLine());
276 for (int i = 0; i < ctx.getChildCount(); ++i) {
277 final ParseTree treeNode = ctx.getChild(i);
278 if (treeNode instanceof Revision_stmtContext) {
279 updateRevisionForRevisionStatement(treeNode);
285 public void exitRevision_stmts(final Revision_stmtsContext ctx) {
286 exitLog("revisions");
289 private void updateRevisionForRevisionStatement(final ParseTree treeNode) {
290 final String revisionDateStr = stringFromNode(treeNode);
292 final Date revisionDate = SIMPLE_DATE_FORMAT.parse(revisionDateStr);
293 if ((revisionDate != null) && (this.moduleQName.getRevision().compareTo(revisionDate) < 0)) {
294 this.moduleQName = new QName(moduleQName.getNamespace(), revisionDate, moduleQName.getPrefix(), moduleQName.getLocalName());
295 moduleBuilder.setQNameModule(moduleQName.getModule());
296 setLog("revision", revisionDate.toString());
297 for (int i = 0; i < treeNode.getChildCount(); ++i) {
298 ParseTree child = treeNode.getChild(i);
299 if (child instanceof Reference_stmtContext) {
300 moduleBuilder.setReference(stringFromNode(child));
304 } catch (ParseException e) {
305 LOG.warn("Failed to parse revision string: {}", revisionDateStr, e);
310 public void enterImport_stmt(final Import_stmtContext ctx) {
311 final int line = ctx.getStart().getLine();
312 final String importName = stringFromNode(ctx);
313 enterLog("import", importName, line);
315 String importPrefix = null;
316 Date importRevision = null;
318 for (int i = 0; i < ctx.getChildCount(); ++i) {
319 final ParseTree treeNode = ctx.getChild(i);
320 if (treeNode instanceof Prefix_stmtContext) {
321 importPrefix = stringFromNode(treeNode);
323 if (treeNode instanceof Revision_date_stmtContext) {
324 String importRevisionStr = stringFromNode(treeNode);
326 importRevision = SIMPLE_DATE_FORMAT.parse(importRevisionStr);
327 } catch (ParseException e) {
328 LOG.warn("Failed to parse import revision-date at line {}: {}", line, importRevisionStr, e);
332 moduleBuilder.addModuleImport(importName, importRevision, importPrefix);
333 LOG.trace("setting import ({}; {}; {})", importName, importRevision, importPrefix);
337 public void exitImport_stmt(final Import_stmtContext ctx) {
342 public void enterAugment_stmt(final YangParser.Augment_stmtContext ctx) {
343 final int line = ctx.getStart().getLine();
344 final String augmentPath = stringFromNode(ctx);
345 enterLog(AUGMENT_STR, augmentPath, line);
348 AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, augmentOrder++);
350 for (int i = 0; i < ctx.getChildCount(); i++) {
351 ParseTree child = ctx.getChild(i);
352 if (child instanceof Description_stmtContext) {
353 builder.setDescription(stringFromNode(child));
354 } else if (child instanceof Reference_stmtContext) {
355 builder.setReference(stringFromNode(child));
356 } else if (child instanceof Status_stmtContext) {
357 builder.setStatus(parseStatus((Status_stmtContext) child));
358 } else if (child instanceof When_stmtContext) {
359 builder.addWhenCondition(stringFromNode(child));
363 moduleBuilder.enterNode(builder);
367 public void exitAugment_stmt(final YangParser.Augment_stmtContext ctx) {
368 moduleBuilder.exitNode();
369 exitLog(AUGMENT_STR);
374 public void enterExtension_stmt(final YangParser.Extension_stmtContext ctx) {
375 final int line = ctx.getStart().getLine();
376 final String extName = stringFromNode(ctx);
377 enterLog("extension", extName, line);
378 QName qname = QName.create(moduleQName, extName);
379 SchemaPath path = stack.addNodeToPath(qname);
381 ExtensionBuilder builder = moduleBuilder.addExtension(qname, line, path);
382 parseSchemaNodeArgs(ctx, builder);
384 String argument = null;
386 for (int i = 0; i < ctx.getChildCount(); i++) {
387 ParseTree child = ctx.getChild(i);
388 if (child instanceof Argument_stmtContext) {
389 argument = stringFromNode(child);
390 yin = parseYinValue((Argument_stmtContext) child);
394 builder.setArgument(argument);
395 builder.setYinElement(yin);
397 moduleBuilder.enterNode(builder);
401 public void exitExtension_stmt(final YangParser.Extension_stmtContext ctx) {
402 moduleBuilder.exitNode();
403 exitLog("extension", stack.removeNodeFromPath());
407 public void enterTypedef_stmt(final YangParser.Typedef_stmtContext ctx) {
408 final int line = ctx.getStart().getLine();
409 final String typedefName = stringFromNode(ctx);
410 enterLog("typedef", typedefName, line);
411 QName typedefQName = QName.create(moduleQName, typedefName);
412 SchemaPath path = stack.addNodeToPath(typedefQName);
414 TypeDefinitionBuilder builder = moduleBuilder.addTypedef(line, typedefQName, path);
415 parseSchemaNodeArgs(ctx, builder);
416 builder.setUnits(parseUnits(ctx));
417 builder.setDefaultValue(parseDefault(ctx));
419 moduleBuilder.enterNode(builder);
423 public void exitTypedef_stmt(final YangParser.Typedef_stmtContext ctx) {
424 moduleBuilder.exitNode();
425 exitLog("typedef", stack.removeNodeFromPath());
429 public void enterType_stmt(final YangParser.Type_stmtContext ctx) {
430 final int line = ctx.getStart().getLine();
431 final String typeName = stringFromNode(ctx);
432 enterLog("type", typeName, line);
434 final QName typeQName = parseQName(typeName);
436 TypeDefinition<?> type;
437 Type_body_stmtsContext typeBody = null;
438 for (int i = 0; i < ctx.getChildCount(); i++) {
439 if (ctx.getChild(i) instanceof Type_body_stmtsContext) {
440 typeBody = (Type_body_stmtsContext) ctx.getChild(i);
445 // if this is base yang type...
446 if (BaseTypes.isYangBuildInType(typeName)) {
447 if (typeBody == null) {
448 // check for types which must have body
449 checkMissingBody(typeName, moduleName, line);
450 // if there are no constraints, just grab default base yang type
451 type = BaseTypes.defaultBaseTypeFor(typeName).orNull();
452 stack.addNodeToPath(type.getQName());
453 moduleBuilder.setType(type);
458 qname = BaseTypes.UNION_QNAME;
459 stack.addNodeToPath(qname);
460 UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, moduleQName.getModule());
461 Builder parent = moduleBuilder.getActualNode();
462 unionBuilder.setParent(parent);
463 moduleBuilder.enterNode(unionBuilder);
466 qname = BaseTypes.IDENTITYREF_QNAME;
467 SchemaPath path = stack.addNodeToPath(qname);
468 moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody));
471 type = parseTypeWithBody(typeName, typeBody, stack.currentSchemaPath(), moduleQName, moduleBuilder.getActualNode());
472 moduleBuilder.setType(type);
473 stack.addNodeToPath(type.getQName());
477 type = parseUnknownTypeWithBody(typeQName, typeBody, stack.currentSchemaPath(), moduleQName, moduleBuilder.getActualNode());
478 // add parent node of this type statement to dirty nodes
479 moduleBuilder.markActualNodeDirty();
480 moduleBuilder.setType(type);
481 stack.addNodeToPath(type.getQName());
486 private QName parseQName(final String typeName) {
487 final QName typeQName;
488 if (typeName.indexOf(':') != -1) {
489 final Iterator<String> split = COLON_SPLITTER.split(typeName).iterator();
490 final String prefix = split.next();
491 final String name = split.next();
492 if (prefix.equals(moduleQName.getPrefix())) {
493 typeQName = QName.create(moduleQName, name);
495 typeQName = QName.create(QNameModule.create(null, null), prefix, name);
498 typeQName = QName.create(moduleQName, typeName);
504 public void exitType_stmt(final YangParser.Type_stmtContext ctx) {
505 final String typeName = stringFromNode(ctx);
506 if ("union".equals(typeName)) {
507 moduleBuilder.exitNode();
509 exitLog("type", stack.removeNodeFromPath());
513 public void enterGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
514 final int line = ctx.getStart().getLine();
515 final String groupName = stringFromNode(ctx);
516 enterLog("grouping", groupName, line);
517 QName groupQName = QName.create(moduleQName, groupName);
518 SchemaPath path = stack.addNodeToPath(groupQName);
520 GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName, path);
521 parseSchemaNodeArgs(ctx, builder);
523 moduleBuilder.enterNode(builder);
527 public void exitGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
528 moduleBuilder.exitNode();
529 exitLog("grouping", stack.removeNodeFromPath());
533 public void enterContainer_stmt(final Container_stmtContext ctx) {
534 final int line = ctx.getStart().getLine();
535 final String containerName = stringFromNode(ctx);
536 enterLog("container", containerName, line);
538 QName containerQName = QName.create(moduleQName, containerName);
539 SchemaPath path = stack.addNodeToPath(containerQName);
541 ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(line, containerQName, path);
542 parseSchemaNodeArgs(ctx, builder);
543 parseConstraints(ctx, builder.getConstraints());
544 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
546 for (int i = 0; i < ctx.getChildCount(); ++i) {
547 final ParseTree childNode = ctx.getChild(i);
548 if (childNode instanceof Presence_stmtContext) {
549 builder.setPresence(true);
554 moduleBuilder.enterNode(builder);
558 public void exitContainer_stmt(final Container_stmtContext ctx) {
559 moduleBuilder.exitNode();
560 exitLog("container", stack.removeNodeFromPath());
564 public void enterLeaf_stmt(final Leaf_stmtContext ctx) {
565 final int line = ctx.getStart().getLine();
566 final String leafName = stringFromNode(ctx);
567 enterLog("leaf", leafName, line);
569 QName leafQName = QName.create(moduleQName, leafName);
570 SchemaPath path = stack.addNodeToPath(leafQName);
572 LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, path);
573 parseSchemaNodeArgs(ctx, builder);
574 parseConstraints(ctx, builder.getConstraints());
575 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
577 String defaultStr = null;
578 String unitsStr = null;
579 for (int i = 0; i < ctx.getChildCount(); i++) {
580 ParseTree child = ctx.getChild(i);
581 if (child instanceof Default_stmtContext) {
582 defaultStr = stringFromNode(child);
583 } else if (child instanceof Units_stmtContext) {
584 unitsStr = stringFromNode(child);
587 builder.setDefaultStr(defaultStr);
588 builder.setUnits(unitsStr);
590 moduleBuilder.enterNode(builder);
594 public void exitLeaf_stmt(final YangParser.Leaf_stmtContext ctx) {
595 moduleBuilder.exitNode();
596 exitLog("leaf", stack.removeNodeFromPath());
600 public void enterUses_stmt(final YangParser.Uses_stmtContext ctx) {
601 final int line = ctx.getStart().getLine();
602 final String groupingPathStr = stringFromNode(ctx);
603 enterLog("uses", groupingPathStr, line);
605 UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPathStr);
607 moduleBuilder.enterNode(builder);
611 public void exitUses_stmt(final YangParser.Uses_stmtContext ctx) {
612 moduleBuilder.exitNode();
617 public void enterUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
618 final int line = ctx.getStart().getLine();
619 final String augmentPath = stringFromNode(ctx);
620 enterLog(AUGMENT_STR, augmentPath, line);
623 AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, augmentOrder++);
625 for (int i = 0; i < ctx.getChildCount(); i++) {
626 ParseTree child = ctx.getChild(i);
627 if (child instanceof Description_stmtContext) {
628 builder.setDescription(stringFromNode(child));
629 } else if (child instanceof Reference_stmtContext) {
630 builder.setReference(stringFromNode(child));
631 } else if (child instanceof Status_stmtContext) {
632 builder.setStatus(parseStatus((Status_stmtContext) child));
633 } else if (child instanceof When_stmtContext) {
634 builder.addWhenCondition(stringFromNode(child));
638 moduleBuilder.enterNode(builder);
642 public void exitUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
643 moduleBuilder.exitNode();
644 exitLog(AUGMENT_STR);
649 public void enterRefine_stmt(final YangParser.Refine_stmtContext ctx) {
650 final String refineString = stringFromNode(ctx);
651 enterLog("refine", refineString, ctx.getStart().getLine());
653 RefineHolderImpl refine = parseRefine(ctx, moduleName);
654 moduleBuilder.addRefine(refine);
655 moduleBuilder.enterNode(refine);
659 public void exitRefine_stmt(final YangParser.Refine_stmtContext ctx) {
660 moduleBuilder.exitNode();
665 public void enterLeaf_list_stmt(final Leaf_list_stmtContext ctx) {
666 final int line = ctx.getStart().getLine();
667 final String leafListName = stringFromNode(ctx);
668 enterLog("leaf-list", leafListName, line);
669 QName leafListQName = QName.create(moduleQName, leafListName);
670 SchemaPath path = stack.addNodeToPath(leafListQName);
672 LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, path);
673 moduleBuilder.enterNode(builder);
675 parseSchemaNodeArgs(ctx, builder);
676 parseConstraints(ctx, builder.getConstraints());
677 builder.setConfiguration(getConfig(ctx, builder, moduleName, ctx.getStart().getLine()));
679 for (int i = 0; i < ctx.getChildCount(); ++i) {
680 final ParseTree childNode = ctx.getChild(i);
681 if (childNode instanceof Ordered_by_stmtContext) {
682 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
683 final boolean userOrdered = parseUserOrdered(orderedBy);
684 builder.setUserOrdered(userOrdered);
691 public void exitLeaf_list_stmt(final YangParser.Leaf_list_stmtContext ctx) {
692 moduleBuilder.exitNode();
693 exitLog("leaf-list", stack.removeNodeFromPath());
697 public void enterList_stmt(final List_stmtContext ctx) {
698 final int line = ctx.getStart().getLine();
699 final String listName = stringFromNode(ctx);
700 enterLog("list", listName, line);
702 QName listQName = QName.create(moduleQName, listName);
703 SchemaPath path = stack.addNodeToPath(listQName);
705 ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, path);
706 moduleBuilder.enterNode(builder);
708 parseSchemaNodeArgs(ctx, builder);
709 parseConstraints(ctx, builder.getConstraints());
710 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
712 for (int i = 0; i < ctx.getChildCount(); ++i) {
713 ParseTree childNode = ctx.getChild(i);
714 if (childNode instanceof Ordered_by_stmtContext) {
715 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
716 final boolean userOrdered = parseUserOrdered(orderedBy);
717 builder.setUserOrdered(userOrdered);
718 } else if (childNode instanceof Key_stmtContext) {
719 List<String> key = createListKey((Key_stmtContext) childNode);
720 builder.setKeys(key);
726 public void exitList_stmt(final List_stmtContext ctx) {
727 moduleBuilder.exitNode();
728 exitLog("list", stack.removeNodeFromPath());
732 public void enterAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
733 final int line = ctx.getStart().getLine();
734 final String anyXmlName = stringFromNode(ctx);
735 enterLog("anyxml", anyXmlName, line);
737 QName anyXmlQName = QName.create(moduleQName, anyXmlName);
738 SchemaPath path = stack.addNodeToPath(anyXmlQName);
740 AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, path);
741 moduleBuilder.enterNode(builder);
743 parseSchemaNodeArgs(ctx, builder);
744 parseConstraints(ctx, builder.getConstraints());
745 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
749 public void exitAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
750 moduleBuilder.exitNode();
751 exitLog("anyxml", stack.removeNodeFromPath());
755 public void enterChoice_stmt(final YangParser.Choice_stmtContext ctx) {
756 final int line = ctx.getStart().getLine();
757 final String choiceName = stringFromNode(ctx);
758 enterLog("choice", choiceName, line);
760 QName choiceQName = QName.create(moduleQName, choiceName);
761 SchemaPath path = stack.addNodeToPath(choiceQName);
763 ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName, path);
764 moduleBuilder.enterNode(builder);
766 parseSchemaNodeArgs(ctx, builder);
767 parseConstraints(ctx, builder.getConstraints());
768 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
770 // set 'default' case
771 for (int i = 0; i < ctx.getChildCount(); i++) {
772 ParseTree child = ctx.getChild(i);
773 if (child instanceof Default_stmtContext) {
774 String defaultCase = stringFromNode(child);
775 builder.setDefaultCase(defaultCase);
782 public void exitChoice_stmt(final YangParser.Choice_stmtContext ctx) {
783 moduleBuilder.exitNode();
784 exitLog("choice", stack.removeNodeFromPath());
788 public void enterCase_stmt(final YangParser.Case_stmtContext ctx) {
789 final int line = ctx.getStart().getLine();
790 final String caseName = stringFromNode(ctx);
791 enterLog("case", caseName, line);
793 QName caseQName = QName.create(moduleQName, caseName);
794 SchemaPath path = stack.addNodeToPath(caseQName);
796 ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName, path);
797 moduleBuilder.enterNode(builder);
799 parseSchemaNodeArgs(ctx, builder);
800 parseConstraints(ctx, builder.getConstraints());
804 public void exitCase_stmt(final YangParser.Case_stmtContext ctx) {
805 moduleBuilder.exitNode();
806 exitLog("case", stack.removeNodeFromPath());
810 public void enterNotification_stmt(final YangParser.Notification_stmtContext ctx) {
811 final int line = ctx.getStart().getLine();
812 final String notificationName = stringFromNode(ctx);
813 enterLog("notification", notificationName, line);
815 QName notificationQName = QName.create(moduleQName, notificationName);
816 SchemaPath path = stack.addNodeToPath(notificationQName);
818 NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName, path);
819 moduleBuilder.enterNode(builder);
821 parseSchemaNodeArgs(ctx, builder);
825 public void exitNotification_stmt(final YangParser.Notification_stmtContext ctx) {
826 moduleBuilder.exitNode();
827 exitLog("notification", stack.removeNodeFromPath());
832 public void enterIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
833 handleUnknownNode(ctx.getStart().getLine(), ctx);
837 public void exitIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
838 moduleBuilder.exitNode();
839 exitLog("unknown-node", stack.removeNodeFromPath());
842 @Override public void enterUnknown_statement(final YangParser.Unknown_statementContext ctx) {
843 handleUnknownNode(ctx.getStart().getLine(), ctx);
846 @Override public void exitUnknown_statement(final YangParser.Unknown_statementContext ctx) {
847 moduleBuilder.exitNode();
848 exitLog("unknown-node", stack.removeNodeFromPath());
852 public void enterRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
853 final int line = ctx.getStart().getLine();
854 final String rpcName = stringFromNode(ctx);
855 enterLog("rpc", rpcName, line);
857 QName rpcQName = QName.create(moduleQName, rpcName);
858 SchemaPath path = stack.addNodeToPath(rpcQName);
860 RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName, path);
861 moduleBuilder.enterNode(rpcBuilder);
864 parseSchemaNodeArgs(ctx, rpcBuilder);
868 public void exitRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
869 moduleBuilder.exitNode();
870 exitLog("rpc", stack.removeNodeFromPath());
874 public void enterInput_stmt(final YangParser.Input_stmtContext ctx) {
875 final int line = ctx.getStart().getLine();
876 final String input = "input";
877 enterLog(input, input, line);
879 QName rpcQName = QName.create(moduleQName, input);
880 SchemaPath path = stack.addNodeToPath(rpcQName);
882 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(line, rpcQName, path);
883 moduleBuilder.enterNode(builder);
884 builder.setConfiguration(true);
886 parseSchemaNodeArgs(ctx, builder);
887 parseConstraints(ctx, builder.getConstraints());
891 public void exitInput_stmt(final YangParser.Input_stmtContext ctx) {
892 moduleBuilder.exitNode();
893 exitLog("input", stack.removeNodeFromPath());
897 public void enterOutput_stmt(final YangParser.Output_stmtContext ctx) {
898 final int line = ctx.getStart().getLine();
899 final String output = "output";
900 enterLog(output, output, line);
902 QName rpcQName = QName.create(moduleQName, output);
903 SchemaPath path = stack.addNodeToPath(rpcQName);
905 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line);
906 moduleBuilder.enterNode(builder);
907 builder.setConfiguration(true);
909 parseSchemaNodeArgs(ctx, builder);
910 parseConstraints(ctx, builder.getConstraints());
914 public void exitOutput_stmt(final YangParser.Output_stmtContext ctx) {
915 moduleBuilder.exitNode();
916 exitLog("output", stack.removeNodeFromPath());
920 public void enterFeature_stmt(final YangParser.Feature_stmtContext ctx) {
921 final int line = ctx.getStart().getLine();
922 final String featureName = stringFromNode(ctx);
923 enterLog("feature", featureName, line);
925 QName featureQName = QName.create(moduleQName, featureName);
926 SchemaPath path = stack.addNodeToPath(featureQName);
928 FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName, path);
929 moduleBuilder.enterNode(featureBuilder);
931 parseSchemaNodeArgs(ctx, featureBuilder);
935 public void exitFeature_stmt(final YangParser.Feature_stmtContext ctx) {
936 moduleBuilder.exitNode();
937 exitLog("feature", stack.removeNodeFromPath());
941 public void enterDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
942 final int line = ctx.getStart().getLine();
943 final String targetPath = stringFromNode(ctx);
944 enterLog("deviation", targetPath, line);
946 String reference = null;
947 String deviate = null;
948 DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath);
949 moduleBuilder.enterNode(builder);
951 for (int i = 0; i < ctx.getChildCount(); i++) {
952 ParseTree child = ctx.getChild(i);
953 if (child instanceof Reference_stmtContext) {
954 reference = stringFromNode(child);
955 } else if (child instanceof Deviate_not_supported_stmtContext) {
956 deviate = stringFromNode(child);
957 } else if (child instanceof Deviate_add_stmtContext) {
958 deviate = stringFromNode(child);
959 } else if (child instanceof Deviate_replace_stmtContext) {
960 deviate = stringFromNode(child);
961 } else if (child instanceof Deviate_delete_stmtContext) {
962 deviate = stringFromNode(child);
965 builder.setReference(reference);
966 builder.setDeviate(deviate);
970 public void exitDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
971 moduleBuilder.exitNode();
972 exitLog("deviation");
976 public void enterIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
977 final int line = ctx.getStart().getLine();
978 final String identityName = stringFromNode(ctx);
979 enterLog("identity", identityName, line);
981 final QName identityQName = QName.create(moduleQName, identityName);
982 SchemaPath path = stack.addNodeToPath(identityQName);
984 IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line, path);
985 moduleBuilder.enterNode(builder);
987 parseSchemaNodeArgs(ctx, builder);
989 for (int i = 0; i < ctx.getChildCount(); i++) {
990 ParseTree child = ctx.getChild(i);
991 if (child instanceof Base_stmtContext) {
992 String baseIdentityName = stringFromNode(child);
993 builder.setBaseIdentityName(baseIdentityName);
999 public void exitIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
1000 moduleBuilder.exitNode();
1001 exitLog("identity", stack.removeNodeFromPath());
1004 public ModuleBuilder getModuleBuilder() {
1005 return moduleBuilder;
1008 private static void enterLog(final String p1, final String p2, final int line) {
1009 LOG.trace("entering {} {} ({})", p1, p2, line);
1012 private static void exitLog(final String p1) {
1013 LOG.trace("exiting {}", p1);
1016 private static void exitLog(final String p1, final QName p2) {
1017 LOG.trace("exiting {} {}", p1, p2.getLocalName());
1020 private static void setLog(final String p1, final String p2) {
1021 LOG.trace("setting {} {}", p1, p2);
1024 private void handleUnknownNode(final int line, final ParseTree ctx) {
1025 final String nodeParameter = stringFromNode(ctx);
1026 enterLog("unknown-node", nodeParameter, line);
1028 final String nodeTypeStr = ctx.getChild(0).getText();
1029 final Iterator<String> splittedElement = COLON_SPLITTER.split(nodeTypeStr).iterator();
1030 final String e0 = splittedElement.next();
1031 final QName nodeType;
1032 if (splittedElement.hasNext()) {
1033 nodeType = QName.create(moduleQName.getModule(), e0, splittedElement.next());
1035 nodeType = QName.create(moduleQName, e0);
1040 if (!Strings.isNullOrEmpty(nodeParameter)) {
1041 final Iterable<String> splittedName = COLON_SPLITTER.split(nodeParameter);
1042 final Iterator<String> it = splittedName.iterator();
1044 if (Iterables.size(splittedName) == 2) {
1045 qname = QName.create(QNameModule.create(null, null), it.next(), it.next());
1047 qname = QName.create(moduleQName, it.next());
1052 } catch (IllegalArgumentException e) {
1055 SchemaPath path = stack.addNodeToPath(qname);
1057 UnknownSchemaNodeBuilderImpl builder = moduleBuilder.addUnknownSchemaNode(line, qname, path);
1058 builder.setNodeType(nodeType);
1059 builder.setNodeParameter(nodeParameter);
1062 parseSchemaNodeArgs(ctx, builder);
1063 moduleBuilder.enterNode(builder);