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