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.parseUserOrdered;
22 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseYinValue;
23 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.stringFromNode;
25 import com.google.common.base.Splitter;
26 import com.google.common.base.Strings;
27 import com.google.common.collect.Iterables;
29 import java.text.DateFormat;
30 import java.text.ParseException;
31 import java.text.SimpleDateFormat;
32 import java.util.ArrayList;
33 import java.util.Date;
34 import java.util.Iterator;
35 import java.util.List;
37 import java.util.TreeMap;
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.model.api.ModuleImport;
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.TypeAwareBuilder;
82 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
83 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
84 import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder;
85 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
86 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
87 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
88 import org.opendaylight.yangtools.yang.parser.builder.impl.DeviationBuilder;
89 import org.opendaylight.yangtools.yang.parser.builder.impl.FeatureBuilder;
90 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
91 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
92 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
93 import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder;
94 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
95 import org.opendaylight.yangtools.yang.parser.builder.impl.NotificationBuilder;
96 import org.opendaylight.yangtools.yang.parser.builder.impl.RefineHolderImpl;
97 import org.opendaylight.yangtools.yang.parser.builder.impl.RpcDefinitionBuilder;
98 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
99 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilderImpl;
100 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
101 import org.slf4j.Logger;
102 import org.slf4j.LoggerFactory;
105 public final class YangParserListenerImpl extends YangParserBaseListener {
106 private static final Logger LOG = LoggerFactory.getLogger(YangParserListenerImpl.class);
107 private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings();
108 private static final Splitter COLON_SPLITTER = Splitter.on(':');
109 private static final String AUGMENT_STR = "augment";
111 private final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
112 private final SchemaPathStack stack = new SchemaPathStack();
113 private final Map<String, TreeMap<Date, URI>> namespaceContext;
114 private final String sourcePath;
115 private QName moduleQName = new QName(null, new Date(0L), null, "dummy");
116 private ModuleBuilder moduleBuilder;
117 private String moduleName;
118 private int augmentOrder;
119 private String yangModelPrefix;
121 public YangParserListenerImpl(final Map<String, TreeMap<Date, URI>> namespaceContext, final String sourcePath) {
122 this.namespaceContext = namespaceContext;
123 this.sourcePath = sourcePath;
127 * Create a new instance.
129 * FIXME: the resulting type needs to be extracted, such that we can reuse
130 * the "BaseListener" aspect, which need not be exposed to the user.
131 * Maybe factor out a base class into repo.spi?
133 * @param namespaceContext
139 public static YangParserListenerImpl create(final Map<String, TreeMap<Date, URI>> namespaceContext,
140 final String sourcePath, final ParseTreeWalker walker, final ParseTree tree) {
141 final YangParserListenerImpl ret = new YangParserListenerImpl(namespaceContext, sourcePath);
142 walker.walk(ret, tree);
149 public void enterModule_stmt(final YangParser.Module_stmtContext ctx) {
150 moduleName = stringFromNode(ctx);
151 LOG.trace("entering module {}", moduleName);
152 enterLog("module", moduleName, 0);
155 moduleBuilder = new ModuleBuilder(moduleName, sourcePath);
157 String description = null;
158 String reference = null;
159 for (int i = 0; i < ctx.getChildCount(); i++) {
160 ParseTree child = ctx.getChild(i);
161 if (child instanceof Description_stmtContext) {
162 description = stringFromNode(child);
163 } else if (child instanceof Reference_stmtContext) {
164 reference = stringFromNode(child);
166 if (description != null && reference != null) {
171 moduleBuilder.setDescription(description);
172 moduleBuilder.setReference(reference);
176 public void exitModule_stmt(final YangParser.Module_stmtContext ctx) {
182 public void enterSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
183 moduleName = stringFromNode(ctx);
184 LOG.trace("entering submodule {}", moduleName);
185 enterLog("submodule", moduleName, 0);
188 moduleBuilder = new ModuleBuilder(moduleName, true, sourcePath);
190 String description = null;
191 String reference = null;
192 for (int i = 0; i < ctx.getChildCount(); i++) {
193 ParseTree child = ctx.getChild(i);
194 if (child instanceof Description_stmtContext) {
195 description = stringFromNode(child);
196 } else if (child instanceof Reference_stmtContext) {
197 reference = stringFromNode(child);
199 if (description != null && reference != null) {
204 moduleBuilder.setDescription(description);
205 moduleBuilder.setReference(reference);
209 public void exitSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
210 exitLog("submodule");
215 public void enterBelongs_to_stmt(final YangParser.Belongs_to_stmtContext ctx) {
216 final String belongsTo = stringFromNode(ctx);
217 TreeMap<Date, URI> context = namespaceContext.get(belongsTo);
218 Map.Entry<Date, URI> entry = context.firstEntry();
220 // Submodule will contain namespace and revision from module to which it
221 // belongs. If there are multiple modules with same name and different
222 // revisions, it will has revision from the newest one.
223 this.moduleQName = QName.create(entry.getValue(), entry.getKey(), moduleQName.getLocalName());
224 moduleBuilder.setQNameModule(moduleQName.getModule());
225 moduleBuilder.setBelongsTo(belongsTo);
229 public void enterModule_header_stmts(final Module_header_stmtsContext ctx) {
230 enterLog("module_header", "", ctx.getStart().getLine());
231 String yangVersion = null;
232 for (int i = 0; i < ctx.getChildCount(); ++i) {
233 final ParseTree treeNode = ctx.getChild(i);
234 if (treeNode instanceof Namespace_stmtContext) {
235 final String namespaceStr = stringFromNode(treeNode);
236 final URI namespace = URI.create(namespaceStr);
237 this.moduleQName = QName.create(namespace, moduleQName.getRevision(), moduleQName.getLocalName());
238 moduleBuilder.setQNameModule(moduleQName.getModule());
239 setLog("namespace", namespaceStr);
240 } else if (treeNode instanceof Prefix_stmtContext) {
241 yangModelPrefix = stringFromNode(treeNode);
242 this.moduleQName = QName.create(moduleQName.getModule(), yangModelPrefix, moduleQName.getLocalName());
243 moduleBuilder.setPrefix(yangModelPrefix);
244 setLog("prefix", yangModelPrefix);
245 } else if (treeNode instanceof Yang_version_stmtContext) {
246 yangVersion = stringFromNode(treeNode);
247 setLog("yang-version", yangVersion);
251 if (yangVersion == null) {
254 moduleBuilder.setYangVersion(yangVersion);
258 public void exitModule_header_stmts(final Module_header_stmtsContext ctx) {
259 exitLog("module_header");
263 public void enterMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
264 enterLog("meta_stmt", "", ctx.getStart().getLine());
265 for (int i = 0; i < ctx.getChildCount(); i++) {
266 ParseTree child = ctx.getChild(i);
267 if (child instanceof Organization_stmtContext) {
268 final String organization = stringFromNode(child);
269 moduleBuilder.setOrganization(organization);
270 setLog("organization", organization);
271 } else if (child instanceof Contact_stmtContext) {
272 final String contact = stringFromNode(child);
273 moduleBuilder.setContact(contact);
274 setLog("contact", contact);
275 } else if (child instanceof Description_stmtContext) {
276 final String description = stringFromNode(child);
277 moduleBuilder.setDescription(description);
278 setLog("description", description);
279 } else if (child instanceof Reference_stmtContext) {
280 final String reference = stringFromNode(child);
281 moduleBuilder.setReference(reference);
282 setLog("reference", reference);
288 public void exitMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
289 exitLog("meta_stmt");
293 public void enterRevision_stmts(final Revision_stmtsContext ctx) {
294 enterLog("revisions", "", ctx.getStart().getLine());
295 for (int i = 0; i < ctx.getChildCount(); ++i) {
296 final ParseTree treeNode = ctx.getChild(i);
297 if (treeNode instanceof Revision_stmtContext) {
298 updateRevisionForRevisionStatement(treeNode);
304 public void exitRevision_stmts(final Revision_stmtsContext ctx) {
305 exitLog("revisions");
308 private void updateRevisionForRevisionStatement(final ParseTree treeNode) {
309 final String revisionDateStr = stringFromNode(treeNode);
311 final Date revisionDate = SIMPLE_DATE_FORMAT.parse(revisionDateStr);
312 if ((revisionDate != null) && (this.moduleQName.getRevision().compareTo(revisionDate) < 0)) {
313 this.moduleQName = QName.create(moduleQName.getNamespace(), revisionDate, moduleQName.getLocalName());
314 moduleBuilder.setQNameModule(moduleQName.getModule());
315 setLog("revision", revisionDate.toString());
316 for (int i = 0; i < treeNode.getChildCount(); ++i) {
317 ParseTree child = treeNode.getChild(i);
318 if (child instanceof Reference_stmtContext) {
319 moduleBuilder.setReference(stringFromNode(child));
323 } catch (ParseException e) {
324 LOG.warn("Failed to parse revision string: {}", revisionDateStr, e);
329 public void enterImport_stmt(final Import_stmtContext ctx) {
330 final int line = ctx.getStart().getLine();
331 final String importName = stringFromNode(ctx);
332 enterLog("import", importName, line);
334 String importPrefix = null;
335 Date importRevision = null;
337 for (int i = 0; i < ctx.getChildCount(); ++i) {
338 final ParseTree treeNode = ctx.getChild(i);
339 if (treeNode instanceof Prefix_stmtContext) {
340 importPrefix = stringFromNode(treeNode);
342 if (treeNode instanceof Revision_date_stmtContext) {
343 String importRevisionStr = stringFromNode(treeNode);
345 importRevision = SIMPLE_DATE_FORMAT.parse(importRevisionStr);
346 } catch (ParseException e) {
347 LOG.warn("Failed to parse import revision-date at line {}: {}", line, importRevisionStr, e);
351 moduleBuilder.addModuleImport(importName, importRevision, importPrefix);
352 LOG.trace("setting import ({}; {}; {})", importName, importRevision, importPrefix);
356 public void exitImport_stmt(final Import_stmtContext ctx) {
361 public void enterInclude_stmt(YangParser.Include_stmtContext ctx) {
362 final int line = ctx.getStart().getLine();
363 final String includeName = stringFromNode(ctx);
364 enterLog("import", includeName, line);
366 Date includeRevision = null;
367 for (int i = 0; i < ctx.getChildCount(); i++) {
368 ParseTree treeNode = ctx.getChild(i);
369 if (treeNode instanceof Revision_date_stmtContext) {
370 String importRevisionStr = stringFromNode(treeNode);
372 includeRevision = SIMPLE_DATE_FORMAT.parse(importRevisionStr);
373 } catch (ParseException e) {
374 LOG.warn("Failed to parse import revision-date at line {}: {}", line, importRevisionStr, e);
378 moduleBuilder.addInclude(includeName, includeRevision);
381 @Override public void exitInclude_stmt(YangParser.Include_stmtContext ctx) {
386 public void enterAugment_stmt(final YangParser.Augment_stmtContext ctx) {
387 final int line = ctx.getStart().getLine();
388 final String augmentPath = stringFromNode(ctx);
389 enterLog(AUGMENT_STR, augmentPath, line);
392 SchemaPath targetPath = parseXPathString(augmentPath, line);
393 AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, targetPath, augmentOrder++);
395 for (int i = 0; i < ctx.getChildCount(); i++) {
396 ParseTree child = ctx.getChild(i);
397 if (child instanceof Description_stmtContext) {
398 builder.setDescription(stringFromNode(child));
399 } else if (child instanceof Reference_stmtContext) {
400 builder.setReference(stringFromNode(child));
401 } else if (child instanceof Status_stmtContext) {
402 builder.setStatus(parseStatus((Status_stmtContext) child));
403 } else if (child instanceof When_stmtContext) {
404 builder.addWhenCondition(stringFromNode(child));
408 moduleBuilder.enterNode(builder);
412 public void exitAugment_stmt(final YangParser.Augment_stmtContext ctx) {
413 moduleBuilder.exitNode();
414 exitLog(AUGMENT_STR);
419 public void enterExtension_stmt(final YangParser.Extension_stmtContext ctx) {
420 final int line = ctx.getStart().getLine();
421 final String extName = stringFromNode(ctx);
422 enterLog("extension", extName, line);
423 QName qname = QName.create(moduleQName, extName);
424 SchemaPath path = stack.addNodeToPath(qname);
426 ExtensionBuilder builder = moduleBuilder.addExtension(qname, line, path);
427 parseSchemaNodeArgs(ctx, builder);
429 String argument = null;
431 for (int i = 0; i < ctx.getChildCount(); i++) {
432 ParseTree child = ctx.getChild(i);
433 if (child instanceof Argument_stmtContext) {
434 argument = stringFromNode(child);
435 yin = parseYinValue((Argument_stmtContext) child);
439 builder.setArgument(argument);
440 builder.setYinElement(yin);
442 moduleBuilder.enterNode(builder);
446 public void exitExtension_stmt(final YangParser.Extension_stmtContext ctx) {
447 moduleBuilder.exitNode();
448 exitLog("extension", stack.removeNodeFromPath());
452 public void enterTypedef_stmt(final YangParser.Typedef_stmtContext ctx) {
453 final int line = ctx.getStart().getLine();
454 final String typedefName = stringFromNode(ctx);
455 enterLog("typedef", typedefName, line);
456 QName typedefQName = QName.create(moduleQName, typedefName);
457 SchemaPath path = stack.addNodeToPath(typedefQName);
459 TypeDefinitionBuilder builder = moduleBuilder.addTypedef(line, typedefQName, path);
460 parseSchemaNodeArgs(ctx, builder);
461 builder.setUnits(parseUnits(ctx));
462 builder.setDefaultValue(parseDefault(ctx));
464 moduleBuilder.enterNode(builder);
468 public void exitTypedef_stmt(final YangParser.Typedef_stmtContext ctx) {
469 moduleBuilder.exitNode();
470 exitLog("typedef", stack.removeNodeFromPath());
474 public void enterType_stmt(final YangParser.Type_stmtContext ctx) {
475 final int line = ctx.getStart().getLine();
476 final String typeName = stringFromNode(ctx);
477 enterLog("type", typeName, line);
479 final QName typeQName = parseQName(typeName, line);
481 TypeDefinition<?> type;
482 Type_body_stmtsContext typeBody = null;
483 for (int i = 0; i < ctx.getChildCount(); i++) {
484 if (ctx.getChild(i) instanceof Type_body_stmtsContext) {
485 typeBody = (Type_body_stmtsContext) ctx.getChild(i);
490 // if this is base yang type...
491 if (BaseTypes.isYangBuildInType(typeName)) {
492 if (typeBody == null) {
493 // check for types which must have body
494 checkMissingBody(typeName, moduleName, line);
495 // if there are no constraints, just grab default base yang type
496 type = BaseTypes.defaultBaseTypeFor(typeName).orNull();
497 stack.addNodeToPath(type.getQName());
498 moduleBuilder.setType(type);
503 qname = BaseTypes.UNION_QNAME;
504 stack.addNodeToPath(qname);
505 UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, moduleQName.getModule());
506 Builder parent = moduleBuilder.getActualNode();
507 unionBuilder.setParent(parent);
508 moduleBuilder.enterNode(unionBuilder);
511 qname = BaseTypes.IDENTITYREF_QNAME;
512 SchemaPath path = stack.addNodeToPath(qname);
513 moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody));
516 type = parseTypeWithBody(typeName, typeBody, stack.currentSchemaPath(), moduleQName,
517 moduleBuilder.getActualNode());
518 moduleBuilder.setType(type);
519 stack.addNodeToPath(type.getQName());
523 TypeAwareBuilder parent = (TypeAwareBuilder) moduleBuilder.getActualNode();
524 if (typeBody == null) {
525 parent.setTypeQName(typeQName);
526 moduleBuilder.markActualNodeDirty();
528 ParserListenerUtils.parseUnknownTypeWithBody(typeBody, parent, typeQName, moduleBuilder,
529 moduleQName, stack.currentSchemaPath());
531 stack.addNodeToPath(QName.create(moduleQName.getModule(), typeQName.getLocalName()));
535 private QName parseQName(final String qnameString, final int line) {
537 if (qnameString.indexOf(':') == -1) {
538 qname = QName.create(moduleQName.getNamespace(), moduleQName.getRevision(), qnameString);
540 final Iterator<String> split = COLON_SPLITTER.split(qnameString).iterator();
541 final String prefix = split.next();
542 final String name = split.next();
543 if (prefix.equals(moduleBuilder.getPrefix())) {
544 qname = QName.create(moduleQName.getNamespace(), moduleQName.getRevision(), name);
546 ModuleImport imp = moduleBuilder.getImport(prefix);
548 LOG.warn("Error in module {} at line {}: No import found with prefix {}", moduleName, line, prefix);
549 return QName.create(name);
551 Date revision = imp.getRevision();
552 TreeMap<Date, URI> namespaces = namespaceContext.get(imp.getModuleName());
554 if (revision == null) {
555 if (namespaces == null) {
556 throw new YangParseException(moduleName, line, "imported module " + imp.getModuleName()
557 + " with prefix " + imp.getPrefix() + " not found.");
559 revision = namespaces.lastEntry().getKey();
560 namespace = namespaces.lastEntry().getValue();
562 namespace = namespaces.get(revision);
564 qname = QName.create(namespace, revision, name);
571 public void exitType_stmt(final YangParser.Type_stmtContext ctx) {
572 final String typeName = stringFromNode(ctx);
573 if ("union".equals(typeName)) {
574 moduleBuilder.exitNode();
576 exitLog("type", stack.removeNodeFromPath());
580 public void enterGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
581 final int line = ctx.getStart().getLine();
582 final String groupName = stringFromNode(ctx);
583 enterLog("grouping", groupName, line);
584 QName groupQName = QName.create(moduleQName, groupName);
585 SchemaPath path = stack.addNodeToPath(groupQName);
587 GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName, path);
588 parseSchemaNodeArgs(ctx, builder);
590 moduleBuilder.enterNode(builder);
594 public void exitGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
595 moduleBuilder.exitNode();
596 exitLog("grouping", stack.removeNodeFromPath());
600 public void enterContainer_stmt(final Container_stmtContext ctx) {
601 final int line = ctx.getStart().getLine();
602 final String containerName = stringFromNode(ctx);
603 enterLog("container", containerName, line);
605 QName containerQName = QName.create(moduleQName, containerName);
606 SchemaPath path = stack.addNodeToPath(containerQName);
608 ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(line, containerQName, path);
609 parseSchemaNodeArgs(ctx, builder);
610 parseConstraints(ctx, builder.getConstraints());
611 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
613 for (int i = 0; i < ctx.getChildCount(); ++i) {
614 final ParseTree childNode = ctx.getChild(i);
615 if (childNode instanceof Presence_stmtContext) {
616 builder.setPresence(true);
621 moduleBuilder.enterNode(builder);
625 public void exitContainer_stmt(final Container_stmtContext ctx) {
626 moduleBuilder.exitNode();
627 exitLog("container", stack.removeNodeFromPath());
631 public void enterLeaf_stmt(final Leaf_stmtContext ctx) {
632 final int line = ctx.getStart().getLine();
633 final String leafName = stringFromNode(ctx);
634 enterLog("leaf", leafName, line);
636 QName leafQName = QName.create(moduleQName, leafName);
637 SchemaPath path = stack.addNodeToPath(leafQName);
639 LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, path);
640 parseSchemaNodeArgs(ctx, builder);
641 parseConstraints(ctx, builder.getConstraints());
642 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
644 String defaultStr = null;
645 String unitsStr = null;
646 for (int i = 0; i < ctx.getChildCount(); i++) {
647 ParseTree child = ctx.getChild(i);
648 if (child instanceof Default_stmtContext) {
649 defaultStr = stringFromNode(child);
650 } else if (child instanceof Units_stmtContext) {
651 unitsStr = stringFromNode(child);
654 builder.setDefaultStr(defaultStr);
655 builder.setUnits(unitsStr);
657 moduleBuilder.enterNode(builder);
661 public void exitLeaf_stmt(final YangParser.Leaf_stmtContext ctx) {
662 moduleBuilder.exitNode();
663 exitLog("leaf", stack.removeNodeFromPath());
667 public void enterUses_stmt(final YangParser.Uses_stmtContext ctx) {
668 final int line = ctx.getStart().getLine();
669 final String groupingPathStr = stringFromNode(ctx);
670 final SchemaPath groupingPath = parseXPathString(groupingPathStr, line);
671 enterLog("uses", groupingPathStr, line);
673 UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPath);
675 moduleBuilder.enterNode(builder);
679 public void exitUses_stmt(final YangParser.Uses_stmtContext ctx) {
680 moduleBuilder.exitNode();
685 public void enterUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
686 final int line = ctx.getStart().getLine();
687 final String augmentPath = stringFromNode(ctx);
688 enterLog(AUGMENT_STR, augmentPath, line);
691 SchemaPath targetPath = parseXPathString(augmentPath, line);
692 AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, targetPath, augmentOrder++);
694 for (int i = 0; i < ctx.getChildCount(); i++) {
695 ParseTree child = ctx.getChild(i);
696 if (child instanceof Description_stmtContext) {
697 builder.setDescription(stringFromNode(child));
698 } else if (child instanceof Reference_stmtContext) {
699 builder.setReference(stringFromNode(child));
700 } else if (child instanceof Status_stmtContext) {
701 builder.setStatus(parseStatus((Status_stmtContext) child));
702 } else if (child instanceof When_stmtContext) {
703 builder.addWhenCondition(stringFromNode(child));
707 moduleBuilder.enterNode(builder);
711 public void exitUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
712 moduleBuilder.exitNode();
713 exitLog(AUGMENT_STR);
718 public void enterRefine_stmt(final YangParser.Refine_stmtContext ctx) {
719 final String refineString = stringFromNode(ctx);
720 enterLog("refine", refineString, ctx.getStart().getLine());
722 RefineHolderImpl refine = parseRefine(ctx, moduleName);
723 moduleBuilder.addRefine(refine);
724 moduleBuilder.enterNode(refine);
728 public void exitRefine_stmt(final YangParser.Refine_stmtContext ctx) {
729 moduleBuilder.exitNode();
734 public void enterLeaf_list_stmt(final Leaf_list_stmtContext ctx) {
735 final int line = ctx.getStart().getLine();
736 final String leafListName = stringFromNode(ctx);
737 enterLog("leaf-list", leafListName, line);
738 QName leafListQName = QName.create(moduleQName, leafListName);
739 SchemaPath path = stack.addNodeToPath(leafListQName);
741 LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, path);
742 moduleBuilder.enterNode(builder);
744 parseSchemaNodeArgs(ctx, builder);
745 parseConstraints(ctx, builder.getConstraints());
746 builder.setConfiguration(getConfig(ctx, builder, moduleName, ctx.getStart().getLine()));
748 for (int i = 0; i < ctx.getChildCount(); ++i) {
749 final ParseTree childNode = ctx.getChild(i);
750 if (childNode instanceof Ordered_by_stmtContext) {
751 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
752 final boolean userOrdered = parseUserOrdered(orderedBy);
753 builder.setUserOrdered(userOrdered);
760 public void exitLeaf_list_stmt(final YangParser.Leaf_list_stmtContext ctx) {
761 moduleBuilder.exitNode();
762 exitLog("leaf-list", stack.removeNodeFromPath());
766 public void enterList_stmt(final List_stmtContext ctx) {
767 final int line = ctx.getStart().getLine();
768 final String listName = stringFromNode(ctx);
769 enterLog("list", listName, line);
771 QName listQName = QName.create(moduleQName, listName);
772 SchemaPath path = stack.addNodeToPath(listQName);
774 ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, path);
775 moduleBuilder.enterNode(builder);
777 parseSchemaNodeArgs(ctx, builder);
778 parseConstraints(ctx, builder.getConstraints());
779 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
781 for (int i = 0; i < ctx.getChildCount(); ++i) {
782 ParseTree childNode = ctx.getChild(i);
783 if (childNode instanceof Ordered_by_stmtContext) {
784 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
785 final boolean userOrdered = parseUserOrdered(orderedBy);
786 builder.setUserOrdered(userOrdered);
787 } else if (childNode instanceof Key_stmtContext) {
788 List<String> key = createListKey((Key_stmtContext) childNode);
789 builder.setKeys(key);
795 public void exitList_stmt(final List_stmtContext ctx) {
796 moduleBuilder.exitNode();
797 exitLog("list", stack.removeNodeFromPath());
801 public void enterAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
802 final int line = ctx.getStart().getLine();
803 final String anyXmlName = stringFromNode(ctx);
804 enterLog("anyxml", anyXmlName, line);
806 QName anyXmlQName = QName.create(moduleQName, anyXmlName);
807 SchemaPath path = stack.addNodeToPath(anyXmlQName);
809 AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, path);
810 moduleBuilder.enterNode(builder);
812 parseSchemaNodeArgs(ctx, builder);
813 parseConstraints(ctx, builder.getConstraints());
814 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
818 public void exitAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
819 moduleBuilder.exitNode();
820 exitLog("anyxml", stack.removeNodeFromPath());
824 public void enterChoice_stmt(final YangParser.Choice_stmtContext ctx) {
825 final int line = ctx.getStart().getLine();
826 final String choiceName = stringFromNode(ctx);
827 enterLog("choice", choiceName, line);
829 QName choiceQName = QName.create(moduleQName, choiceName);
830 SchemaPath path = stack.addNodeToPath(choiceQName);
832 ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName, path);
833 moduleBuilder.enterNode(builder);
835 parseSchemaNodeArgs(ctx, builder);
836 parseConstraints(ctx, builder.getConstraints());
837 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
839 // set 'default' case
840 for (int i = 0; i < ctx.getChildCount(); i++) {
841 ParseTree child = ctx.getChild(i);
842 if (child instanceof Default_stmtContext) {
843 String defaultCase = stringFromNode(child);
844 builder.setDefaultCase(defaultCase);
851 public void exitChoice_stmt(final YangParser.Choice_stmtContext ctx) {
852 moduleBuilder.exitNode();
853 exitLog("choice", stack.removeNodeFromPath());
857 public void enterCase_stmt(final YangParser.Case_stmtContext ctx) {
858 final int line = ctx.getStart().getLine();
859 final String caseName = stringFromNode(ctx);
860 enterLog("case", caseName, line);
862 QName caseQName = QName.create(moduleQName, caseName);
863 SchemaPath path = stack.addNodeToPath(caseQName);
865 ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName, path);
866 moduleBuilder.enterNode(builder);
868 parseSchemaNodeArgs(ctx, builder);
869 parseConstraints(ctx, builder.getConstraints());
873 public void exitCase_stmt(final YangParser.Case_stmtContext ctx) {
874 moduleBuilder.exitNode();
875 exitLog("case", stack.removeNodeFromPath());
879 public void enterNotification_stmt(final YangParser.Notification_stmtContext ctx) {
880 final int line = ctx.getStart().getLine();
881 final String notificationName = stringFromNode(ctx);
882 enterLog("notification", notificationName, line);
884 QName notificationQName = QName.create(moduleQName, notificationName);
885 SchemaPath path = stack.addNodeToPath(notificationQName);
887 NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName, path);
888 moduleBuilder.enterNode(builder);
890 parseSchemaNodeArgs(ctx, builder);
894 public void exitNotification_stmt(final YangParser.Notification_stmtContext ctx) {
895 moduleBuilder.exitNode();
896 exitLog("notification", stack.removeNodeFromPath());
901 public void enterIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
902 handleUnknownNode(ctx.getStart().getLine(), ctx);
906 public void exitIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
907 moduleBuilder.exitNode();
908 exitLog("unknown-node", stack.removeNodeFromPath());
911 @Override public void enterUnknown_statement(final YangParser.Unknown_statementContext ctx) {
912 handleUnknownNode(ctx.getStart().getLine(), ctx);
915 @Override public void exitUnknown_statement(final YangParser.Unknown_statementContext ctx) {
916 moduleBuilder.exitNode();
917 exitLog("unknown-node", stack.removeNodeFromPath());
921 public void enterRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
922 final int line = ctx.getStart().getLine();
923 final String rpcName = stringFromNode(ctx);
924 enterLog("rpc", rpcName, line);
926 QName rpcQName = QName.create(moduleQName, rpcName);
927 SchemaPath path = stack.addNodeToPath(rpcQName);
929 RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName, path);
930 moduleBuilder.enterNode(rpcBuilder);
933 parseSchemaNodeArgs(ctx, rpcBuilder);
937 public void exitRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
938 moduleBuilder.exitNode();
939 exitLog("rpc", stack.removeNodeFromPath());
943 public void enterInput_stmt(final YangParser.Input_stmtContext ctx) {
944 final int line = ctx.getStart().getLine();
945 final String input = "input";
946 enterLog(input, input, line);
948 QName rpcQName = QName.create(moduleQName, input);
949 SchemaPath path = stack.addNodeToPath(rpcQName);
951 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(line, rpcQName, path);
952 moduleBuilder.enterNode(builder);
953 builder.setConfiguration(true);
955 parseSchemaNodeArgs(ctx, builder);
956 parseConstraints(ctx, builder.getConstraints());
960 public void exitInput_stmt(final YangParser.Input_stmtContext ctx) {
961 moduleBuilder.exitNode();
962 exitLog("input", stack.removeNodeFromPath());
966 public void enterOutput_stmt(final YangParser.Output_stmtContext ctx) {
967 final int line = ctx.getStart().getLine();
968 final String output = "output";
969 enterLog(output, output, line);
971 QName rpcQName = QName.create(moduleQName, output);
972 SchemaPath path = stack.addNodeToPath(rpcQName);
974 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line);
975 moduleBuilder.enterNode(builder);
976 builder.setConfiguration(true);
978 parseSchemaNodeArgs(ctx, builder);
979 parseConstraints(ctx, builder.getConstraints());
983 public void exitOutput_stmt(final YangParser.Output_stmtContext ctx) {
984 moduleBuilder.exitNode();
985 exitLog("output", stack.removeNodeFromPath());
989 public void enterFeature_stmt(final YangParser.Feature_stmtContext ctx) {
990 final int line = ctx.getStart().getLine();
991 final String featureName = stringFromNode(ctx);
992 enterLog("feature", featureName, line);
994 QName featureQName = QName.create(moduleQName, featureName);
995 SchemaPath path = stack.addNodeToPath(featureQName);
997 FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName, path);
998 moduleBuilder.enterNode(featureBuilder);
1000 parseSchemaNodeArgs(ctx, featureBuilder);
1004 public void exitFeature_stmt(final YangParser.Feature_stmtContext ctx) {
1005 moduleBuilder.exitNode();
1006 exitLog("feature", stack.removeNodeFromPath());
1010 public void enterDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
1011 final int line = ctx.getStart().getLine();
1012 final String targetPathStr = stringFromNode(ctx);
1013 if (!targetPathStr.startsWith("/")) {
1014 throw new YangParseException(moduleName, line,
1015 "Deviation argument string must be an absolute schema node identifier.");
1017 enterLog("deviation", targetPathStr, line);
1019 String reference = null;
1020 String deviate = null;
1022 SchemaPath targetPath = parseXPathString(targetPathStr, line);
1023 DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath);
1024 moduleBuilder.enterNode(builder);
1026 for (int i = 0; i < ctx.getChildCount(); i++) {
1027 ParseTree child = ctx.getChild(i);
1028 if (child instanceof Reference_stmtContext) {
1029 reference = stringFromNode(child);
1030 } else if (child instanceof Deviate_not_supported_stmtContext) {
1031 deviate = stringFromNode(child);
1032 } else if (child instanceof Deviate_add_stmtContext) {
1033 deviate = stringFromNode(child);
1034 } else if (child instanceof Deviate_replace_stmtContext) {
1035 deviate = stringFromNode(child);
1036 } else if (child instanceof Deviate_delete_stmtContext) {
1037 deviate = stringFromNode(child);
1040 builder.setReference(reference);
1041 builder.setDeviate(deviate);
1044 public SchemaPath parseXPathString(final String xpathString, final int line) {
1045 final boolean absolute = !xpathString.isEmpty() && xpathString.charAt(0) == '/';
1047 final List<QName> path = new ArrayList<>();
1048 for (String pathElement : SLASH_SPLITTER.split(xpathString)) {
1049 final Iterator<String> it = COLON_SPLITTER.split(pathElement).iterator();
1050 final String s = it.next();
1052 path.add(parseQName(pathElement, line));
1054 path.add(QName.create(moduleQName, s));
1057 return SchemaPath.create(path, absolute);
1061 public void exitDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
1062 moduleBuilder.exitNode();
1063 exitLog("deviation");
1067 public void enterIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
1068 final int line = ctx.getStart().getLine();
1069 final String identityName = stringFromNode(ctx);
1070 enterLog("identity", identityName, line);
1072 final QName identityQName = QName.create(moduleQName, identityName);
1073 SchemaPath path = stack.addNodeToPath(identityQName);
1075 IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line, path);
1076 moduleBuilder.enterNode(builder);
1078 parseSchemaNodeArgs(ctx, builder);
1080 for (int i = 0; i < ctx.getChildCount(); i++) {
1081 ParseTree child = ctx.getChild(i);
1082 if (child instanceof Base_stmtContext) {
1083 String baseIdentityName = stringFromNode(child);
1084 builder.setBaseIdentityName(baseIdentityName);
1090 public void exitIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
1091 moduleBuilder.exitNode();
1092 exitLog("identity", stack.removeNodeFromPath());
1095 public ModuleBuilder getModuleBuilder() {
1096 return moduleBuilder;
1099 private static void enterLog(final String p1, final String p2, final int line) {
1100 LOG.trace("entering {} {} ({})", p1, p2, line);
1103 private static void exitLog(final String p1) {
1104 LOG.trace("exiting {}", p1);
1107 private static void exitLog(final String p1, final QName p2) {
1108 LOG.trace("exiting {} {}", p1, p2.getLocalName());
1111 private static void setLog(final String p1, final String p2) {
1112 LOG.trace("setting {} {}", p1, p2);
1115 private void handleUnknownNode(final int line, final ParseTree ctx) {
1116 final String nodeParameter = stringFromNode(ctx);
1117 enterLog("unknown-node", nodeParameter, line);
1119 final String nodeTypeStr = ctx.getChild(0).getText();
1120 QName nodeType = parseQName(nodeTypeStr, line);
1124 if (Strings.isNullOrEmpty(nodeParameter)) {
1127 final Iterable<String> splittedName = COLON_SPLITTER.split(nodeParameter);
1128 final Iterator<String> it = splittedName.iterator();
1129 if (Iterables.size(splittedName) == 2) {
1130 qname = parseQName(nodeParameter, line);
1132 qname = QName.create(moduleQName, it.next());
1135 } catch (IllegalArgumentException e) {
1139 SchemaPath path = stack.addNodeToPath(qname);
1141 UnknownSchemaNodeBuilderImpl builder = moduleBuilder.addUnknownSchemaNode(line, qname, path);
1142 builder.setNodeType(nodeType);
1143 builder.setNodeParameter(nodeParameter);
1145 parseSchemaNodeArgs(ctx, builder);
1146 moduleBuilder.enterNode(builder);