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