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