BUG-1800: restore QNameModule sharing in parser
[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.common.QNameModule;
74 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
75 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
76 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
77 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
78 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
79 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
80 import org.opendaylight.yangtools.yang.parser.builder.api.ExtensionBuilder;
81 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
82 import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
83 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
84 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
85 import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder;
86 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
87 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
88 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
89 import org.opendaylight.yangtools.yang.parser.builder.impl.DeviationBuilder;
90 import org.opendaylight.yangtools.yang.parser.builder.impl.FeatureBuilder;
91 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
92 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
93 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder;
94 import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder;
95 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
96 import org.opendaylight.yangtools.yang.parser.builder.impl.NotificationBuilder;
97 import org.opendaylight.yangtools.yang.parser.builder.impl.RefineHolderImpl;
98 import org.opendaylight.yangtools.yang.parser.builder.impl.RpcDefinitionBuilder;
99 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
100 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilderImpl;
101 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
102 import org.slf4j.Logger;
103 import org.slf4j.LoggerFactory;
104
105
106 public final class YangParserListenerImpl extends YangParserBaseListener {
107     private static final Logger LOG = LoggerFactory.getLogger(YangParserListenerImpl.class);
108     private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings();
109     private static final Splitter COLON_SPLITTER = Splitter.on(':');
110     private static final String AUGMENT_STR = "augment";
111
112     private final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
113     private final SchemaPathStack stack = new SchemaPathStack();
114     private final Map<String, TreeMap<Date, URI>> namespaceContext;
115     private final String sourcePath;
116     private QName moduleQName = new QName(null, new Date(0L), null, "dummy");
117     private ModuleBuilder moduleBuilder;
118     private String moduleName;
119     private int augmentOrder;
120     private String yangModelPrefix;
121
122     public YangParserListenerImpl(final Map<String, TreeMap<Date, URI>> namespaceContext, final String sourcePath) {
123         this.namespaceContext = namespaceContext;
124         this.sourcePath = sourcePath;
125     }
126
127     /**
128      * Create a new instance.
129      *
130      * FIXME: the resulting type needs to be extracted, such that we can reuse
131      * the "BaseListener" aspect, which need not be exposed to the user. Maybe
132      * factor out a base class into repo.spi?
133      *
134      * @param namespaceContext
135      * @param sourcePath
136      * @param walker
137      * @param tree
138      * @return new instance of YangParserListenerImpl
139      */
140     public static YangParserListenerImpl create(final Map<String, TreeMap<Date, URI>> namespaceContext,
141             final String sourcePath, final ParseTreeWalker walker, final ParseTree tree) {
142         final YangParserListenerImpl ret = new YangParserListenerImpl(namespaceContext, sourcePath);
143         walker.walk(ret, tree);
144         return ret;
145     }
146
147
148
149     @Override
150     public void enterModule_stmt(final YangParser.Module_stmtContext ctx) {
151         moduleName = stringFromNode(ctx);
152         LOG.trace("entering module {}", moduleName);
153         enterLog("module", moduleName, 0);
154         stack.push();
155
156         moduleBuilder = new ModuleBuilder(moduleName, sourcePath);
157
158         String description = null;
159         String reference = null;
160         for (int i = 0; i < ctx.getChildCount(); i++) {
161             ParseTree child = ctx.getChild(i);
162             if (child instanceof Description_stmtContext) {
163                 description = stringFromNode(child);
164             } else if (child instanceof Reference_stmtContext) {
165                 reference = stringFromNode(child);
166             } else {
167                 if (description != null && reference != null) {
168                     break;
169                 }
170             }
171         }
172         moduleBuilder.setDescription(description);
173         moduleBuilder.setReference(reference);
174     }
175
176     @Override
177     public void exitModule_stmt(final YangParser.Module_stmtContext ctx) {
178         exitLog("module");
179         stack.pop();
180     }
181
182     @Override
183     public void enterSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
184         moduleName = stringFromNode(ctx);
185         LOG.trace("entering submodule {}", moduleName);
186         enterLog("submodule", moduleName, 0);
187         stack.push();
188
189         moduleBuilder = new ModuleBuilder(moduleName, true, sourcePath);
190
191         String description = null;
192         String reference = null;
193         for (int i = 0; i < ctx.getChildCount(); i++) {
194             ParseTree child = ctx.getChild(i);
195             if (child instanceof Description_stmtContext) {
196                 description = stringFromNode(child);
197             } else if (child instanceof Reference_stmtContext) {
198                 reference = stringFromNode(child);
199             } else {
200                 if (description != null && reference != null) {
201                     break;
202                 }
203             }
204         }
205         moduleBuilder.setDescription(description);
206         moduleBuilder.setReference(reference);
207     }
208
209     @Override
210     public void exitSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
211         exitLog("submodule");
212         stack.pop();
213     }
214
215     @Override
216     public void enterBelongs_to_stmt(final YangParser.Belongs_to_stmtContext ctx) {
217         final String belongsTo = stringFromNode(ctx);
218         TreeMap<Date, URI> context = namespaceContext.get(belongsTo);
219         Map.Entry<Date, URI> entry = context.firstEntry();
220         // TODO
221         // Submodule will contain namespace and revision from module to which it
222         // belongs. If there are multiple modules with same name and different
223         // revisions, it will has revision from the newest one.
224         this.moduleQName = QName.create(entry.getValue(), entry.getKey(), moduleQName.getLocalName());
225         moduleBuilder.setQNameModule(moduleQName.getModule());
226         moduleBuilder.setBelongsTo(belongsTo);
227         for (int i = 0; i < ctx.getChildCount(); ++i) {
228             final ParseTree treeNode = ctx.getChild(i);
229             if (treeNode instanceof Prefix_stmtContext) {
230                 yangModelPrefix = stringFromNode(treeNode);
231                 moduleBuilder.setPrefix(yangModelPrefix);
232                 setLog("prefix", yangModelPrefix);
233             }
234         }
235     }
236
237     @Override
238     public void enterModule_header_stmts(final Module_header_stmtsContext ctx) {
239         enterLog("module_header", "", ctx.getStart().getLine());
240         String yangVersion = null;
241         for (int i = 0; i < ctx.getChildCount(); ++i) {
242             final ParseTree treeNode = ctx.getChild(i);
243             if (treeNode instanceof Namespace_stmtContext) {
244                 final String namespaceStr = stringFromNode(treeNode);
245                 final URI namespace = URI.create(namespaceStr);
246                 this.moduleQName = QName.create(namespace, moduleQName.getRevision(), moduleQName.getLocalName());
247                 moduleBuilder.setQNameModule(moduleQName.getModule());
248                 setLog("namespace", namespaceStr);
249             } else if (treeNode instanceof Prefix_stmtContext) {
250                 yangModelPrefix = stringFromNode(treeNode);
251                 this.moduleQName = QName.create(moduleQName.getModule(), yangModelPrefix, moduleQName.getLocalName());
252                 moduleBuilder.setPrefix(yangModelPrefix);
253                 setLog("prefix", yangModelPrefix);
254             } else if (treeNode instanceof Yang_version_stmtContext) {
255                 yangVersion = stringFromNode(treeNode);
256                 setLog("yang-version", yangVersion);
257             }
258         }
259
260         if (yangVersion == null) {
261             yangVersion = "1";
262         }
263         moduleBuilder.setYangVersion(yangVersion);
264     }
265
266     @Override
267     public void exitModule_header_stmts(final Module_header_stmtsContext ctx) {
268         exitLog("module_header");
269     }
270
271     @Override
272     public void enterMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
273         enterLog("meta_stmt", "", ctx.getStart().getLine());
274         for (int i = 0; i < ctx.getChildCount(); i++) {
275             ParseTree child = ctx.getChild(i);
276             if (child instanceof Organization_stmtContext) {
277                 final String organization = stringFromNode(child);
278                 moduleBuilder.setOrganization(organization);
279                 setLog("organization", organization);
280             } else if (child instanceof Contact_stmtContext) {
281                 final String contact = stringFromNode(child);
282                 moduleBuilder.setContact(contact);
283                 setLog("contact", contact);
284             } else if (child instanceof Description_stmtContext) {
285                 final String description = stringFromNode(child);
286                 moduleBuilder.setDescription(description);
287                 setLog("description", description);
288             } else if (child instanceof Reference_stmtContext) {
289                 final String reference = stringFromNode(child);
290                 moduleBuilder.setReference(reference);
291                 setLog("reference", reference);
292             }
293         }
294     }
295
296     @Override
297     public void exitMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
298         exitLog("meta_stmt");
299     }
300
301     @Override
302     public void enterRevision_stmts(final Revision_stmtsContext ctx) {
303         enterLog("revisions", "", ctx.getStart().getLine());
304         for (int i = 0; i < ctx.getChildCount(); ++i) {
305             final ParseTree treeNode = ctx.getChild(i);
306             if (treeNode instanceof Revision_stmtContext) {
307                 updateRevisionForRevisionStatement(treeNode);
308             }
309         }
310     }
311
312     @Override
313     public void exitRevision_stmts(final Revision_stmtsContext ctx) {
314         exitLog("revisions");
315     }
316
317     private void updateRevisionForRevisionStatement(final ParseTree treeNode) {
318         final String revisionDateStr = stringFromNode(treeNode);
319         try {
320             final Date revisionDate = SIMPLE_DATE_FORMAT.parse(revisionDateStr);
321             if ((revisionDate != null) && (this.moduleQName.getRevision().compareTo(revisionDate) < 0)) {
322                 this.moduleQName = QName.create(moduleQName.getNamespace(), revisionDate, moduleQName.getLocalName());
323                 moduleBuilder.setQNameModule(moduleQName.getModule());
324                 setLog("revision", revisionDate.toString());
325                 for (int i = 0; i < treeNode.getChildCount(); ++i) {
326                     ParseTree child = treeNode.getChild(i);
327                     if (child instanceof Reference_stmtContext) {
328                         moduleBuilder.setReference(stringFromNode(child));
329                     }
330                 }
331             }
332         } catch (ParseException e) {
333             LOG.warn("Failed to parse revision string: {}", revisionDateStr, e);
334         }
335     }
336
337     @Override
338     public void enterImport_stmt(final Import_stmtContext ctx) {
339         final int line = ctx.getStart().getLine();
340         final String importName = stringFromNode(ctx);
341         enterLog("import", importName, line);
342
343         String importPrefix = null;
344         Date importRevision = null;
345
346         for (int i = 0; i < ctx.getChildCount(); ++i) {
347             final ParseTree treeNode = ctx.getChild(i);
348             if (treeNode instanceof Prefix_stmtContext) {
349                 importPrefix = stringFromNode(treeNode);
350             } else 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, 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, 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                     // FIXME: this lookup does not look right, as we will end up with
586                     //        a qname which does not have a namespace. At any rate we
587                     //        should arrive at a QNameModule!
588                     namespace = namespaces.get(revision);
589                 }
590
591                 final QNameModule mod = QNameModule.cachedReference(QNameModule.create(namespace, revision));
592                 qname = QName.create(mod, name);
593             }
594         }
595         return qname;
596     }
597
598     @Override
599     public void exitType_stmt(final YangParser.Type_stmtContext ctx) {
600         final String typeName = stringFromNode(ctx);
601         if ("union".equals(typeName)) {
602             moduleBuilder.exitNode();
603         }
604         exitLog("type", stack.removeNodeFromPath());
605     }
606
607     @Override
608     public void enterGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
609         final int line = ctx.getStart().getLine();
610         final String groupName = stringFromNode(ctx);
611         enterLog("grouping", groupName, line);
612         QName groupQName = QName.create(moduleQName, groupName);
613         SchemaPath path = stack.addNodeToPath(groupQName);
614
615         GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName, path);
616         parseSchemaNodeArgs(ctx, builder);
617
618         moduleBuilder.enterNode(builder);
619     }
620
621     @Override
622     public void exitGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
623         moduleBuilder.exitNode();
624         exitLog("grouping", stack.removeNodeFromPath());
625     }
626
627     @Override
628     public void enterContainer_stmt(final Container_stmtContext ctx) {
629         final int line = ctx.getStart().getLine();
630         final String containerName = stringFromNode(ctx);
631         enterLog("container", containerName, line);
632
633         QName containerQName = QName.create(moduleQName, containerName);
634         SchemaPath path = stack.addNodeToPath(containerQName);
635
636         ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(line, containerQName, path);
637         parseSchemaNodeArgs(ctx, builder);
638         parseConstraints(ctx, builder.getConstraints());
639         builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
640
641         for (int i = 0; i < ctx.getChildCount(); ++i) {
642             final ParseTree childNode = ctx.getChild(i);
643             if (childNode instanceof Presence_stmtContext) {
644                 builder.setPresence(true);
645                 break;
646             }
647         }
648
649         moduleBuilder.enterNode(builder);
650     }
651
652     @Override
653     public void exitContainer_stmt(final Container_stmtContext ctx) {
654         moduleBuilder.exitNode();
655         exitLog("container", stack.removeNodeFromPath());
656     }
657
658     @Override
659     public void enterLeaf_stmt(final Leaf_stmtContext ctx) {
660         final int line = ctx.getStart().getLine();
661         final String leafName = stringFromNode(ctx);
662         enterLog("leaf", leafName, line);
663
664         QName leafQName = QName.create(moduleQName, leafName);
665         SchemaPath path = stack.addNodeToPath(leafQName);
666
667         LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, path);
668         parseSchemaNodeArgs(ctx, builder);
669         parseConstraints(ctx, builder.getConstraints());
670         builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
671
672         String defaultStr = null;
673         String unitsStr = null;
674         for (int i = 0; i < ctx.getChildCount(); i++) {
675             ParseTree child = ctx.getChild(i);
676             if (child instanceof Default_stmtContext) {
677                 defaultStr = stringFromNode(child);
678             } else if (child instanceof Units_stmtContext) {
679                 unitsStr = stringFromNode(child);
680             }
681         }
682         builder.setDefaultStr(defaultStr);
683         builder.setUnits(unitsStr);
684
685         moduleBuilder.enterNode(builder);
686     }
687
688     @Override
689     public void exitLeaf_stmt(final YangParser.Leaf_stmtContext ctx) {
690         moduleBuilder.exitNode();
691         exitLog("leaf", stack.removeNodeFromPath());
692     }
693
694     @Override
695     public void enterUses_stmt(final YangParser.Uses_stmtContext ctx) {
696         final int line = ctx.getStart().getLine();
697         final String groupingPathStr = stringFromNode(ctx);
698         final SchemaPath groupingPath = parseXPathString(groupingPathStr, line);
699         enterLog("uses", groupingPathStr, line);
700
701         UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPath);
702
703         moduleBuilder.enterNode(builder);
704     }
705
706     @Override
707     public void exitUses_stmt(final YangParser.Uses_stmtContext ctx) {
708         moduleBuilder.exitNode();
709         exitLog("uses");
710     }
711
712     @Override
713     public void enterUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
714         final int line = ctx.getStart().getLine();
715         final String augmentPath = stringFromNode(ctx);
716         enterLog(AUGMENT_STR, augmentPath, line);
717         stack.push();
718
719         SchemaPath targetPath = parseXPathString(augmentPath, line);
720         AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, targetPath, augmentOrder++);
721
722         for (int i = 0; i < ctx.getChildCount(); i++) {
723             ParseTree child = ctx.getChild(i);
724             if (child instanceof Description_stmtContext) {
725                 builder.setDescription(stringFromNode(child));
726             } else if (child instanceof Reference_stmtContext) {
727                 builder.setReference(stringFromNode(child));
728             } else if (child instanceof Status_stmtContext) {
729                 builder.setStatus(parseStatus((Status_stmtContext) child));
730             } else if (child instanceof When_stmtContext) {
731                 builder.addWhenCondition(stringFromNode(child));
732             }
733         }
734
735         moduleBuilder.enterNode(builder);
736     }
737
738     @Override
739     public void exitUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
740         moduleBuilder.exitNode();
741         exitLog(AUGMENT_STR);
742         stack.pop();
743     }
744
745     @Override
746     public void enterRefine_stmt(final YangParser.Refine_stmtContext ctx) {
747         final String refineString = stringFromNode(ctx);
748         enterLog("refine", refineString, ctx.getStart().getLine());
749
750         RefineHolderImpl refine = parseRefine(ctx, moduleName);
751         moduleBuilder.addRefine(refine);
752         moduleBuilder.enterNode(refine);
753     }
754
755     @Override
756     public void exitRefine_stmt(final YangParser.Refine_stmtContext ctx) {
757         moduleBuilder.exitNode();
758         exitLog("refine");
759     }
760
761     @Override
762     public void enterLeaf_list_stmt(final Leaf_list_stmtContext ctx) {
763         final int line = ctx.getStart().getLine();
764         final String leafListName = stringFromNode(ctx);
765         enterLog("leaf-list", leafListName, line);
766         QName leafListQName = QName.create(moduleQName, leafListName);
767         SchemaPath path = stack.addNodeToPath(leafListQName);
768
769         LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, path);
770         moduleBuilder.enterNode(builder);
771
772         parseSchemaNodeArgs(ctx, builder);
773         parseConstraints(ctx, builder.getConstraints());
774         builder.setConfiguration(getConfig(ctx, builder, moduleName, ctx.getStart().getLine()));
775
776         for (int i = 0; i < ctx.getChildCount(); ++i) {
777             final ParseTree childNode = ctx.getChild(i);
778             if (childNode instanceof Ordered_by_stmtContext) {
779                 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
780                 final boolean userOrdered = parseUserOrdered(orderedBy);
781                 builder.setUserOrdered(userOrdered);
782                 break;
783             }
784         }
785     }
786
787     @Override
788     public void exitLeaf_list_stmt(final YangParser.Leaf_list_stmtContext ctx) {
789         moduleBuilder.exitNode();
790         exitLog("leaf-list", stack.removeNodeFromPath());
791     }
792
793     @Override
794     public void enterList_stmt(final List_stmtContext ctx) {
795         final int line = ctx.getStart().getLine();
796         final String listName = stringFromNode(ctx);
797         enterLog("list", listName, line);
798
799         QName listQName = QName.create(moduleQName, listName);
800         SchemaPath path = stack.addNodeToPath(listQName);
801
802         ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, path);
803         moduleBuilder.enterNode(builder);
804
805         parseSchemaNodeArgs(ctx, builder);
806         parseConstraints(ctx, builder.getConstraints());
807         builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
808
809         for (int i = 0; i < ctx.getChildCount(); ++i) {
810             ParseTree childNode = ctx.getChild(i);
811             if (childNode instanceof Ordered_by_stmtContext) {
812                 final Ordered_by_stmtContext orderedBy = (Ordered_by_stmtContext) childNode;
813                 final boolean userOrdered = parseUserOrdered(orderedBy);
814                 builder.setUserOrdered(userOrdered);
815             } else if (childNode instanceof Key_stmtContext) {
816                 List<String> key = createListKey((Key_stmtContext) childNode);
817                 builder.setKeys(key);
818             }
819         }
820     }
821
822     @Override
823     public void exitList_stmt(final List_stmtContext ctx) {
824         moduleBuilder.exitNode();
825         exitLog("list", stack.removeNodeFromPath());
826     }
827
828     @Override
829     public void enterAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
830         final int line = ctx.getStart().getLine();
831         final String anyXmlName = stringFromNode(ctx);
832         enterLog("anyxml", anyXmlName, line);
833
834         QName anyXmlQName = QName.create(moduleQName, anyXmlName);
835         SchemaPath path = stack.addNodeToPath(anyXmlQName);
836
837         AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, path);
838         moduleBuilder.enterNode(builder);
839
840         parseSchemaNodeArgs(ctx, builder);
841         parseConstraints(ctx, builder.getConstraints());
842         builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
843     }
844
845     @Override
846     public void exitAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
847         moduleBuilder.exitNode();
848         exitLog("anyxml", stack.removeNodeFromPath());
849     }
850
851     @Override
852     public void enterChoice_stmt(final YangParser.Choice_stmtContext ctx) {
853         final int line = ctx.getStart().getLine();
854         final String choiceName = stringFromNode(ctx);
855         enterLog("choice", choiceName, line);
856
857         QName choiceQName = QName.create(moduleQName, choiceName);
858         SchemaPath path = stack.addNodeToPath(choiceQName);
859
860         ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName, path);
861         moduleBuilder.enterNode(builder);
862
863         parseSchemaNodeArgs(ctx, builder);
864         parseConstraints(ctx, builder.getConstraints());
865         builder.setConfiguration(getConfig(ctx, builder, moduleName, line));
866
867         // set 'default' case
868         for (int i = 0; i < ctx.getChildCount(); i++) {
869             ParseTree child = ctx.getChild(i);
870             if (child instanceof Default_stmtContext) {
871                 String defaultCase = stringFromNode(child);
872                 builder.setDefaultCase(defaultCase);
873                 break;
874             }
875         }
876     }
877
878     @Override
879     public void exitChoice_stmt(final YangParser.Choice_stmtContext ctx) {
880         moduleBuilder.exitNode();
881         exitLog("choice", stack.removeNodeFromPath());
882     }
883
884     @Override
885     public void enterCase_stmt(final YangParser.Case_stmtContext ctx) {
886         final int line = ctx.getStart().getLine();
887         final String caseName = stringFromNode(ctx);
888         enterLog("case", caseName, line);
889
890         QName caseQName = QName.create(moduleQName, caseName);
891         SchemaPath path = stack.addNodeToPath(caseQName);
892
893         ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName, path);
894         moduleBuilder.enterNode(builder);
895
896         parseSchemaNodeArgs(ctx, builder);
897         parseConstraints(ctx, builder.getConstraints());
898     }
899
900     @Override
901     public void exitCase_stmt(final YangParser.Case_stmtContext ctx) {
902         moduleBuilder.exitNode();
903         exitLog("case", stack.removeNodeFromPath());
904     }
905
906     @Override
907     public void enterNotification_stmt(final YangParser.Notification_stmtContext ctx) {
908         final int line = ctx.getStart().getLine();
909         final String notificationName = stringFromNode(ctx);
910         enterLog("notification", notificationName, line);
911
912         QName notificationQName = QName.create(moduleQName, notificationName);
913         SchemaPath path = stack.addNodeToPath(notificationQName);
914
915         NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName, path);
916         moduleBuilder.enterNode(builder);
917
918         parseSchemaNodeArgs(ctx, builder);
919     }
920
921     @Override
922     public void exitNotification_stmt(final YangParser.Notification_stmtContext ctx) {
923         moduleBuilder.exitNode();
924         exitLog("notification", stack.removeNodeFromPath());
925     }
926
927     // Unknown nodes
928     @Override
929     public void enterIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
930         handleUnknownNode(ctx.getStart().getLine(), ctx);
931     }
932
933     @Override
934     public void exitIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
935         moduleBuilder.exitNode();
936         exitLog("unknown-node", stack.removeNodeFromPath());
937     }
938
939     @Override public void enterUnknown_statement(final YangParser.Unknown_statementContext ctx) {
940         handleUnknownNode(ctx.getStart().getLine(), ctx);
941     }
942
943     @Override public void exitUnknown_statement(final YangParser.Unknown_statementContext ctx) {
944         moduleBuilder.exitNode();
945         exitLog("unknown-node", stack.removeNodeFromPath());
946     }
947
948     @Override
949     public void enterRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
950         final int line = ctx.getStart().getLine();
951         final String rpcName = stringFromNode(ctx);
952         enterLog("rpc", rpcName, line);
953
954         QName rpcQName = QName.create(moduleQName, rpcName);
955         SchemaPath path = stack.addNodeToPath(rpcQName);
956
957         RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName, path);
958         moduleBuilder.enterNode(rpcBuilder);
959
960
961         parseSchemaNodeArgs(ctx, rpcBuilder);
962     }
963
964     @Override
965     public void exitRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
966         moduleBuilder.exitNode();
967         exitLog("rpc", stack.removeNodeFromPath());
968     }
969
970     @Override
971     public void enterInput_stmt(final YangParser.Input_stmtContext ctx) {
972         final int line = ctx.getStart().getLine();
973         final String input = "input";
974         enterLog(input, input, line);
975
976         QName rpcQName = QName.create(moduleQName, input);
977         SchemaPath path = stack.addNodeToPath(rpcQName);
978
979         ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(line, rpcQName, path);
980         moduleBuilder.enterNode(builder);
981         builder.setConfiguration(true);
982
983         parseSchemaNodeArgs(ctx, builder);
984         parseConstraints(ctx, builder.getConstraints());
985     }
986
987     @Override
988     public void exitInput_stmt(final YangParser.Input_stmtContext ctx) {
989         moduleBuilder.exitNode();
990         exitLog("input", stack.removeNodeFromPath());
991     }
992
993     @Override
994     public void enterOutput_stmt(final YangParser.Output_stmtContext ctx) {
995         final int line = ctx.getStart().getLine();
996         final String output = "output";
997         enterLog(output, output, line);
998
999         QName rpcQName = QName.create(moduleQName, output);
1000         SchemaPath path = stack.addNodeToPath(rpcQName);
1001
1002         ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line);
1003         moduleBuilder.enterNode(builder);
1004         builder.setConfiguration(true);
1005
1006         parseSchemaNodeArgs(ctx, builder);
1007         parseConstraints(ctx, builder.getConstraints());
1008     }
1009
1010     @Override
1011     public void exitOutput_stmt(final YangParser.Output_stmtContext ctx) {
1012         moduleBuilder.exitNode();
1013         exitLog("output", stack.removeNodeFromPath());
1014     }
1015
1016     @Override
1017     public void enterFeature_stmt(final YangParser.Feature_stmtContext ctx) {
1018         final int line = ctx.getStart().getLine();
1019         final String featureName = stringFromNode(ctx);
1020         enterLog("feature", featureName, line);
1021
1022         QName featureQName = QName.create(moduleQName, featureName);
1023         SchemaPath path = stack.addNodeToPath(featureQName);
1024
1025         FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName, path);
1026         moduleBuilder.enterNode(featureBuilder);
1027
1028         parseSchemaNodeArgs(ctx, featureBuilder);
1029     }
1030
1031     @Override
1032     public void exitFeature_stmt(final YangParser.Feature_stmtContext ctx) {
1033         moduleBuilder.exitNode();
1034         exitLog("feature", stack.removeNodeFromPath());
1035     }
1036
1037     @Override
1038     public void enterDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
1039         final int line = ctx.getStart().getLine();
1040         final String targetPathStr = stringFromNode(ctx);
1041         if (!targetPathStr.startsWith("/")) {
1042             throw new YangParseException(moduleName, line,
1043                     "Deviation argument string must be an absolute schema node identifier.");
1044         }
1045         enterLog("deviation", targetPathStr, line);
1046
1047         String reference = null;
1048         String deviate = null;
1049
1050         SchemaPath targetPath = parseXPathString(targetPathStr, line);
1051         DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath);
1052         moduleBuilder.enterNode(builder);
1053
1054         for (int i = 0; i < ctx.getChildCount(); i++) {
1055             ParseTree child = ctx.getChild(i);
1056             if (child instanceof Reference_stmtContext) {
1057                 reference = stringFromNode(child);
1058             } else if (child instanceof Deviate_not_supported_stmtContext) {
1059                 deviate = stringFromNode(child);
1060             } else if (child instanceof Deviate_add_stmtContext) {
1061                 deviate = stringFromNode(child);
1062             } else if (child instanceof Deviate_replace_stmtContext) {
1063                 deviate = stringFromNode(child);
1064             } else if (child instanceof Deviate_delete_stmtContext) {
1065                 deviate = stringFromNode(child);
1066             }
1067         }
1068         builder.setReference(reference);
1069         builder.setDeviate(deviate);
1070     }
1071
1072     public SchemaPath parseXPathString(final String xpathString, final int line) {
1073         final boolean absolute = !xpathString.isEmpty() && xpathString.charAt(0) == '/';
1074
1075         final List<QName> path = new ArrayList<>();
1076         for (String pathElement : SLASH_SPLITTER.split(xpathString)) {
1077             final Iterator<String> it = COLON_SPLITTER.split(pathElement).iterator();
1078             final String s = it.next();
1079             if (it.hasNext()) {
1080                 path.add(parseQName(pathElement, line));
1081             } else {
1082                 path.add(QName.create(moduleQName, s));
1083             }
1084         }
1085         return SchemaPath.create(path, absolute);
1086     }
1087
1088     @Override
1089     public void exitDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
1090         moduleBuilder.exitNode();
1091         exitLog("deviation");
1092     }
1093
1094     @Override
1095     public void enterIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
1096         final int line = ctx.getStart().getLine();
1097         final String identityName = stringFromNode(ctx);
1098         enterLog("identity", identityName, line);
1099
1100         final QName identityQName = QName.create(moduleQName, identityName);
1101         SchemaPath path = stack.addNodeToPath(identityQName);
1102
1103         IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line, path);
1104         moduleBuilder.enterNode(builder);
1105
1106         parseSchemaNodeArgs(ctx, builder);
1107
1108         for (int i = 0; i < ctx.getChildCount(); i++) {
1109             ParseTree child = ctx.getChild(i);
1110             if (child instanceof Base_stmtContext) {
1111                 String baseIdentityName = stringFromNode(child);
1112                 builder.setBaseIdentityName(baseIdentityName);
1113             }
1114         }
1115     }
1116
1117     @Override
1118     public void exitIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
1119         moduleBuilder.exitNode();
1120         exitLog("identity", stack.removeNodeFromPath());
1121     }
1122
1123     public ModuleBuilder getModuleBuilder() {
1124         return moduleBuilder;
1125     }
1126
1127     private static void enterLog(final String p1, final String p2, final int line) {
1128         LOG.trace("entering {} {} ({})", p1, p2, line);
1129     }
1130
1131     private static void exitLog(final String p1) {
1132         LOG.trace("exiting {}", p1);
1133     }
1134
1135     private static void exitLog(final String p1, final QName p2) {
1136         LOG.trace("exiting {} {}", p1, p2.getLocalName());
1137     }
1138
1139     private static void setLog(final String p1, final String p2) {
1140         LOG.trace("setting {} {}", p1, p2);
1141     }
1142
1143     private void handleUnknownNode(final int line, final ParseTree ctx) {
1144         final String nodeParameter = stringFromNode(ctx);
1145         enterLog("unknown-node", nodeParameter, line);
1146
1147         final String nodeTypeStr = ctx.getChild(0).getText();
1148         QName nodeType = parseQName(nodeTypeStr, line);
1149
1150         QName qname = null;
1151         try {
1152             //FIXME: rewrite whole method to handle unknown nodes properly.
1153             // This should be bugfix for bug https://bugs.opendaylight.org/show_bug.cgi?id=1539
1154             // After this fix bug https://bugs.opendaylight.org/show_bug.cgi?id=1538 MUST be fixed since
1155             // they are dependent!!!
1156             if (Strings.isNullOrEmpty(nodeParameter)) {
1157                 qname = nodeType;
1158             } else {
1159                 final Iterable<String> splittedName = COLON_SPLITTER.split(nodeParameter);
1160                 final Iterator<String> it = splittedName.iterator();
1161                 if (Iterables.size(splittedName) == 2) {
1162                     qname = parseQName(nodeParameter, line);
1163                 } else {
1164                     qname = QName.create(moduleQName, it.next());
1165                 }
1166             }
1167         } catch (IllegalArgumentException | YangParseException ex) {
1168             qname = nodeType;
1169         }
1170
1171         SchemaPath path = stack.addNodeToPath(qname);
1172
1173         UnknownSchemaNodeBuilderImpl builder = moduleBuilder.addUnknownSchemaNode(line, qname, path);
1174         builder.setNodeType(nodeType);
1175         builder.setNodeParameter(nodeParameter);
1176
1177         parseSchemaNodeArgs(ctx, builder);
1178         moduleBuilder.enterNode(builder);
1179     }
1180
1181 }