Merge "BUG-1275: teach NormalizedNode builders about size hints"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / YangParserListenerImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.impl;
9
10 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.checkMissingBody;
11 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.createListKey;
12 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.getConfig;
13 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.getIdentityrefBase;
14 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseConstraints;
15 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseDefault;
16 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseRefine;
17 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseSchemaNodeArgs;
18 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseStatus;
19 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseTypeWithBody;
20 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseUnits;
21 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseUnknownTypeWithBody;
22 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseUserOrdered;
23 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseYinValue;
24 import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.stringFromNode;
25
26 import com.google.common.base.Splitter;
27 import com.google.common.base.Strings;
28 import com.google.common.collect.Iterables;
29
30 import java.net.URI;
31 import java.text.DateFormat;
32 import java.text.ParseException;
33 import java.text.SimpleDateFormat;
34 import java.util.Date;
35 import java.util.Iterator;
36 import java.util.List;
37
38 import org.antlr.v4.runtime.tree.ParseTree;
39 import org.antlr.v4.runtime.tree.ParseTreeWalker;
40 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser;
41 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Argument_stmtContext;
42 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Base_stmtContext;
43 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Contact_stmtContext;
44 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Container_stmtContext;
45 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Default_stmtContext;
46 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Description_stmtContext;
47 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_add_stmtContext;
48 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_delete_stmtContext;
49 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_not_supported_stmtContext;
50 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Deviate_replace_stmtContext;
51 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Import_stmtContext;
52 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Key_stmtContext;
53 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Leaf_list_stmtContext;
54 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Leaf_stmtContext;
55 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.List_stmtContext;
56 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Module_header_stmtsContext;
57 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Namespace_stmtContext;
58 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Ordered_by_stmtContext;
59 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Organization_stmtContext;
60 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Prefix_stmtContext;
61 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Presence_stmtContext;
62 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Reference_stmtContext;
63 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_date_stmtContext;
64 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtContext;
65 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_stmtsContext;
66 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Status_stmtContext;
67 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Type_body_stmtsContext;
68 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Units_stmtContext;
69 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.When_stmtContext;
70 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Yang_version_stmtContext;
71 import org.opendaylight.yangtools.antlrv4.code.gen.YangParserBaseListener;
72 import org.opendaylight.yangtools.yang.common.QName;
73 import org.opendaylight.yangtools.yang.common.QNameModule;
74 import org.opendaylight.yangtools.yang.model.api.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.parser.builder.api.AugmentationSchemaBuilder;
78 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
79 import org.opendaylight.yangtools.yang.parser.builder.api.ExtensionBuilder;
80 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
81 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
82 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
83 import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder;
84 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
85 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
86 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
87 import org.opendaylight.yangtools.yang.parser.builder.impl.DeviationBuilder;
88 import org.opendaylight.yangtools.yang.parser.builder.impl.FeatureBuilder;
89 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
90 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
91 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
92 import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder;
93 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
94 import org.opendaylight.yangtools.yang.parser.builder.impl.NotificationBuilder;
95 import org.opendaylight.yangtools.yang.parser.builder.impl.RefineHolderImpl;
96 import org.opendaylight.yangtools.yang.parser.builder.impl.RpcDefinitionBuilder;
97 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
98 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilderImpl;
99 import org.slf4j.Logger;
100 import org.slf4j.LoggerFactory;
101
102
103 public final class YangParserListenerImpl extends YangParserBaseListener {
104     private static final Logger LOG = LoggerFactory.getLogger(YangParserListenerImpl.class);
105     private static final Splitter COLON_SPLITTER = Splitter.on(':');
106     private static final String AUGMENT_STR = "augment";
107
108     private final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
109     private final SchemaPathStack stack = new SchemaPathStack();
110     private final String sourcePath;
111     private QName moduleQName = new QName(null, new Date(0L), null, "dummy");
112     private ModuleBuilder moduleBuilder;
113     private String moduleName;
114     private int augmentOrder;
115
116     public YangParserListenerImpl(final String sourcePath) {
117         this.sourcePath = sourcePath;
118     }
119
120     /**
121      * Create a new instance.
122      *
123      * FIXME: the resulting type needs to be extracted, such that we can reuse
124      *        the "BaseListener" aspect, which need not be exposed to the user.
125      *        Maybe factor out a base class into repo.spi?
126      *
127      * @param sourcePath
128      * @param walker
129      * @param tree
130      * @return
131      */
132     public static YangParserListenerImpl create(final String sourcePath, final ParseTreeWalker walker, final ParseTree tree) {
133         final YangParserListenerImpl ret = new YangParserListenerImpl(sourcePath);
134         walker.walk(ret, tree);
135         return ret;
136     }
137
138     @Override
139     public void enterModule_stmt(final YangParser.Module_stmtContext ctx) {
140         moduleName = stringFromNode(ctx);
141         LOG.trace("entering module {}", moduleName);
142         enterLog("module", moduleName, 0);
143         stack.push();
144
145         moduleBuilder = new ModuleBuilder(moduleName, sourcePath);
146
147         String description = null;
148         String reference = null;
149         for (int i = 0; i < ctx.getChildCount(); i++) {
150             ParseTree child = ctx.getChild(i);
151             if (child instanceof Description_stmtContext) {
152                 description = stringFromNode(child);
153             } else if (child instanceof Reference_stmtContext) {
154                 reference = stringFromNode(child);
155             } else {
156                 if (description != null && reference != null) {
157                     break;
158                 }
159             }
160         }
161         moduleBuilder.setDescription(description);
162         moduleBuilder.setReference(reference);
163     }
164
165     @Override
166     public void exitModule_stmt(final YangParser.Module_stmtContext ctx) {
167         exitLog("module");
168         stack.pop();
169     }
170
171     @Override
172     public void enterSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
173         moduleName = stringFromNode(ctx);
174         LOG.trace("entering submodule {}", moduleName);
175         enterLog("submodule", moduleName, 0);
176         stack.push();
177
178         moduleBuilder = new ModuleBuilder(moduleName, true, sourcePath);
179
180         String description = null;
181         String reference = null;
182         for (int i = 0; i < ctx.getChildCount(); i++) {
183             ParseTree child = ctx.getChild(i);
184             if (child instanceof Description_stmtContext) {
185                 description = stringFromNode(child);
186             } else if (child instanceof Reference_stmtContext) {
187                 reference = stringFromNode(child);
188             } else {
189                 if (description != null && reference != null) {
190                     break;
191                 }
192             }
193         }
194         moduleBuilder.setDescription(description);
195         moduleBuilder.setReference(reference);
196     }
197
198     @Override
199     public void exitSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
200         exitLog("submodule");
201         stack.pop();
202     }
203
204     @Override
205     public void enterBelongs_to_stmt(final YangParser.Belongs_to_stmtContext ctx) {
206         moduleBuilder.setBelongsTo(stringFromNode(ctx));
207     }
208
209     @Override
210     public void enterModule_header_stmts(final Module_header_stmtsContext ctx) {
211         enterLog("module_header", "", ctx.getStart().getLine());
212         String yangVersion = null;
213         for (int i = 0; i < ctx.getChildCount(); ++i) {
214             final ParseTree treeNode = ctx.getChild(i);
215             if (treeNode instanceof Namespace_stmtContext) {
216                 final String namespaceStr = stringFromNode(treeNode);
217                 final URI namespace = URI.create(namespaceStr);
218                 this.moduleQName = new QName(namespace, moduleQName.getRevision(), moduleQName.getPrefix(), moduleQName.getLocalName());
219                 moduleBuilder.setQNameModule(moduleQName.getModule());
220                 setLog("namespace", namespaceStr);
221             } else if (treeNode instanceof Prefix_stmtContext) {
222                 final String yangModelPrefix = stringFromNode(treeNode);
223                 this.moduleQName = QName.create(moduleQName.getModule(), yangModelPrefix, moduleQName.getLocalName());
224                 moduleBuilder.setPrefix(yangModelPrefix);
225                 setLog("prefix", yangModelPrefix);
226             } else if (treeNode instanceof Yang_version_stmtContext) {
227                 yangVersion = stringFromNode(treeNode);
228                 setLog("yang-version", yangVersion);
229             }
230         }
231
232         if (yangVersion == null) {
233             yangVersion = "1";
234         }
235         moduleBuilder.setYangVersion(yangVersion);
236     }
237
238     @Override
239     public void exitModule_header_stmts(final Module_header_stmtsContext ctx) {
240         exitLog("module_header");
241     }
242
243     @Override
244     public void enterMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
245         enterLog("meta_stmt", "", ctx.getStart().getLine());
246         for (int i = 0; i < ctx.getChildCount(); i++) {
247             ParseTree child = ctx.getChild(i);
248             if (child instanceof Organization_stmtContext) {
249                 final String organization = stringFromNode(child);
250                 moduleBuilder.setOrganization(organization);
251                 setLog("organization", organization);
252             } else if (child instanceof Contact_stmtContext) {
253                 final String contact = stringFromNode(child);
254                 moduleBuilder.setContact(contact);
255                 setLog("contact", contact);
256             } else if (child instanceof Description_stmtContext) {
257                 final String description = stringFromNode(child);
258                 moduleBuilder.setDescription(description);
259                 setLog("description", description);
260             } else if (child instanceof Reference_stmtContext) {
261                 final String reference = stringFromNode(child);
262                 moduleBuilder.setReference(reference);
263                 setLog("reference", reference);
264             }
265         }
266     }
267
268     @Override
269     public void exitMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
270         exitLog("meta_stmt");
271     }
272
273     @Override
274     public void enterRevision_stmts(final Revision_stmtsContext ctx) {
275         enterLog("revisions", "", ctx.getStart().getLine());
276         for (int i = 0; i < ctx.getChildCount(); ++i) {
277             final ParseTree treeNode = ctx.getChild(i);
278             if (treeNode instanceof Revision_stmtContext) {
279                 updateRevisionForRevisionStatement(treeNode);
280             }
281         }
282     }
283
284     @Override
285     public void exitRevision_stmts(final Revision_stmtsContext ctx) {
286         exitLog("revisions");
287     }
288
289     private void updateRevisionForRevisionStatement(final ParseTree treeNode) {
290         final String revisionDateStr = stringFromNode(treeNode);
291         try {
292             final Date revisionDate = SIMPLE_DATE_FORMAT.parse(revisionDateStr);
293             if ((revisionDate != null) && (this.moduleQName.getRevision().compareTo(revisionDate) < 0)) {
294                 this.moduleQName = new QName(moduleQName.getNamespace(), revisionDate, moduleQName.getPrefix(), moduleQName.getLocalName());
295                 moduleBuilder.setQNameModule(moduleQName.getModule());
296                 setLog("revision", revisionDate.toString());
297                 for (int i = 0; i < treeNode.getChildCount(); ++i) {
298                     ParseTree child = treeNode.getChild(i);
299                     if (child instanceof Reference_stmtContext) {
300                         moduleBuilder.setReference(stringFromNode(child));
301                     }
302                 }
303             }
304         } catch (ParseException e) {
305             LOG.warn("Failed to parse revision string: {}", revisionDateStr, e);
306         }
307     }
308
309     @Override
310     public void enterImport_stmt(final Import_stmtContext ctx) {
311         final int line = ctx.getStart().getLine();
312         final String importName = stringFromNode(ctx);
313         enterLog("import", importName, line);
314
315         String importPrefix = null;
316         Date importRevision = null;
317
318         for (int i = 0; i < ctx.getChildCount(); ++i) {
319             final ParseTree treeNode = ctx.getChild(i);
320             if (treeNode instanceof Prefix_stmtContext) {
321                 importPrefix = stringFromNode(treeNode);
322             }
323             if (treeNode instanceof Revision_date_stmtContext) {
324                 String importRevisionStr = stringFromNode(treeNode);
325                 try {
326                     importRevision = SIMPLE_DATE_FORMAT.parse(importRevisionStr);
327                 } catch (ParseException e) {
328                     LOG.warn("Failed to parse import revision-date at line {}: {}", line, importRevisionStr, e);
329                 }
330             }
331         }
332         moduleBuilder.addModuleImport(importName, importRevision, importPrefix);
333         LOG.trace("setting import ({}; {}; {})", importName, importRevision, importPrefix);
334     }
335
336     @Override
337     public void exitImport_stmt(final Import_stmtContext ctx) {
338         exitLog("import");
339     }
340
341     @Override
342     public void enterAugment_stmt(final YangParser.Augment_stmtContext ctx) {
343         final int line = ctx.getStart().getLine();
344         final String augmentPath = stringFromNode(ctx);
345         enterLog(AUGMENT_STR, augmentPath, line);
346         stack.push();
347
348         AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, augmentOrder++);
349
350         for (int i = 0; i < ctx.getChildCount(); i++) {
351             ParseTree child = ctx.getChild(i);
352             if (child instanceof Description_stmtContext) {
353                 builder.setDescription(stringFromNode(child));
354             } else if (child instanceof Reference_stmtContext) {
355                 builder.setReference(stringFromNode(child));
356             } else if (child instanceof Status_stmtContext) {
357                 builder.setStatus(parseStatus((Status_stmtContext) child));
358             } else if (child instanceof When_stmtContext) {
359                 builder.addWhenCondition(stringFromNode(child));
360             }
361         }
362
363         moduleBuilder.enterNode(builder);
364     }
365
366     @Override
367     public void exitAugment_stmt(final YangParser.Augment_stmtContext ctx) {
368         moduleBuilder.exitNode();
369         exitLog(AUGMENT_STR);
370         stack.pop();
371     }
372
373     @Override
374     public void enterExtension_stmt(final YangParser.Extension_stmtContext ctx) {
375         final int line = ctx.getStart().getLine();
376         final String extName = stringFromNode(ctx);
377         enterLog("extension", extName, line);
378         QName qname = QName.create(moduleQName, extName);
379         SchemaPath path = stack.addNodeToPath(qname);
380
381         ExtensionBuilder builder = moduleBuilder.addExtension(qname, line, path);
382         parseSchemaNodeArgs(ctx, builder);
383
384         String argument = null;
385         boolean yin = false;
386         for (int i = 0; i < ctx.getChildCount(); i++) {
387             ParseTree child = ctx.getChild(i);
388             if (child instanceof Argument_stmtContext) {
389                 argument = stringFromNode(child);
390                 yin = parseYinValue((Argument_stmtContext) child);
391                 break;
392             }
393         }
394         builder.setArgument(argument);
395         builder.setYinElement(yin);
396
397         moduleBuilder.enterNode(builder);
398     }
399
400     @Override
401     public void exitExtension_stmt(final YangParser.Extension_stmtContext ctx) {
402         moduleBuilder.exitNode();
403         exitLog("extension", stack.removeNodeFromPath());
404     }
405
406     @Override
407     public void enterTypedef_stmt(final YangParser.Typedef_stmtContext ctx) {
408         final int line = ctx.getStart().getLine();
409         final String typedefName = stringFromNode(ctx);
410         enterLog("typedef", typedefName, line);
411         QName typedefQName = QName.create(moduleQName, typedefName);
412         SchemaPath path = stack.addNodeToPath(typedefQName);
413
414         TypeDefinitionBuilder builder = moduleBuilder.addTypedef(line, typedefQName, path);
415         parseSchemaNodeArgs(ctx, builder);
416         builder.setUnits(parseUnits(ctx));
417         builder.setDefaultValue(parseDefault(ctx));
418
419         moduleBuilder.enterNode(builder);
420     }
421
422     @Override
423     public void exitTypedef_stmt(final YangParser.Typedef_stmtContext ctx) {
424         moduleBuilder.exitNode();
425         exitLog("typedef", stack.removeNodeFromPath());
426     }
427
428     @Override
429     public void enterType_stmt(final YangParser.Type_stmtContext ctx) {
430         final int line = ctx.getStart().getLine();
431         final String typeName = stringFromNode(ctx);
432         enterLog("type", typeName, line);
433
434         final QName typeQName = parseQName(typeName);
435
436         TypeDefinition<?> type;
437         Type_body_stmtsContext typeBody = null;
438         for (int i = 0; i < ctx.getChildCount(); i++) {
439             if (ctx.getChild(i) instanceof Type_body_stmtsContext) {
440                 typeBody = (Type_body_stmtsContext) ctx.getChild(i);
441                 break;
442             }
443         }
444
445         // if this is base yang type...
446         if (BaseTypes.isYangBuildInType(typeName)) {
447             if (typeBody == null) {
448                 // check for types which must have body
449                 checkMissingBody(typeName, moduleName, line);
450                 // if there are no constraints, just grab default base yang type
451                 type = BaseTypes.defaultBaseTypeFor(typeName).orNull();
452                 stack.addNodeToPath(type.getQName());
453                 moduleBuilder.setType(type);
454             } else {
455                 QName qname;
456                 switch (typeName) {
457                 case "union":
458                     qname = BaseTypes.UNION_QNAME;
459                     stack.addNodeToPath(qname);
460                     UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, moduleQName.getModule());
461                     Builder parent = moduleBuilder.getActualNode();
462                     unionBuilder.setParent(parent);
463                     moduleBuilder.enterNode(unionBuilder);
464                     break;
465                 case "identityref":
466                     qname = BaseTypes.IDENTITYREF_QNAME;
467                     SchemaPath path = stack.addNodeToPath(qname);
468                     moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody));
469                     break;
470                 default:
471                     type = parseTypeWithBody(typeName, typeBody, stack.currentSchemaPath(), moduleQName, moduleBuilder.getActualNode());
472                     moduleBuilder.setType(type);
473                     stack.addNodeToPath(type.getQName());
474                 }
475             }
476         } else {
477             type = parseUnknownTypeWithBody(typeQName, typeBody, stack.currentSchemaPath(), moduleQName, moduleBuilder.getActualNode());
478             // add parent node of this type statement to dirty nodes
479             moduleBuilder.markActualNodeDirty();
480             moduleBuilder.setType(type);
481             stack.addNodeToPath(type.getQName());
482         }
483
484     }
485
486     private QName parseQName(final String typeName) {
487         final QName typeQName;
488         if (typeName.indexOf(':') != -1) {
489             final Iterator<String> split = COLON_SPLITTER.split(typeName).iterator();
490             final String prefix = split.next();
491             final String name = split.next();
492             if (prefix.equals(moduleQName.getPrefix())) {
493                 typeQName = QName.create(moduleQName, name);
494             } else {
495                 typeQName = QName.create(QNameModule.create(null, null), prefix, name);
496             }
497         } else {
498             typeQName = QName.create(moduleQName, typeName);
499         }
500         return typeQName;
501     }
502
503     @Override
504     public void exitType_stmt(final YangParser.Type_stmtContext ctx) {
505         final String typeName = stringFromNode(ctx);
506         if ("union".equals(typeName)) {
507             moduleBuilder.exitNode();
508         }
509         exitLog("type", stack.removeNodeFromPath());
510     }
511
512     @Override
513     public void enterGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
514         final int line = ctx.getStart().getLine();
515         final String groupName = stringFromNode(ctx);
516         enterLog("grouping", groupName, line);
517         QName groupQName = QName.create(moduleQName, groupName);
518         SchemaPath path = stack.addNodeToPath(groupQName);
519
520         GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName, path);
521         parseSchemaNodeArgs(ctx, builder);
522
523         moduleBuilder.enterNode(builder);
524     }
525
526     @Override
527     public void exitGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
528         moduleBuilder.exitNode();
529         exitLog("grouping", stack.removeNodeFromPath());
530     }
531
532     @Override
533     public void enterContainer_stmt(final Container_stmtContext ctx) {
534         final int line = ctx.getStart().getLine();
535         final String containerName = stringFromNode(ctx);
536         enterLog("container", containerName, line);
537
538         QName containerQName = QName.create(moduleQName, containerName);
539         SchemaPath path = stack.addNodeToPath(containerQName);
540
541         ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(line, containerQName, path);
542         parseSchemaNodeArgs(ctx, builder);
543         parseConstraints(ctx, builder.getConstraints());
544         builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
545
546         for (int i = 0; i < ctx.getChildCount(); ++i) {
547             final ParseTree childNode = ctx.getChild(i);
548             if (childNode instanceof Presence_stmtContext) {
549                 builder.setPresence(true);
550                 break;
551             }
552         }
553
554         moduleBuilder.enterNode(builder);
555     }
556
557     @Override
558     public void exitContainer_stmt(final Container_stmtContext ctx) {
559         moduleBuilder.exitNode();
560         exitLog("container", stack.removeNodeFromPath());
561     }
562
563     @Override
564     public void enterLeaf_stmt(final Leaf_stmtContext ctx) {
565         final int line = ctx.getStart().getLine();
566         final String leafName = stringFromNode(ctx);
567         enterLog("leaf", leafName, line);
568
569         QName leafQName = QName.create(moduleQName, leafName);
570         SchemaPath path = stack.addNodeToPath(leafQName);
571
572         LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, path);
573         parseSchemaNodeArgs(ctx, builder);
574         parseConstraints(ctx, builder.getConstraints());
575         builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
576
577         String defaultStr = null;
578         String unitsStr = null;
579         for (int i = 0; i < ctx.getChildCount(); i++) {
580             ParseTree child = ctx.getChild(i);
581             if (child instanceof Default_stmtContext) {
582                 defaultStr = stringFromNode(child);
583             } else if (child instanceof Units_stmtContext) {
584                 unitsStr = stringFromNode(child);
585             }
586         }
587         builder.setDefaultStr(defaultStr);
588         builder.setUnits(unitsStr);
589
590         moduleBuilder.enterNode(builder);
591     }
592
593     @Override
594     public void exitLeaf_stmt(final YangParser.Leaf_stmtContext ctx) {
595         moduleBuilder.exitNode();
596         exitLog("leaf", stack.removeNodeFromPath());
597     }
598
599     @Override
600     public void enterUses_stmt(final YangParser.Uses_stmtContext ctx) {
601         final int line = ctx.getStart().getLine();
602         final String groupingPathStr = stringFromNode(ctx);
603         enterLog("uses", groupingPathStr, line);
604
605         UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPathStr);
606
607         moduleBuilder.enterNode(builder);
608     }
609
610     @Override
611     public void exitUses_stmt(final YangParser.Uses_stmtContext ctx) {
612         moduleBuilder.exitNode();
613         exitLog("uses");
614     }
615
616     @Override
617     public void enterUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
618         final int line = ctx.getStart().getLine();
619         final String augmentPath = stringFromNode(ctx);
620         enterLog(AUGMENT_STR, augmentPath, line);
621         stack.push();
622
623         AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, augmentOrder++);
624
625         for (int i = 0; i < ctx.getChildCount(); i++) {
626             ParseTree child = ctx.getChild(i);
627             if (child instanceof Description_stmtContext) {
628                 builder.setDescription(stringFromNode(child));
629             } else if (child instanceof Reference_stmtContext) {
630                 builder.setReference(stringFromNode(child));
631             } else if (child instanceof Status_stmtContext) {
632                 builder.setStatus(parseStatus((Status_stmtContext) child));
633             } else if (child instanceof When_stmtContext) {
634                 builder.addWhenCondition(stringFromNode(child));
635             }
636         }
637
638         moduleBuilder.enterNode(builder);
639     }
640
641     @Override
642     public void exitUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
643         moduleBuilder.exitNode();
644         exitLog(AUGMENT_STR);
645         stack.pop();
646     }
647
648     @Override
649     public void enterRefine_stmt(final YangParser.Refine_stmtContext ctx) {
650         final String refineString = stringFromNode(ctx);
651         enterLog("refine", refineString, ctx.getStart().getLine());
652
653         RefineHolderImpl refine = parseRefine(ctx, moduleName);
654         moduleBuilder.addRefine(refine);
655         moduleBuilder.enterNode(refine);
656     }
657
658     @Override
659     public void exitRefine_stmt(final YangParser.Refine_stmtContext ctx) {
660         moduleBuilder.exitNode();
661         exitLog("refine");
662     }
663
664     @Override
665     public void enterLeaf_list_stmt(final Leaf_list_stmtContext ctx) {
666         final int line = ctx.getStart().getLine();
667         final String leafListName = stringFromNode(ctx);
668         enterLog("leaf-list", leafListName, line);
669         QName leafListQName = QName.create(moduleQName, leafListName);
670         SchemaPath path = stack.addNodeToPath(leafListQName);
671
672         LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, path);
673         moduleBuilder.enterNode(builder);
674
675         parseSchemaNodeArgs(ctx, builder);
676         parseConstraints(ctx, builder.getConstraints());
677         builder.setConfiguration(getConfig(ctx, builder, moduleName, ctx.getStart().getLine()));
678
679         for (int i = 0; i < ctx.getChildCount(); ++i) {
680             final ParseTree childNode = ctx.getChild(i);
681             if (childNode instanceof Ordered_by_stmtContext) {
682                 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
683                 final boolean userOrdered = parseUserOrdered(orderedBy);
684                 builder.setUserOrdered(userOrdered);
685                 break;
686             }
687         }
688     }
689
690     @Override
691     public void exitLeaf_list_stmt(final YangParser.Leaf_list_stmtContext ctx) {
692         moduleBuilder.exitNode();
693         exitLog("leaf-list", stack.removeNodeFromPath());
694     }
695
696     @Override
697     public void enterList_stmt(final List_stmtContext ctx) {
698         final int line = ctx.getStart().getLine();
699         final String listName = stringFromNode(ctx);
700         enterLog("list", listName, line);
701
702         QName listQName = QName.create(moduleQName, listName);
703         SchemaPath path = stack.addNodeToPath(listQName);
704
705         ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, path);
706         moduleBuilder.enterNode(builder);
707
708         parseSchemaNodeArgs(ctx, builder);
709         parseConstraints(ctx, builder.getConstraints());
710         builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
711
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);
721             }
722         }
723     }
724
725     @Override
726     public void exitList_stmt(final List_stmtContext ctx) {
727         moduleBuilder.exitNode();
728         exitLog("list", stack.removeNodeFromPath());
729     }
730
731     @Override
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);
736
737         QName anyXmlQName = QName.create(moduleQName, anyXmlName);
738         SchemaPath path = stack.addNodeToPath(anyXmlQName);
739
740         AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, path);
741         moduleBuilder.enterNode(builder);
742
743         parseSchemaNodeArgs(ctx, builder);
744         parseConstraints(ctx, builder.getConstraints());
745         builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
746     }
747
748     @Override
749     public void exitAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
750         moduleBuilder.exitNode();
751         exitLog("anyxml", stack.removeNodeFromPath());
752     }
753
754     @Override
755     public void enterChoice_stmt(final YangParser.Choice_stmtContext ctx) {
756         final int line = ctx.getStart().getLine();
757         final String choiceName = stringFromNode(ctx);
758         enterLog("choice", choiceName, line);
759
760         QName choiceQName = QName.create(moduleQName, choiceName);
761         SchemaPath path = stack.addNodeToPath(choiceQName);
762
763         ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName, path);
764         moduleBuilder.enterNode(builder);
765
766         parseSchemaNodeArgs(ctx, builder);
767         parseConstraints(ctx, builder.getConstraints());
768         builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
769
770         // set 'default' case
771         for (int i = 0; i < ctx.getChildCount(); i++) {
772             ParseTree child = ctx.getChild(i);
773             if (child instanceof Default_stmtContext) {
774                 String defaultCase = stringFromNode(child);
775                 builder.setDefaultCase(defaultCase);
776                 break;
777             }
778         }
779     }
780
781     @Override
782     public void exitChoice_stmt(final YangParser.Choice_stmtContext ctx) {
783         moduleBuilder.exitNode();
784         exitLog("choice", stack.removeNodeFromPath());
785     }
786
787     @Override
788     public void enterCase_stmt(final YangParser.Case_stmtContext ctx) {
789         final int line = ctx.getStart().getLine();
790         final String caseName = stringFromNode(ctx);
791         enterLog("case", caseName, line);
792
793         QName caseQName = QName.create(moduleQName, caseName);
794         SchemaPath path = stack.addNodeToPath(caseQName);
795
796         ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName, path);
797         moduleBuilder.enterNode(builder);
798
799         parseSchemaNodeArgs(ctx, builder);
800         parseConstraints(ctx, builder.getConstraints());
801     }
802
803     @Override
804     public void exitCase_stmt(final YangParser.Case_stmtContext ctx) {
805         moduleBuilder.exitNode();
806         exitLog("case", stack.removeNodeFromPath());
807     }
808
809     @Override
810     public void enterNotification_stmt(final YangParser.Notification_stmtContext ctx) {
811         final int line = ctx.getStart().getLine();
812         final String notificationName = stringFromNode(ctx);
813         enterLog("notification", notificationName, line);
814
815         QName notificationQName = QName.create(moduleQName, notificationName);
816         SchemaPath path = stack.addNodeToPath(notificationQName);
817
818         NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName, path);
819         moduleBuilder.enterNode(builder);
820
821         parseSchemaNodeArgs(ctx, builder);
822     }
823
824     @Override
825     public void exitNotification_stmt(final YangParser.Notification_stmtContext ctx) {
826         moduleBuilder.exitNode();
827         exitLog("notification", stack.removeNodeFromPath());
828     }
829
830     // Unknown nodes
831     @Override
832     public void enterIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
833         handleUnknownNode(ctx.getStart().getLine(), ctx);
834     }
835
836     @Override
837     public void exitIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
838         moduleBuilder.exitNode();
839         exitLog("unknown-node", stack.removeNodeFromPath());
840     }
841
842     @Override public void enterUnknown_statement(final YangParser.Unknown_statementContext ctx) {
843         handleUnknownNode(ctx.getStart().getLine(), ctx);
844     }
845
846     @Override public void exitUnknown_statement(final YangParser.Unknown_statementContext ctx) {
847         moduleBuilder.exitNode();
848         exitLog("unknown-node", stack.removeNodeFromPath());
849     }
850
851     @Override
852     public void enterRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
853         final int line = ctx.getStart().getLine();
854         final String rpcName = stringFromNode(ctx);
855         enterLog("rpc", rpcName, line);
856
857         QName rpcQName = QName.create(moduleQName, rpcName);
858         SchemaPath path = stack.addNodeToPath(rpcQName);
859
860         RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName, path);
861         moduleBuilder.enterNode(rpcBuilder);
862
863
864         parseSchemaNodeArgs(ctx, rpcBuilder);
865     }
866
867     @Override
868     public void exitRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
869         moduleBuilder.exitNode();
870         exitLog("rpc", stack.removeNodeFromPath());
871     }
872
873     @Override
874     public void enterInput_stmt(final YangParser.Input_stmtContext ctx) {
875         final int line = ctx.getStart().getLine();
876         final String input = "input";
877         enterLog(input, input, line);
878
879         QName rpcQName = QName.create(moduleQName, input);
880         SchemaPath path = stack.addNodeToPath(rpcQName);
881
882         ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(line, rpcQName, path);
883         moduleBuilder.enterNode(builder);
884         builder.setConfiguration(true);
885
886         parseSchemaNodeArgs(ctx, builder);
887         parseConstraints(ctx, builder.getConstraints());
888     }
889
890     @Override
891     public void exitInput_stmt(final YangParser.Input_stmtContext ctx) {
892         moduleBuilder.exitNode();
893         exitLog("input", stack.removeNodeFromPath());
894     }
895
896     @Override
897     public void enterOutput_stmt(final YangParser.Output_stmtContext ctx) {
898         final int line = ctx.getStart().getLine();
899         final String output = "output";
900         enterLog(output, output, line);
901
902         QName rpcQName = QName.create(moduleQName, output);
903         SchemaPath path = stack.addNodeToPath(rpcQName);
904
905         ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line);
906         moduleBuilder.enterNode(builder);
907         builder.setConfiguration(true);
908
909         parseSchemaNodeArgs(ctx, builder);
910         parseConstraints(ctx, builder.getConstraints());
911     }
912
913     @Override
914     public void exitOutput_stmt(final YangParser.Output_stmtContext ctx) {
915         moduleBuilder.exitNode();
916         exitLog("output", stack.removeNodeFromPath());
917     }
918
919     @Override
920     public void enterFeature_stmt(final YangParser.Feature_stmtContext ctx) {
921         final int line = ctx.getStart().getLine();
922         final String featureName = stringFromNode(ctx);
923         enterLog("feature", featureName, line);
924
925         QName featureQName = QName.create(moduleQName, featureName);
926         SchemaPath path = stack.addNodeToPath(featureQName);
927
928         FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName, path);
929         moduleBuilder.enterNode(featureBuilder);
930
931         parseSchemaNodeArgs(ctx, featureBuilder);
932     }
933
934     @Override
935     public void exitFeature_stmt(final YangParser.Feature_stmtContext ctx) {
936         moduleBuilder.exitNode();
937         exitLog("feature", stack.removeNodeFromPath());
938     }
939
940     @Override
941     public void enterDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
942         final int line = ctx.getStart().getLine();
943         final String targetPath = stringFromNode(ctx);
944         enterLog("deviation", targetPath, line);
945
946         String reference = null;
947         String deviate = null;
948         DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath);
949         moduleBuilder.enterNode(builder);
950
951         for (int i = 0; i < ctx.getChildCount(); i++) {
952             ParseTree child = ctx.getChild(i);
953             if (child instanceof Reference_stmtContext) {
954                 reference = stringFromNode(child);
955             } else if (child instanceof Deviate_not_supported_stmtContext) {
956                 deviate = stringFromNode(child);
957             } else if (child instanceof Deviate_add_stmtContext) {
958                 deviate = stringFromNode(child);
959             } else if (child instanceof Deviate_replace_stmtContext) {
960                 deviate = stringFromNode(child);
961             } else if (child instanceof Deviate_delete_stmtContext) {
962                 deviate = stringFromNode(child);
963             }
964         }
965         builder.setReference(reference);
966         builder.setDeviate(deviate);
967     }
968
969     @Override
970     public void exitDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
971         moduleBuilder.exitNode();
972         exitLog("deviation");
973     }
974
975     @Override
976     public void enterIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
977         final int line = ctx.getStart().getLine();
978         final String identityName = stringFromNode(ctx);
979         enterLog("identity", identityName, line);
980
981         final QName identityQName = QName.create(moduleQName, identityName);
982         SchemaPath path = stack.addNodeToPath(identityQName);
983
984         IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line, path);
985         moduleBuilder.enterNode(builder);
986
987         parseSchemaNodeArgs(ctx, builder);
988
989         for (int i = 0; i < ctx.getChildCount(); i++) {
990             ParseTree child = ctx.getChild(i);
991             if (child instanceof Base_stmtContext) {
992                 String baseIdentityName = stringFromNode(child);
993                 builder.setBaseIdentityName(baseIdentityName);
994             }
995         }
996     }
997
998     @Override
999     public void exitIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
1000         moduleBuilder.exitNode();
1001         exitLog("identity", stack.removeNodeFromPath());
1002     }
1003
1004     public ModuleBuilder getModuleBuilder() {
1005         return moduleBuilder;
1006     }
1007
1008     private static void enterLog(final String p1, final String p2, final int line) {
1009         LOG.trace("entering {} {} ({})", p1, p2, line);
1010     }
1011
1012     private static void exitLog(final String p1) {
1013         LOG.trace("exiting {}", p1);
1014     }
1015
1016     private static void exitLog(final String p1, final QName p2) {
1017         LOG.trace("exiting {} {}", p1, p2.getLocalName());
1018     }
1019
1020     private static void setLog(final String p1, final String p2) {
1021         LOG.trace("setting {} {}", p1, p2);
1022     }
1023
1024     private void handleUnknownNode(final int line, final ParseTree ctx) {
1025         final String nodeParameter = stringFromNode(ctx);
1026         enterLog("unknown-node", nodeParameter, line);
1027
1028         final String nodeTypeStr = ctx.getChild(0).getText();
1029         final Iterator<String> splittedElement = COLON_SPLITTER.split(nodeTypeStr).iterator();
1030         final String e0 = splittedElement.next();
1031         final QName nodeType;
1032         if (splittedElement.hasNext()) {
1033             nodeType = QName.create(moduleQName.getModule(), e0, splittedElement.next());
1034         } else {
1035             nodeType = QName.create(moduleQName, e0);
1036         }
1037
1038         QName qname;
1039         try {
1040             if (!Strings.isNullOrEmpty(nodeParameter)) {
1041                 final Iterable<String> splittedName = COLON_SPLITTER.split(nodeParameter);
1042                 final Iterator<String> it = splittedName.iterator();
1043
1044                 if (Iterables.size(splittedName) == 2) {
1045                     qname = QName.create(QNameModule.create(null, null), it.next(), it.next());
1046                 } else {
1047                     qname = QName.create(moduleQName, it.next());
1048                 }
1049             } else {
1050                 qname = nodeType;
1051             }
1052         } catch (IllegalArgumentException e) {
1053             qname = nodeType;
1054         }
1055         SchemaPath path = stack.addNodeToPath(qname);
1056
1057         UnknownSchemaNodeBuilderImpl builder = moduleBuilder.addUnknownSchemaNode(line, qname, path);
1058         builder.setNodeType(nodeType);
1059         builder.setNodeParameter(nodeParameter);
1060
1061
1062         parseSchemaNodeArgs(ctx, builder);
1063         moduleBuilder.enterNode(builder);
1064     }
1065
1066 }