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()));
536 * Method transforms string representation of yang element (i.e. leaf name, container name etc.) into QName.
537 * The namespace of QName is assigned from parent module same as revision date of module. If String qname parameter
538 * contains ":" the string is evaluated as prefix:name of element. In this case method will look into import map
539 * and extract correct ModuleImport. If such import is not present in import map the method will throw {@link YangParseException}
541 * If ModuleImport is present but the value of namespace in ModuleImport is <code>null</code> the method wil throw {@link YangParseException}
543 * @param qnameString QName value as String
544 * @param line line in Yang model document where QName occur.
545 * @return transformed string qname parameter as QName structure.
547 * @throws YangParseException
549 private QName parseQName(final String qnameString, final int line) {
551 if (qnameString.indexOf(':') == -1) {
552 qname = QName.create(moduleQName.getNamespace(), moduleQName.getRevision(), qnameString);
554 final Iterator<String> split = COLON_SPLITTER.split(qnameString).iterator();
555 final String prefix = split.next();
556 final String name = split.next();
557 if (prefix.equals(moduleBuilder.getPrefix())) {
558 qname = QName.create(moduleQName.getNamespace(), moduleQName.getRevision(), name);
560 ModuleImport imp = moduleBuilder.getImport(prefix);
562 LOG.debug("Error in module {} at line {}: No import found with prefix {}", moduleName, line, prefix);
563 throw new YangParseException(moduleName, line, "Error in module " + moduleName
564 + " No import found with prefix " + prefix + " not found.");
566 Date revision = imp.getRevision();
567 TreeMap<Date, URI> namespaces = namespaceContext.get(imp.getModuleName());
569 if (revision == null) {
570 if (namespaces == null) {
571 throw new YangParseException(moduleName, line, "imported module " + imp.getModuleName()
572 + " with prefix " + imp.getPrefix() + " not found.");
574 revision = namespaces.lastEntry().getKey();
575 namespace = namespaces.lastEntry().getValue();
577 namespace = namespaces.get(revision);
579 qname = QName.create(namespace, revision, name);
586 public void exitType_stmt(final YangParser.Type_stmtContext ctx) {
587 final String typeName = stringFromNode(ctx);
588 if ("union".equals(typeName)) {
589 moduleBuilder.exitNode();
591 exitLog("type", stack.removeNodeFromPath());
595 public void enterGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
596 final int line = ctx.getStart().getLine();
597 final String groupName = stringFromNode(ctx);
598 enterLog("grouping", groupName, line);
599 QName groupQName = QName.create(moduleQName, groupName);
600 SchemaPath path = stack.addNodeToPath(groupQName);
602 GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName, path);
603 parseSchemaNodeArgs(ctx, builder);
605 moduleBuilder.enterNode(builder);
609 public void exitGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
610 moduleBuilder.exitNode();
611 exitLog("grouping", stack.removeNodeFromPath());
615 public void enterContainer_stmt(final Container_stmtContext ctx) {
616 final int line = ctx.getStart().getLine();
617 final String containerName = stringFromNode(ctx);
618 enterLog("container", containerName, line);
620 QName containerQName = QName.create(moduleQName, containerName);
621 SchemaPath path = stack.addNodeToPath(containerQName);
623 ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(line, containerQName, path);
624 parseSchemaNodeArgs(ctx, builder);
625 parseConstraints(ctx, builder.getConstraints());
626 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
628 for (int i = 0; i < ctx.getChildCount(); ++i) {
629 final ParseTree childNode = ctx.getChild(i);
630 if (childNode instanceof Presence_stmtContext) {
631 builder.setPresence(true);
636 moduleBuilder.enterNode(builder);
640 public void exitContainer_stmt(final Container_stmtContext ctx) {
641 moduleBuilder.exitNode();
642 exitLog("container", stack.removeNodeFromPath());
646 public void enterLeaf_stmt(final Leaf_stmtContext ctx) {
647 final int line = ctx.getStart().getLine();
648 final String leafName = stringFromNode(ctx);
649 enterLog("leaf", leafName, line);
651 QName leafQName = QName.create(moduleQName, leafName);
652 SchemaPath path = stack.addNodeToPath(leafQName);
654 LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, path);
655 parseSchemaNodeArgs(ctx, builder);
656 parseConstraints(ctx, builder.getConstraints());
657 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
659 String defaultStr = null;
660 String unitsStr = null;
661 for (int i = 0; i < ctx.getChildCount(); i++) {
662 ParseTree child = ctx.getChild(i);
663 if (child instanceof Default_stmtContext) {
664 defaultStr = stringFromNode(child);
665 } else if (child instanceof Units_stmtContext) {
666 unitsStr = stringFromNode(child);
669 builder.setDefaultStr(defaultStr);
670 builder.setUnits(unitsStr);
672 moduleBuilder.enterNode(builder);
676 public void exitLeaf_stmt(final YangParser.Leaf_stmtContext ctx) {
677 moduleBuilder.exitNode();
678 exitLog("leaf", stack.removeNodeFromPath());
682 public void enterUses_stmt(final YangParser.Uses_stmtContext ctx) {
683 final int line = ctx.getStart().getLine();
684 final String groupingPathStr = stringFromNode(ctx);
685 final SchemaPath groupingPath = parseXPathString(groupingPathStr, line);
686 enterLog("uses", groupingPathStr, line);
688 UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPath);
690 moduleBuilder.enterNode(builder);
694 public void exitUses_stmt(final YangParser.Uses_stmtContext ctx) {
695 moduleBuilder.exitNode();
700 public void enterUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
701 final int line = ctx.getStart().getLine();
702 final String augmentPath = stringFromNode(ctx);
703 enterLog(AUGMENT_STR, augmentPath, line);
706 SchemaPath targetPath = parseXPathString(augmentPath, line);
707 AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, targetPath, augmentOrder++);
709 for (int i = 0; i < ctx.getChildCount(); i++) {
710 ParseTree child = ctx.getChild(i);
711 if (child instanceof Description_stmtContext) {
712 builder.setDescription(stringFromNode(child));
713 } else if (child instanceof Reference_stmtContext) {
714 builder.setReference(stringFromNode(child));
715 } else if (child instanceof Status_stmtContext) {
716 builder.setStatus(parseStatus((Status_stmtContext) child));
717 } else if (child instanceof When_stmtContext) {
718 builder.addWhenCondition(stringFromNode(child));
722 moduleBuilder.enterNode(builder);
726 public void exitUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
727 moduleBuilder.exitNode();
728 exitLog(AUGMENT_STR);
733 public void enterRefine_stmt(final YangParser.Refine_stmtContext ctx) {
734 final String refineString = stringFromNode(ctx);
735 enterLog("refine", refineString, ctx.getStart().getLine());
737 RefineHolderImpl refine = parseRefine(ctx, moduleName);
738 moduleBuilder.addRefine(refine);
739 moduleBuilder.enterNode(refine);
743 public void exitRefine_stmt(final YangParser.Refine_stmtContext ctx) {
744 moduleBuilder.exitNode();
749 public void enterLeaf_list_stmt(final Leaf_list_stmtContext ctx) {
750 final int line = ctx.getStart().getLine();
751 final String leafListName = stringFromNode(ctx);
752 enterLog("leaf-list", leafListName, line);
753 QName leafListQName = QName.create(moduleQName, leafListName);
754 SchemaPath path = stack.addNodeToPath(leafListQName);
756 LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, path);
757 moduleBuilder.enterNode(builder);
759 parseSchemaNodeArgs(ctx, builder);
760 parseConstraints(ctx, builder.getConstraints());
761 builder.setConfiguration(getConfig(ctx, builder, moduleName, ctx.getStart().getLine()));
763 for (int i = 0; i < ctx.getChildCount(); ++i) {
764 final ParseTree childNode = ctx.getChild(i);
765 if (childNode instanceof Ordered_by_stmtContext) {
766 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
767 final boolean userOrdered = parseUserOrdered(orderedBy);
768 builder.setUserOrdered(userOrdered);
775 public void exitLeaf_list_stmt(final YangParser.Leaf_list_stmtContext ctx) {
776 moduleBuilder.exitNode();
777 exitLog("leaf-list", stack.removeNodeFromPath());
781 public void enterList_stmt(final List_stmtContext ctx) {
782 final int line = ctx.getStart().getLine();
783 final String listName = stringFromNode(ctx);
784 enterLog("list", listName, line);
786 QName listQName = QName.create(moduleQName, listName);
787 SchemaPath path = stack.addNodeToPath(listQName);
789 ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, path);
790 moduleBuilder.enterNode(builder);
792 parseSchemaNodeArgs(ctx, builder);
793 parseConstraints(ctx, builder.getConstraints());
794 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
796 for (int i = 0; i < ctx.getChildCount(); ++i) {
797 ParseTree childNode = ctx.getChild(i);
798 if (childNode instanceof Ordered_by_stmtContext) {
799 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
800 final boolean userOrdered = parseUserOrdered(orderedBy);
801 builder.setUserOrdered(userOrdered);
802 } else if (childNode instanceof Key_stmtContext) {
803 List<String> key = createListKey((Key_stmtContext) childNode);
804 builder.setKeys(key);
810 public void exitList_stmt(final List_stmtContext ctx) {
811 moduleBuilder.exitNode();
812 exitLog("list", stack.removeNodeFromPath());
816 public void enterAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
817 final int line = ctx.getStart().getLine();
818 final String anyXmlName = stringFromNode(ctx);
819 enterLog("anyxml", anyXmlName, line);
821 QName anyXmlQName = QName.create(moduleQName, anyXmlName);
822 SchemaPath path = stack.addNodeToPath(anyXmlQName);
824 AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, path);
825 moduleBuilder.enterNode(builder);
827 parseSchemaNodeArgs(ctx, builder);
828 parseConstraints(ctx, builder.getConstraints());
829 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
833 public void exitAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
834 moduleBuilder.exitNode();
835 exitLog("anyxml", stack.removeNodeFromPath());
839 public void enterChoice_stmt(final YangParser.Choice_stmtContext ctx) {
840 final int line = ctx.getStart().getLine();
841 final String choiceName = stringFromNode(ctx);
842 enterLog("choice", choiceName, line);
844 QName choiceQName = QName.create(moduleQName, choiceName);
845 SchemaPath path = stack.addNodeToPath(choiceQName);
847 ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName, path);
848 moduleBuilder.enterNode(builder);
850 parseSchemaNodeArgs(ctx, builder);
851 parseConstraints(ctx, builder.getConstraints());
852 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
854 // set 'default' case
855 for (int i = 0; i < ctx.getChildCount(); i++) {
856 ParseTree child = ctx.getChild(i);
857 if (child instanceof Default_stmtContext) {
858 String defaultCase = stringFromNode(child);
859 builder.setDefaultCase(defaultCase);
866 public void exitChoice_stmt(final YangParser.Choice_stmtContext ctx) {
867 moduleBuilder.exitNode();
868 exitLog("choice", stack.removeNodeFromPath());
872 public void enterCase_stmt(final YangParser.Case_stmtContext ctx) {
873 final int line = ctx.getStart().getLine();
874 final String caseName = stringFromNode(ctx);
875 enterLog("case", caseName, line);
877 QName caseQName = QName.create(moduleQName, caseName);
878 SchemaPath path = stack.addNodeToPath(caseQName);
880 ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName, path);
881 moduleBuilder.enterNode(builder);
883 parseSchemaNodeArgs(ctx, builder);
884 parseConstraints(ctx, builder.getConstraints());
888 public void exitCase_stmt(final YangParser.Case_stmtContext ctx) {
889 moduleBuilder.exitNode();
890 exitLog("case", stack.removeNodeFromPath());
894 public void enterNotification_stmt(final YangParser.Notification_stmtContext ctx) {
895 final int line = ctx.getStart().getLine();
896 final String notificationName = stringFromNode(ctx);
897 enterLog("notification", notificationName, line);
899 QName notificationQName = QName.create(moduleQName, notificationName);
900 SchemaPath path = stack.addNodeToPath(notificationQName);
902 NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName, path);
903 moduleBuilder.enterNode(builder);
905 parseSchemaNodeArgs(ctx, builder);
909 public void exitNotification_stmt(final YangParser.Notification_stmtContext ctx) {
910 moduleBuilder.exitNode();
911 exitLog("notification", stack.removeNodeFromPath());
916 public void enterIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
917 handleUnknownNode(ctx.getStart().getLine(), ctx);
921 public void exitIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
922 moduleBuilder.exitNode();
923 exitLog("unknown-node", stack.removeNodeFromPath());
926 @Override public void enterUnknown_statement(final YangParser.Unknown_statementContext ctx) {
927 handleUnknownNode(ctx.getStart().getLine(), ctx);
930 @Override public void exitUnknown_statement(final YangParser.Unknown_statementContext ctx) {
931 moduleBuilder.exitNode();
932 exitLog("unknown-node", stack.removeNodeFromPath());
936 public void enterRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
937 final int line = ctx.getStart().getLine();
938 final String rpcName = stringFromNode(ctx);
939 enterLog("rpc", rpcName, line);
941 QName rpcQName = QName.create(moduleQName, rpcName);
942 SchemaPath path = stack.addNodeToPath(rpcQName);
944 RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName, path);
945 moduleBuilder.enterNode(rpcBuilder);
948 parseSchemaNodeArgs(ctx, rpcBuilder);
952 public void exitRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
953 moduleBuilder.exitNode();
954 exitLog("rpc", stack.removeNodeFromPath());
958 public void enterInput_stmt(final YangParser.Input_stmtContext ctx) {
959 final int line = ctx.getStart().getLine();
960 final String input = "input";
961 enterLog(input, input, line);
963 QName rpcQName = QName.create(moduleQName, input);
964 SchemaPath path = stack.addNodeToPath(rpcQName);
966 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(line, rpcQName, path);
967 moduleBuilder.enterNode(builder);
968 builder.setConfiguration(true);
970 parseSchemaNodeArgs(ctx, builder);
971 parseConstraints(ctx, builder.getConstraints());
975 public void exitInput_stmt(final YangParser.Input_stmtContext ctx) {
976 moduleBuilder.exitNode();
977 exitLog("input", stack.removeNodeFromPath());
981 public void enterOutput_stmt(final YangParser.Output_stmtContext ctx) {
982 final int line = ctx.getStart().getLine();
983 final String output = "output";
984 enterLog(output, output, line);
986 QName rpcQName = QName.create(moduleQName, output);
987 SchemaPath path = stack.addNodeToPath(rpcQName);
989 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line);
990 moduleBuilder.enterNode(builder);
991 builder.setConfiguration(true);
993 parseSchemaNodeArgs(ctx, builder);
994 parseConstraints(ctx, builder.getConstraints());
998 public void exitOutput_stmt(final YangParser.Output_stmtContext ctx) {
999 moduleBuilder.exitNode();
1000 exitLog("output", stack.removeNodeFromPath());
1004 public void enterFeature_stmt(final YangParser.Feature_stmtContext ctx) {
1005 final int line = ctx.getStart().getLine();
1006 final String featureName = stringFromNode(ctx);
1007 enterLog("feature", featureName, line);
1009 QName featureQName = QName.create(moduleQName, featureName);
1010 SchemaPath path = stack.addNodeToPath(featureQName);
1012 FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName, path);
1013 moduleBuilder.enterNode(featureBuilder);
1015 parseSchemaNodeArgs(ctx, featureBuilder);
1019 public void exitFeature_stmt(final YangParser.Feature_stmtContext ctx) {
1020 moduleBuilder.exitNode();
1021 exitLog("feature", stack.removeNodeFromPath());
1025 public void enterDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
1026 final int line = ctx.getStart().getLine();
1027 final String targetPathStr = stringFromNode(ctx);
1028 if (!targetPathStr.startsWith("/")) {
1029 throw new YangParseException(moduleName, line,
1030 "Deviation argument string must be an absolute schema node identifier.");
1032 enterLog("deviation", targetPathStr, line);
1034 String reference = null;
1035 String deviate = null;
1037 SchemaPath targetPath = parseXPathString(targetPathStr, line);
1038 DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath);
1039 moduleBuilder.enterNode(builder);
1041 for (int i = 0; i < ctx.getChildCount(); i++) {
1042 ParseTree child = ctx.getChild(i);
1043 if (child instanceof Reference_stmtContext) {
1044 reference = stringFromNode(child);
1045 } else if (child instanceof Deviate_not_supported_stmtContext) {
1046 deviate = stringFromNode(child);
1047 } else if (child instanceof Deviate_add_stmtContext) {
1048 deviate = stringFromNode(child);
1049 } else if (child instanceof Deviate_replace_stmtContext) {
1050 deviate = stringFromNode(child);
1051 } else if (child instanceof Deviate_delete_stmtContext) {
1052 deviate = stringFromNode(child);
1055 builder.setReference(reference);
1056 builder.setDeviate(deviate);
1059 public SchemaPath parseXPathString(final String xpathString, final int line) {
1060 final boolean absolute = !xpathString.isEmpty() && xpathString.charAt(0) == '/';
1062 final List<QName> path = new ArrayList<>();
1063 for (String pathElement : SLASH_SPLITTER.split(xpathString)) {
1064 final Iterator<String> it = COLON_SPLITTER.split(pathElement).iterator();
1065 final String s = it.next();
1067 path.add(parseQName(pathElement, line));
1069 path.add(QName.create(moduleQName, s));
1072 return SchemaPath.create(path, absolute);
1076 public void exitDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
1077 moduleBuilder.exitNode();
1078 exitLog("deviation");
1082 public void enterIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
1083 final int line = ctx.getStart().getLine();
1084 final String identityName = stringFromNode(ctx);
1085 enterLog("identity", identityName, line);
1087 final QName identityQName = QName.create(moduleQName, identityName);
1088 SchemaPath path = stack.addNodeToPath(identityQName);
1090 IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line, path);
1091 moduleBuilder.enterNode(builder);
1093 parseSchemaNodeArgs(ctx, builder);
1095 for (int i = 0; i < ctx.getChildCount(); i++) {
1096 ParseTree child = ctx.getChild(i);
1097 if (child instanceof Base_stmtContext) {
1098 String baseIdentityName = stringFromNode(child);
1099 builder.setBaseIdentityName(baseIdentityName);
1105 public void exitIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
1106 moduleBuilder.exitNode();
1107 exitLog("identity", stack.removeNodeFromPath());
1110 public ModuleBuilder getModuleBuilder() {
1111 return moduleBuilder;
1114 private static void enterLog(final String p1, final String p2, final int line) {
1115 LOG.trace("entering {} {} ({})", p1, p2, line);
1118 private static void exitLog(final String p1) {
1119 LOG.trace("exiting {}", p1);
1122 private static void exitLog(final String p1, final QName p2) {
1123 LOG.trace("exiting {} {}", p1, p2.getLocalName());
1126 private static void setLog(final String p1, final String p2) {
1127 LOG.trace("setting {} {}", p1, p2);
1130 private void handleUnknownNode(final int line, final ParseTree ctx) {
1131 final String nodeParameter = stringFromNode(ctx);
1132 enterLog("unknown-node", nodeParameter, line);
1134 final String nodeTypeStr = ctx.getChild(0).getText();
1135 QName nodeType = parseQName(nodeTypeStr, line);
1139 //FIXME: rewrite whole method to handle unknown nodes properly.
1140 // This should be bugfix for bug https://bugs.opendaylight.org/show_bug.cgi?id=1539
1141 // After this fix bug https://bugs.opendaylight.org/show_bug.cgi?id=1538 MUST be fixed since
1142 // they are dependent!!!
1143 if (Strings.isNullOrEmpty(nodeParameter)) {
1146 final Iterable<String> splittedName = COLON_SPLITTER.split(nodeParameter);
1147 final Iterator<String> it = splittedName.iterator();
1148 if (Iterables.size(splittedName) == 2) {
1149 qname = parseQName(nodeParameter, line);
1151 qname = QName.create(moduleQName, it.next());
1154 } catch (IllegalArgumentException | YangParseException ex) {
1158 SchemaPath path = stack.addNodeToPath(qname);
1160 UnknownSchemaNodeBuilderImpl builder = moduleBuilder.addUnknownSchemaNode(line, qname, path);
1161 builder.setNodeType(nodeType);
1162 builder.setNodeParameter(nodeParameter);
1164 parseSchemaNodeArgs(ctx, builder);
1165 moduleBuilder.enterNode(builder);