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.controller.yang.parser.impl;
10 import static org.opendaylight.controller.yang.parser.util.YangModelBuilderUtil.*;
13 import java.text.DateFormat;
14 import java.text.ParseException;
15 import java.text.SimpleDateFormat;
16 import java.util.Date;
17 import java.util.List;
18 import java.util.Stack;
20 import org.antlr.v4.runtime.tree.ParseTree;
21 import org.opendaylight.controller.antlrv4.code.gen.*;
22 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Argument_stmtContext;
23 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Base_stmtContext;
24 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Contact_stmtContext;
25 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Container_stmtContext;
26 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Default_stmtContext;
27 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Description_stmtContext;
28 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_add_stmtContext;
29 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_delete_stmtContext;
30 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_not_supported_stmtContext;
31 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_replace_stmtContext;
32 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Import_stmtContext;
33 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Key_stmtContext;
34 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Leaf_list_stmtContext;
35 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Leaf_stmtContext;
36 import org.opendaylight.controller.antlrv4.code.gen.YangParser.List_stmtContext;
37 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Module_header_stmtsContext;
38 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Namespace_stmtContext;
39 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Ordered_by_stmtContext;
40 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Organization_stmtContext;
41 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Prefix_stmtContext;
42 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Presence_stmtContext;
43 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Reference_stmtContext;
44 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_date_stmtContext;
45 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_stmtContext;
46 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_stmtsContext;
47 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Status_stmtContext;
48 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Type_body_stmtsContext;
49 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Units_stmtContext;
50 import org.opendaylight.controller.antlrv4.code.gen.YangParser.When_stmtContext;
51 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Yang_version_stmtContext;
52 import org.opendaylight.controller.yang.common.QName;
53 import org.opendaylight.controller.yang.model.api.SchemaPath;
54 import org.opendaylight.controller.yang.model.api.TypeDefinition;
55 import org.opendaylight.controller.yang.model.util.YangTypesConverter;
56 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
57 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
58 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
59 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
60 import org.opendaylight.controller.yang.parser.builder.impl.AnyXmlBuilder;
61 import org.opendaylight.controller.yang.parser.builder.impl.ChoiceBuilder;
62 import org.opendaylight.controller.yang.parser.builder.impl.ChoiceCaseBuilder;
63 import org.opendaylight.controller.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
64 import org.opendaylight.controller.yang.parser.builder.impl.DeviationBuilder;
65 import org.opendaylight.controller.yang.parser.builder.impl.ExtensionBuilder;
66 import org.opendaylight.controller.yang.parser.builder.impl.FeatureBuilder;
67 import org.opendaylight.controller.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
68 import org.opendaylight.controller.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
69 import org.opendaylight.controller.yang.parser.builder.impl.LeafSchemaNodeBuilder;
70 import org.opendaylight.controller.yang.parser.builder.impl.ListSchemaNodeBuilder;
71 import org.opendaylight.controller.yang.parser.builder.impl.ModuleBuilder;
72 import org.opendaylight.controller.yang.parser.builder.impl.NotificationBuilder;
73 import org.opendaylight.controller.yang.parser.builder.impl.RpcDefinitionBuilder;
74 import org.opendaylight.controller.yang.parser.builder.impl.UnionTypeBuilder;
75 import org.opendaylight.controller.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
76 import org.opendaylight.controller.yang.parser.util.RefineHolder;
77 import org.slf4j.Logger;
78 import org.slf4j.LoggerFactory;
80 public final class YangParserListenerImpl extends YangParserBaseListener {
81 private static final Logger logger = LoggerFactory.getLogger(YangParserListenerImpl.class);
83 private ModuleBuilder moduleBuilder;
84 private String moduleName;
85 private URI namespace;
86 private String yangModelPrefix;
87 private Date revision = new Date(0L);
89 public final static DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
90 private final Stack<String> actualPath = new Stack<String>();
93 public void enterModule_stmt(YangParser.Module_stmtContext ctx) {
94 moduleName = stringFromNode(ctx);
95 logger.debug("enter module " + moduleName);
96 actualPath.push(moduleName);
97 moduleBuilder = new ModuleBuilder(moduleName);
99 String description = null;
100 String reference = null;
102 for (int i = 0; i < ctx.getChildCount(); i++) {
103 ParseTree child = ctx.getChild(i);
104 if (child instanceof Description_stmtContext) {
105 description = stringFromNode(child);
106 } else if (child instanceof Reference_stmtContext) {
107 reference = stringFromNode(child);
109 if (description != null && reference != null) {
114 moduleBuilder.setDescription(description);
115 moduleBuilder.setReference(reference);
119 public void exitModule_stmt(YangParser.Module_stmtContext ctx) {
120 exitLog("module", actualPath.pop());
124 public void enterModule_header_stmts(Module_header_stmtsContext ctx) {
125 enterLog("module_header", "", ctx.getStart().getLine());
126 String yangVersion = null;
127 for (int i = 0; i < ctx.getChildCount(); ++i) {
128 final ParseTree treeNode = ctx.getChild(i);
129 if (treeNode instanceof Namespace_stmtContext) {
130 final String namespaceStr = stringFromNode(treeNode);
131 namespace = URI.create(namespaceStr);
132 moduleBuilder.setNamespace(namespace);
133 setLog("namespace", namespaceStr);
134 } else if (treeNode instanceof Prefix_stmtContext) {
135 yangModelPrefix = stringFromNode(treeNode);
136 moduleBuilder.setPrefix(yangModelPrefix);
137 setLog("prefix", yangModelPrefix);
138 } else if (treeNode instanceof Yang_version_stmtContext) {
139 yangVersion = stringFromNode(treeNode);
140 setLog("yang-version", yangVersion);
144 if (yangVersion == null) {
147 moduleBuilder.setYangVersion(yangVersion);
151 public void exitModule_header_stmts(Module_header_stmtsContext ctx) {
152 exitLog("module_header", "");
156 public void enterMeta_stmts(YangParser.Meta_stmtsContext ctx) {
157 enterLog("meta_stmt", "", ctx.getStart().getLine());
158 for (int i = 0; i < ctx.getChildCount(); i++) {
159 ParseTree child = ctx.getChild(i);
160 if (child instanceof Organization_stmtContext) {
161 final String organization = stringFromNode(child);
162 moduleBuilder.setOrganization(organization);
163 setLog("organization", organization);
164 } else if (child instanceof Contact_stmtContext) {
165 final String contact = stringFromNode(child);
166 moduleBuilder.setContact(contact);
167 setLog("contact", contact);
168 } else if (child instanceof Description_stmtContext) {
169 final String description = stringFromNode(child);
170 moduleBuilder.setDescription(description);
171 setLog("description", description);
172 } else if (child instanceof Reference_stmtContext) {
173 final String reference = stringFromNode(child);
174 moduleBuilder.setReference(reference);
175 setLog("reference", reference);
181 public void exitMeta_stmts(YangParser.Meta_stmtsContext ctx) {
182 exitLog("meta_stmt", "");
186 public void enterRevision_stmts(Revision_stmtsContext ctx) {
187 enterLog("revisions", "", ctx.getStart().getLine());
189 for (int i = 0; i < ctx.getChildCount(); ++i) {
190 final ParseTree treeNode = ctx.getChild(i);
191 if (treeNode instanceof Revision_stmtContext) {
192 updateRevisionForRevisionStatement(treeNode);
199 public void exitRevision_stmts(Revision_stmtsContext ctx) {
200 exitLog("revisions", "");
203 private void updateRevisionForRevisionStatement(final ParseTree treeNode) {
204 final String revisionDateStr = stringFromNode(treeNode);
206 final Date revision = simpleDateFormat.parse(revisionDateStr);
207 if ((revision != null) && (this.revision.compareTo(revision) < 0)) {
208 this.revision = revision;
209 moduleBuilder.setRevision(this.revision);
210 setLog("revision", this.revision.toString());
211 for (int i = 0; i < treeNode.getChildCount(); ++i) {
212 ParseTree child = treeNode.getChild(i);
213 if (child instanceof Reference_stmtContext) {
214 moduleBuilder.setReference(stringFromNode(child));
218 } catch (ParseException e) {
219 final String message = "Failed to parse revision string: " + revisionDateStr;
220 logger.warn(message);
225 public void enterImport_stmt(Import_stmtContext ctx) {
226 final int line = ctx.getStart().getLine();
227 final String importName = stringFromNode(ctx);
228 enterLog("import", importName, line);
230 String importPrefix = null;
231 Date importRevision = null;
233 for (int i = 0; i < ctx.getChildCount(); ++i) {
234 final ParseTree treeNode = ctx.getChild(i);
235 if (treeNode instanceof Prefix_stmtContext) {
236 importPrefix = stringFromNode(treeNode);
238 if (treeNode instanceof Revision_date_stmtContext) {
239 String importRevisionStr = stringFromNode(treeNode);
241 importRevision = simpleDateFormat.parse(importRevisionStr);
242 } catch (ParseException e) {
243 logger.warn("Failed to parse import revision-date at line " + line + ": " + importRevisionStr);
247 moduleBuilder.addModuleImport(importName, importRevision, importPrefix);
248 setLog("import", "(" + importName + "; " + importRevision + "; " + importPrefix + ")");
252 public void exitImport_stmt(Import_stmtContext ctx) {
253 exitLog("import", "");
257 public void enterAugment_stmt(YangParser.Augment_stmtContext ctx) {
258 final int line = ctx.getStart().getLine();
259 final String augmentPath = stringFromNode(ctx);
260 enterLog("augment", augmentPath, line);
262 AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath);
264 for (int i = 0; i < ctx.getChildCount(); i++) {
265 ParseTree child = ctx.getChild(i);
266 if (child instanceof Description_stmtContext) {
267 builder.setDescription(stringFromNode(child));
268 } else if (child instanceof Reference_stmtContext) {
269 builder.setReference(stringFromNode(child));
270 } else if (child instanceof Status_stmtContext) {
271 builder.setStatus(parseStatus((Status_stmtContext) child));
272 } else if (child instanceof When_stmtContext) {
273 builder.addWhenCondition(stringFromNode(child));
277 moduleBuilder.enterNode(builder);
278 actualPath.push(augmentPath);
282 public void exitAugment_stmt(YangParser.Augment_stmtContext ctx) {
283 moduleBuilder.exitNode();
284 exitLog("augment", actualPath.pop());
288 public void enterExtension_stmt(YangParser.Extension_stmtContext ctx) {
289 final int line = ctx.getStart().getLine();
290 final String extName = stringFromNode(ctx);
291 enterLog("extension", extName, line);
293 QName qname = new QName(namespace, revision, yangModelPrefix, extName);
294 ExtensionBuilder builder = moduleBuilder.addExtension(qname, line);
295 parseSchemaNodeArgs(ctx, builder);
297 String argument = null;
299 for (int i = 0; i < ctx.getChildCount(); i++) {
300 ParseTree child = ctx.getChild(i);
301 if (child instanceof Argument_stmtContext) {
302 argument = stringFromNode(child);
303 yin = parseYinValue((Argument_stmtContext) child);
307 builder.setArgument(argument);
308 builder.setYinElement(yin);
310 moduleBuilder.enterNode(builder);
311 actualPath.push(extName);
315 public void exitExtension_stmt(YangParser.Extension_stmtContext ctx) {
316 moduleBuilder.exitNode();
317 exitLog("extension", actualPath.pop());
321 public void enterTypedef_stmt(YangParser.Typedef_stmtContext ctx) {
322 final int line = ctx.getStart().getLine();
323 final String typedefName = stringFromNode(ctx);
324 enterLog("typedef", typedefName, line);
326 QName typedefQName = new QName(namespace, revision, yangModelPrefix, typedefName);
327 TypeDefinitionBuilder builder = moduleBuilder.addTypedef(line, typedefQName);
328 moduleBuilder.enterNode(builder);
329 actualPath.push(typedefName);
331 builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix));
332 parseSchemaNodeArgs(ctx, builder);
333 builder.setUnits(parseUnits(ctx));
337 public void exitTypedef_stmt(YangParser.Typedef_stmtContext ctx) {
338 moduleBuilder.exitNode();
339 exitLog("typedef", actualPath.pop());
343 public void enterType_stmt(YangParser.Type_stmtContext ctx) {
344 final int line = ctx.getStart().getLine();
345 final String typeName = stringFromNode(ctx);
346 enterLog("type", typeName, line);
348 final QName typeQName = parseQName(typeName);
350 TypeDefinition<?> type = null;
351 Type_body_stmtsContext typeBody = null;
352 for (int i = 0; i < ctx.getChildCount(); i++) {
353 if (ctx.getChild(i) instanceof Type_body_stmtsContext) {
354 typeBody = (Type_body_stmtsContext) ctx.getChild(i);
359 // if this is base yang type...
360 if (YangTypesConverter.isBaseYangType(typeName)) {
361 if (typeBody == null) {
362 // check for types which must have body
363 checkMissingBody(typeName, moduleName, line);
364 // if there are no constraints, just grab default base yang type
365 type = YangTypesConverter.javaTypeForBaseYangType(actualPath, namespace, revision, typeName);
366 moduleBuilder.setType(type);
368 if ("union".equals(typeName)) {
369 SchemaPath p = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, typeName);
370 UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(actualPath, namespace, revision, line);
371 moduleBuilder.enterNode(unionBuilder);
372 unionBuilder.setPath(p);
373 } else if ("identityref".equals(typeName)) {
374 SchemaPath path = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, typeName);
375 moduleBuilder.addIdentityrefType(getIdentityrefBase(typeBody), actualPath, path, line);
377 type = parseTypeWithBody(moduleName, typeName, typeBody, actualPath, namespace, revision,
378 yangModelPrefix, moduleBuilder.getActualNode());
379 moduleBuilder.setType(type);
383 type = parseUnknownTypeWithBody(typeQName, typeBody, actualPath, namespace, revision, yangModelPrefix,
384 moduleBuilder.getActualNode());
385 // add parent node of this type statement to dirty nodes
386 moduleBuilder.addDirtyNode(actualPath);
387 moduleBuilder.setType(type);
390 actualPath.push(typeName);
393 private QName parseQName(String typeName) {
395 if (typeName.contains(":")) {
396 String[] splittedName = typeName.split(":");
397 String prefix = splittedName[0];
398 String name = splittedName[1];
399 if (prefix.equals(yangModelPrefix)) {
400 typeQName = new QName(namespace, revision, prefix, name);
402 typeQName = new QName(null, null, prefix, name);
405 typeQName = new QName(namespace, revision, yangModelPrefix, typeName);
411 public void exitType_stmt(YangParser.Type_stmtContext ctx) {
412 final String typeName = stringFromNode(ctx);
413 if ("union".equals(typeName)) {
414 moduleBuilder.exitNode();
416 exitLog("type", actualPath.pop());
420 public void enterGrouping_stmt(YangParser.Grouping_stmtContext ctx) {
421 final int line = ctx.getStart().getLine();
422 final String groupName = stringFromNode(ctx);
423 enterLog("grouping", groupName, line);
425 QName groupQName = new QName(namespace, revision, yangModelPrefix, groupName);
426 GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName);
427 moduleBuilder.enterNode(builder);
428 actualPath.push(groupName);
430 builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix));
431 parseSchemaNodeArgs(ctx, builder);
435 public void exitGrouping_stmt(YangParser.Grouping_stmtContext ctx) {
436 moduleBuilder.exitNode();
437 exitLog("grouping", actualPath.pop());
441 public void enterContainer_stmt(Container_stmtContext ctx) {
442 final int line = ctx.getStart().getLine();
443 final String containerName = stringFromNode(ctx);
444 enterLog("container", containerName, line);
446 QName containerQName = new QName(namespace, revision, yangModelPrefix, containerName);
447 SchemaPath path = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, containerName);
449 ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(line, containerQName, path);
450 moduleBuilder.enterNode(builder);
451 actualPath.push(containerName);
453 parseSchemaNodeArgs(ctx, builder);
454 parseConstraints(ctx, builder.getConstraints());
455 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
457 for (int i = 0; i < ctx.getChildCount(); ++i) {
458 final ParseTree childNode = ctx.getChild(i);
459 if (childNode instanceof Presence_stmtContext) {
460 builder.setPresence(true);
467 public void exitContainer_stmt(Container_stmtContext ctx) {
468 moduleBuilder.exitNode();
469 exitLog("container", actualPath.pop());
473 public void enterLeaf_stmt(Leaf_stmtContext ctx) {
474 final int line = ctx.getStart().getLine();
475 final String leafName = stringFromNode(ctx);
476 enterLog("leaf", leafName, line);
478 QName leafQName = new QName(namespace, revision, yangModelPrefix, leafName);
479 SchemaPath schemaPath = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, leafName);
481 LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, schemaPath);
482 moduleBuilder.enterNode(builder);
483 actualPath.push(leafName);
485 parseSchemaNodeArgs(ctx, builder);
486 parseConstraints(ctx, builder.getConstraints());
487 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
489 String defaultStr = null;
490 String unitsStr = null;
491 for (int i = 0; i < ctx.getChildCount(); i++) {
492 ParseTree child = ctx.getChild(i);
493 if (child instanceof Default_stmtContext) {
494 defaultStr = stringFromNode(child);
495 } else if (child instanceof Units_stmtContext) {
496 unitsStr = stringFromNode(child);
499 builder.setDefaultStr(defaultStr);
500 builder.setUnits(unitsStr);
504 public void exitLeaf_stmt(YangParser.Leaf_stmtContext ctx) {
505 moduleBuilder.exitNode();
506 exitLog("leaf", actualPath.pop());
510 public void enterUses_stmt(YangParser.Uses_stmtContext ctx) {
511 final int line = ctx.getStart().getLine();
512 final String groupingPathStr = stringFromNode(ctx);
513 enterLog("uses", groupingPathStr, line);
515 UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPathStr);
517 moduleBuilder.enterNode(builder);
518 actualPath.push(groupingPathStr);
522 public void exitUses_stmt(YangParser.Uses_stmtContext ctx) {
523 moduleBuilder.exitNode();
524 exitLog("uses", actualPath.pop());
528 public void enterRefine_stmt(YangParser.Refine_stmtContext ctx) {
529 final String refineString = stringFromNode(ctx);
530 enterLog("refine", refineString, ctx.getStart().getLine());
532 RefineHolder refine = parseRefine(ctx);
533 moduleBuilder.addRefine(refine, actualPath);
534 moduleBuilder.enterNode(refine);
535 actualPath.push(refineString);
539 public void exitRefine_stmt(YangParser.Refine_stmtContext ctx) {
540 moduleBuilder.exitNode();
541 exitLog("refine", actualPath.pop());
545 public void enterLeaf_list_stmt(Leaf_list_stmtContext ctx) {
546 final int line = ctx.getStart().getLine();
547 final String leafListName = stringFromNode(ctx);
548 enterLog("leaf-list", leafListName, line);
550 QName leafListQName = new QName(namespace, revision, yangModelPrefix, leafListName);
551 SchemaPath schemaPath = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, leafListName);
553 LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, schemaPath);
554 moduleBuilder.enterNode(builder);
555 actualPath.push(leafListName);
557 parseSchemaNodeArgs(ctx, builder);
558 parseConstraints(ctx, builder.getConstraints());
559 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, ctx.getStart().getLine()));
561 for (int i = 0; i < ctx.getChildCount(); ++i) {
562 final ParseTree childNode = ctx.getChild(i);
563 if (childNode instanceof Ordered_by_stmtContext) {
564 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
565 final boolean userOrdered = parseUserOrdered(orderedBy);
566 builder.setUserOrdered(userOrdered);
573 public void exitLeaf_list_stmt(YangParser.Leaf_list_stmtContext ctx) {
574 moduleBuilder.exitNode();
575 exitLog("leaf-list", actualPath.pop());
579 public void enterList_stmt(List_stmtContext ctx) {
580 final int line = ctx.getStart().getLine();
581 final String listName = stringFromNode(ctx);
582 enterLog("list", listName, line);
584 QName listQName = new QName(namespace, revision, yangModelPrefix, listName);
585 SchemaPath schemaPath = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, listName);
587 ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, schemaPath);
588 moduleBuilder.enterNode(builder);
589 actualPath.push(listName);
591 parseSchemaNodeArgs(ctx, builder);
592 parseConstraints(ctx, builder.getConstraints());
593 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
595 String keyDefinition = "";
596 for (int i = 0; i < ctx.getChildCount(); ++i) {
597 ParseTree childNode = ctx.getChild(i);
598 if (childNode instanceof Ordered_by_stmtContext) {
599 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
600 final boolean userOrdered = parseUserOrdered(orderedBy);
601 builder.setUserOrdered(userOrdered);
602 } else if (childNode instanceof Key_stmtContext) {
603 keyDefinition = stringFromNode(childNode);
604 List<QName> key = createListKey(keyDefinition, namespace, revision, yangModelPrefix);
605 builder.setKeyDefinition(key);
611 public void exitList_stmt(List_stmtContext ctx) {
612 moduleBuilder.exitNode();
613 exitLog("list", actualPath.pop());
617 public void enterAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) {
618 final int line = ctx.getStart().getLine();
619 final String anyXmlName = stringFromNode(ctx);
620 enterLog("anyxml", anyXmlName, line);
622 QName anyXmlQName = new QName(namespace, revision, yangModelPrefix, anyXmlName);
623 SchemaPath schemaPath = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, anyXmlName);
625 AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, schemaPath);
626 moduleBuilder.enterNode(builder);
627 actualPath.push(anyXmlName);
629 parseSchemaNodeArgs(ctx, builder);
630 parseConstraints(ctx, builder.getConstraints());
631 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
635 public void exitAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) {
636 moduleBuilder.exitNode();
637 exitLog("anyxml", actualPath.pop());
641 public void enterChoice_stmt(YangParser.Choice_stmtContext ctx) {
642 final int line = ctx.getStart().getLine();
643 final String choiceName = stringFromNode(ctx);
644 enterLog("choice", choiceName, line);
646 QName choiceQName = new QName(namespace, revision, yangModelPrefix, choiceName);
648 ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName);
649 moduleBuilder.enterNode(builder);
650 actualPath.push(choiceName);
652 builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix));
653 parseSchemaNodeArgs(ctx, builder);
654 parseConstraints(ctx, builder.getConstraints());
655 builder.setConfiguration(getConfig(ctx, moduleBuilder.getActualParent(), moduleName, line));
657 // set 'default' case
658 for (int i = 0; i < ctx.getChildCount(); i++) {
659 ParseTree child = ctx.getChild(i);
660 if (child instanceof Default_stmtContext) {
661 String defaultCase = stringFromNode(child);
662 builder.setDefaultCase(defaultCase);
669 public void exitChoice_stmt(YangParser.Choice_stmtContext ctx) {
670 moduleBuilder.exitNode();
671 exitLog("choice", actualPath.pop());
675 public void enterCase_stmt(YangParser.Case_stmtContext ctx) {
676 final int line = ctx.getStart().getLine();
677 final String caseName = stringFromNode(ctx);
678 enterLog("case", caseName, line);
680 QName caseQName = new QName(namespace, revision, yangModelPrefix, caseName);
681 ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName);
682 moduleBuilder.enterNode(builder);
683 actualPath.push(caseName);
685 builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix));
686 parseSchemaNodeArgs(ctx, builder);
687 parseConstraints(ctx, builder.getConstraints());
691 public void exitCase_stmt(YangParser.Case_stmtContext ctx) {
692 moduleBuilder.exitNode();
693 exitLog("case", actualPath.pop());
697 public void enterNotification_stmt(YangParser.Notification_stmtContext ctx) {
698 final int line = ctx.getStart().getLine();
699 final String notificationName = stringFromNode(ctx);
700 enterLog("notification", notificationName, line);
702 QName notificationQName = new QName(namespace, revision, yangModelPrefix, notificationName);
703 NotificationBuilder builder = moduleBuilder.addNotification(notificationQName, actualPath, line);
704 moduleBuilder.enterNode(builder);
705 actualPath.push(notificationName);
707 builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix));
708 parseSchemaNodeArgs(ctx, builder);
712 public void exitNotification_stmt(YangParser.Notification_stmtContext ctx) {
713 moduleBuilder.exitNode();
714 exitLog("notification", actualPath.pop());
719 public void enterIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
720 final int line = ctx.getStart().getLine();
721 final String nodeParameter = stringFromNode(ctx);
722 enterLog("unknown-node", nodeParameter, line);
724 QName nodeType = null;
726 final String nodeTypeStr = ctx.getChild(0).getText();
727 final String[] splittedElement = nodeTypeStr.split(":");
728 if (splittedElement.length == 1) {
729 nodeType = new QName(null, null, yangModelPrefix, splittedElement[0]);
731 nodeType = new QName(null, null, splittedElement[0], splittedElement[1]);
735 if (nodeParameter != null) {
736 String[] splittedName = nodeParameter.split(":");
737 if (splittedName.length == 2) {
738 qname = new QName(null, null, splittedName[0], splittedName[1]);
740 qname = new QName(namespace, revision, yangModelPrefix, splittedName[0]);
743 qname = new QName(namespace, revision, yangModelPrefix, nodeParameter);
746 UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(qname, actualPath, line);
747 builder.setNodeType(nodeType);
748 builder.setNodeParameter(nodeParameter);
749 actualPath.push(nodeParameter);
750 builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix));
751 parseSchemaNodeArgs(ctx, builder);
752 moduleBuilder.enterNode(builder);
756 public void exitIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
757 moduleBuilder.exitNode();
758 exitLog("unknown-node", actualPath.pop());
762 public void enterRpc_stmt(YangParser.Rpc_stmtContext ctx) {
763 final int line = ctx.getStart().getLine();
764 final String rpcName = stringFromNode(ctx);
765 enterLog("rpc", rpcName, line);
767 QName rpcQName = new QName(namespace, revision, yangModelPrefix, rpcName);
768 RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName);
769 moduleBuilder.enterNode(rpcBuilder);
770 actualPath.push(rpcName);
772 rpcBuilder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix));
773 parseSchemaNodeArgs(ctx, rpcBuilder);
777 public void exitRpc_stmt(YangParser.Rpc_stmtContext ctx) {
778 moduleBuilder.exitNode();
779 exitLog("rpc", actualPath.pop());
783 public void enterInput_stmt(YangParser.Input_stmtContext ctx) {
784 final int line = ctx.getStart().getLine();
785 final String input = "input";
786 enterLog(input, input, line);
788 QName rpcQName = new QName(namespace, revision, yangModelPrefix, input);
789 SchemaPath path = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, input);
791 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(path, rpcQName, line);
792 moduleBuilder.enterNode(builder);
793 actualPath.push(input);
795 parseSchemaNodeArgs(ctx, builder);
796 parseConstraints(ctx, builder.getConstraints());
800 public void exitInput_stmt(YangParser.Input_stmtContext ctx) {
801 moduleBuilder.exitNode();
802 exitLog("input", actualPath.pop());
806 public void enterOutput_stmt(YangParser.Output_stmtContext ctx) {
807 final int line = ctx.getStart().getLine();
808 final String output = "output";
809 enterLog(output, output, line);
811 QName rpcQName = new QName(namespace, revision, yangModelPrefix, output);
812 SchemaPath path = createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix, output);
814 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line);
815 moduleBuilder.enterNode(builder);
816 actualPath.push(output);
818 parseSchemaNodeArgs(ctx, builder);
819 parseConstraints(ctx, builder.getConstraints());
823 public void exitOutput_stmt(YangParser.Output_stmtContext ctx) {
824 moduleBuilder.exitNode();
825 exitLog("output", actualPath.pop());
829 public void enterFeature_stmt(YangParser.Feature_stmtContext ctx) {
830 final int line = ctx.getStart().getLine();
831 final String featureName = stringFromNode(ctx);
832 enterLog("feature", featureName, line);
834 QName featureQName = new QName(namespace, revision, yangModelPrefix, featureName);
835 FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName);
836 moduleBuilder.enterNode(featureBuilder);
837 actualPath.push(featureName);
839 featureBuilder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix));
840 parseSchemaNodeArgs(ctx, featureBuilder);
844 public void exitFeature_stmt(YangParser.Feature_stmtContext ctx) {
845 moduleBuilder.exitNode();
846 exitLog("feature", actualPath.pop());
850 public void enterDeviation_stmt(YangParser.Deviation_stmtContext ctx) {
851 final int line = ctx.getStart().getLine();
852 final String targetPath = stringFromNode(ctx);
853 enterLog("deviation", targetPath, line);
855 String reference = null;
856 String deviate = null;
857 DeviationBuilder builder = moduleBuilder.addDeviation(targetPath, actualPath, line);
858 moduleBuilder.enterNode(builder);
859 actualPath.push(targetPath);
861 for (int i = 0; i < ctx.getChildCount(); i++) {
862 ParseTree child = ctx.getChild(i);
863 if (child instanceof Reference_stmtContext) {
864 reference = stringFromNode(child);
865 } else if (child instanceof Deviate_not_supported_stmtContext) {
866 deviate = stringFromNode(child);
867 } else if (child instanceof Deviate_add_stmtContext) {
868 deviate = stringFromNode(child);
869 } else if (child instanceof Deviate_replace_stmtContext) {
870 deviate = stringFromNode(child);
871 } else if (child instanceof Deviate_delete_stmtContext) {
872 deviate = stringFromNode(child);
875 builder.setReference(reference);
876 builder.setDeviate(deviate);
880 public void exitDeviation_stmt(YangParser.Deviation_stmtContext ctx) {
881 moduleBuilder.exitNode();
882 exitLog("deviation", actualPath.pop());
886 public void enterIdentity_stmt(YangParser.Identity_stmtContext ctx) {
887 final int line = ctx.getStart().getLine();
888 final String identityName = stringFromNode(ctx);
889 enterLog("identity", identityName, line);
891 final QName identityQName = new QName(namespace, revision, yangModelPrefix, identityName);
892 IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, actualPath, line);
893 moduleBuilder.enterNode(builder);
894 actualPath.push(identityName);
896 builder.setPath(createActualSchemaPath(actualPath, namespace, revision, yangModelPrefix));
897 parseSchemaNodeArgs(ctx, builder);
899 for (int i = 0; i < ctx.getChildCount(); i++) {
900 ParseTree child = ctx.getChild(i);
901 if (child instanceof Base_stmtContext) {
902 String baseIdentityName = stringFromNode(child);
903 builder.setBaseIdentityName(baseIdentityName);
909 public void exitIdentity_stmt(YangParser.Identity_stmtContext ctx) {
910 moduleBuilder.exitNode();
911 exitLog("identity", actualPath.pop());
914 public ModuleBuilder getModuleBuilder() {
915 return moduleBuilder;
918 private void enterLog(String p1, String p2, int line) {
919 logger.debug("entering " + p1 + " " + p2 + " (" + line + ")");
922 private void exitLog(String p1, String p2) {
923 logger.debug("exiting " + p1 + " " + p2);
926 private void setLog(String p1, String p2) {
927 logger.debug("setting " + p1 + " " + p2);