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;
26 import java.text.DateFormat;
27 import java.text.ParseException;
28 import java.text.SimpleDateFormat;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.Iterator;
32 import java.util.List;
34 import java.util.NavigableMap;
36 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.common.QNameModule;
74 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
75 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
76 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
77 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
78 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
79 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
80 import org.opendaylight.yangtools.yang.parser.builder.api.ExtensionBuilder;
81 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
82 import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
83 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
84 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
85 import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder;
86 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
87 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
88 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
89 import org.opendaylight.yangtools.yang.parser.builder.impl.DeviationBuilder;
90 import org.opendaylight.yangtools.yang.parser.builder.impl.FeatureBuilder;
91 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
92 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
93 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
94 import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder;
95 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
96 import org.opendaylight.yangtools.yang.parser.builder.impl.NotificationBuilder;
97 import org.opendaylight.yangtools.yang.parser.builder.impl.RefineHolderImpl;
98 import org.opendaylight.yangtools.yang.parser.builder.impl.RpcDefinitionBuilder;
99 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
100 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilderImpl;
101 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
102 import org.slf4j.Logger;
103 import org.slf4j.LoggerFactory;
105 import com.google.common.base.Splitter;
106 import com.google.common.base.Strings;
107 import com.google.common.collect.Iterables;
109 public final class YangParserListenerImpl extends YangParserBaseListener {
110 private static final Logger LOG = LoggerFactory.getLogger(YangParserListenerImpl.class);
111 private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings();
112 private static final Splitter COLON_SPLITTER = Splitter.on(':');
113 private static final String AUGMENT_STR = "augment";
115 private static final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
116 private static final String IMPORT_STR = "import";
117 private static final String UNION_STR = "union";
118 private static final String UNKNOWN_NODE_STR = "unknown-node";
119 private final SchemaPathStack stack = new SchemaPathStack();
120 private final Map<String, NavigableMap<Date, URI>> namespaceContext;
121 private final String sourcePath;
122 private QName moduleQName = QName.create(null, new Date(0L), "dummy");
123 private ModuleBuilder moduleBuilder;
124 private String moduleName;
125 private int augmentOrder;
126 private String yangModelPrefix;
128 public YangParserListenerImpl(final Map<String, NavigableMap<Date, URI>> namespaceContext, final String sourcePath) {
129 this.namespaceContext = namespaceContext;
130 this.sourcePath = sourcePath;
134 * Create a new instance.
136 * FIXME: the resulting type needs to be extracted, such that we can reuse
137 * the "BaseListener" aspect, which need not be exposed to the user. Maybe
138 * factor out a base class into repo.spi?
140 * @param namespaceContext
144 * @return new instance of YangParserListenerImpl
146 public static YangParserListenerImpl create(final Map<String, NavigableMap<Date, URI>> namespaceContext,
147 final String sourcePath, final ParseTreeWalker walker, final ParseTree tree) {
148 final YangParserListenerImpl ret = new YangParserListenerImpl(namespaceContext, sourcePath);
149 walker.walk(ret, tree);
154 public void enterModule_stmt(final YangParser.Module_stmtContext ctx) {
155 moduleName = stringFromNode(ctx);
156 LOG.trace("entering module {}", moduleName);
157 enterLog("module", moduleName, 0);
160 moduleBuilder = new ModuleBuilder(moduleName, sourcePath);
162 String description = null;
163 String reference = null;
164 for (int i = 0; i < ctx.getChildCount(); i++) {
165 ParseTree child = ctx.getChild(i);
166 if (child instanceof Description_stmtContext) {
167 description = stringFromNode(child);
168 } else if (child instanceof Reference_stmtContext) {
169 reference = stringFromNode(child);
171 if (description != null && reference != null) {
176 moduleBuilder.setDescription(description);
177 moduleBuilder.setReference(reference);
181 public void exitModule_stmt(final YangParser.Module_stmtContext ctx) {
187 public void enterSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
188 moduleName = stringFromNode(ctx);
189 LOG.trace("entering submodule {}", moduleName);
190 enterLog("submodule", moduleName, 0);
193 moduleBuilder = new ModuleBuilder(moduleName, true, sourcePath);
195 String description = null;
196 String reference = null;
197 for (int i = 0; i < ctx.getChildCount(); i++) {
198 ParseTree child = ctx.getChild(i);
199 if (child instanceof Description_stmtContext) {
200 description = stringFromNode(child);
201 } else if (child instanceof Reference_stmtContext) {
202 reference = stringFromNode(child);
204 if (description != null && reference != null) {
209 moduleBuilder.setDescription(description);
210 moduleBuilder.setReference(reference);
214 public void exitSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
215 exitLog("submodule");
220 public void enterBelongs_to_stmt(final YangParser.Belongs_to_stmtContext ctx) {
221 final String belongsTo = stringFromNode(ctx);
222 NavigableMap<Date, URI> context = namespaceContext.get(belongsTo);
223 final Map.Entry<Date, URI> entry = context.firstEntry();
225 // Submodule will contain namespace and revision from module to which it
226 // belongs. If there are multiple modules with same name and different
227 // revisions, it will has revision from the newest one.
228 this.moduleQName = QName.create(entry.getValue(), entry.getKey(), moduleQName.getLocalName());
229 moduleBuilder.setQNameModule(moduleQName.getModule());
230 moduleBuilder.setBelongsTo(belongsTo);
231 for (int i = 0; i < ctx.getChildCount(); ++i) {
232 final ParseTree treeNode = ctx.getChild(i);
233 if (treeNode instanceof Prefix_stmtContext) {
234 yangModelPrefix = stringFromNode(treeNode);
235 moduleBuilder.setPrefix(yangModelPrefix);
236 setLog("prefix", yangModelPrefix);
242 public void enterModule_header_stmts(final Module_header_stmtsContext ctx) {
243 enterLog("module_header", "", ctx.getStart().getLine());
244 String yangVersion = null;
245 for (int i = 0; i < ctx.getChildCount(); ++i) {
246 final ParseTree treeNode = ctx.getChild(i);
247 if (treeNode instanceof Namespace_stmtContext) {
248 final String namespaceStr = stringFromNode(treeNode);
249 final URI namespace = URI.create(namespaceStr);
250 this.moduleQName = QName.create(namespace, moduleQName.getRevision(), moduleQName.getLocalName());
251 moduleBuilder.setQNameModule(moduleQName.getModule());
252 setLog("namespace", namespaceStr);
253 } else if (treeNode instanceof Prefix_stmtContext) {
254 yangModelPrefix = stringFromNode(treeNode);
255 this.moduleQName = QName.create(moduleQName.getModule(), moduleQName.getLocalName());
256 moduleBuilder.setPrefix(yangModelPrefix);
257 setLog("prefix", yangModelPrefix);
258 } else if (treeNode instanceof Yang_version_stmtContext) {
259 yangVersion = stringFromNode(treeNode);
260 setLog("yang-version", yangVersion);
264 if (yangVersion == null) {
267 moduleBuilder.setYangVersion(yangVersion);
271 public void exitModule_header_stmts(final Module_header_stmtsContext ctx) {
272 exitLog("module_header");
276 public void enterMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
277 enterLog("meta_stmt", "", ctx.getStart().getLine());
278 for (int i = 0; i < ctx.getChildCount(); i++) {
279 final ParseTree child = ctx.getChild(i);
280 if (child instanceof Organization_stmtContext) {
281 final String organization = stringFromNode(child);
282 moduleBuilder.setOrganization(organization);
283 setLog("organization", organization);
284 } else if (child instanceof Contact_stmtContext) {
285 final String contact = stringFromNode(child);
286 moduleBuilder.setContact(contact);
287 setLog("contact", contact);
288 } else if (child instanceof Description_stmtContext) {
289 final String description = stringFromNode(child);
290 moduleBuilder.setDescription(description);
291 setLog("description", description);
292 } else if (child instanceof Reference_stmtContext) {
293 final String reference = stringFromNode(child);
294 moduleBuilder.setReference(reference);
295 setLog("reference", reference);
301 public void exitMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
302 exitLog("meta_stmt");
306 public void enterRevision_stmts(final Revision_stmtsContext ctx) {
307 enterLog("revisions", "", ctx.getStart().getLine());
308 for (int i = 0; i < ctx.getChildCount(); ++i) {
309 final ParseTree treeNode = ctx.getChild(i);
310 if (treeNode instanceof Revision_stmtContext) {
311 updateRevisionForRevisionStatement(treeNode);
317 public void exitRevision_stmts(final Revision_stmtsContext ctx) {
318 exitLog("revisions");
321 private void updateRevisionForRevisionStatement(final ParseTree treeNode) {
322 final String revisionDateStr = stringFromNode(treeNode);
324 final Date revisionDate = SIMPLE_DATE_FORMAT.parse(revisionDateStr);
325 if ((revisionDate != null) && (this.moduleQName.getRevision().compareTo(revisionDate) < 0)) {
326 this.moduleQName = QName.create(moduleQName.getNamespace(), revisionDate, moduleQName.getLocalName());
327 moduleBuilder.setQNameModule(moduleQName.getModule());
328 setLog("revision", revisionDate.toString());
329 for (int i = 0; i < treeNode.getChildCount(); ++i) {
330 ParseTree child = treeNode.getChild(i);
331 if (child instanceof Reference_stmtContext) {
332 moduleBuilder.setReference(stringFromNode(child));
336 } catch (ParseException e) {
337 LOG.warn("Failed to parse revision string: {}", revisionDateStr, e);
342 public void enterImport_stmt(final Import_stmtContext ctx) {
343 final int line = ctx.getStart().getLine();
344 final String importName = stringFromNode(ctx);
345 enterLog(IMPORT_STR, importName, line);
347 String importPrefix = null;
348 Date importRevision = null;
350 for (int i = 0; i < ctx.getChildCount(); ++i) {
351 final ParseTree treeNode = ctx.getChild(i);
352 if (treeNode instanceof Prefix_stmtContext) {
353 importPrefix = stringFromNode(treeNode);
354 } else if (treeNode instanceof Revision_date_stmtContext) {
355 String importRevisionStr = stringFromNode(treeNode);
357 importRevision = SIMPLE_DATE_FORMAT.parse(importRevisionStr);
358 } catch (ParseException e) {
359 LOG.warn("Failed to parse import revision-date at line {}: {}", line, importRevisionStr, e);
363 moduleBuilder.addModuleImport(importName, importRevision, importPrefix);
364 LOG.trace("setting import ({}; {}; {})", importName, importRevision, importPrefix);
368 public void exitImport_stmt(final Import_stmtContext ctx) {
373 public void enterInclude_stmt(YangParser.Include_stmtContext ctx) {
374 final int line = ctx.getStart().getLine();
375 final String includeName = stringFromNode(ctx);
376 enterLog(IMPORT_STR, includeName, line);
378 Date includeRevision = null;
379 for (int i = 0; i < ctx.getChildCount(); i++) {
380 final ParseTree treeNode = ctx.getChild(i);
381 if (treeNode instanceof Revision_date_stmtContext) {
382 final String importRevisionStr = stringFromNode(treeNode);
384 includeRevision = SIMPLE_DATE_FORMAT.parse(importRevisionStr);
385 } catch (ParseException e) {
386 LOG.warn("Failed to parse import revision-date at line {}: {}", line, importRevisionStr, e);
390 moduleBuilder.addInclude(includeName, includeRevision);
394 public void exitInclude_stmt(YangParser.Include_stmtContext ctx) {
399 public void enterAugment_stmt(final YangParser.Augment_stmtContext ctx) {
400 final int line = ctx.getStart().getLine();
401 final String augmentPath = stringFromNode(ctx);
402 enterLog(AUGMENT_STR, augmentPath, line);
405 final SchemaPath targetPath = parseXPathString(augmentPath, line);
406 final AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, targetPath,
409 for (int i = 0; i < ctx.getChildCount(); i++) {
410 ParseTree child = ctx.getChild(i);
411 if (child instanceof Description_stmtContext) {
412 builder.setDescription(stringFromNode(child));
413 } else if (child instanceof Reference_stmtContext) {
414 builder.setReference(stringFromNode(child));
415 } else if (child instanceof Status_stmtContext) {
416 builder.setStatus(parseStatus((Status_stmtContext) child));
417 } else if (child instanceof When_stmtContext) {
418 builder.addWhenCondition(stringFromNode(child));
422 moduleBuilder.enterNode(builder);
426 public void exitAugment_stmt(final YangParser.Augment_stmtContext ctx) {
427 moduleBuilder.exitNode();
428 exitLog(AUGMENT_STR);
433 public void enterExtension_stmt(final YangParser.Extension_stmtContext ctx) {
434 final int line = ctx.getStart().getLine();
435 final String extName = stringFromNode(ctx);
436 enterLog("extension", extName, line);
437 final QName qname = QName.create(moduleQName, extName);
438 final SchemaPath path = stack.addNodeToPath(qname);
440 final ExtensionBuilder builder = moduleBuilder.addExtension(qname, line, path);
441 parseSchemaNodeArgs(ctx, builder);
443 String argument = null;
445 for (int i = 0; i < ctx.getChildCount(); i++) {
446 final ParseTree child = ctx.getChild(i);
447 if (child instanceof Argument_stmtContext) {
448 argument = stringFromNode(child);
449 yin = parseYinValue((Argument_stmtContext) child);
453 builder.setArgument(argument);
454 builder.setYinElement(yin);
456 moduleBuilder.enterNode(builder);
460 public void exitExtension_stmt(final YangParser.Extension_stmtContext ctx) {
461 moduleBuilder.exitNode();
462 exitLog("extension", stack.removeNodeFromPath());
466 public void enterTypedef_stmt(final YangParser.Typedef_stmtContext ctx) {
467 final int line = ctx.getStart().getLine();
468 final String typedefName = stringFromNode(ctx);
469 enterLog("typedef", typedefName, line);
470 final QName typedefQName = QName.create(moduleQName, typedefName);
471 final SchemaPath path = stack.addNodeToPath(typedefQName);
473 final TypeDefinitionBuilder builder = moduleBuilder.addTypedef(line, typedefQName, path);
474 parseSchemaNodeArgs(ctx, builder);
475 builder.setUnits(parseUnits(ctx));
476 builder.setDefaultValue(parseDefault(ctx));
478 moduleBuilder.enterNode(builder);
482 public void exitTypedef_stmt(final YangParser.Typedef_stmtContext ctx) {
483 moduleBuilder.exitNode();
484 exitLog("typedef", stack.removeNodeFromPath());
488 public void enterType_stmt(final YangParser.Type_stmtContext ctx) {
489 final int line = ctx.getStart().getLine();
490 final String typeName = stringFromNode(ctx);
491 enterLog("type", typeName, line);
493 final QName typeQName = parseQName(typeName, line);
495 TypeDefinition<?> type;
496 Type_body_stmtsContext typeBody = null;
497 for (int i = 0; i < ctx.getChildCount(); i++) {
498 if (ctx.getChild(i) instanceof Type_body_stmtsContext) {
499 typeBody = (Type_body_stmtsContext) ctx.getChild(i);
504 // if this is base yang type...
505 if (BaseTypes.isYangBuildInType(typeName)) {
506 if (typeBody == null) {
507 // check for types which must have body
508 checkMissingBody(typeName, moduleName, line);
509 // if there are no constraints, just grab default base yang type
510 type = BaseTypes.defaultBaseTypeFor(typeName).orNull();
511 stack.addNodeToPath(type.getQName());
512 moduleBuilder.setType(type);
517 stack.addNodeToPath(BaseTypes.UNION_QNAME);
518 final UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, moduleQName.getModule());
519 final Builder parentBuilder = moduleBuilder.getActualNode();
520 unionBuilder.setParent(parentBuilder);
521 moduleBuilder.enterNode(unionBuilder);
524 qname = BaseTypes.IDENTITYREF_QNAME;
525 final SchemaPath path = stack.addNodeToPath(qname);
526 moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody));
529 type = parseTypeWithBody(typeName, typeBody, stack.currentSchemaPath(), moduleQName,
530 moduleBuilder.getActualNode());
531 moduleBuilder.setType(type);
532 stack.addNodeToPath(type.getQName());
536 final TypeAwareBuilder parent = (TypeAwareBuilder) moduleBuilder.getActualNode();
537 if (typeBody == null) {
538 parent.setTypeQName(typeQName);
539 moduleBuilder.markActualNodeDirty();
541 ParserListenerUtils.parseUnknownTypeWithBody(typeBody, parent, typeQName, moduleBuilder, moduleQName,
542 stack.currentSchemaPath());
544 stack.addNodeToPath(QName.create(moduleQName.getModule(), typeQName.getLocalName()));
549 * Method transforms string representation of yang element (i.e. leaf name,
550 * container name etc.) into QName. The namespace of QName is assigned from
551 * parent module same as revision date of module. If String qname parameter
552 * contains ":" the string is evaluated as prefix:name of element. In this
553 * case method will look into import map and extract correct ModuleImport.
554 * If such import is not present in import map the method will throw
555 * {@link YangParseException} <br>
556 * If ModuleImport is present but the value of namespace in ModuleImport is
557 * <code>null</code> the method will throw {@link YangParseException}
560 * QName value as String
562 * line in Yang model document where QName occur.
563 * @return transformed string qname parameter as QName structure.
565 * @throws YangParseException
567 private QName parseQName(final String qnameString, final int line) {
569 if (qnameString.indexOf(':') == -1) {
570 qname = QName.create(moduleQName, qnameString);
572 final Iterator<String> split = COLON_SPLITTER.split(qnameString).iterator();
573 final String prefix = split.next();
574 final String name = split.next();
575 if (prefix.equals(moduleBuilder.getPrefix())) {
576 qname = QName.create(moduleQName, name);
578 final ModuleImport imp = moduleBuilder.getImport(prefix);
580 LOG.debug("Error in module {} at line {}: No import found with prefix {}", moduleName, line, prefix);
581 throw new YangParseException(moduleName, line, "Error in module " + moduleName
582 + " No import found with prefix " + prefix + " not found.");
584 Date revision = imp.getRevision();
585 NavigableMap<Date, URI> namespaces = namespaceContext.get(imp.getModuleName());
586 if (namespaces == null) {
587 throw new YangParseException(moduleName, line, String.format("Imported module %s not found",
588 imp.getModuleName()));
591 if (revision == null) {
592 revision = namespaces.lastEntry().getKey();
593 namespace = namespaces.lastEntry().getValue();
595 // FIXME: this lookup does not look right, as we will end up
597 // a qname which does not have a namespace. At any rate we
598 // should arrive at a QNameModule!
599 namespace = namespaces.get(revision);
602 final QNameModule mod = QNameModule.cachedReference(QNameModule.create(namespace, revision));
603 qname = QName.create(mod, name);
610 public void exitType_stmt(final YangParser.Type_stmtContext ctx) {
611 final String typeName = stringFromNode(ctx);
612 if (UNION_STR.equals(typeName)) {
613 moduleBuilder.exitNode();
615 exitLog("type", stack.removeNodeFromPath());
619 public void enterGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
620 final int line = ctx.getStart().getLine();
621 final String groupName = stringFromNode(ctx);
622 enterLog("grouping", groupName, line);
623 final QName groupQName = QName.create(moduleQName, groupName);
624 final SchemaPath path = stack.addNodeToPath(groupQName);
626 final GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName, path);
627 parseSchemaNodeArgs(ctx, builder);
629 moduleBuilder.enterNode(builder);
633 public void exitGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
634 moduleBuilder.exitNode();
635 exitLog("grouping", stack.removeNodeFromPath());
639 public void enterContainer_stmt(final Container_stmtContext ctx) {
640 final int line = ctx.getStart().getLine();
641 final String containerName = stringFromNode(ctx);
642 enterLog("container", containerName, line);
644 final QName containerQName = QName.create(moduleQName, containerName);
645 final SchemaPath path = stack.addNodeToPath(containerQName);
647 final ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(line, containerQName, path);
648 parseSchemaNodeArgs(ctx, builder);
649 parseConstraints(ctx, builder.getConstraints());
650 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
652 for (int i = 0; i < ctx.getChildCount(); ++i) {
653 final ParseTree childNode = ctx.getChild(i);
654 if (childNode instanceof Presence_stmtContext) {
655 builder.setPresence(true);
660 moduleBuilder.enterNode(builder);
664 public void exitContainer_stmt(final Container_stmtContext ctx) {
665 moduleBuilder.exitNode();
666 exitLog("container", stack.removeNodeFromPath());
670 public void enterLeaf_stmt(final Leaf_stmtContext ctx) {
671 final int line = ctx.getStart().getLine();
672 final String leafName = stringFromNode(ctx);
673 enterLog("leaf", leafName, line);
675 final QName leafQName = QName.create(moduleQName, leafName);
676 final SchemaPath path = stack.addNodeToPath(leafQName);
678 final LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, path);
679 parseSchemaNodeArgs(ctx, builder);
680 parseConstraints(ctx, builder.getConstraints());
681 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
683 String defaultStr = null;
684 String unitsStr = null;
685 for (int i = 0; i < ctx.getChildCount(); i++) {
686 final ParseTree child = ctx.getChild(i);
687 if (child instanceof Default_stmtContext) {
688 defaultStr = stringFromNode(child);
689 } else if (child instanceof Units_stmtContext) {
690 unitsStr = stringFromNode(child);
693 builder.setDefaultStr(defaultStr);
694 builder.setUnits(unitsStr);
696 moduleBuilder.enterNode(builder);
700 public void exitLeaf_stmt(final YangParser.Leaf_stmtContext ctx) {
701 moduleBuilder.exitNode();
702 exitLog("leaf", stack.removeNodeFromPath());
706 public void enterUses_stmt(final YangParser.Uses_stmtContext ctx) {
707 final int line = ctx.getStart().getLine();
708 final String groupingPathStr = stringFromNode(ctx);
709 final SchemaPath groupingPath = parseXPathString(groupingPathStr, line);
710 enterLog("uses", groupingPathStr, line);
712 final UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPath);
714 moduleBuilder.enterNode(builder);
718 public void exitUses_stmt(final YangParser.Uses_stmtContext ctx) {
719 moduleBuilder.exitNode();
724 public void enterUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
725 final int line = ctx.getStart().getLine();
726 final String augmentPath = stringFromNode(ctx);
727 enterLog(AUGMENT_STR, augmentPath, line);
730 final SchemaPath targetPath = parseXPathString(augmentPath, line);
731 final AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, targetPath,
734 for (int i = 0; i < ctx.getChildCount(); i++) {
735 final ParseTree child = ctx.getChild(i);
736 if (child instanceof Description_stmtContext) {
737 builder.setDescription(stringFromNode(child));
738 } else if (child instanceof Reference_stmtContext) {
739 builder.setReference(stringFromNode(child));
740 } else if (child instanceof Status_stmtContext) {
741 builder.setStatus(parseStatus((Status_stmtContext) child));
742 } else if (child instanceof When_stmtContext) {
743 builder.addWhenCondition(stringFromNode(child));
747 moduleBuilder.enterNode(builder);
751 public void exitUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
752 moduleBuilder.exitNode();
753 exitLog(AUGMENT_STR);
758 public void enterRefine_stmt(final YangParser.Refine_stmtContext ctx) {
759 final String refineString = stringFromNode(ctx);
760 enterLog("refine", refineString, ctx.getStart().getLine());
762 final RefineHolderImpl refine = parseRefine(ctx, moduleName);
763 moduleBuilder.addRefine(refine);
764 moduleBuilder.enterNode(refine);
768 public void exitRefine_stmt(final YangParser.Refine_stmtContext ctx) {
769 moduleBuilder.exitNode();
774 public void enterLeaf_list_stmt(final Leaf_list_stmtContext ctx) {
775 final int line = ctx.getStart().getLine();
776 final String leafListName = stringFromNode(ctx);
777 enterLog("leaf-list", leafListName, line);
778 final QName leafListQName = QName.create(moduleQName, leafListName);
779 final SchemaPath path = stack.addNodeToPath(leafListQName);
781 final LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, path);
782 moduleBuilder.enterNode(builder);
784 parseSchemaNodeArgs(ctx, builder);
785 parseConstraints(ctx, builder.getConstraints());
786 builder.setConfiguration(getConfig(ctx, builder, moduleName, ctx.getStart().getLine()));
788 for (int i = 0; i < ctx.getChildCount(); ++i) {
789 final ParseTree childNode = ctx.getChild(i);
790 if (childNode instanceof Ordered_by_stmtContext) {
791 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
792 final boolean userOrdered = parseUserOrdered(orderedBy);
793 builder.setUserOrdered(userOrdered);
800 public void exitLeaf_list_stmt(final YangParser.Leaf_list_stmtContext ctx) {
801 moduleBuilder.exitNode();
802 exitLog("leaf-list", stack.removeNodeFromPath());
806 public void enterList_stmt(final List_stmtContext ctx) {
807 final int line = ctx.getStart().getLine();
808 final String listName = stringFromNode(ctx);
809 enterLog("list", listName, line);
811 final QName listQName = QName.create(moduleQName, listName);
812 final SchemaPath path = stack.addNodeToPath(listQName);
814 final ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, path);
815 moduleBuilder.enterNode(builder);
817 parseSchemaNodeArgs(ctx, builder);
818 parseConstraints(ctx, builder.getConstraints());
819 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
821 for (int i = 0; i < ctx.getChildCount(); ++i) {
822 final ParseTree childNode = ctx.getChild(i);
823 if (childNode instanceof Ordered_by_stmtContext) {
824 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
825 final boolean userOrdered = parseUserOrdered(orderedBy);
826 builder.setUserOrdered(userOrdered);
827 } else if (childNode instanceof Key_stmtContext) {
828 final Set<String> key = createListKey((Key_stmtContext) childNode);
829 builder.setKeys(key);
830 } else if (childNode instanceof YangParser.Identifier_stmtContext
831 && UNION_STR.equals(childNode.getChild(0).toString())) {
832 throw new YangParseException(moduleName, line, "Union statement is not allowed inside a list statement");
838 public void exitList_stmt(final List_stmtContext ctx) {
839 moduleBuilder.exitNode();
840 exitLog("list", stack.removeNodeFromPath());
844 public void enterAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
845 final int line = ctx.getStart().getLine();
846 final String anyXmlName = stringFromNode(ctx);
847 enterLog("anyxml", anyXmlName, line);
849 final QName anyXmlQName = QName.create(moduleQName, anyXmlName);
850 final SchemaPath path = stack.addNodeToPath(anyXmlQName);
852 final AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, path);
853 moduleBuilder.enterNode(builder);
855 parseSchemaNodeArgs(ctx, builder);
856 parseConstraints(ctx, builder.getConstraints());
857 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
861 public void exitAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
862 moduleBuilder.exitNode();
863 exitLog("anyxml", stack.removeNodeFromPath());
867 public void enterChoice_stmt(final YangParser.Choice_stmtContext ctx) {
868 final int line = ctx.getStart().getLine();
869 final String choiceName = stringFromNode(ctx);
870 enterLog("choice", choiceName, line);
872 final QName choiceQName = QName.create(moduleQName, choiceName);
873 final SchemaPath path = stack.addNodeToPath(choiceQName);
875 final ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName, path);
876 moduleBuilder.enterNode(builder);
878 parseSchemaNodeArgs(ctx, builder);
879 parseConstraints(ctx, builder.getConstraints());
880 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
882 // set 'default' case
883 for (int i = 0; i < ctx.getChildCount(); i++) {
884 final ParseTree child = ctx.getChild(i);
885 if (child instanceof Default_stmtContext) {
886 final String defaultCase = stringFromNode(child);
887 builder.setDefaultCase(defaultCase);
894 public void exitChoice_stmt(final YangParser.Choice_stmtContext ctx) {
895 moduleBuilder.exitNode();
896 exitLog("choice", stack.removeNodeFromPath());
900 public void enterCase_stmt(final YangParser.Case_stmtContext ctx) {
901 final int line = ctx.getStart().getLine();
902 final String caseName = stringFromNode(ctx);
903 enterLog("case", caseName, line);
905 final QName caseQName = QName.create(moduleQName, caseName);
906 final SchemaPath path = stack.addNodeToPath(caseQName);
908 final ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName, path);
909 moduleBuilder.enterNode(builder);
911 parseSchemaNodeArgs(ctx, builder);
912 parseConstraints(ctx, builder.getConstraints());
916 public void exitCase_stmt(final YangParser.Case_stmtContext ctx) {
917 moduleBuilder.exitNode();
918 exitLog("case", stack.removeNodeFromPath());
922 public void enterNotification_stmt(final YangParser.Notification_stmtContext ctx) {
923 final int line = ctx.getStart().getLine();
924 final String notificationName = stringFromNode(ctx);
925 enterLog("notification", notificationName, line);
927 final QName notificationQName = QName.create(moduleQName, notificationName);
928 final SchemaPath path = stack.addNodeToPath(notificationQName);
930 final NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName, path);
931 moduleBuilder.enterNode(builder);
933 parseSchemaNodeArgs(ctx, builder);
937 public void exitNotification_stmt(final YangParser.Notification_stmtContext ctx) {
938 moduleBuilder.exitNode();
939 exitLog("notification", stack.removeNodeFromPath());
944 public void enterIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
945 handleUnknownNode(ctx.getStart().getLine(), ctx);
949 public void exitIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
950 moduleBuilder.exitNode();
951 exitLog(UNKNOWN_NODE_STR, stack.removeNodeFromPath());
955 public void enterUnknown_statement(final YangParser.Unknown_statementContext ctx) {
956 handleUnknownNode(ctx.getStart().getLine(), ctx);
960 public void exitUnknown_statement(final YangParser.Unknown_statementContext ctx) {
961 moduleBuilder.exitNode();
962 exitLog(UNKNOWN_NODE_STR, stack.removeNodeFromPath());
966 public void enterRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
967 final int line = ctx.getStart().getLine();
968 final String rpcName = stringFromNode(ctx);
969 enterLog("rpc", rpcName, line);
971 final QName rpcQName = QName.create(moduleQName, rpcName);
972 final SchemaPath path = stack.addNodeToPath(rpcQName);
974 final RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName, path);
975 moduleBuilder.enterNode(rpcBuilder);
977 parseSchemaNodeArgs(ctx, rpcBuilder);
981 public void exitRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
982 moduleBuilder.exitNode();
983 exitLog("rpc", stack.removeNodeFromPath());
987 public void enterInput_stmt(final YangParser.Input_stmtContext ctx) {
988 final int line = ctx.getStart().getLine();
989 final String input = "input";
990 enterLog(input, input, line);
992 final QName rpcQName = QName.create(moduleQName, input);
993 final SchemaPath path = stack.addNodeToPath(rpcQName);
995 final ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(line, rpcQName, path);
996 moduleBuilder.enterNode(builder);
997 builder.setConfiguration(true);
999 parseSchemaNodeArgs(ctx, builder);
1000 parseConstraints(ctx, builder.getConstraints());
1004 public void exitInput_stmt(final YangParser.Input_stmtContext ctx) {
1005 moduleBuilder.exitNode();
1006 exitLog("input", stack.removeNodeFromPath());
1010 public void enterOutput_stmt(final YangParser.Output_stmtContext ctx) {
1011 final int line = ctx.getStart().getLine();
1012 final String output = "output";
1013 enterLog(output, output, line);
1015 final QName rpcQName = QName.create(moduleQName, output);
1016 final SchemaPath path = stack.addNodeToPath(rpcQName);
1018 final ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line);
1019 moduleBuilder.enterNode(builder);
1020 builder.setConfiguration(true);
1022 parseSchemaNodeArgs(ctx, builder);
1023 parseConstraints(ctx, builder.getConstraints());
1027 public void exitOutput_stmt(final YangParser.Output_stmtContext ctx) {
1028 moduleBuilder.exitNode();
1029 exitLog("output", stack.removeNodeFromPath());
1033 public void enterFeature_stmt(final YangParser.Feature_stmtContext ctx) {
1034 final int line = ctx.getStart().getLine();
1035 final String featureName = stringFromNode(ctx);
1036 enterLog("feature", featureName, line);
1038 final QName featureQName = QName.create(moduleQName, featureName);
1039 final SchemaPath path = stack.addNodeToPath(featureQName);
1041 final FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName, path);
1042 moduleBuilder.enterNode(featureBuilder);
1044 parseSchemaNodeArgs(ctx, featureBuilder);
1048 public void exitFeature_stmt(final YangParser.Feature_stmtContext ctx) {
1049 moduleBuilder.exitNode();
1050 exitLog("feature", stack.removeNodeFromPath());
1054 public void enterDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
1055 final int line = ctx.getStart().getLine();
1056 final String targetPathStr = stringFromNode(ctx);
1057 if (!targetPathStr.startsWith("/")) {
1058 throw new YangParseException(moduleName, line,
1059 "Deviation argument string must be an absolute schema node identifier.");
1061 enterLog("deviation", targetPathStr, line);
1063 String reference = null;
1064 String deviate = null;
1066 final SchemaPath targetPath = parseXPathString(targetPathStr, line);
1067 final DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath);
1068 moduleBuilder.enterNode(builder);
1070 for (int i = 0; i < ctx.getChildCount(); i++) {
1071 ParseTree child = ctx.getChild(i);
1072 if (child instanceof Reference_stmtContext) {
1073 reference = stringFromNode(child);
1074 } else if (child instanceof Deviate_not_supported_stmtContext) {
1075 deviate = stringFromNode(child);
1076 } else if (child instanceof Deviate_add_stmtContext) {
1077 deviate = stringFromNode(child);
1078 } else if (child instanceof Deviate_replace_stmtContext) {
1079 deviate = stringFromNode(child);
1080 } else if (child instanceof Deviate_delete_stmtContext) {
1081 deviate = stringFromNode(child);
1084 builder.setReference(reference);
1085 builder.setDeviate(deviate);
1088 public SchemaPath parseXPathString(final String xpathString, final int line) {
1089 final boolean absolute = !xpathString.isEmpty() && xpathString.charAt(0) == '/';
1091 final List<QName> path = new ArrayList<>();
1092 for (String pathElement : SLASH_SPLITTER.split(xpathString)) {
1093 final Iterator<String> it = COLON_SPLITTER.split(pathElement).iterator();
1094 final String s = it.next();
1096 path.add(parseQName(pathElement, line));
1098 path.add(QName.create(moduleQName, s));
1101 return SchemaPath.create(path, absolute);
1105 public void exitDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
1106 moduleBuilder.exitNode();
1107 exitLog("deviation");
1111 public void enterIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
1112 final int line = ctx.getStart().getLine();
1113 final String identityName = stringFromNode(ctx);
1114 enterLog("identity", identityName, line);
1116 final QName identityQName = QName.create(moduleQName, identityName);
1117 final SchemaPath path = stack.addNodeToPath(identityQName);
1119 final IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line, path);
1120 moduleBuilder.enterNode(builder);
1122 parseSchemaNodeArgs(ctx, builder);
1124 for (int i = 0; i < ctx.getChildCount(); i++) {
1125 final ParseTree child = ctx.getChild(i);
1126 if (child instanceof Base_stmtContext) {
1127 String baseIdentityName = stringFromNode(child);
1128 builder.setBaseIdentityName(baseIdentityName);
1134 public void exitIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
1135 moduleBuilder.exitNode();
1136 exitLog("identity", stack.removeNodeFromPath());
1139 public ModuleBuilder getModuleBuilder() {
1140 return moduleBuilder;
1143 private static void enterLog(final String p1, final String p2, final int line) {
1144 LOG.trace("entering {} {} ({})", p1, p2, line);
1147 private static void exitLog(final String p1) {
1148 LOG.trace("exiting {}", p1);
1151 private static void exitLog(final String p1, final QName p2) {
1152 LOG.trace("exiting {} {}", p1, p2.getLocalName());
1155 private static void setLog(final String p1, final String p2) {
1156 LOG.trace("setting {} {}", p1, p2);
1159 private void handleUnknownNode(final int line, final ParseTree ctx) {
1160 final String nodeParameter = stringFromNode(ctx);
1161 enterLog(UNKNOWN_NODE_STR, nodeParameter, line);
1163 final String nodeTypeStr = ctx.getChild(0).getText();
1164 final QName nodeType = parseQName(nodeTypeStr, line);
1168 // FIXME: rewrite whole method to handle unknown nodes properly.
1169 // This should be bugfix for bug
1170 // https://bugs.opendaylight.org/show_bug.cgi?id=1539
1171 // After this fix bug
1172 // https://bugs.opendaylight.org/show_bug.cgi?id=1538 MUST be fixed
1174 // they are dependent!!!
1175 if (Strings.isNullOrEmpty(nodeParameter)) {
1178 final Iterable<String> splittedName = COLON_SPLITTER.split(nodeParameter);
1179 final Iterator<String> it = splittedName.iterator();
1180 if (Iterables.size(splittedName) == 2) {
1181 qname = parseQName(nodeParameter, line);
1183 qname = QName.create(moduleQName, it.next());
1186 } catch (IllegalArgumentException | YangParseException ex) {
1190 final SchemaPath path = stack.addNodeToPath(qname);
1192 final UnknownSchemaNodeBuilderImpl builder = moduleBuilder.addUnknownSchemaNode(line, qname, path);
1193 builder.setNodeType(nodeType);
1194 builder.setNodeParameter(nodeParameter);
1196 parseSchemaNodeArgs(ctx, builder);
1197 moduleBuilder.enterNode(builder);