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.createActualSchemaPath;
12 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.createListKey;
13 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.getConfig;
14 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.getIdentityrefBase;
15 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseConstraints;
16 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseDefault;
17 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseRefine;
18 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseSchemaNodeArgs;
19 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseStatus;
20 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseTypeWithBody;
21 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseUnits;
22 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseUnknownTypeWithBody;
23 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseUserOrdered;
24 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseYinValue;
25 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.stringFromNode;
27 import com.google.common.base.Splitter;
28 import com.google.common.base.Strings;
29 import com.google.common.collect.Iterables;
32 import java.text.DateFormat;
33 import java.text.ParseException;
34 import java.text.SimpleDateFormat;
35 import java.util.Date;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.Stack;
40 import org.antlr.v4.runtime.tree.ParseTree;
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.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.model.util.YangTypesConverter;
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.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.slf4j.Logger;
101 import org.slf4j.LoggerFactory;
103 public final class YangParserListenerImpl extends YangParserBaseListener {
104 private static final Logger LOGGER = LoggerFactory.getLogger(YangParserListenerImpl.class);
105 private static final Splitter COLON_SPLITTER = Splitter.on(':');
106 private static final String AUGMENT_STR = "augment";
108 private final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
109 private final Stack<Stack<QName>> actualPath = new Stack<>();
110 private final String sourcePath;
111 private ModuleBuilder moduleBuilder;
112 private String moduleName;
113 private URI namespace;
114 private String yangModelPrefix;
115 private Date revision = new Date(0L);
116 private int augmentOrder;
118 private void addNodeToPath(final QName name) {
119 actualPath.peek().push(name);
122 private QName removeNodeFromPath() {
123 return actualPath.peek().pop();
126 public YangParserListenerImpl(final String sourcePath) {
127 this.sourcePath = sourcePath;
131 public void enterModule_stmt(final YangParser.Module_stmtContext ctx) {
132 moduleName = stringFromNode(ctx);
133 LOGGER.trace("entering module {}", moduleName);
134 enterLog("module", moduleName, 0);
135 actualPath.push(new Stack<QName>());
137 moduleBuilder = new ModuleBuilder(moduleName, sourcePath);
139 String description = null;
140 String reference = null;
141 for (int i = 0; i < ctx.getChildCount(); i++) {
142 ParseTree child = ctx.getChild(i);
143 if (child instanceof Description_stmtContext) {
144 description = stringFromNode(child);
145 } else if (child instanceof Reference_stmtContext) {
146 reference = stringFromNode(child);
148 if (description != null && reference != null) {
153 moduleBuilder.setDescription(description);
154 moduleBuilder.setReference(reference);
158 public void exitModule_stmt(final YangParser.Module_stmtContext ctx) {
163 @Override public void enterSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
164 moduleName = stringFromNode(ctx);
165 LOGGER.trace("entering submodule {}", moduleName);
166 enterLog("submodule", moduleName, 0);
167 actualPath.push(new Stack<QName>());
169 moduleBuilder = new ModuleBuilder(moduleName, true, sourcePath);
171 String description = null;
172 String reference = null;
173 for (int i = 0; i < ctx.getChildCount(); i++) {
174 ParseTree child = ctx.getChild(i);
175 if (child instanceof Description_stmtContext) {
176 description = stringFromNode(child);
177 } else if (child instanceof Reference_stmtContext) {
178 reference = stringFromNode(child);
180 if (description != null && reference != null) {
185 moduleBuilder.setDescription(description);
186 moduleBuilder.setReference(reference);
190 public void exitSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
191 exitLog("submodule");
196 public void enterBelongs_to_stmt(final YangParser.Belongs_to_stmtContext ctx) {
197 moduleBuilder.setBelongsTo(stringFromNode(ctx));
201 public void enterModule_header_stmts(final Module_header_stmtsContext ctx) {
202 enterLog("module_header", "", ctx.getStart().getLine());
203 String yangVersion = null;
204 for (int i = 0; i < ctx.getChildCount(); ++i) {
205 final ParseTree treeNode = ctx.getChild(i);
206 if (treeNode instanceof Namespace_stmtContext) {
207 final String namespaceStr = stringFromNode(treeNode);
208 namespace = URI.create(namespaceStr);
209 moduleBuilder.setNamespace(namespace);
210 setLog("namespace", namespaceStr);
211 } else if (treeNode instanceof Prefix_stmtContext) {
212 yangModelPrefix = stringFromNode(treeNode);
213 moduleBuilder.setPrefix(yangModelPrefix);
214 setLog("prefix", yangModelPrefix);
215 } else if (treeNode instanceof Yang_version_stmtContext) {
216 yangVersion = stringFromNode(treeNode);
217 setLog("yang-version", yangVersion);
221 if (yangVersion == null) {
224 moduleBuilder.setYangVersion(yangVersion);
228 public void exitModule_header_stmts(final Module_header_stmtsContext ctx) {
229 exitLog("module_header");
233 public void enterMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
234 enterLog("meta_stmt", "", ctx.getStart().getLine());
235 for (int i = 0; i < ctx.getChildCount(); i++) {
236 ParseTree child = ctx.getChild(i);
237 if (child instanceof Organization_stmtContext) {
238 final String organization = stringFromNode(child);
239 moduleBuilder.setOrganization(organization);
240 setLog("organization", organization);
241 } else if (child instanceof Contact_stmtContext) {
242 final String contact = stringFromNode(child);
243 moduleBuilder.setContact(contact);
244 setLog("contact", contact);
245 } else if (child instanceof Description_stmtContext) {
246 final String description = stringFromNode(child);
247 moduleBuilder.setDescription(description);
248 setLog("description", description);
249 } else if (child instanceof Reference_stmtContext) {
250 final String reference = stringFromNode(child);
251 moduleBuilder.setReference(reference);
252 setLog("reference", reference);
258 public void exitMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
259 exitLog("meta_stmt");
263 public void enterRevision_stmts(final Revision_stmtsContext ctx) {
264 enterLog("revisions", "", ctx.getStart().getLine());
265 for (int i = 0; i < ctx.getChildCount(); ++i) {
266 final ParseTree treeNode = ctx.getChild(i);
267 if (treeNode instanceof Revision_stmtContext) {
268 updateRevisionForRevisionStatement(treeNode);
274 public void exitRevision_stmts(final Revision_stmtsContext ctx) {
275 exitLog("revisions");
278 private void updateRevisionForRevisionStatement(final ParseTree treeNode) {
279 final String revisionDateStr = stringFromNode(treeNode);
281 final Date revisionDate = SIMPLE_DATE_FORMAT.parse(revisionDateStr);
282 if ((revisionDate != null) && (this.revision.compareTo(revisionDate) < 0)) {
283 this.revision = revisionDate;
284 moduleBuilder.setRevision(this.revision);
285 setLog("revision", this.revision.toString());
286 for (int i = 0; i < treeNode.getChildCount(); ++i) {
287 ParseTree child = treeNode.getChild(i);
288 if (child instanceof Reference_stmtContext) {
289 moduleBuilder.setReference(stringFromNode(child));
293 } catch (ParseException e) {
294 final String message = "Failed to parse revision string: " + revisionDateStr;
295 LOGGER.warn(message);
300 public void enterImport_stmt(final Import_stmtContext ctx) {
301 final int line = ctx.getStart().getLine();
302 final String importName = stringFromNode(ctx);
303 enterLog("import", importName, line);
305 String importPrefix = null;
306 Date importRevision = null;
308 for (int i = 0; i < ctx.getChildCount(); ++i) {
309 final ParseTree treeNode = ctx.getChild(i);
310 if (treeNode instanceof Prefix_stmtContext) {
311 importPrefix = stringFromNode(treeNode);
313 if (treeNode instanceof Revision_date_stmtContext) {
314 String importRevisionStr = stringFromNode(treeNode);
316 importRevision = SIMPLE_DATE_FORMAT.parse(importRevisionStr);
317 } catch (ParseException e) {
318 LOGGER.warn("Failed to parse import revision-date at line " + line + ": " + importRevisionStr);
322 moduleBuilder.addModuleImport(importName, importRevision, importPrefix);
323 setLog("import", "(" + importName + "; " + importRevision + "; " + importPrefix + ")");
327 public void exitImport_stmt(final Import_stmtContext ctx) {
332 public void enterAugment_stmt(final YangParser.Augment_stmtContext ctx) {
333 final int line = ctx.getStart().getLine();
334 final String augmentPath = stringFromNode(ctx);
335 enterLog(AUGMENT_STR, augmentPath, line);
336 actualPath.push(new Stack<QName>());
338 AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, augmentOrder++);
340 for (int i = 0; i < ctx.getChildCount(); i++) {
341 ParseTree child = ctx.getChild(i);
342 if (child instanceof Description_stmtContext) {
343 builder.setDescription(stringFromNode(child));
344 } else if (child instanceof Reference_stmtContext) {
345 builder.setReference(stringFromNode(child));
346 } else if (child instanceof Status_stmtContext) {
347 builder.setStatus(parseStatus((Status_stmtContext) child));
348 } else if (child instanceof When_stmtContext) {
349 builder.addWhenCondition(stringFromNode(child));
353 moduleBuilder.enterNode(builder);
357 public void exitAugment_stmt(final YangParser.Augment_stmtContext ctx) {
358 moduleBuilder.exitNode();
359 exitLog(AUGMENT_STR);
364 public void enterExtension_stmt(final YangParser.Extension_stmtContext ctx) {
365 final int line = ctx.getStart().getLine();
366 final String extName = stringFromNode(ctx);
367 enterLog("extension", extName, line);
368 QName qname = new QName(namespace, revision, yangModelPrefix, extName);
369 addNodeToPath(qname);
370 SchemaPath path = createActualSchemaPath(actualPath.peek());
372 ExtensionBuilder builder = moduleBuilder.addExtension(qname, line, path);
373 parseSchemaNodeArgs(ctx, builder);
375 String argument = null;
377 for (int i = 0; i < ctx.getChildCount(); i++) {
378 ParseTree child = ctx.getChild(i);
379 if (child instanceof Argument_stmtContext) {
380 argument = stringFromNode(child);
381 yin = parseYinValue((Argument_stmtContext) child);
385 builder.setArgument(argument);
386 builder.setYinElement(yin);
388 moduleBuilder.enterNode(builder);
392 public void exitExtension_stmt(final YangParser.Extension_stmtContext ctx) {
393 moduleBuilder.exitNode();
394 exitLog("extension", removeNodeFromPath());
398 public void enterTypedef_stmt(final YangParser.Typedef_stmtContext ctx) {
399 final int line = ctx.getStart().getLine();
400 final String typedefName = stringFromNode(ctx);
401 enterLog("typedef", typedefName, line);
402 QName typedefQName = new QName(namespace, revision, yangModelPrefix, typedefName);
403 addNodeToPath(typedefQName);
404 SchemaPath path = createActualSchemaPath(actualPath.peek());
406 TypeDefinitionBuilder builder = moduleBuilder.addTypedef(line, typedefQName, path);
407 parseSchemaNodeArgs(ctx, builder);
408 builder.setUnits(parseUnits(ctx));
409 builder.setDefaultValue(parseDefault(ctx));
411 moduleBuilder.enterNode(builder);
415 public void exitTypedef_stmt(final YangParser.Typedef_stmtContext ctx) {
416 moduleBuilder.exitNode();
417 exitLog("typedef", removeNodeFromPath());
421 public void enterType_stmt(final YangParser.Type_stmtContext ctx) {
422 final int line = ctx.getStart().getLine();
423 final String typeName = stringFromNode(ctx);
424 enterLog("type", typeName, line);
426 final QName typeQName = parseQName(typeName);
428 TypeDefinition<?> type;
429 Type_body_stmtsContext typeBody = null;
430 for (int i = 0; i < ctx.getChildCount(); i++) {
431 if (ctx.getChild(i) instanceof Type_body_stmtsContext) {
432 typeBody = (Type_body_stmtsContext) ctx.getChild(i);
437 // if this is base yang type...
438 if (YangTypesConverter.isBaseYangType(typeName)) {
439 if (typeBody == null) {
440 // check for types which must have body
441 checkMissingBody(typeName, moduleName, line);
442 // if there are no constraints, just grab default base yang type
443 type = YangTypesConverter.javaTypeForBaseYangType(typeName);
444 addNodeToPath(type.getQName());
445 moduleBuilder.setType(type);
450 qname = BaseTypes.constructQName("union");
451 addNodeToPath(qname);
452 UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, namespace, revision);
453 Builder parent = moduleBuilder.getActualNode();
454 unionBuilder.setParent(parent);
455 moduleBuilder.enterNode(unionBuilder);
458 qname = BaseTypes.constructQName("identityref");
459 addNodeToPath(qname);
460 SchemaPath path = createActualSchemaPath(actualPath.peek());
461 moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody));
464 type = parseTypeWithBody(typeName, typeBody, actualPath.peek(), namespace, revision,
465 yangModelPrefix, moduleBuilder.getActualNode());
466 moduleBuilder.setType(type);
467 addNodeToPath(type.getQName());
471 type = parseUnknownTypeWithBody(typeQName, typeBody, actualPath.peek(), namespace, revision,
472 yangModelPrefix, moduleBuilder.getActualNode());
473 // add parent node of this type statement to dirty nodes
474 moduleBuilder.markActualNodeDirty();
475 moduleBuilder.setType(type);
476 addNodeToPath(type.getQName());
481 private QName parseQName(final String typeName) {
482 final QName typeQName;
483 if (typeName.indexOf(':') != -1) {
484 final Iterator<String> split = COLON_SPLITTER.split(typeName).iterator();
485 final String prefix = split.next();
486 final String name = split.next();
487 if (prefix.equals(yangModelPrefix)) {
488 typeQName = new QName(namespace, revision, prefix, name);
490 typeQName = new QName(null, null, prefix, name);
493 typeQName = new QName(namespace, revision, yangModelPrefix, typeName);
499 public void exitType_stmt(final YangParser.Type_stmtContext ctx) {
500 final String typeName = stringFromNode(ctx);
501 if ("union".equals(typeName)) {
502 moduleBuilder.exitNode();
504 exitLog("type", removeNodeFromPath());
508 public void enterGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
509 final int line = ctx.getStart().getLine();
510 final String groupName = stringFromNode(ctx);
511 enterLog("grouping", groupName, line);
512 QName groupQName = new QName(namespace, revision, yangModelPrefix, groupName);
513 addNodeToPath(groupQName);
514 SchemaPath path = createActualSchemaPath(actualPath.peek());
516 GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName, path);
517 parseSchemaNodeArgs(ctx, builder);
519 moduleBuilder.enterNode(builder);
523 public void exitGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
524 moduleBuilder.exitNode();
525 exitLog("grouping", removeNodeFromPath());
529 public void enterContainer_stmt(final Container_stmtContext ctx) {
530 final int line = ctx.getStart().getLine();
531 final String containerName = stringFromNode(ctx);
532 enterLog("container", containerName, line);
534 QName containerQName = new QName(namespace, revision, yangModelPrefix, containerName);
535 addNodeToPath(containerQName);
536 SchemaPath path = createActualSchemaPath(actualPath.peek());
538 ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(line, containerQName, path);
539 parseSchemaNodeArgs(ctx, builder);
540 parseConstraints(ctx, builder.getConstraints());
541 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
543 for (int i = 0; i < ctx.getChildCount(); ++i) {
544 final ParseTree childNode = ctx.getChild(i);
545 if (childNode instanceof Presence_stmtContext) {
546 builder.setPresence(true);
551 moduleBuilder.enterNode(builder);
555 public void exitContainer_stmt(final Container_stmtContext ctx) {
556 moduleBuilder.exitNode();
557 exitLog("container", removeNodeFromPath());
561 public void enterLeaf_stmt(final Leaf_stmtContext ctx) {
562 final int line = ctx.getStart().getLine();
563 final String leafName = stringFromNode(ctx);
564 enterLog("leaf", leafName, line);
566 QName leafQName = new QName(namespace, revision, yangModelPrefix, leafName);
567 addNodeToPath(leafQName);
568 SchemaPath path = createActualSchemaPath(actualPath.peek());
570 LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, path);
571 parseSchemaNodeArgs(ctx, builder);
572 parseConstraints(ctx, builder.getConstraints());
573 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
575 String defaultStr = null;
576 String unitsStr = null;
577 for (int i = 0; i < ctx.getChildCount(); i++) {
578 ParseTree child = ctx.getChild(i);
579 if (child instanceof Default_stmtContext) {
580 defaultStr = stringFromNode(child);
581 } else if (child instanceof Units_stmtContext) {
582 unitsStr = stringFromNode(child);
585 builder.setDefaultStr(defaultStr);
586 builder.setUnits(unitsStr);
588 moduleBuilder.enterNode(builder);
592 public void exitLeaf_stmt(final YangParser.Leaf_stmtContext ctx) {
593 moduleBuilder.exitNode();
594 exitLog("leaf", removeNodeFromPath());
598 public void enterUses_stmt(final YangParser.Uses_stmtContext ctx) {
599 final int line = ctx.getStart().getLine();
600 final String groupingPathStr = stringFromNode(ctx);
601 enterLog("uses", groupingPathStr, line);
603 UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPathStr);
605 moduleBuilder.enterNode(builder);
609 public void exitUses_stmt(final YangParser.Uses_stmtContext ctx) {
610 moduleBuilder.exitNode();
615 public void enterUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
616 actualPath.push(new Stack<QName>());
617 final int line = ctx.getStart().getLine();
618 final String augmentPath = stringFromNode(ctx);
619 enterLog(AUGMENT_STR, augmentPath, line);
621 AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, augmentOrder++);
623 for (int i = 0; i < ctx.getChildCount(); i++) {
624 ParseTree child = ctx.getChild(i);
625 if (child instanceof Description_stmtContext) {
626 builder.setDescription(stringFromNode(child));
627 } else if (child instanceof Reference_stmtContext) {
628 builder.setReference(stringFromNode(child));
629 } else if (child instanceof Status_stmtContext) {
630 builder.setStatus(parseStatus((Status_stmtContext) child));
631 } else if (child instanceof When_stmtContext) {
632 builder.addWhenCondition(stringFromNode(child));
636 moduleBuilder.enterNode(builder);
640 public void exitUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
641 moduleBuilder.exitNode();
642 exitLog(AUGMENT_STR);
647 public void enterRefine_stmt(final YangParser.Refine_stmtContext ctx) {
648 final String refineString = stringFromNode(ctx);
649 enterLog("refine", refineString, ctx.getStart().getLine());
651 RefineHolderImpl refine = parseRefine(ctx, moduleName);
652 moduleBuilder.addRefine(refine);
653 moduleBuilder.enterNode(refine);
657 public void exitRefine_stmt(final YangParser.Refine_stmtContext ctx) {
658 moduleBuilder.exitNode();
663 public void enterLeaf_list_stmt(final Leaf_list_stmtContext ctx) {
664 final int line = ctx.getStart().getLine();
665 final String leafListName = stringFromNode(ctx);
666 enterLog("leaf-list", leafListName, line);
667 QName leafListQName = new QName(namespace, revision, yangModelPrefix, leafListName);
668 addNodeToPath(leafListQName);
669 SchemaPath path = createActualSchemaPath(actualPath.peek());
671 LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, path);
672 moduleBuilder.enterNode(builder);
674 parseSchemaNodeArgs(ctx, builder);
675 parseConstraints(ctx, builder.getConstraints());
676 builder.setConfiguration(getConfig(ctx, builder, moduleName, ctx.getStart().getLine()));
678 for (int i = 0; i < ctx.getChildCount(); ++i) {
679 final ParseTree childNode = ctx.getChild(i);
680 if (childNode instanceof Ordered_by_stmtContext) {
681 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
682 final boolean userOrdered = parseUserOrdered(orderedBy);
683 builder.setUserOrdered(userOrdered);
690 public void exitLeaf_list_stmt(final YangParser.Leaf_list_stmtContext ctx) {
691 moduleBuilder.exitNode();
692 exitLog("leaf-list", removeNodeFromPath());
696 public void enterList_stmt(final List_stmtContext ctx) {
697 final int line = ctx.getStart().getLine();
698 final String listName = stringFromNode(ctx);
699 enterLog("list", listName, line);
701 QName listQName = new QName(namespace, revision, yangModelPrefix, listName);
702 addNodeToPath(listQName);
703 SchemaPath path = createActualSchemaPath(actualPath.peek());
705 ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, path);
706 moduleBuilder.enterNode(builder);
708 parseSchemaNodeArgs(ctx, builder);
709 parseConstraints(ctx, builder.getConstraints());
710 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
712 for (int i = 0; i < ctx.getChildCount(); ++i) {
713 ParseTree childNode = ctx.getChild(i);
714 if (childNode instanceof Ordered_by_stmtContext) {
715 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
716 final boolean userOrdered = parseUserOrdered(orderedBy);
717 builder.setUserOrdered(userOrdered);
718 } else if (childNode instanceof Key_stmtContext) {
719 List<String> key = createListKey((Key_stmtContext) childNode);
720 builder.setKeys(key);
726 public void exitList_stmt(final List_stmtContext ctx) {
727 moduleBuilder.exitNode();
728 exitLog("list", removeNodeFromPath());
732 public void enterAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
733 final int line = ctx.getStart().getLine();
734 final String anyXmlName = stringFromNode(ctx);
735 enterLog("anyxml", anyXmlName, line);
737 QName anyXmlQName = new QName(namespace, revision, yangModelPrefix, anyXmlName);
738 addNodeToPath(anyXmlQName);
739 SchemaPath path = createActualSchemaPath(actualPath.peek());
741 AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, path);
742 moduleBuilder.enterNode(builder);
744 parseSchemaNodeArgs(ctx, builder);
745 parseConstraints(ctx, builder.getConstraints());
746 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
750 public void exitAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
751 moduleBuilder.exitNode();
752 exitLog("anyxml", removeNodeFromPath());
756 public void enterChoice_stmt(final YangParser.Choice_stmtContext ctx) {
757 final int line = ctx.getStart().getLine();
758 final String choiceName = stringFromNode(ctx);
759 enterLog("choice", choiceName, line);
761 QName choiceQName = new QName(namespace, revision, yangModelPrefix, choiceName);
762 addNodeToPath(choiceQName);
763 SchemaPath path = createActualSchemaPath(actualPath.peek());
765 ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName, path);
766 moduleBuilder.enterNode(builder);
768 parseSchemaNodeArgs(ctx, builder);
769 parseConstraints(ctx, builder.getConstraints());
770 builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
772 // set 'default' case
773 for (int i = 0; i < ctx.getChildCount(); i++) {
774 ParseTree child = ctx.getChild(i);
775 if (child instanceof Default_stmtContext) {
776 String defaultCase = stringFromNode(child);
777 builder.setDefaultCase(defaultCase);
784 public void exitChoice_stmt(final YangParser.Choice_stmtContext ctx) {
785 moduleBuilder.exitNode();
786 exitLog("choice", removeNodeFromPath());
790 public void enterCase_stmt(final YangParser.Case_stmtContext ctx) {
791 final int line = ctx.getStart().getLine();
792 final String caseName = stringFromNode(ctx);
793 enterLog("case", caseName, line);
795 QName caseQName = new QName(namespace, revision, yangModelPrefix, caseName);
796 addNodeToPath(caseQName);
797 SchemaPath path = createActualSchemaPath(actualPath.peek());
799 ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName, path);
800 moduleBuilder.enterNode(builder);
802 parseSchemaNodeArgs(ctx, builder);
803 parseConstraints(ctx, builder.getConstraints());
807 public void exitCase_stmt(final YangParser.Case_stmtContext ctx) {
808 moduleBuilder.exitNode();
809 exitLog("case", removeNodeFromPath());
813 public void enterNotification_stmt(final YangParser.Notification_stmtContext ctx) {
814 final int line = ctx.getStart().getLine();
815 final String notificationName = stringFromNode(ctx);
816 enterLog("notification", notificationName, line);
818 QName notificationQName = new QName(namespace, revision, yangModelPrefix, notificationName);
819 addNodeToPath(notificationQName);
820 SchemaPath path = createActualSchemaPath(actualPath.peek());
822 NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName, path);
823 moduleBuilder.enterNode(builder);
825 parseSchemaNodeArgs(ctx, builder);
829 public void exitNotification_stmt(final YangParser.Notification_stmtContext ctx) {
830 moduleBuilder.exitNode();
831 exitLog("notification", removeNodeFromPath());
836 public void enterIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
837 handleUnknownNode(ctx.getStart().getLine(), ctx);
841 public void exitIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
842 moduleBuilder.exitNode();
843 exitLog("unknown-node", removeNodeFromPath());
846 @Override public void enterUnknown_statement(final YangParser.Unknown_statementContext ctx) {
847 handleUnknownNode(ctx.getStart().getLine(), ctx);
850 @Override public void exitUnknown_statement(final YangParser.Unknown_statementContext ctx) {
851 moduleBuilder.exitNode();
852 exitLog("unknown-node", removeNodeFromPath());
855 @Override public void enterUnknown_statement2(final YangParser.Unknown_statement2Context ctx) {
856 handleUnknownNode(ctx.getStart().getLine(), ctx);
859 @Override public void exitUnknown_statement2(final YangParser.Unknown_statement2Context ctx) {
860 moduleBuilder.exitNode();
861 exitLog("unknown-node", removeNodeFromPath());
864 @Override public void enterUnknown_statement3(final YangParser.Unknown_statement3Context ctx) {
865 handleUnknownNode(ctx.getStart().getLine(), ctx);
868 @Override public void exitUnknown_statement3(final YangParser.Unknown_statement3Context ctx) {
869 moduleBuilder.exitNode();
870 exitLog("unknown-node", removeNodeFromPath());
874 public void enterRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
875 final int line = ctx.getStart().getLine();
876 final String rpcName = stringFromNode(ctx);
877 enterLog("rpc", rpcName, line);
879 QName rpcQName = new QName(namespace, revision, yangModelPrefix, rpcName);
880 addNodeToPath(rpcQName);
881 SchemaPath path = createActualSchemaPath(actualPath.peek());
883 RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName, path);
884 moduleBuilder.enterNode(rpcBuilder);
887 parseSchemaNodeArgs(ctx, rpcBuilder);
891 public void exitRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
892 moduleBuilder.exitNode();
893 exitLog("rpc", removeNodeFromPath());
897 public void enterInput_stmt(final YangParser.Input_stmtContext ctx) {
898 final int line = ctx.getStart().getLine();
899 final String input = "input";
900 enterLog(input, input, line);
902 QName rpcQName = new QName(namespace, revision, yangModelPrefix, input);
903 addNodeToPath(rpcQName);
904 SchemaPath path = createActualSchemaPath(actualPath.peek());
906 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(line, rpcQName, path);
907 moduleBuilder.enterNode(builder);
908 builder.setConfiguration(true);
910 parseSchemaNodeArgs(ctx, builder);
911 parseConstraints(ctx, builder.getConstraints());
915 public void exitInput_stmt(final YangParser.Input_stmtContext ctx) {
916 moduleBuilder.exitNode();
917 exitLog("input", removeNodeFromPath());
921 public void enterOutput_stmt(final YangParser.Output_stmtContext ctx) {
922 final int line = ctx.getStart().getLine();
923 final String output = "output";
924 enterLog(output, output, line);
926 QName rpcQName = new QName(namespace, revision, yangModelPrefix, output);
927 addNodeToPath(rpcQName);
928 SchemaPath path = createActualSchemaPath(actualPath.peek());
930 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line);
931 moduleBuilder.enterNode(builder);
932 builder.setConfiguration(true);
934 parseSchemaNodeArgs(ctx, builder);
935 parseConstraints(ctx, builder.getConstraints());
939 public void exitOutput_stmt(final YangParser.Output_stmtContext ctx) {
940 moduleBuilder.exitNode();
941 exitLog("output", removeNodeFromPath());
945 public void enterFeature_stmt(final YangParser.Feature_stmtContext ctx) {
946 final int line = ctx.getStart().getLine();
947 final String featureName = stringFromNode(ctx);
948 enterLog("feature", featureName, line);
950 QName featureQName = new QName(namespace, revision, yangModelPrefix, featureName);
951 addNodeToPath(featureQName);
952 SchemaPath path = createActualSchemaPath(actualPath.peek());
954 FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName, path);
955 moduleBuilder.enterNode(featureBuilder);
957 parseSchemaNodeArgs(ctx, featureBuilder);
961 public void exitFeature_stmt(final YangParser.Feature_stmtContext ctx) {
962 moduleBuilder.exitNode();
963 exitLog("feature", removeNodeFromPath());
967 public void enterDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
968 final int line = ctx.getStart().getLine();
969 final String targetPath = stringFromNode(ctx);
970 enterLog("deviation", targetPath, line);
972 String reference = null;
973 String deviate = null;
974 DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath);
975 moduleBuilder.enterNode(builder);
977 for (int i = 0; i < ctx.getChildCount(); i++) {
978 ParseTree child = ctx.getChild(i);
979 if (child instanceof Reference_stmtContext) {
980 reference = stringFromNode(child);
981 } else if (child instanceof Deviate_not_supported_stmtContext) {
982 deviate = stringFromNode(child);
983 } else if (child instanceof Deviate_add_stmtContext) {
984 deviate = stringFromNode(child);
985 } else if (child instanceof Deviate_replace_stmtContext) {
986 deviate = stringFromNode(child);
987 } else if (child instanceof Deviate_delete_stmtContext) {
988 deviate = stringFromNode(child);
991 builder.setReference(reference);
992 builder.setDeviate(deviate);
996 public void exitDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
997 moduleBuilder.exitNode();
998 exitLog("deviation");
1002 public void enterIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
1003 final int line = ctx.getStart().getLine();
1004 final String identityName = stringFromNode(ctx);
1005 enterLog("identity", identityName, line);
1007 final QName identityQName = new QName(namespace, revision, yangModelPrefix, identityName);
1008 addNodeToPath(identityQName);
1009 SchemaPath path = createActualSchemaPath(actualPath.peek());
1011 IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line, path);
1012 moduleBuilder.enterNode(builder);
1015 parseSchemaNodeArgs(ctx, builder);
1017 for (int i = 0; i < ctx.getChildCount(); i++) {
1018 ParseTree child = ctx.getChild(i);
1019 if (child instanceof Base_stmtContext) {
1020 String baseIdentityName = stringFromNode(child);
1021 builder.setBaseIdentityName(baseIdentityName);
1027 public void exitIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
1028 moduleBuilder.exitNode();
1029 exitLog("identity", removeNodeFromPath());
1032 public ModuleBuilder getModuleBuilder() {
1033 return moduleBuilder;
1036 private void enterLog(final String p1, final String p2, final int line) {
1037 LOGGER.trace("entering {} {} ({})", p1, p2, line);
1040 private void exitLog(final String p1) {
1041 LOGGER.trace("exiting {}", p1);
1044 private void exitLog(final String p1, final QName p2) {
1045 LOGGER.trace("exiting {} {}", p1, p2.getLocalName());
1048 private void setLog(final String p1, final String p2) {
1049 LOGGER.trace("setting {} {}", p1, p2);
1052 private void handleUnknownNode(final int line, final ParseTree ctx) {
1053 final String nodeParameter = stringFromNode(ctx);
1054 enterLog("unknown-node", nodeParameter, line);
1056 final String nodeTypeStr = ctx.getChild(0).getText();
1057 final Iterator<String> splittedElement = COLON_SPLITTER.split(nodeTypeStr).iterator();
1058 final String e0 = splittedElement.next();
1059 final QName nodeType;
1060 if (splittedElement.hasNext()) {
1061 nodeType = new QName(namespace, revision, e0, splittedElement.next());
1063 nodeType = new QName(namespace, revision, yangModelPrefix, e0);
1068 if (!Strings.isNullOrEmpty(nodeParameter)) {
1069 final Iterable<String> splittedName = COLON_SPLITTER.split(nodeParameter);
1070 final Iterator<String> it = splittedName.iterator();
1072 if (Iterables.size(splittedName) == 2) {
1073 qname = new QName(null, null, it.next(), it.next());
1075 qname = new QName(namespace, revision, yangModelPrefix, it.next());
1080 } catch (IllegalArgumentException e) {
1083 addNodeToPath(qname);
1084 SchemaPath path = createActualSchemaPath(actualPath.peek());
1086 UnknownSchemaNodeBuilderImpl builder = moduleBuilder.addUnknownSchemaNode(line, qname, path);
1087 builder.setNodeType(nodeType);
1088 builder.setNodeParameter(nodeParameter);
1091 parseSchemaNodeArgs(ctx, builder);
1092 moduleBuilder.enterNode(builder);