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