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;
38 import java.util.TreeMap;
39 import org.antlr.v4.runtime.tree.ParseTree;
40 import org.antlr.v4.runtime.tree.ParseTreeWalker;
41 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser;
42 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Argument_stmtContext;
43 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Base_stmtContext;
44 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Contact_stmtContext;
45 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Container_stmtContext;
46 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Default_stmtContext;
47 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Description_stmtContext;
48 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_add_stmtContext;
49 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_delete_stmtContext;
50 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_not_supported_stmtContext;
51 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_replace_stmtContext;
52 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Import_stmtContext;
53 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Key_stmtContext;
54 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Leaf_list_stmtContext;
55 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Leaf_stmtContext;
56 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.List_stmtContext;
57 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Module_header_stmtsContext;
58 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Namespace_stmtContext;
59 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Ordered_by_stmtContext;
60 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Organization_stmtContext;
61 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Prefix_stmtContext;
62 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Presence_stmtContext;
63 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Reference_stmtContext;
64 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_date_stmtContext;
65 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtContext;
66 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtsContext;
67 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Status_stmtContext;
68 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Type_body_stmtsContext;
69 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Units_stmtContext;
70 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.When_stmtContext;
71 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Yang_version_stmtContext;
72 import org.opendaylight.yangtools.antlrv4.code.gen.YangParserBaseListener;
73 import org.opendaylight.yangtools.yang.common.QName;
74 import org.opendaylight.yangtools.yang.common.QNameModule;
75 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
76 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
77 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
78 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
79 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
80 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
81 import org.opendaylight.yangtools.yang.parser.builder.api.ExtensionBuilder;
82 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
83 import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
84 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
85 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
86 import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder;
87 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
88 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
89 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
90 import org.opendaylight.yangtools.yang.parser.builder.impl.DeviationBuilder;
91 import org.opendaylight.yangtools.yang.parser.builder.impl.FeatureBuilder;
92 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
93 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
94 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
95 import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder;
96 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
97 import org.opendaylight.yangtools.yang.parser.builder.impl.NotificationBuilder;
98 import org.opendaylight.yangtools.yang.parser.builder.impl.RefineHolderImpl;
99 import org.opendaylight.yangtools.yang.parser.builder.impl.RpcDefinitionBuilder;
100 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
101 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilderImpl;
102 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
103 import org.slf4j.Logger;
104 import org.slf4j.LoggerFactory;
107 public final class YangParserListenerImpl extends YangParserBaseListener {
108 private static final Logger LOG = LoggerFactory.getLogger(YangParserListenerImpl.class);
109 private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings();
110 private static final Splitter COLON_SPLITTER = Splitter.on(':');
111 private static final String AUGMENT_STR = "augment";
113 private final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
114 private final SchemaPathStack stack = new SchemaPathStack();
115 private final Map<String, TreeMap<Date, URI>> namespaceContext;
116 private final String sourcePath;
117 private QName moduleQName = QName.create(null, new Date(0L), "dummy");
118 private ModuleBuilder moduleBuilder;
119 private String moduleName;
120 private int augmentOrder;
121 private String yangModelPrefix;
123 public YangParserListenerImpl(final Map<String, TreeMap<Date, URI>> namespaceContext, final String sourcePath) {
124 this.namespaceContext = namespaceContext;
125 this.sourcePath = sourcePath;
129 * Create a new instance.
131 * FIXME: the resulting type needs to be extracted, such that we can reuse
132 * the "BaseListener" aspect, which need not be exposed to the user. Maybe
133 * factor out a base class into repo.spi?
135 * @param namespaceContext
139 * @return new instance of YangParserListenerImpl
141 public static YangParserListenerImpl create(final Map<String, TreeMap<Date, URI>> namespaceContext,
142 final String sourcePath, final ParseTreeWalker walker, final ParseTree tree) {
143 final YangParserListenerImpl ret = new YangParserListenerImpl(namespaceContext, sourcePath);
144 walker.walk(ret, tree);
151 public void enterModule_stmt(final YangParser.Module_stmtContext ctx) {
152 moduleName = stringFromNode(ctx);
153 LOG.trace("entering module {}", moduleName);
154 enterLog("module", moduleName, 0);
157 moduleBuilder = new ModuleBuilder(moduleName, sourcePath);
159 String description = null;
160 String reference = null;
161 for (int i = 0; i < ctx.getChildCount(); i++) {
162 ParseTree child = ctx.getChild(i);
163 if (child instanceof Description_stmtContext) {
164 description = stringFromNode(child);
165 } else if (child instanceof Reference_stmtContext) {
166 reference = stringFromNode(child);
168 if (description != null && reference != null) {
173 moduleBuilder.setDescription(description);
174 moduleBuilder.setReference(reference);
178 public void exitModule_stmt(final YangParser.Module_stmtContext ctx) {
184 public void enterSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
185 moduleName = stringFromNode(ctx);
186 LOG.trace("entering submodule {}", moduleName);
187 enterLog("submodule", moduleName, 0);
190 moduleBuilder = new ModuleBuilder(moduleName, true, sourcePath);
192 String description = null;
193 String reference = null;
194 for (int i = 0; i < ctx.getChildCount(); i++) {
195 ParseTree child = ctx.getChild(i);
196 if (child instanceof Description_stmtContext) {
197 description = stringFromNode(child);
198 } else if (child instanceof Reference_stmtContext) {
199 reference = stringFromNode(child);
201 if (description != null && reference != null) {
206 moduleBuilder.setDescription(description);
207 moduleBuilder.setReference(reference);
211 public void exitSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
212 exitLog("submodule");
217 public void enterBelongs_to_stmt(final YangParser.Belongs_to_stmtContext ctx) {
218 final String belongsTo = stringFromNode(ctx);
219 TreeMap<Date, URI> context = namespaceContext.get(belongsTo);
220 final Map.Entry<Date, URI> entry = context.firstEntry();
222 // Submodule will contain namespace and revision from module to which it
223 // belongs. If there are multiple modules with same name and different
224 // revisions, it will has revision from the newest one.
225 this.moduleQName = QName.create(entry.getValue(), entry.getKey(), moduleQName.getLocalName());
226 moduleBuilder.setQNameModule(moduleQName.getModule());
227 moduleBuilder.setBelongsTo(belongsTo);
228 for (int i = 0; i < ctx.getChildCount(); ++i) {
229 final ParseTree treeNode = ctx.getChild(i);
230 if (treeNode instanceof Prefix_stmtContext) {
231 yangModelPrefix = stringFromNode(treeNode);
232 moduleBuilder.setPrefix(yangModelPrefix);
233 setLog("prefix", yangModelPrefix);
239 public void enterModule_header_stmts(final Module_header_stmtsContext ctx) {
240 enterLog("module_header", "", ctx.getStart().getLine());
241 String yangVersion = null;
242 for (int i = 0; i < ctx.getChildCount(); ++i) {
243 final ParseTree treeNode = ctx.getChild(i);
244 if (treeNode instanceof Namespace_stmtContext) {
245 final String namespaceStr = stringFromNode(treeNode);
246 final URI namespace = URI.create(namespaceStr);
247 this.moduleQName = QName.create(namespace, moduleQName.getRevision(), moduleQName.getLocalName());
248 moduleBuilder.setQNameModule(moduleQName.getModule());
249 setLog("namespace", namespaceStr);
250 } else if (treeNode instanceof Prefix_stmtContext) {
251 yangModelPrefix = stringFromNode(treeNode);
252 this.moduleQName = QName.create(moduleQName.getModule(), moduleQName.getLocalName());
253 moduleBuilder.setPrefix(yangModelPrefix);
254 setLog("prefix", yangModelPrefix);
255 } else if (treeNode instanceof Yang_version_stmtContext) {
256 yangVersion = stringFromNode(treeNode);
257 setLog("yang-version", yangVersion);
261 if (yangVersion == null) {
264 moduleBuilder.setYangVersion(yangVersion);
268 public void exitModule_header_stmts(final Module_header_stmtsContext ctx) {
269 exitLog("module_header");
273 public void enterMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
274 enterLog("meta_stmt", "", ctx.getStart().getLine());
275 for (int i = 0; i < ctx.getChildCount(); i++) {
276 final ParseTree child = ctx.getChild(i);
277 if (child instanceof Organization_stmtContext) {
278 final String organization = stringFromNode(child);
279 moduleBuilder.setOrganization(organization);
280 setLog("organization", organization);
281 } else if (child instanceof Contact_stmtContext) {
282 final String contact = stringFromNode(child);
283 moduleBuilder.setContact(contact);
284 setLog("contact", contact);
285 } else if (child instanceof Description_stmtContext) {
286 final String description = stringFromNode(child);
287 moduleBuilder.setDescription(description);
288 setLog("description", description);
289 } else if (child instanceof Reference_stmtContext) {
290 final String reference = stringFromNode(child);
291 moduleBuilder.setReference(reference);
292 setLog("reference", reference);
298 public void exitMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
299 exitLog("meta_stmt");
303 public void enterRevision_stmts(final Revision_stmtsContext ctx) {
304 enterLog("revisions", "", ctx.getStart().getLine());
305 for (int i = 0; i < ctx.getChildCount(); ++i) {
306 final ParseTree treeNode = ctx.getChild(i);
307 if (treeNode instanceof Revision_stmtContext) {
308 updateRevisionForRevisionStatement(treeNode);
314 public void exitRevision_stmts(final Revision_stmtsContext ctx) {
315 exitLog("revisions");
318 private void updateRevisionForRevisionStatement(final ParseTree treeNode) {
319 final String revisionDateStr = stringFromNode(treeNode);
321 final Date revisionDate = SIMPLE_DATE_FORMAT.parse(revisionDateStr);
322 if ((revisionDate != null) && (this.moduleQName.getRevision().compareTo(revisionDate) < 0)) {
323 this.moduleQName = QName.create(moduleQName.getNamespace(), revisionDate, moduleQName.getLocalName());
324 moduleBuilder.setQNameModule(moduleQName.getModule());
325 setLog("revision", revisionDate.toString());
326 for (int i = 0; i < treeNode.getChildCount(); ++i) {
327 ParseTree child = treeNode.getChild(i);
328 if (child instanceof Reference_stmtContext) {
329 moduleBuilder.setReference(stringFromNode(child));
333 } catch (ParseException e) {
334 LOG.warn("Failed to parse revision string: {}", revisionDateStr, e);
339 public void enterImport_stmt(final Import_stmtContext ctx) {
340 final int line = ctx.getStart().getLine();
341 final String importName = stringFromNode(ctx);
342 enterLog("import", importName, line);
344 String importPrefix = null;
345 Date importRevision = null;
347 for (int i = 0; i < ctx.getChildCount(); ++i) {
348 final ParseTree treeNode = ctx.getChild(i);
349 if (treeNode instanceof Prefix_stmtContext) {
350 importPrefix = stringFromNode(treeNode);
351 } else if (treeNode instanceof Revision_date_stmtContext) {
352 String importRevisionStr = stringFromNode(treeNode);
354 importRevision = SIMPLE_DATE_FORMAT.parse(importRevisionStr);
355 } catch (ParseException e) {
356 LOG.warn("Failed to parse import revision-date at line {}: {}", line, importRevisionStr, e);
360 moduleBuilder.addModuleImport(importName, importRevision, importPrefix);
361 LOG.trace("setting import ({}; {}; {})", importName, importRevision, importPrefix);
365 public void exitImport_stmt(final Import_stmtContext ctx) {
370 public void enterInclude_stmt(YangParser.Include_stmtContext ctx) {
371 final int line = ctx.getStart().getLine();
372 final String includeName = stringFromNode(ctx);
373 enterLog("import", includeName, line);
375 Date includeRevision = null;
376 for (int i = 0; i < ctx.getChildCount(); i++) {
377 final ParseTree treeNode = ctx.getChild(i);
378 if (treeNode instanceof Revision_date_stmtContext) {
379 final String importRevisionStr = stringFromNode(treeNode);
381 includeRevision = SIMPLE_DATE_FORMAT.parse(importRevisionStr);
382 } catch (ParseException e) {
383 LOG.warn("Failed to parse import revision-date at line {}: {}", line, importRevisionStr, e);
387 moduleBuilder.addInclude(includeName, includeRevision);
390 @Override public void exitInclude_stmt(YangParser.Include_stmtContext ctx) {
395 public void enterAugment_stmt(final YangParser.Augment_stmtContext ctx) {
396 final int line = ctx.getStart().getLine();
397 final String augmentPath = stringFromNode(ctx);
398 enterLog(AUGMENT_STR, augmentPath, line);
401 final SchemaPath targetPath = parseXPathString(augmentPath, line);
402 final AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, targetPath, augmentOrder++);
404 for (int i = 0; i < ctx.getChildCount(); i++) {
405 ParseTree child = ctx.getChild(i);
406 if (child instanceof Description_stmtContext) {
407 builder.setDescription(stringFromNode(child));
408 } else if (child instanceof Reference_stmtContext) {
409 builder.setReference(stringFromNode(child));
410 } else if (child instanceof Status_stmtContext) {
411 builder.setStatus(parseStatus((Status_stmtContext) child));
412 } else if (child instanceof When_stmtContext) {
413 builder.addWhenCondition(stringFromNode(child));
417 moduleBuilder.enterNode(builder);
421 public void exitAugment_stmt(final YangParser.Augment_stmtContext ctx) {
422 moduleBuilder.exitNode();
423 exitLog(AUGMENT_STR);
428 public void enterExtension_stmt(final YangParser.Extension_stmtContext ctx) {
429 final int line = ctx.getStart().getLine();
430 final String extName = stringFromNode(ctx);
431 enterLog("extension", extName, line);
432 final QName qname = QName.create(moduleQName, extName);
433 final SchemaPath path = stack.addNodeToPath(qname);
435 final ExtensionBuilder builder = moduleBuilder.addExtension(qname, line, path);
436 parseSchemaNodeArgs(ctx, builder);
438 String argument = null;
440 for (int i = 0; i < ctx.getChildCount(); i++) {
441 final ParseTree child = ctx.getChild(i);
442 if (child instanceof Argument_stmtContext) {
443 argument = stringFromNode(child);
444 yin = parseYinValue((Argument_stmtContext) child);
448 builder.setArgument(argument);
449 builder.setYinElement(yin);
451 moduleBuilder.enterNode(builder);
455 public void exitExtension_stmt(final YangParser.Extension_stmtContext ctx) {
456 moduleBuilder.exitNode();
457 exitLog("extension", stack.removeNodeFromPath());
461 public void enterTypedef_stmt(final YangParser.Typedef_stmtContext ctx) {
462 final int line = ctx.getStart().getLine();
463 final String typedefName = stringFromNode(ctx);
464 enterLog("typedef", typedefName, line);
465 final QName typedefQName = QName.create(moduleQName, typedefName);
466 final SchemaPath path = stack.addNodeToPath(typedefQName);
468 final TypeDefinitionBuilder builder = moduleBuilder.addTypedef(line, typedefQName, path);
469 parseSchemaNodeArgs(ctx, builder);
470 builder.setUnits(parseUnits(ctx));
471 builder.setDefaultValue(parseDefault(ctx));
473 moduleBuilder.enterNode(builder);
477 public void exitTypedef_stmt(final YangParser.Typedef_stmtContext ctx) {
478 moduleBuilder.exitNode();
479 exitLog("typedef", stack.removeNodeFromPath());
483 public void enterType_stmt(final YangParser.Type_stmtContext ctx) {
484 final int line = ctx.getStart().getLine();
485 final String typeName = stringFromNode(ctx);
486 enterLog("type", typeName, line);
488 final QName typeQName = parseQName(typeName, line);
490 TypeDefinition<?> type;
491 Type_body_stmtsContext typeBody = null;
492 for (int i = 0; i < ctx.getChildCount(); i++) {
493 if (ctx.getChild(i) instanceof Type_body_stmtsContext) {
494 typeBody = (Type_body_stmtsContext) ctx.getChild(i);
499 // if this is base yang type...
500 if (BaseTypes.isYangBuildInType(typeName)) {
501 if (typeBody == null) {
502 // check for types which must have body
503 checkMissingBody(typeName, moduleName, line);
504 // if there are no constraints, just grab default base yang type
505 type = BaseTypes.defaultBaseTypeFor(typeName).orNull();
506 stack.addNodeToPath(type.getQName());
507 moduleBuilder.setType(type);
512 qname = BaseTypes.UNION_QNAME;
513 stack.addNodeToPath(qname);
514 final UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, moduleQName.getModule());
515 final Builder parent = moduleBuilder.getActualNode();
516 unionBuilder.setParent(parent);
517 moduleBuilder.enterNode(unionBuilder);
520 qname = BaseTypes.IDENTITYREF_QNAME;
521 final SchemaPath path = stack.addNodeToPath(qname);
522 moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody));
525 type = parseTypeWithBody(typeName, typeBody, stack.currentSchemaPath(), moduleQName,
526 moduleBuilder.getActualNode());
527 moduleBuilder.setType(type);
528 stack.addNodeToPath(type.getQName());
532 final TypeAwareBuilder parent = (TypeAwareBuilder) moduleBuilder.getActualNode();
533 if (typeBody == null) {
534 parent.setTypeQName(typeQName);
535 moduleBuilder.markActualNodeDirty();
537 ParserListenerUtils.parseUnknownTypeWithBody(typeBody, parent, typeQName, moduleBuilder,
538 moduleQName, stack.currentSchemaPath());
540 stack.addNodeToPath(QName.create(moduleQName.getModule(), typeQName.getLocalName()));
545 * Method transforms string representation of yang element (i.e. leaf name, container name etc.) into QName.
546 * The namespace of QName is assigned from parent module same as revision date of module. If String qname parameter
547 * contains ":" the string is evaluated as prefix:name of element. In this case method will look into import map
548 * and extract correct ModuleImport. If such import is not present in import map the method will throw {@link YangParseException}
550 * If ModuleImport is present but the value of namespace in ModuleImport is <code>null</code> the method will throw {@link YangParseException}
552 * @param qnameString QName value as String
553 * @param line line in Yang model document where QName occur.
554 * @return transformed string qname parameter as QName structure.
556 * @throws YangParseException
558 private QName parseQName(final String qnameString, final int line) {
560 if (qnameString.indexOf(':') == -1) {
561 qname = QName.create(moduleQName, qnameString);
563 final Iterator<String> split = COLON_SPLITTER.split(qnameString).iterator();
564 final String prefix = split.next();
565 final String name = split.next();
566 if (prefix.equals(moduleBuilder.getPrefix())) {
567 qname = QName.create(moduleQName, name);
569 final ModuleImport imp = moduleBuilder.getImport(prefix);
571 LOG.debug("Error in module {} at line {}: No import found with prefix {}", moduleName, line, prefix);
572 throw new YangParseException(moduleName, line, "Error in module " + moduleName
573 + " No import found with prefix " + prefix + " not found.");
575 Date revision = imp.getRevision();
576 TreeMap<Date, URI> namespaces = namespaceContext.get(imp.getModuleName());
577 if (namespaces == null) {
578 throw new YangParseException(moduleName, line, String.format("Imported module %s not found",
579 imp.getModuleName()));
582 if (revision == null) {
583 revision = namespaces.lastEntry().getKey();
584 namespace = namespaces.lastEntry().getValue();
586 // FIXME: this lookup does not look right, as we will end up with
587 // a qname which does not have a namespace. At any rate we
588 // should arrive at a QNameModule!
589 namespace = namespaces.get(revision);
592 final QNameModule mod = QNameModule.cachedReference(QNameModule.create(namespace, revision));
593 qname = QName.create(mod, name);
600 public void exitType_stmt(final YangParser.Type_stmtContext ctx) {
601 final String typeName = stringFromNode(ctx);
602 if ("union".equals(typeName)) {
603 moduleBuilder.exitNode();
605 exitLog("type", stack.removeNodeFromPath());
609 public void enterGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
610 final int line = ctx.getStart().getLine();
611 final String groupName = stringFromNode(ctx);
612 enterLog("grouping", groupName, line);
613 final QName groupQName = QName.create(moduleQName, groupName);
614 final SchemaPath path = stack.addNodeToPath(groupQName);
616 final GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName, path);
617 parseSchemaNodeArgs(ctx, builder);
619 moduleBuilder.enterNode(builder);
623 public void exitGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
624 moduleBuilder.exitNode();
625 exitLog("grouping", stack.removeNodeFromPath());
629 public void enterContainer_stmt(final Container_stmtContext ctx) {
630 final int line = ctx.getStart().getLine();
631 final String containerName = stringFromNode(ctx);
632 enterLog("container", containerName, line);
634 final QName containerQName = QName.create(moduleQName, containerName);
635 final SchemaPath path = stack.addNodeToPath(containerQName);
637 final ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(line, containerQName, path);
638 parseSchemaNodeArgs(ctx, builder);
639 parseConstraints(ctx, builder.getConstraints());
640 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
642 for (int i = 0; i < ctx.getChildCount(); ++i) {
643 final ParseTree childNode = ctx.getChild(i);
644 if (childNode instanceof Presence_stmtContext) {
645 builder.setPresence(true);
650 moduleBuilder.enterNode(builder);
654 public void exitContainer_stmt(final Container_stmtContext ctx) {
655 moduleBuilder.exitNode();
656 exitLog("container", stack.removeNodeFromPath());
660 public void enterLeaf_stmt(final Leaf_stmtContext ctx) {
661 final int line = ctx.getStart().getLine();
662 final String leafName = stringFromNode(ctx);
663 enterLog("leaf", leafName, line);
665 final QName leafQName = QName.create(moduleQName, leafName);
666 final SchemaPath path = stack.addNodeToPath(leafQName);
668 final LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, path);
669 parseSchemaNodeArgs(ctx, builder);
670 parseConstraints(ctx, builder.getConstraints());
671 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
673 String defaultStr = null;
674 String unitsStr = null;
675 for (int i = 0; i < ctx.getChildCount(); i++) {
676 final ParseTree child = ctx.getChild(i);
677 if (child instanceof Default_stmtContext) {
678 defaultStr = stringFromNode(child);
679 } else if (child instanceof Units_stmtContext) {
680 unitsStr = stringFromNode(child);
683 builder.setDefaultStr(defaultStr);
684 builder.setUnits(unitsStr);
686 moduleBuilder.enterNode(builder);
690 public void exitLeaf_stmt(final YangParser.Leaf_stmtContext ctx) {
691 moduleBuilder.exitNode();
692 exitLog("leaf", stack.removeNodeFromPath());
696 public void enterUses_stmt(final YangParser.Uses_stmtContext ctx) {
697 final int line = ctx.getStart().getLine();
698 final String groupingPathStr = stringFromNode(ctx);
699 final SchemaPath groupingPath = parseXPathString(groupingPathStr, line);
700 enterLog("uses", groupingPathStr, line);
702 final UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPath);
704 moduleBuilder.enterNode(builder);
708 public void exitUses_stmt(final YangParser.Uses_stmtContext ctx) {
709 moduleBuilder.exitNode();
714 public void enterUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
715 final int line = ctx.getStart().getLine();
716 final String augmentPath = stringFromNode(ctx);
717 enterLog(AUGMENT_STR, augmentPath, line);
720 final SchemaPath targetPath = parseXPathString(augmentPath, line);
721 final AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, targetPath, augmentOrder++);
723 for (int i = 0; i < ctx.getChildCount(); i++) {
724 final ParseTree child = ctx.getChild(i);
725 if (child instanceof Description_stmtContext) {
726 builder.setDescription(stringFromNode(child));
727 } else if (child instanceof Reference_stmtContext) {
728 builder.setReference(stringFromNode(child));
729 } else if (child instanceof Status_stmtContext) {
730 builder.setStatus(parseStatus((Status_stmtContext) child));
731 } else if (child instanceof When_stmtContext) {
732 builder.addWhenCondition(stringFromNode(child));
736 moduleBuilder.enterNode(builder);
740 public void exitUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
741 moduleBuilder.exitNode();
742 exitLog(AUGMENT_STR);
747 public void enterRefine_stmt(final YangParser.Refine_stmtContext ctx) {
748 final String refineString = stringFromNode(ctx);
749 enterLog("refine", refineString, ctx.getStart().getLine());
751 final RefineHolderImpl refine = parseRefine(ctx, moduleName);
752 moduleBuilder.addRefine(refine);
753 moduleBuilder.enterNode(refine);
757 public void exitRefine_stmt(final YangParser.Refine_stmtContext ctx) {
758 moduleBuilder.exitNode();
763 public void enterLeaf_list_stmt(final Leaf_list_stmtContext ctx) {
764 final int line = ctx.getStart().getLine();
765 final String leafListName = stringFromNode(ctx);
766 enterLog("leaf-list", leafListName, line);
767 final QName leafListQName = QName.create(moduleQName, leafListName);
768 final SchemaPath path = stack.addNodeToPath(leafListQName);
770 final LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, path);
771 moduleBuilder.enterNode(builder);
773 parseSchemaNodeArgs(ctx, builder);
774 parseConstraints(ctx, builder.getConstraints());
775 builder.setConfiguration(getConfig(ctx, builder, moduleName, ctx.getStart().getLine()));
777 for (int i = 0; i < ctx.getChildCount(); ++i) {
778 final ParseTree childNode = ctx.getChild(i);
779 if (childNode instanceof Ordered_by_stmtContext) {
780 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
781 final boolean userOrdered = parseUserOrdered(orderedBy);
782 builder.setUserOrdered(userOrdered);
789 public void exitLeaf_list_stmt(final YangParser.Leaf_list_stmtContext ctx) {
790 moduleBuilder.exitNode();
791 exitLog("leaf-list", stack.removeNodeFromPath());
795 public void enterList_stmt(final List_stmtContext ctx) {
796 final int line = ctx.getStart().getLine();
797 final String listName = stringFromNode(ctx);
798 enterLog("list", listName, line);
800 final QName listQName = QName.create(moduleQName, listName);
801 final SchemaPath path = stack.addNodeToPath(listQName);
803 final ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, path);
804 moduleBuilder.enterNode(builder);
806 parseSchemaNodeArgs(ctx, builder);
807 parseConstraints(ctx, builder.getConstraints());
808 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
810 for (int i = 0; i < ctx.getChildCount(); ++i) {
811 final ParseTree childNode = ctx.getChild(i);
812 if (childNode instanceof Ordered_by_stmtContext) {
813 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
814 final boolean userOrdered = parseUserOrdered(orderedBy);
815 builder.setUserOrdered(userOrdered);
816 } else if (childNode instanceof Key_stmtContext) {
817 final Set<String> key = createListKey((Key_stmtContext) childNode);
818 builder.setKeys(key);
824 public void exitList_stmt(final List_stmtContext ctx) {
825 moduleBuilder.exitNode();
826 exitLog("list", stack.removeNodeFromPath());
830 public void enterAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
831 final int line = ctx.getStart().getLine();
832 final String anyXmlName = stringFromNode(ctx);
833 enterLog("anyxml", anyXmlName, line);
835 final QName anyXmlQName = QName.create(moduleQName, anyXmlName);
836 final SchemaPath path = stack.addNodeToPath(anyXmlQName);
838 final AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, path);
839 moduleBuilder.enterNode(builder);
841 parseSchemaNodeArgs(ctx, builder);
842 parseConstraints(ctx, builder.getConstraints());
843 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
847 public void exitAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
848 moduleBuilder.exitNode();
849 exitLog("anyxml", stack.removeNodeFromPath());
853 public void enterChoice_stmt(final YangParser.Choice_stmtContext ctx) {
854 final int line = ctx.getStart().getLine();
855 final String choiceName = stringFromNode(ctx);
856 enterLog("choice", choiceName, line);
858 final QName choiceQName = QName.create(moduleQName, choiceName);
859 final SchemaPath path = stack.addNodeToPath(choiceQName);
861 final ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName, path);
862 moduleBuilder.enterNode(builder);
864 parseSchemaNodeArgs(ctx, builder);
865 parseConstraints(ctx, builder.getConstraints());
866 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
868 // set 'default' case
869 for (int i = 0; i < ctx.getChildCount(); i++) {
870 final ParseTree child = ctx.getChild(i);
871 if (child instanceof Default_stmtContext) {
872 final String defaultCase = stringFromNode(child);
873 builder.setDefaultCase(defaultCase);
880 public void exitChoice_stmt(final YangParser.Choice_stmtContext ctx) {
881 moduleBuilder.exitNode();
882 exitLog("choice", stack.removeNodeFromPath());
886 public void enterCase_stmt(final YangParser.Case_stmtContext ctx) {
887 final int line = ctx.getStart().getLine();
888 final String caseName = stringFromNode(ctx);
889 enterLog("case", caseName, line);
891 final QName caseQName = QName.create(moduleQName, caseName);
892 final SchemaPath path = stack.addNodeToPath(caseQName);
894 final ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName, path);
895 moduleBuilder.enterNode(builder);
897 parseSchemaNodeArgs(ctx, builder);
898 parseConstraints(ctx, builder.getConstraints());
902 public void exitCase_stmt(final YangParser.Case_stmtContext ctx) {
903 moduleBuilder.exitNode();
904 exitLog("case", stack.removeNodeFromPath());
908 public void enterNotification_stmt(final YangParser.Notification_stmtContext ctx) {
909 final int line = ctx.getStart().getLine();
910 final String notificationName = stringFromNode(ctx);
911 enterLog("notification", notificationName, line);
913 final QName notificationQName = QName.create(moduleQName, notificationName);
914 final SchemaPath path = stack.addNodeToPath(notificationQName);
916 final NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName, path);
917 moduleBuilder.enterNode(builder);
919 parseSchemaNodeArgs(ctx, builder);
923 public void exitNotification_stmt(final YangParser.Notification_stmtContext ctx) {
924 moduleBuilder.exitNode();
925 exitLog("notification", stack.removeNodeFromPath());
930 public void enterIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
931 handleUnknownNode(ctx.getStart().getLine(), ctx);
935 public void exitIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
936 moduleBuilder.exitNode();
937 exitLog("unknown-node", stack.removeNodeFromPath());
940 @Override public void enterUnknown_statement(final YangParser.Unknown_statementContext ctx) {
941 handleUnknownNode(ctx.getStart().getLine(), ctx);
944 @Override public void exitUnknown_statement(final YangParser.Unknown_statementContext ctx) {
945 moduleBuilder.exitNode();
946 exitLog("unknown-node", stack.removeNodeFromPath());
950 public void enterRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
951 final int line = ctx.getStart().getLine();
952 final String rpcName = stringFromNode(ctx);
953 enterLog("rpc", rpcName, line);
955 final QName rpcQName = QName.create(moduleQName, rpcName);
956 final SchemaPath path = stack.addNodeToPath(rpcQName);
958 final RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName, path);
959 moduleBuilder.enterNode(rpcBuilder);
961 parseSchemaNodeArgs(ctx, rpcBuilder);
965 public void exitRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
966 moduleBuilder.exitNode();
967 exitLog("rpc", stack.removeNodeFromPath());
971 public void enterInput_stmt(final YangParser.Input_stmtContext ctx) {
972 final int line = ctx.getStart().getLine();
973 final String input = "input";
974 enterLog(input, input, line);
976 final QName rpcQName = QName.create(moduleQName, input);
977 final SchemaPath path = stack.addNodeToPath(rpcQName);
979 final ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(line, rpcQName, path);
980 moduleBuilder.enterNode(builder);
981 builder.setConfiguration(true);
983 parseSchemaNodeArgs(ctx, builder);
984 parseConstraints(ctx, builder.getConstraints());
988 public void exitInput_stmt(final YangParser.Input_stmtContext ctx) {
989 moduleBuilder.exitNode();
990 exitLog("input", stack.removeNodeFromPath());
994 public void enterOutput_stmt(final YangParser.Output_stmtContext ctx) {
995 final int line = ctx.getStart().getLine();
996 final String output = "output";
997 enterLog(output, output, line);
999 final QName rpcQName = QName.create(moduleQName, output);
1000 final SchemaPath path = stack.addNodeToPath(rpcQName);
1002 final ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line);
1003 moduleBuilder.enterNode(builder);
1004 builder.setConfiguration(true);
1006 parseSchemaNodeArgs(ctx, builder);
1007 parseConstraints(ctx, builder.getConstraints());
1011 public void exitOutput_stmt(final YangParser.Output_stmtContext ctx) {
1012 moduleBuilder.exitNode();
1013 exitLog("output", stack.removeNodeFromPath());
1017 public void enterFeature_stmt(final YangParser.Feature_stmtContext ctx) {
1018 final int line = ctx.getStart().getLine();
1019 final String featureName = stringFromNode(ctx);
1020 enterLog("feature", featureName, line);
1022 final QName featureQName = QName.create(moduleQName, featureName);
1023 final SchemaPath path = stack.addNodeToPath(featureQName);
1025 final FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName, path);
1026 moduleBuilder.enterNode(featureBuilder);
1028 parseSchemaNodeArgs(ctx, featureBuilder);
1032 public void exitFeature_stmt(final YangParser.Feature_stmtContext ctx) {
1033 moduleBuilder.exitNode();
1034 exitLog("feature", stack.removeNodeFromPath());
1038 public void enterDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
1039 final int line = ctx.getStart().getLine();
1040 final String targetPathStr = stringFromNode(ctx);
1041 if (!targetPathStr.startsWith("/")) {
1042 throw new YangParseException(moduleName, line,
1043 "Deviation argument string must be an absolute schema node identifier.");
1045 enterLog("deviation", targetPathStr, line);
1047 String reference = null;
1048 String deviate = null;
1050 final SchemaPath targetPath = parseXPathString(targetPathStr, line);
1051 final DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath);
1052 moduleBuilder.enterNode(builder);
1054 for (int i = 0; i < ctx.getChildCount(); i++) {
1055 ParseTree child = ctx.getChild(i);
1056 if (child instanceof Reference_stmtContext) {
1057 reference = stringFromNode(child);
1058 } else if (child instanceof Deviate_not_supported_stmtContext) {
1059 deviate = stringFromNode(child);
1060 } else if (child instanceof Deviate_add_stmtContext) {
1061 deviate = stringFromNode(child);
1062 } else if (child instanceof Deviate_replace_stmtContext) {
1063 deviate = stringFromNode(child);
1064 } else if (child instanceof Deviate_delete_stmtContext) {
1065 deviate = stringFromNode(child);
1068 builder.setReference(reference);
1069 builder.setDeviate(deviate);
1072 public SchemaPath parseXPathString(final String xpathString, final int line) {
1073 final boolean absolute = !xpathString.isEmpty() && xpathString.charAt(0) == '/';
1075 final List<QName> path = new ArrayList<>();
1076 for (String pathElement : SLASH_SPLITTER.split(xpathString)) {
1077 final Iterator<String> it = COLON_SPLITTER.split(pathElement).iterator();
1078 final String s = it.next();
1080 path.add(parseQName(pathElement, line));
1082 path.add(QName.create(moduleQName, s));
1085 return SchemaPath.create(path, absolute);
1089 public void exitDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
1090 moduleBuilder.exitNode();
1091 exitLog("deviation");
1095 public void enterIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
1096 final int line = ctx.getStart().getLine();
1097 final String identityName = stringFromNode(ctx);
1098 enterLog("identity", identityName, line);
1100 final QName identityQName = QName.create(moduleQName, identityName);
1101 final SchemaPath path = stack.addNodeToPath(identityQName);
1103 final IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line, path);
1104 moduleBuilder.enterNode(builder);
1106 parseSchemaNodeArgs(ctx, builder);
1108 for (int i = 0; i < ctx.getChildCount(); i++) {
1109 final ParseTree child = ctx.getChild(i);
1110 if (child instanceof Base_stmtContext) {
1111 String baseIdentityName = stringFromNode(child);
1112 builder.setBaseIdentityName(baseIdentityName);
1118 public void exitIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
1119 moduleBuilder.exitNode();
1120 exitLog("identity", stack.removeNodeFromPath());
1123 public ModuleBuilder getModuleBuilder() {
1124 return moduleBuilder;
1127 private static void enterLog(final String p1, final String p2, final int line) {
1128 LOG.trace("entering {} {} ({})", p1, p2, line);
1131 private static void exitLog(final String p1) {
1132 LOG.trace("exiting {}", p1);
1135 private static void exitLog(final String p1, final QName p2) {
1136 LOG.trace("exiting {} {}", p1, p2.getLocalName());
1139 private static void setLog(final String p1, final String p2) {
1140 LOG.trace("setting {} {}", p1, p2);
1143 private void handleUnknownNode(final int line, final ParseTree ctx) {
1144 final String nodeParameter = stringFromNode(ctx);
1145 enterLog("unknown-node", nodeParameter, line);
1147 final String nodeTypeStr = ctx.getChild(0).getText();
1148 final QName nodeType = parseQName(nodeTypeStr, line);
1152 //FIXME: rewrite whole method to handle unknown nodes properly.
1153 // This should be bugfix for bug https://bugs.opendaylight.org/show_bug.cgi?id=1539
1154 // After this fix bug https://bugs.opendaylight.org/show_bug.cgi?id=1538 MUST be fixed since
1155 // they are dependent!!!
1156 if (Strings.isNullOrEmpty(nodeParameter)) {
1159 final Iterable<String> splittedName = COLON_SPLITTER.split(nodeParameter);
1160 final Iterator<String> it = splittedName.iterator();
1161 if (Iterables.size(splittedName) == 2) {
1162 qname = parseQName(nodeParameter, line);
1164 qname = QName.create(moduleQName, it.next());
1167 } catch (IllegalArgumentException | YangParseException ex) {
1171 final SchemaPath path = stack.addNodeToPath(qname);
1173 final UnknownSchemaNodeBuilderImpl builder = moduleBuilder.addUnknownSchemaNode(line, qname, path);
1174 builder.setNodeType(nodeType);
1175 builder.setNodeParameter(nodeParameter);
1177 parseSchemaNodeArgs(ctx, builder);
1178 moduleBuilder.enterNode(builder);