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