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.ArrayList;
17 import java.util.Date;
18 import java.util.List;
19 import java.util.Stack;
21 import org.antlr.v4.runtime.tree.ParseTree;
22 import org.opendaylight.controller.antlrv4.code.gen.YangParser;
23 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Argument_stmtContext;
24 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Base_stmtContext;
25 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Contact_stmtContext;
26 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Container_stmtContext;
27 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Default_stmtContext;
28 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Description_stmtContext;
29 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_add_stmtContext;
30 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_delete_stmtContext;
31 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_not_supported_stmtContext;
32 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Deviate_replace_stmtContext;
33 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Import_stmtContext;
34 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Key_stmtContext;
35 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Leaf_list_stmtContext;
36 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Leaf_stmtContext;
37 import org.opendaylight.controller.antlrv4.code.gen.YangParser.List_stmtContext;
38 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Module_header_stmtsContext;
39 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Namespace_stmtContext;
40 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Ordered_by_stmtContext;
41 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Organization_stmtContext;
42 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Prefix_stmtContext;
43 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Presence_stmtContext;
44 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Reference_stmtContext;
45 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_date_stmtContext;
46 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_stmtContext;
47 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_stmtsContext;
48 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Status_stmtContext;
49 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Type_body_stmtsContext;
50 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Units_stmtContext;
51 import org.opendaylight.controller.antlrv4.code.gen.YangParser.When_stmtContext;
52 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Yang_version_stmtContext;
53 import org.opendaylight.controller.antlrv4.code.gen.YangParserBaseListener;
54 import org.opendaylight.controller.yang.common.QName;
55 import org.opendaylight.controller.yang.model.api.SchemaPath;
56 import org.opendaylight.controller.yang.model.api.Status;
57 import org.opendaylight.controller.yang.model.api.TypeDefinition;
58 import org.opendaylight.controller.yang.model.util.YangTypesConverter;
59 import org.opendaylight.controller.yang.parser.builder.api.AugmentationSchemaBuilder;
60 import org.opendaylight.controller.yang.parser.builder.api.GroupingBuilder;
61 import org.opendaylight.controller.yang.parser.builder.api.TypeDefinitionBuilder;
62 import org.opendaylight.controller.yang.parser.builder.api.UsesNodeBuilder;
63 import org.opendaylight.controller.yang.parser.builder.impl.AnyXmlBuilder;
64 import org.opendaylight.controller.yang.parser.builder.impl.ChoiceBuilder;
65 import org.opendaylight.controller.yang.parser.builder.impl.ChoiceCaseBuilder;
66 import org.opendaylight.controller.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
67 import org.opendaylight.controller.yang.parser.builder.impl.DeviationBuilder;
68 import org.opendaylight.controller.yang.parser.builder.impl.ExtensionBuilder;
69 import org.opendaylight.controller.yang.parser.builder.impl.FeatureBuilder;
70 import org.opendaylight.controller.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
71 import org.opendaylight.controller.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
72 import org.opendaylight.controller.yang.parser.builder.impl.LeafSchemaNodeBuilder;
73 import org.opendaylight.controller.yang.parser.builder.impl.ListSchemaNodeBuilder;
74 import org.opendaylight.controller.yang.parser.builder.impl.ModuleBuilder;
75 import org.opendaylight.controller.yang.parser.builder.impl.NotificationBuilder;
76 import org.opendaylight.controller.yang.parser.builder.impl.RpcDefinitionBuilder;
77 import org.opendaylight.controller.yang.parser.builder.impl.UnionTypeBuilder;
78 import org.opendaylight.controller.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
79 import org.opendaylight.controller.yang.parser.util.RefineHolder;
80 import org.slf4j.Logger;
81 import org.slf4j.LoggerFactory;
83 public final class YangParserListenerImpl extends YangParserBaseListener {
85 private static final Logger logger = LoggerFactory
86 .getLogger(YangParserListenerImpl.class);
88 private ModuleBuilder moduleBuilder;
90 private String moduleName;
91 private URI namespace;
92 private String yangModelPrefix;
93 private Date revision = new Date(0L);
95 public final static DateFormat simpleDateFormat = new SimpleDateFormat(
97 private final Stack<String> actualPath = new Stack<String>();
100 public void enterModule_stmt(YangParser.Module_stmtContext ctx) {
101 moduleName = stringFromNode(ctx);
102 actualPath.push(moduleName);
103 moduleBuilder = new ModuleBuilder(moduleName);
105 String description = null;
106 String reference = null;
108 for (int i = 0; i < ctx.getChildCount(); i++) {
109 ParseTree child = ctx.getChild(i);
110 if (child instanceof Description_stmtContext) {
111 description = stringFromNode(child);
112 } else if (child instanceof Reference_stmtContext) {
113 reference = stringFromNode(child);
115 if (description != null && reference != null) {
120 moduleBuilder.setDescription(description);
121 moduleBuilder.setReference(reference);
125 public void exitModule_stmt(YangParser.Module_stmtContext ctx) {
126 final String moduleName = actualPath.pop();
127 logger.debug("Exiting module " + moduleName);
131 public void enterModule_header_stmts(final Module_header_stmtsContext ctx) {
132 super.enterModule_header_stmts(ctx);
134 String yangVersion = null;
135 for (int i = 0; i < ctx.getChildCount(); ++i) {
136 final ParseTree treeNode = ctx.getChild(i);
137 if (treeNode instanceof Namespace_stmtContext) {
138 final String namespaceStr = stringFromNode(treeNode);
139 namespace = URI.create(namespaceStr);
140 moduleBuilder.setNamespace(namespace);
141 } else if (treeNode instanceof Prefix_stmtContext) {
142 yangModelPrefix = stringFromNode(treeNode);
143 moduleBuilder.setPrefix(yangModelPrefix);
144 } else if (treeNode instanceof Yang_version_stmtContext) {
145 yangVersion = stringFromNode(treeNode);
149 if (yangVersion == null) {
152 moduleBuilder.setYangVersion(yangVersion);
156 public void enterMeta_stmts(YangParser.Meta_stmtsContext ctx) {
157 for (int i = 0; i < ctx.getChildCount(); i++) {
158 ParseTree child = ctx.getChild(i);
159 if (child instanceof Organization_stmtContext) {
160 final String organization = stringFromNode(child);
161 moduleBuilder.setOrganization(organization);
162 } else if (child instanceof Contact_stmtContext) {
163 final String contact = stringFromNode(child);
164 moduleBuilder.setContact(contact);
165 } else if (child instanceof Description_stmtContext) {
166 final String description = stringFromNode(child);
167 moduleBuilder.setDescription(description);
168 } else if (child instanceof Reference_stmtContext) {
169 final String reference = stringFromNode(child);
170 moduleBuilder.setReference(reference);
176 public void exitSubmodule_header_stmts(
177 YangParser.Submodule_header_stmtsContext ctx) {
178 final String submodule = actualPath.pop();
179 logger.debug("exiting submodule " + submodule);
183 public void enterRevision_stmts(Revision_stmtsContext ctx) {
185 for (int i = 0; i < ctx.getChildCount(); ++i) {
186 final ParseTree treeNode = ctx.getChild(i);
187 if (treeNode instanceof Revision_stmtContext) {
188 updateRevisionForRevisionStatement(treeNode);
194 private void updateRevisionForRevisionStatement(final ParseTree treeNode) {
195 final String revisionDateStr = stringFromNode(treeNode);
197 final Date revision = simpleDateFormat.parse(revisionDateStr);
198 if ((revision != null) && (this.revision.compareTo(revision) < 0)) {
199 this.revision = revision;
200 moduleBuilder.setRevision(this.revision);
201 for (int i = 0; i < treeNode.getChildCount(); ++i) {
202 ParseTree child = treeNode.getChild(i);
203 if (child instanceof Reference_stmtContext) {
204 moduleBuilder.setReference(stringFromNode(child));
208 } catch (ParseException e) {
209 final String message = "Failed to parse revision string: "
211 logger.warn(message);
216 public void enterImport_stmt(Import_stmtContext ctx) {
217 final String importName = stringFromNode(ctx);
218 String importPrefix = null;
219 Date importRevision = null;
221 for (int i = 0; i < ctx.getChildCount(); ++i) {
222 final ParseTree treeNode = ctx.getChild(i);
223 if (treeNode instanceof Prefix_stmtContext) {
224 importPrefix = stringFromNode(treeNode);
226 if (treeNode instanceof Revision_date_stmtContext) {
227 String importRevisionStr = stringFromNode(treeNode);
229 importRevision = simpleDateFormat.parse(importRevisionStr);
230 } catch (ParseException e) {
231 logger.warn("Failed to parse import revision-date: "
232 + importRevisionStr);
236 moduleBuilder.addModuleImport(importName, importRevision, importPrefix);
240 public void enterAugment_stmt(YangParser.Augment_stmtContext ctx) {
241 final String augmentPath = stringFromNode(ctx);
242 AugmentationSchemaBuilder builder = moduleBuilder.addAugment(
243 augmentPath, actualPath, ctx.getStart().getLine());
244 moduleBuilder.enterNode(builder);
245 updatePath(augmentPath);
247 for (int i = 0; i < ctx.getChildCount(); i++) {
248 ParseTree child = ctx.getChild(i);
249 if (child instanceof Description_stmtContext) {
250 String desc = stringFromNode(child);
251 builder.setDescription(desc);
252 } else if (child instanceof Reference_stmtContext) {
253 String ref = stringFromNode(child);
254 builder.setReference(ref);
255 } else if (child instanceof Status_stmtContext) {
256 Status status = parseStatus((Status_stmtContext) child);
257 builder.setStatus(status);
258 } else if (child instanceof When_stmtContext) {
259 String when = stringFromNode(child);
260 builder.addWhenCondition(when);
266 public void exitAugment_stmt(YangParser.Augment_stmtContext ctx) {
267 final String augment = actualPath.pop();
268 logger.debug("exiting augment " + augment);
269 moduleBuilder.exitNode();
273 public void enterExtension_stmt(YangParser.Extension_stmtContext ctx) {
274 final String extName = stringFromNode(ctx);
275 QName qname = new QName(namespace, revision, yangModelPrefix, extName);
276 ExtensionBuilder builder = moduleBuilder.addExtension(qname, ctx
277 .getStart().getLine());
278 parseSchemaNodeArgs(ctx, builder);
280 String argument = null;
282 for (int i = 0; i < ctx.getChildCount(); i++) {
283 ParseTree child = ctx.getChild(i);
284 if (child instanceof Argument_stmtContext) {
285 argument = stringFromNode(child);
286 yin = parseYinValue((Argument_stmtContext) child);
290 builder.setArgument(argument);
291 builder.setYinElement(yin);
295 public void enterTypedef_stmt(YangParser.Typedef_stmtContext ctx) {
296 final String typedefName = stringFromNode(ctx);
297 QName typedefQName = new QName(namespace, revision, yangModelPrefix,
299 TypeDefinitionBuilder builder = moduleBuilder.addTypedef(typedefQName,
300 actualPath, ctx.getStart().getLine());
301 moduleBuilder.enterNode(builder);
302 updatePath(typedefName);
304 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
306 parseSchemaNodeArgs(ctx, builder);
307 builder.setUnits(parseUnits(ctx));
311 public void exitTypedef_stmt(YangParser.Typedef_stmtContext ctx) {
312 final String actContainer = actualPath.pop();
313 logger.debug("exiting " + actContainer);
314 moduleBuilder.exitNode();
318 public void enterType_stmt(YangParser.Type_stmtContext ctx) {
319 final String typeName = stringFromNode(ctx);
320 final int line = ctx.getStart().getLine();
321 final QName typeQName = parseQName(typeName);
323 TypeDefinition<?> type = null;
324 Type_body_stmtsContext typeBody = null;
325 for (int i = 0; i < ctx.getChildCount(); i++) {
326 if (ctx.getChild(i) instanceof Type_body_stmtsContext) {
327 typeBody = (Type_body_stmtsContext) ctx.getChild(i);
332 // if this is base yang type...
333 if (YangTypesConverter.isBaseYangType(typeName)) {
334 if (typeBody == null) {
335 // check for types which must have body
336 checkMissingBody(typeName, moduleName, line);
337 // if there are no constraints, just grab default base yang type
338 type = YangTypesConverter.javaTypeForBaseYangType(actualPath,
339 namespace, revision, typeName);
340 moduleBuilder.setType(type, actualPath);
342 if ("union".equals(typeName)) {
343 List<String> typePath = new ArrayList<String>(actualPath);
344 typePath.add(typeName);
345 SchemaPath p = createActualSchemaPath(typePath, namespace,
346 revision, yangModelPrefix);
347 UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(
348 actualPath, namespace, revision, line);
349 moduleBuilder.enterNode(unionBuilder);
350 unionBuilder.setPath(p);
351 } else if ("identityref".equals(typeName)) {
352 SchemaPath path = createActualSchemaPath(actualPath,
353 namespace, revision, yangModelPrefix);
354 moduleBuilder.addIdentityrefType(
355 getIdentityrefBase(typeBody), actualPath, path,
358 type = parseTypeBody(typeName, typeBody, actualPath,
359 namespace, revision, yangModelPrefix,
360 moduleBuilder.getActualNode());
361 moduleBuilder.setType(type, actualPath);
365 type = parseUnknownTypeBody(typeQName, typeBody, actualPath,
366 namespace, revision, yangModelPrefix,
367 moduleBuilder.getActualNode(), moduleBuilder);
368 // mark parent node of this type statement as dirty
369 moduleBuilder.addDirtyNode(actualPath);
370 moduleBuilder.setType(type, actualPath);
373 updatePath(typeName);
376 private QName parseQName(String typeName) {
378 if (typeName.contains(":")) {
379 String[] splittedName = typeName.split(":");
380 String prefix = splittedName[0];
381 String name = splittedName[1];
382 if (prefix.equals(yangModelPrefix)) {
383 typeQName = new QName(namespace, revision, prefix, name);
385 typeQName = new QName(null, null, prefix, name);
388 typeQName = new QName(namespace, revision, yangModelPrefix,
395 public void exitType_stmt(YangParser.Type_stmtContext ctx) {
396 final String actContainer = actualPath.pop();
397 logger.debug("exiting " + actContainer);
399 final String typeName = stringFromNode(ctx);
400 if ("union".equals(typeName)) {
401 moduleBuilder.exitNode();
406 public void enterGrouping_stmt(YangParser.Grouping_stmtContext ctx) {
407 final String groupName = stringFromNode(ctx);
408 QName groupQName = new QName(namespace, revision, yangModelPrefix,
410 GroupingBuilder builder = moduleBuilder.addGrouping(groupQName,
411 actualPath, ctx.getStart().getLine());
412 moduleBuilder.enterNode(builder);
413 updatePath(groupName);
414 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
416 parseSchemaNodeArgs(ctx, builder);
420 public void exitGrouping_stmt(YangParser.Grouping_stmtContext ctx) {
421 String actContainer = actualPath.pop();
422 logger.debug("exiting " + actContainer);
423 moduleBuilder.exitNode();
427 public void enterContainer_stmt(Container_stmtContext ctx) {
428 final String containerName = stringFromNode(ctx);
429 QName containerQName = new QName(namespace, revision, yangModelPrefix,
431 ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(
432 containerQName, actualPath, ctx.getStart().getLine());
433 moduleBuilder.enterNode(builder);
434 updatePath(containerName);
436 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
438 parseSchemaNodeArgs(ctx, builder);
439 parseConstraints(ctx, builder.getConstraints());
441 for (int i = 0; i < ctx.getChildCount(); ++i) {
442 final ParseTree childNode = ctx.getChild(i);
443 if (childNode instanceof Presence_stmtContext) {
444 builder.setPresence(true);
451 public void exitContainer_stmt(Container_stmtContext ctx) {
452 final String actContainer = actualPath.pop();
453 logger.debug("exiting " + actContainer);
454 moduleBuilder.exitNode();
458 public void enterLeaf_stmt(Leaf_stmtContext ctx) {
459 final String leafName = stringFromNode(ctx);
460 QName leafQName = new QName(namespace, revision, yangModelPrefix,
462 LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(leafQName,
463 actualPath, ctx.getStart().getLine());
464 moduleBuilder.enterNode(builder);
465 updatePath(leafName);
467 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
469 parseSchemaNodeArgs(ctx, builder);
470 parseConstraints(ctx, builder.getConstraints());
472 String defaultStr = null;
473 String unitsStr = null;
474 for (int i = 0; i < ctx.getChildCount(); i++) {
475 ParseTree child = ctx.getChild(i);
476 if (child instanceof Default_stmtContext) {
477 defaultStr = stringFromNode(child);
478 } else if (child instanceof Units_stmtContext) {
479 unitsStr = stringFromNode(child);
482 builder.setDefaultStr(defaultStr);
483 builder.setUnits(unitsStr);
487 public void exitLeaf_stmt(YangParser.Leaf_stmtContext ctx) {
488 final String actLeaf = actualPath.pop();
489 logger.debug("exiting " + actLeaf);
490 moduleBuilder.exitNode();
494 public void enterUses_stmt(YangParser.Uses_stmtContext ctx) {
495 final String groupingPathStr = stringFromNode(ctx);
496 UsesNodeBuilder builder = moduleBuilder.addUsesNode(groupingPathStr,
497 actualPath, ctx.getStart().getLine());
499 moduleBuilder.enterNode(builder);
500 updatePath(groupingPathStr);
501 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
506 public void exitUses_stmt(YangParser.Uses_stmtContext ctx) {
507 final String actContainer = actualPath.pop();
508 logger.debug("exiting " + actContainer);
509 moduleBuilder.exitNode();
513 public void enterRefine_stmt(YangParser.Refine_stmtContext ctx) {
514 String refineString = stringFromNode(ctx);
515 RefineHolder refine = parseRefine(ctx);
516 moduleBuilder.addRefine(refine, actualPath);
517 moduleBuilder.enterNode(refine);
518 updatePath(refineString);
522 public void exitRefine_stmt(YangParser.Refine_stmtContext ctx) {
523 final String actContainer = actualPath.pop();
524 logger.debug("exiting " + actContainer);
525 moduleBuilder.exitNode();
529 public void enterLeaf_list_stmt(Leaf_list_stmtContext ctx) {
530 final String leafListName = stringFromNode(ctx);
531 QName leafListQName = new QName(namespace, revision, yangModelPrefix,
533 LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(
534 leafListQName, actualPath, ctx.getStart().getLine());
535 moduleBuilder.enterNode(builder);
536 updatePath(leafListName);
538 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
540 parseSchemaNodeArgs(ctx, builder);
541 parseConstraints(ctx, builder.getConstraints());
543 for (int i = 0; i < ctx.getChildCount(); ++i) {
544 final ParseTree childNode = ctx.getChild(i);
545 if (childNode instanceof Ordered_by_stmtContext) {
546 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
547 final boolean userOrdered = parseUserOrdered(orderedBy);
548 builder.setUserOrdered(userOrdered);
555 public void exitLeaf_list_stmt(YangParser.Leaf_list_stmtContext ctx) {
556 final String actContainer = actualPath.pop();
557 logger.debug("exiting " + actContainer);
558 moduleBuilder.exitNode();
562 public void enterList_stmt(List_stmtContext ctx) {
563 final String containerName = stringFromNode(ctx);
564 QName containerQName = new QName(namespace, revision, yangModelPrefix,
566 ListSchemaNodeBuilder builder = moduleBuilder.addListNode(
567 containerQName, actualPath, ctx.getStart().getLine());
568 moduleBuilder.enterNode(builder);
569 updatePath(containerName);
571 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
573 parseSchemaNodeArgs(ctx, builder);
574 parseConstraints(ctx, builder.getConstraints());
576 String keyDefinition = "";
577 for (int i = 0; i < ctx.getChildCount(); ++i) {
578 ParseTree childNode = ctx.getChild(i);
579 if (childNode instanceof Ordered_by_stmtContext) {
580 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
581 final boolean userOrdered = parseUserOrdered(orderedBy);
582 builder.setUserOrdered(userOrdered);
583 } else if (childNode instanceof Key_stmtContext) {
584 keyDefinition = stringFromNode(childNode);
585 List<QName> key = createListKey(keyDefinition, namespace,
586 revision, yangModelPrefix);
587 builder.setKeyDefinition(key);
593 public void exitList_stmt(List_stmtContext ctx) {
594 final String actContainer = actualPath.pop();
595 logger.debug("exiting " + actContainer);
596 moduleBuilder.exitNode();
600 public void enterAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) {
601 final String anyXmlName = stringFromNode(ctx);
602 QName anyXmlQName = new QName(namespace, revision, yangModelPrefix,
604 AnyXmlBuilder builder = moduleBuilder.addAnyXml(anyXmlQName,
605 actualPath, ctx.getStart().getLine());
606 moduleBuilder.enterNode(builder);
607 updatePath(anyXmlName);
609 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
611 parseSchemaNodeArgs(ctx, builder);
612 parseConstraints(ctx, builder.getConstraints());
616 public void exitAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) {
617 final String actContainer = actualPath.pop();
618 logger.debug("exiting " + actContainer);
619 moduleBuilder.exitNode();
623 public void enterChoice_stmt(YangParser.Choice_stmtContext ctx) {
624 final String choiceName = stringFromNode(ctx);
625 QName choiceQName = new QName(namespace, revision, yangModelPrefix,
627 ChoiceBuilder builder = moduleBuilder.addChoice(choiceQName,
628 actualPath, ctx.getStart().getLine());
629 moduleBuilder.enterNode(builder);
631 updatePath(choiceName);
632 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
634 parseSchemaNodeArgs(ctx, builder);
635 parseConstraints(ctx, builder.getConstraints());
637 // set 'default' case
638 for (int i = 0; i < ctx.getChildCount(); i++) {
639 ParseTree child = ctx.getChild(i);
640 if (child instanceof Default_stmtContext) {
641 String defaultCase = stringFromNode(child);
642 builder.setDefaultCase(defaultCase);
649 public void exitChoice_stmt(YangParser.Choice_stmtContext ctx) {
650 final String actContainer = actualPath.pop();
651 logger.debug("exiting " + actContainer);
652 moduleBuilder.exitNode();
656 public void enterCase_stmt(YangParser.Case_stmtContext ctx) {
657 final String caseName = stringFromNode(ctx);
658 QName choiceQName = new QName(namespace, revision, yangModelPrefix,
660 ChoiceCaseBuilder builder = moduleBuilder.addCase(choiceQName,
661 actualPath, ctx.getStart().getLine());
662 moduleBuilder.enterNode(builder);
664 updatePath(caseName);
665 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
667 parseSchemaNodeArgs(ctx, builder);
668 parseConstraints(ctx, builder.getConstraints());
672 public void exitCase_stmt(YangParser.Case_stmtContext ctx) {
673 final String actContainer = actualPath.pop();
674 logger.debug("exiting " + actContainer);
675 moduleBuilder.exitNode();
679 public void enterNotification_stmt(YangParser.Notification_stmtContext ctx) {
680 final String notificationName = stringFromNode(ctx);
681 QName notificationQName = new QName(namespace, revision,
682 yangModelPrefix, notificationName);
683 NotificationBuilder builder = moduleBuilder.addNotification(
684 notificationQName, actualPath, ctx.getStart().getLine());
685 moduleBuilder.enterNode(builder);
686 updatePath(notificationName);
688 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
690 parseSchemaNodeArgs(ctx, builder);
694 public void exitNotification_stmt(YangParser.Notification_stmtContext ctx) {
695 final String actContainer = actualPath.pop();
696 logger.debug("exiting " + actContainer);
697 moduleBuilder.exitNode();
702 public void enterIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
703 final String nodeParameter = stringFromNode(ctx);
704 QName nodeType = null;
706 final String nodeTypeStr = ctx.getChild(0).getText();
707 final String[] splittedElement = nodeTypeStr.split(":");
708 if (splittedElement.length == 1) {
709 nodeType = new QName(null, null, yangModelPrefix,
712 nodeType = new QName(null, null, splittedElement[0],
717 if (nodeParameter != null) {
718 String[] splittedName = nodeParameter.split(":");
719 if (splittedName.length == 2) {
720 qname = new QName(null, null, splittedName[0], splittedName[1]);
722 qname = new QName(namespace, revision, yangModelPrefix,
726 qname = new QName(namespace, revision, yangModelPrefix,
730 UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(
731 qname, actualPath, ctx.getStart().getLine());
732 builder.setNodeType(nodeType);
733 builder.setNodeParameter(nodeParameter);
734 updatePath(nodeParameter);
735 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
737 parseSchemaNodeArgs(ctx, builder);
738 moduleBuilder.enterNode(builder);
742 public void exitIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
743 final String actContainer = actualPath.pop();
744 logger.debug("exiting " + actContainer);
745 moduleBuilder.exitNode();
749 public void enterRpc_stmt(YangParser.Rpc_stmtContext ctx) {
750 final String rpcName = stringFromNode(ctx);
751 QName rpcQName = new QName(namespace, revision, yangModelPrefix,
753 RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(rpcQName,
754 actualPath, ctx.getStart().getLine());
755 moduleBuilder.enterNode(rpcBuilder);
758 rpcBuilder.setPath(createActualSchemaPath(actualPath, namespace,
759 revision, yangModelPrefix));
760 parseSchemaNodeArgs(ctx, rpcBuilder);
764 public void exitRpc_stmt(YangParser.Rpc_stmtContext ctx) {
765 final String actContainer = actualPath.pop();
766 logger.debug("exiting " + actContainer);
767 moduleBuilder.exitNode();
771 public void enterInput_stmt(YangParser.Input_stmtContext ctx) {
772 final String input = "input";
773 QName rpcQName = new QName(namespace, revision, yangModelPrefix, input);
774 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(
775 rpcQName, ctx.getStart().getLine());
776 moduleBuilder.enterNode(builder);
779 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
781 parseSchemaNodeArgs(ctx, builder);
782 parseConstraints(ctx, builder.getConstraints());
786 public void exitInput_stmt(YangParser.Input_stmtContext ctx) {
787 final String actContainer = actualPath.pop();
788 logger.debug("exiting " + actContainer);
789 moduleBuilder.exitNode();
793 public void enterOutput_stmt(YangParser.Output_stmtContext ctx) {
794 final String output = "output";
795 QName rpcQName = new QName(namespace, revision, yangModelPrefix, output);
796 ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(
797 rpcQName, ctx.getStart().getLine());
798 moduleBuilder.enterNode(builder);
801 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
803 parseSchemaNodeArgs(ctx, builder);
804 parseConstraints(ctx, builder.getConstraints());
808 public void exitOutput_stmt(YangParser.Output_stmtContext ctx) {
809 final String actContainer = actualPath.pop();
810 logger.debug("exiting " + actContainer);
811 moduleBuilder.exitNode();
815 public void enterFeature_stmt(YangParser.Feature_stmtContext ctx) {
816 final String featureName = stringFromNode(ctx);
817 QName featureQName = new QName(namespace, revision, yangModelPrefix,
819 FeatureBuilder featureBuilder = moduleBuilder.addFeature(featureQName,
820 actualPath, ctx.getStart().getLine());
821 moduleBuilder.enterNode(featureBuilder);
822 updatePath(featureName);
824 featureBuilder.setPath(createActualSchemaPath(actualPath, namespace,
825 revision, yangModelPrefix));
826 parseSchemaNodeArgs(ctx, featureBuilder);
830 public void exitFeature_stmt(YangParser.Feature_stmtContext ctx) {
831 final String actContainer = actualPath.pop();
832 logger.debug("exiting " + actContainer);
833 moduleBuilder.exitNode();
837 public void enterDeviation_stmt(YangParser.Deviation_stmtContext ctx) {
838 final String targetPath = stringFromNode(ctx);
839 String reference = null;
840 String deviate = null;
841 DeviationBuilder builder = moduleBuilder.addDeviation(targetPath,
842 actualPath, ctx.getStart().getLine());
843 moduleBuilder.enterNode(builder);
844 updatePath(targetPath);
846 for (int i = 0; i < ctx.getChildCount(); i++) {
847 ParseTree child = ctx.getChild(i);
848 if (child instanceof Reference_stmtContext) {
849 reference = stringFromNode(child);
850 } else if (child instanceof Deviate_not_supported_stmtContext) {
851 deviate = stringFromNode(child);
852 } else if (child instanceof Deviate_add_stmtContext) {
853 deviate = stringFromNode(child);
854 } else if (child instanceof Deviate_replace_stmtContext) {
855 deviate = stringFromNode(child);
856 } else if (child instanceof Deviate_delete_stmtContext) {
857 deviate = stringFromNode(child);
860 builder.setReference(reference);
861 builder.setDeviate(deviate);
865 public void exitDeviation_stmt(YangParser.Deviation_stmtContext ctx) {
866 final String actContainer = actualPath.pop();
867 logger.debug("exiting " + actContainer);
868 moduleBuilder.exitNode();
872 public void enterConfig_stmt(YangParser.Config_stmtContext ctx) {
873 boolean configuration = parseConfig(ctx);
874 moduleBuilder.addConfiguration(configuration, actualPath, ctx
875 .getStart().getLine());
879 public void enterIdentity_stmt(YangParser.Identity_stmtContext ctx) {
880 final String identityName = stringFromNode(ctx);
881 final QName identityQName = new QName(namespace, revision,
882 yangModelPrefix, identityName);
883 IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(
884 identityQName, actualPath, ctx.getStart().getLine());
885 moduleBuilder.enterNode(builder);
886 updatePath(identityName);
888 builder.setPath(createActualSchemaPath(actualPath, namespace, revision,
890 parseSchemaNodeArgs(ctx, builder);
892 for (int i = 0; i < ctx.getChildCount(); i++) {
893 ParseTree child = ctx.getChild(i);
894 if (child instanceof Base_stmtContext) {
895 String baseIdentityName = stringFromNode(child);
896 builder.setBaseIdentityName(baseIdentityName);
902 public void exitIdentity_stmt(YangParser.Identity_stmtContext ctx) {
903 final String actContainer = actualPath.pop();
904 logger.debug("exiting " + actContainer);
905 moduleBuilder.exitNode();
908 public ModuleBuilder getModuleBuilder() {
909 return moduleBuilder;
912 private void updatePath(String containerName) {
913 actualPath.push(containerName);