BUG-1578: fixed NPE caused by invalid import statement.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / YangParserListenerImpl.java
index bf7f399b10996f723083aaee0e2c8dd5a51cb131..ece9226253cb04bb012a19c0fa67b7fae1745619 100644 (file)
@@ -7,32 +7,36 @@
  */
 package org.opendaylight.yangtools.yang.parser.impl;
 
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.checkMissingBody;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.createActualSchemaPath;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.createListKey;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.getConfig;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.getIdentityrefBase;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.parseConstraints;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.parseDefault;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.parseRefine;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.parseSchemaNodeArgs;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.parseStatus;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.parseTypeWithBody;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.parseUnits;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.parseUnknownTypeWithBody;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.parseUserOrdered;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.parseYinValue;
-import static org.opendaylight.yangtools.yang.parser.util.ParserListenerUtils.stringFromNode;
-
+import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.checkMissingBody;
+import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.createListKey;
+import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.getConfig;
+import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.getIdentityrefBase;
+import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseConstraints;
+import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseDefault;
+import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseRefine;
+import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseSchemaNodeArgs;
+import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseStatus;
+import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseTypeWithBody;
+import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseUnits;
+import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseUserOrdered;
+import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.parseYinValue;
+import static org.opendaylight.yangtools.yang.parser.impl.ParserListenerUtils.stringFromNode;
+
+import com.google.common.base.Splitter;
 import com.google.common.base.Strings;
+import com.google.common.collect.Iterables;
 import java.net.URI;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.Iterator;
 import java.util.List;
-import java.util.Stack;
+import java.util.Map;
+import java.util.TreeMap;
 import org.antlr.v4.runtime.tree.ParseTree;
+import org.antlr.v4.runtime.tree.ParseTreeWalker;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Argument_stmtContext;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Base_stmtContext;
@@ -66,13 +70,15 @@ import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.When_stmtContext;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Yang_version_stmtContext;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangParserBaseListener;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.ModuleImport;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.util.BaseTypes;
-import org.opendaylight.yangtools.yang.model.util.YangTypesConverter;
 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
+import org.opendaylight.yangtools.yang.parser.builder.api.ExtensionBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.TypeDefinitionBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.AnyXmlBuilder;
@@ -80,7 +86,6 @@ import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.ChoiceCaseBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.DeviationBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.ExtensionBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.FeatureBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.LeafListSchemaNodeBuilder;
@@ -88,46 +93,64 @@ import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder
 import org.opendaylight.yangtools.yang.parser.builder.impl.ListSchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.NotificationBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.impl.RefineHolderImpl;
 import org.opendaylight.yangtools.yang.parser.builder.impl.RpcDefinitionBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
-import org.opendaylight.yangtools.yang.parser.util.RefineHolder;
+import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilderImpl;
+import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+
 public final class YangParserListenerImpl extends YangParserBaseListener {
-    private static final Logger LOGGER = LoggerFactory.getLogger(YangParserListenerImpl.class);
+    private static final Logger LOG = LoggerFactory.getLogger(YangParserListenerImpl.class);
+    private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings();
+    private static final Splitter COLON_SPLITTER = Splitter.on(':');
     private static final String AUGMENT_STR = "augment";
 
+    private final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
+    private final SchemaPathStack stack = new SchemaPathStack();
+    private final Map<String, TreeMap<Date, URI>> namespaceContext;
     private final String sourcePath;
+    private QName moduleQName = new QName(null, new Date(0L), null, "dummy");
     private ModuleBuilder moduleBuilder;
     private String moduleName;
-    private URI namespace;
-    private String yangModelPrefix;
-    private Date revision = new Date(0L);
-
-    private final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
-    private final Stack<Stack<QName>> actualPath = new Stack<>();
     private int augmentOrder;
+    private String yangModelPrefix;
 
-    private void addNodeToPath(QName name) {
-        actualPath.peek().push(name);
+    public YangParserListenerImpl(final Map<String, TreeMap<Date, URI>> namespaceContext, final String sourcePath) {
+        this.namespaceContext = namespaceContext;
+        this.sourcePath = sourcePath;
     }
 
-    private QName removeNodeFromPath() {
-        return actualPath.peek().pop();
+    /**
+     * Create a new instance.
+     *
+     * FIXME: the resulting type needs to be extracted, such that we can reuse
+     *        the "BaseListener" aspect, which need not be exposed to the user.
+     *        Maybe factor out a base class into repo.spi?
+     *
+     * @param namespaceContext
+     * @param sourcePath
+     * @param walker
+     * @param tree
+     * @return
+     */
+    public static YangParserListenerImpl create(final Map<String, TreeMap<Date, URI>> namespaceContext,
+            final String sourcePath, final ParseTreeWalker walker, final ParseTree tree) {
+        final YangParserListenerImpl ret = new YangParserListenerImpl(namespaceContext, sourcePath);
+        walker.walk(ret, tree);
+        return ret;
     }
 
-    public YangParserListenerImpl(String sourcePath) {
-        this.sourcePath = sourcePath;
-    }
+
 
     @Override
-    public void enterModule_stmt(YangParser.Module_stmtContext ctx) {
+    public void enterModule_stmt(final YangParser.Module_stmtContext ctx) {
         moduleName = stringFromNode(ctx);
-        LOGGER.trace("entering module " + moduleName);
+        LOG.trace("entering module {}", moduleName);
         enterLog("module", moduleName, 0);
-        actualPath.push(new Stack<QName>());
+        stack.push();
 
         moduleBuilder = new ModuleBuilder(moduleName, sourcePath);
 
@@ -150,16 +173,17 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitModule_stmt(YangParser.Module_stmtContext ctx) {
-        exitLog("module", "");
-        actualPath.pop();
+    public void exitModule_stmt(final YangParser.Module_stmtContext ctx) {
+        exitLog("module");
+        stack.pop();
     }
 
-    @Override public void enterSubmodule_stmt(YangParser.Submodule_stmtContext ctx) {
+    @Override
+    public void enterSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
         moduleName = stringFromNode(ctx);
-        LOGGER.trace("entering submodule " + moduleName);
+        LOG.trace("entering submodule {}", moduleName);
         enterLog("submodule", moduleName, 0);
-        actualPath.push(new Stack<QName>());
+        stack.push();
 
         moduleBuilder = new ModuleBuilder(moduleName, true, sourcePath);
 
@@ -181,28 +205,49 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         moduleBuilder.setReference(reference);
     }
 
-    @Override public void exitSubmodule_stmt(YangParser.Submodule_stmtContext ctx) {
-        exitLog("submodule", "");
-        actualPath.pop();
+    @Override
+    public void exitSubmodule_stmt(final YangParser.Submodule_stmtContext ctx) {
+        exitLog("submodule");
+        stack.pop();
     }
 
-    @Override public void enterBelongs_to_stmt(YangParser.Belongs_to_stmtContext ctx) {
-        moduleBuilder.setBelongsTo(stringFromNode(ctx));
+    @Override
+    public void enterBelongs_to_stmt(final YangParser.Belongs_to_stmtContext ctx) {
+        final String belongsTo = stringFromNode(ctx);
+        TreeMap<Date, URI> context = namespaceContext.get(belongsTo);
+        Map.Entry<Date, URI> entry = context.firstEntry();
+        // TODO
+        // Submodule will contain namespace and revision from module to which it
+        // belongs. If there are multiple modules with same name and different
+        // revisions, it will has revision from the newest one.
+        this.moduleQName = QName.create(entry.getValue(), entry.getKey(), moduleQName.getLocalName());
+        moduleBuilder.setQNameModule(moduleQName.getModule());
+        moduleBuilder.setBelongsTo(belongsTo);
+        for (int i = 0; i < ctx.getChildCount(); ++i) {
+            final ParseTree treeNode = ctx.getChild(i);
+            if (treeNode instanceof Prefix_stmtContext) {
+                yangModelPrefix = stringFromNode(treeNode);
+                moduleBuilder.setPrefix(yangModelPrefix);
+                setLog("prefix", yangModelPrefix);
+            }
+        }
     }
 
     @Override
-    public void enterModule_header_stmts(Module_header_stmtsContext ctx) {
+    public void enterModule_header_stmts(final Module_header_stmtsContext ctx) {
         enterLog("module_header", "", ctx.getStart().getLine());
         String yangVersion = null;
         for (int i = 0; i < ctx.getChildCount(); ++i) {
             final ParseTree treeNode = ctx.getChild(i);
             if (treeNode instanceof Namespace_stmtContext) {
                 final String namespaceStr = stringFromNode(treeNode);
-                namespace = URI.create(namespaceStr);
-                moduleBuilder.setNamespace(namespace);
+                final URI namespace = URI.create(namespaceStr);
+                this.moduleQName = QName.create(namespace, moduleQName.getRevision(), moduleQName.getLocalName());
+                moduleBuilder.setQNameModule(moduleQName.getModule());
                 setLog("namespace", namespaceStr);
             } else if (treeNode instanceof Prefix_stmtContext) {
                 yangModelPrefix = stringFromNode(treeNode);
+                this.moduleQName = QName.create(moduleQName.getModule(), yangModelPrefix, moduleQName.getLocalName());
                 moduleBuilder.setPrefix(yangModelPrefix);
                 setLog("prefix", yangModelPrefix);
             } else if (treeNode instanceof Yang_version_stmtContext) {
@@ -218,12 +263,12 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitModule_header_stmts(Module_header_stmtsContext ctx) {
-        exitLog("module_header", "");
+    public void exitModule_header_stmts(final Module_header_stmtsContext ctx) {
+        exitLog("module_header");
     }
 
     @Override
-    public void enterMeta_stmts(YangParser.Meta_stmtsContext ctx) {
+    public void enterMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
         enterLog("meta_stmt", "", ctx.getStart().getLine());
         for (int i = 0; i < ctx.getChildCount(); i++) {
             ParseTree child = ctx.getChild(i);
@@ -248,12 +293,12 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitMeta_stmts(YangParser.Meta_stmtsContext ctx) {
-        exitLog("meta_stmt", "");
+    public void exitMeta_stmts(final YangParser.Meta_stmtsContext ctx) {
+        exitLog("meta_stmt");
     }
 
     @Override
-    public void enterRevision_stmts(Revision_stmtsContext ctx) {
+    public void enterRevision_stmts(final Revision_stmtsContext ctx) {
         enterLog("revisions", "", ctx.getStart().getLine());
         for (int i = 0; i < ctx.getChildCount(); ++i) {
             final ParseTree treeNode = ctx.getChild(i);
@@ -264,18 +309,18 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitRevision_stmts(Revision_stmtsContext ctx) {
-        exitLog("revisions", "");
+    public void exitRevision_stmts(final Revision_stmtsContext ctx) {
+        exitLog("revisions");
     }
 
     private void updateRevisionForRevisionStatement(final ParseTree treeNode) {
         final String revisionDateStr = stringFromNode(treeNode);
         try {
             final Date revisionDate = SIMPLE_DATE_FORMAT.parse(revisionDateStr);
-            if ((revisionDate != null) && (this.revision.compareTo(revisionDate) < 0)) {
-                this.revision = revisionDate;
-                moduleBuilder.setRevision(this.revision);
-                setLog("revision", this.revision.toString());
+            if ((revisionDate != null) && (this.moduleQName.getRevision().compareTo(revisionDate) < 0)) {
+                this.moduleQName = QName.create(moduleQName.getNamespace(), revisionDate, moduleQName.getLocalName());
+                moduleBuilder.setQNameModule(moduleQName.getModule());
+                setLog("revision", revisionDate.toString());
                 for (int i = 0; i < treeNode.getChildCount(); ++i) {
                     ParseTree child = treeNode.getChild(i);
                     if (child instanceof Reference_stmtContext) {
@@ -284,13 +329,12 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
                 }
             }
         } catch (ParseException e) {
-            final String message = "Failed to parse revision string: " + revisionDateStr;
-            LOGGER.warn(message);
+            LOG.warn("Failed to parse revision string: {}", revisionDateStr, e);
         }
     }
 
     @Override
-    public void enterImport_stmt(Import_stmtContext ctx) {
+    public void enterImport_stmt(final Import_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String importName = stringFromNode(ctx);
         enterLog("import", importName, line);
@@ -308,27 +352,53 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
                 try {
                     importRevision = SIMPLE_DATE_FORMAT.parse(importRevisionStr);
                 } catch (ParseException e) {
-                    LOGGER.warn("Failed to parse import revision-date at line " + line + ": " + importRevisionStr);
+                    LOG.warn("Failed to parse import revision-date at line {}: {}", line, importRevisionStr, e);
                 }
             }
         }
         moduleBuilder.addModuleImport(importName, importRevision, importPrefix);
-        setLog("import", "(" + importName + "; " + importRevision + "; " + importPrefix + ")");
+        LOG.trace("setting import ({}; {}; {})", importName, importRevision, importPrefix);
+    }
+
+    @Override
+    public void exitImport_stmt(final Import_stmtContext ctx) {
+        exitLog("import");
     }
 
     @Override
-    public void exitImport_stmt(Import_stmtContext ctx) {
-        exitLog("import", "");
+    public void enterInclude_stmt(YangParser.Include_stmtContext ctx) {
+        final int line = ctx.getStart().getLine();
+        final String includeName = stringFromNode(ctx);
+        enterLog("import", includeName, line);
+
+        Date includeRevision = null;
+        for (int i = 0; i < ctx.getChildCount(); i++) {
+            ParseTree treeNode = ctx.getChild(i);
+            if (treeNode instanceof Revision_date_stmtContext) {
+                String importRevisionStr = stringFromNode(treeNode);
+                try {
+                    includeRevision = SIMPLE_DATE_FORMAT.parse(importRevisionStr);
+                } catch (ParseException e) {
+                    LOG.warn("Failed to parse import revision-date at line {}: {}", line, importRevisionStr, e);
+                }
+            }
+        }
+        moduleBuilder.addInclude(includeName, includeRevision);
+    }
+
+    @Override public void exitInclude_stmt(YangParser.Include_stmtContext ctx) {
+        exitLog("include");
     }
 
     @Override
-    public void enterAugment_stmt(YangParser.Augment_stmtContext ctx) {
+    public void enterAugment_stmt(final YangParser.Augment_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String augmentPath = stringFromNode(ctx);
         enterLog(AUGMENT_STR, augmentPath, line);
-        actualPath.push(new Stack<QName>());
+        stack.push();
 
-        AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, augmentOrder++);
+        SchemaPath targetPath = parseXPathString(augmentPath, line);
+        AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, targetPath, augmentOrder++);
 
         for (int i = 0; i < ctx.getChildCount(); i++) {
             ParseTree child = ctx.getChild(i);
@@ -347,20 +417,19 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitAugment_stmt(YangParser.Augment_stmtContext ctx) {
+    public void exitAugment_stmt(final YangParser.Augment_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog(AUGMENT_STR, "");
-        actualPath.pop();
+        exitLog(AUGMENT_STR);
+        stack.pop();
     }
 
     @Override
-    public void enterExtension_stmt(YangParser.Extension_stmtContext ctx) {
+    public void enterExtension_stmt(final YangParser.Extension_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String extName = stringFromNode(ctx);
         enterLog("extension", extName, line);
-        QName qname = new QName(namespace, revision, yangModelPrefix, extName);
-        addNodeToPath(qname);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName qname = QName.create(moduleQName, extName);
+        SchemaPath path = stack.addNodeToPath(qname);
 
         ExtensionBuilder builder = moduleBuilder.addExtension(qname, line, path);
         parseSchemaNodeArgs(ctx, builder);
@@ -382,19 +451,18 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitExtension_stmt(YangParser.Extension_stmtContext ctx) {
+    public void exitExtension_stmt(final YangParser.Extension_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("extension", removeNodeFromPath());
+        exitLog("extension", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterTypedef_stmt(YangParser.Typedef_stmtContext ctx) {
+    public void enterTypedef_stmt(final YangParser.Typedef_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String typedefName = stringFromNode(ctx);
         enterLog("typedef", typedefName, line);
-        QName typedefQName = new QName(namespace, revision, yangModelPrefix, typedefName);
-        addNodeToPath(typedefQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName typedefQName = QName.create(moduleQName, typedefName);
+        SchemaPath path = stack.addNodeToPath(typedefQName);
 
         TypeDefinitionBuilder builder = moduleBuilder.addTypedef(line, typedefQName, path);
         parseSchemaNodeArgs(ctx, builder);
@@ -405,20 +473,20 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitTypedef_stmt(YangParser.Typedef_stmtContext ctx) {
+    public void exitTypedef_stmt(final YangParser.Typedef_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("typedef", removeNodeFromPath());
+        exitLog("typedef", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterType_stmt(YangParser.Type_stmtContext ctx) {
+    public void enterType_stmt(final YangParser.Type_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String typeName = stringFromNode(ctx);
         enterLog("type", typeName, line);
 
-        final QName typeQName = parseQName(typeName);
+        final QName typeQName = parseQName(typeName, line);
 
-        TypeDefinition<?> type = null;
+        TypeDefinition<?> type;
         Type_body_stmtsContext typeBody = null;
         for (int i = 0; i < ctx.getChildCount(); i++) {
             if (ctx.getChild(i) instanceof Type_body_stmtsContext) {
@@ -428,83 +496,116 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         }
 
         // if this is base yang type...
-        if (YangTypesConverter.isBaseYangType(typeName)) {
+        if (BaseTypes.isYangBuildInType(typeName)) {
             if (typeBody == null) {
                 // check for types which must have body
                 checkMissingBody(typeName, moduleName, line);
                 // if there are no constraints, just grab default base yang type
-                type = YangTypesConverter.javaTypeForBaseYangType(typeName);
-                addNodeToPath(type.getQName());
+                type = BaseTypes.defaultBaseTypeFor(typeName).orNull();
+                stack.addNodeToPath(type.getQName());
                 moduleBuilder.setType(type);
             } else {
                 QName qname;
                 switch (typeName) {
-                    case "union":
-                        qname = BaseTypes.constructQName("union");
-                        addNodeToPath(qname);
-                        UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, namespace, revision);
-                        Builder parent = moduleBuilder.getActualNode();
-                        unionBuilder.setParent(parent);
-                        moduleBuilder.enterNode(unionBuilder);
-                        break;
-                    case "identityref":
-                        qname = BaseTypes.constructQName("identityref");
-                        addNodeToPath(qname);
-                        SchemaPath path = createActualSchemaPath(actualPath.peek());
-                        moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody));
-                        break;
-                    default:
-                        type = parseTypeWithBody(typeName, typeBody, actualPath.peek(), namespace, revision,
-                                yangModelPrefix, moduleBuilder.getActualNode());
-                        moduleBuilder.setType(type);
-                        addNodeToPath(type.getQName());
+                case "union":
+                    qname = BaseTypes.UNION_QNAME;
+                    stack.addNodeToPath(qname);
+                    UnionTypeBuilder unionBuilder = moduleBuilder.addUnionType(line, moduleQName.getModule());
+                    Builder parent = moduleBuilder.getActualNode();
+                    unionBuilder.setParent(parent);
+                    moduleBuilder.enterNode(unionBuilder);
+                    break;
+                case "identityref":
+                    qname = BaseTypes.IDENTITYREF_QNAME;
+                    SchemaPath path = stack.addNodeToPath(qname);
+                    moduleBuilder.addIdentityrefType(line, path, getIdentityrefBase(typeBody));
+                    break;
+                default:
+                    type = parseTypeWithBody(typeName, typeBody, stack.currentSchemaPath(), moduleQName,
+                            moduleBuilder.getActualNode());
+                    moduleBuilder.setType(type);
+                    stack.addNodeToPath(type.getQName());
                 }
             }
         } else {
-            type = parseUnknownTypeWithBody(typeQName, typeBody, actualPath.peek(), namespace, revision,
-                    yangModelPrefix, moduleBuilder.getActualNode());
-            // add parent node of this type statement to dirty nodes
-            moduleBuilder.markActualNodeDirty();
-            moduleBuilder.setType(type);
-            addNodeToPath(type.getQName());
+            TypeAwareBuilder parent = (TypeAwareBuilder) moduleBuilder.getActualNode();
+            if (typeBody == null) {
+                parent.setTypeQName(typeQName);
+                moduleBuilder.markActualNodeDirty();
+            } else {
+                ParserListenerUtils.parseUnknownTypeWithBody(typeBody, parent, typeQName, moduleBuilder,
+                        moduleQName, stack.currentSchemaPath());
+            }
+            stack.addNodeToPath(QName.create(moduleQName.getModule(), typeQName.getLocalName()));
         }
-
     }
 
-    private QName parseQName(String typeName) {
-        QName typeQName;
-        if (typeName.contains(":")) {
-            String[] splittedName = typeName.split(":");
-            String prefix = splittedName[0];
-            String name = splittedName[1];
-            if (prefix.equals(yangModelPrefix)) {
-                typeQName = new QName(namespace, revision, prefix, name);
+    /**
+     * Method transforms string representation of yang element (i.e. leaf name, container name etc.) into QName.
+     * The namespace of QName is assigned from parent module same as revision date of module. If String qname parameter
+     * contains ":" the string is evaluated as prefix:name of element. In this case method will look into import map
+     * and extract correct ModuleImport. If such import is not present in import map the method will throw {@link YangParseException}
+     * <br>
+     * If ModuleImport is present but the value of namespace in ModuleImport is <code>null</code> the method will throw {@link YangParseException}
+     *
+     * @param qnameString QName value as String
+     * @param line line in Yang model document where QName occur.
+     * @return transformed string qname parameter as QName structure.
+     *
+     * @throws YangParseException
+     */
+    private QName parseQName(final String qnameString, final int line) {
+        final QName qname;
+        if (qnameString.indexOf(':') == -1) {
+            qname = QName.create(moduleQName.getNamespace(), moduleQName.getRevision(), qnameString);
+        } else {
+            final Iterator<String> split = COLON_SPLITTER.split(qnameString).iterator();
+            final String prefix = split.next();
+            final String name = split.next();
+            if (prefix.equals(moduleBuilder.getPrefix())) {
+                qname = QName.create(moduleQName.getNamespace(), moduleQName.getRevision(), name);
             } else {
-                typeQName = new QName(null, null, prefix, name);
+                ModuleImport imp = moduleBuilder.getImport(prefix);
+                if (imp == null) {
+                    LOG.debug("Error in module {} at line {}: No import found with prefix {}", moduleName, line, prefix);
+                    throw new YangParseException(moduleName, line, "Error in module " + moduleName
+                        + " No import found with prefix " + prefix + " not found.");
+                }
+                Date revision = imp.getRevision();
+                TreeMap<Date, URI> namespaces = namespaceContext.get(imp.getModuleName());
+                if (namespaces == null) {
+                    throw new YangParseException(moduleName, line, String.format("Imported module %s not found",
+                            imp.getModuleName()));
+                }
+                URI namespace;
+                if (revision == null) {
+                    revision = namespaces.lastEntry().getKey();
+                    namespace = namespaces.lastEntry().getValue();
+                } else {
+                    namespace = namespaces.get(revision);
+                }
+                qname = QName.create(namespace, revision, name);
             }
-        } else {
-            typeQName = new QName(namespace, revision, yangModelPrefix, typeName);
         }
-        return typeQName;
+        return qname;
     }
 
     @Override
-    public void exitType_stmt(YangParser.Type_stmtContext ctx) {
+    public void exitType_stmt(final YangParser.Type_stmtContext ctx) {
         final String typeName = stringFromNode(ctx);
         if ("union".equals(typeName)) {
             moduleBuilder.exitNode();
         }
-        exitLog("type", removeNodeFromPath());
+        exitLog("type", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterGrouping_stmt(YangParser.Grouping_stmtContext ctx) {
+    public void enterGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String groupName = stringFromNode(ctx);
         enterLog("grouping", groupName, line);
-        QName groupQName = new QName(namespace, revision, yangModelPrefix, groupName);
-        addNodeToPath(groupQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName groupQName = QName.create(moduleQName, groupName);
+        SchemaPath path = stack.addNodeToPath(groupQName);
 
         GroupingBuilder builder = moduleBuilder.addGrouping(ctx.getStart().getLine(), groupQName, path);
         parseSchemaNodeArgs(ctx, builder);
@@ -513,20 +614,19 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitGrouping_stmt(YangParser.Grouping_stmtContext ctx) {
+    public void exitGrouping_stmt(final YangParser.Grouping_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("grouping", removeNodeFromPath());
+        exitLog("grouping", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterContainer_stmt(Container_stmtContext ctx) {
+    public void enterContainer_stmt(final Container_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String containerName = stringFromNode(ctx);
         enterLog("container", containerName, line);
 
-        QName containerQName = new QName(namespace, revision, yangModelPrefix, containerName);
-        addNodeToPath(containerQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName containerQName = QName.create(moduleQName, containerName);
+        SchemaPath path = stack.addNodeToPath(containerQName);
 
         ContainerSchemaNodeBuilder builder = moduleBuilder.addContainerNode(line, containerQName, path);
         parseSchemaNodeArgs(ctx, builder);
@@ -545,20 +645,19 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitContainer_stmt(Container_stmtContext ctx) {
+    public void exitContainer_stmt(final Container_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("container", removeNodeFromPath());
+        exitLog("container", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterLeaf_stmt(Leaf_stmtContext ctx) {
+    public void enterLeaf_stmt(final Leaf_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String leafName = stringFromNode(ctx);
         enterLog("leaf", leafName, line);
 
-        QName leafQName = new QName(namespace, revision, yangModelPrefix, leafName);
-        addNodeToPath(leafQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName leafQName = QName.create(moduleQName, leafName);
+        SchemaPath path = stack.addNodeToPath(leafQName);
 
         LeafSchemaNodeBuilder builder = moduleBuilder.addLeafNode(line, leafQName, path);
         parseSchemaNodeArgs(ctx, builder);
@@ -582,36 +681,38 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitLeaf_stmt(YangParser.Leaf_stmtContext ctx) {
+    public void exitLeaf_stmt(final YangParser.Leaf_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("leaf", removeNodeFromPath());
+        exitLog("leaf", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterUses_stmt(YangParser.Uses_stmtContext ctx) {
+    public void enterUses_stmt(final YangParser.Uses_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String groupingPathStr = stringFromNode(ctx);
+        final SchemaPath groupingPath = parseXPathString(groupingPathStr, line);
         enterLog("uses", groupingPathStr, line);
 
-        UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPathStr);
+        UsesNodeBuilder builder = moduleBuilder.addUsesNode(line, groupingPath);
 
         moduleBuilder.enterNode(builder);
     }
 
     @Override
-    public void exitUses_stmt(YangParser.Uses_stmtContext ctx) {
+    public void exitUses_stmt(final YangParser.Uses_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("uses", "");
+        exitLog("uses");
     }
 
     @Override
-    public void enterUses_augment_stmt(YangParser.Uses_augment_stmtContext ctx) {
-        actualPath.push(new Stack<QName>());
+    public void enterUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String augmentPath = stringFromNode(ctx);
         enterLog(AUGMENT_STR, augmentPath, line);
+        stack.push();
 
-        AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, augmentOrder++);
+        SchemaPath targetPath = parseXPathString(augmentPath, line);
+        AugmentationSchemaBuilder builder = moduleBuilder.addAugment(line, augmentPath, targetPath, augmentOrder++);
 
         for (int i = 0; i < ctx.getChildCount(); i++) {
             ParseTree child = ctx.getChild(i);
@@ -630,36 +731,35 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitUses_augment_stmt(YangParser.Uses_augment_stmtContext ctx) {
+    public void exitUses_augment_stmt(final YangParser.Uses_augment_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog(AUGMENT_STR, "");
-        actualPath.pop();
+        exitLog(AUGMENT_STR);
+        stack.pop();
     }
 
     @Override
-    public void enterRefine_stmt(YangParser.Refine_stmtContext ctx) {
+    public void enterRefine_stmt(final YangParser.Refine_stmtContext ctx) {
         final String refineString = stringFromNode(ctx);
         enterLog("refine", refineString, ctx.getStart().getLine());
 
-        RefineHolder refine = parseRefine(ctx, moduleName);
+        RefineHolderImpl refine = parseRefine(ctx, moduleName);
         moduleBuilder.addRefine(refine);
         moduleBuilder.enterNode(refine);
     }
 
     @Override
-    public void exitRefine_stmt(YangParser.Refine_stmtContext ctx) {
+    public void exitRefine_stmt(final YangParser.Refine_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("refine", "");
+        exitLog("refine");
     }
 
     @Override
-    public void enterLeaf_list_stmt(Leaf_list_stmtContext ctx) {
+    public void enterLeaf_list_stmt(final Leaf_list_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String leafListName = stringFromNode(ctx);
         enterLog("leaf-list", leafListName, line);
-        QName leafListQName = new QName(namespace, revision, yangModelPrefix, leafListName);
-        addNodeToPath(leafListQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName leafListQName = QName.create(moduleQName, leafListName);
+        SchemaPath path = stack.addNodeToPath(leafListQName);
 
         LeafListSchemaNodeBuilder builder = moduleBuilder.addLeafListNode(line, leafListQName, path);
         moduleBuilder.enterNode(builder);
@@ -680,20 +780,19 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitLeaf_list_stmt(YangParser.Leaf_list_stmtContext ctx) {
+    public void exitLeaf_list_stmt(final YangParser.Leaf_list_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("leaf-list", removeNodeFromPath());
+        exitLog("leaf-list", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterList_stmt(List_stmtContext ctx) {
+    public void enterList_stmt(final List_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String listName = stringFromNode(ctx);
         enterLog("list", listName, line);
 
-        QName listQName = new QName(namespace, revision, yangModelPrefix, listName);
-        addNodeToPath(listQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName listQName = QName.create(moduleQName, listName);
+        SchemaPath path = stack.addNodeToPath(listQName);
 
         ListSchemaNodeBuilder builder = moduleBuilder.addListNode(line, listQName, path);
         moduleBuilder.enterNode(builder);
@@ -716,20 +815,19 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitList_stmt(List_stmtContext ctx) {
+    public void exitList_stmt(final List_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("list", removeNodeFromPath());
+        exitLog("list", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) {
+    public void enterAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String anyXmlName = stringFromNode(ctx);
         enterLog("anyxml", anyXmlName, line);
 
-        QName anyXmlQName = new QName(namespace, revision, yangModelPrefix, anyXmlName);
-        addNodeToPath(anyXmlQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName anyXmlQName = QName.create(moduleQName, anyXmlName);
+        SchemaPath path = stack.addNodeToPath(anyXmlQName);
 
         AnyXmlBuilder builder = moduleBuilder.addAnyXml(line, anyXmlQName, path);
         moduleBuilder.enterNode(builder);
@@ -740,20 +838,19 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitAnyxml_stmt(YangParser.Anyxml_stmtContext ctx) {
+    public void exitAnyxml_stmt(final YangParser.Anyxml_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("anyxml", removeNodeFromPath());
+        exitLog("anyxml", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterChoice_stmt(YangParser.Choice_stmtContext ctx) {
+    public void enterChoice_stmt(final YangParser.Choice_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String choiceName = stringFromNode(ctx);
         enterLog("choice", choiceName, line);
 
-        QName choiceQName = new QName(namespace, revision, yangModelPrefix, choiceName);
-        addNodeToPath(choiceQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName choiceQName = QName.create(moduleQName, choiceName);
+        SchemaPath path = stack.addNodeToPath(choiceQName);
 
         ChoiceBuilder builder = moduleBuilder.addChoice(line, choiceQName, path);
         moduleBuilder.enterNode(builder);
@@ -774,20 +871,19 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitChoice_stmt(YangParser.Choice_stmtContext ctx) {
+    public void exitChoice_stmt(final YangParser.Choice_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("choice", removeNodeFromPath());
+        exitLog("choice", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterCase_stmt(YangParser.Case_stmtContext ctx) {
+    public void enterCase_stmt(final YangParser.Case_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String caseName = stringFromNode(ctx);
         enterLog("case", caseName, line);
 
-        QName caseQName = new QName(namespace, revision, yangModelPrefix, caseName);
-        addNodeToPath(caseQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName caseQName = QName.create(moduleQName, caseName);
+        SchemaPath path = stack.addNodeToPath(caseQName);
 
         ChoiceCaseBuilder builder = moduleBuilder.addCase(line, caseQName, path);
         moduleBuilder.enterNode(builder);
@@ -797,20 +893,19 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitCase_stmt(YangParser.Case_stmtContext ctx) {
+    public void exitCase_stmt(final YangParser.Case_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("case", removeNodeFromPath());
+        exitLog("case", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterNotification_stmt(YangParser.Notification_stmtContext ctx) {
+    public void enterNotification_stmt(final YangParser.Notification_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String notificationName = stringFromNode(ctx);
         enterLog("notification", notificationName, line);
 
-        QName notificationQName = new QName(namespace, revision, yangModelPrefix, notificationName);
-        addNodeToPath(notificationQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName notificationQName = QName.create(moduleQName, notificationName);
+        SchemaPath path = stack.addNodeToPath(notificationQName);
 
         NotificationBuilder builder = moduleBuilder.addNotification(line, notificationQName, path);
         moduleBuilder.enterNode(builder);
@@ -819,59 +914,40 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitNotification_stmt(YangParser.Notification_stmtContext ctx) {
+    public void exitNotification_stmt(final YangParser.Notification_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("notification", removeNodeFromPath());
+        exitLog("notification", stack.removeNodeFromPath());
     }
 
     // Unknown nodes
     @Override
-    public void enterIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
+    public void enterIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
         handleUnknownNode(ctx.getStart().getLine(), ctx);
     }
 
     @Override
-    public void exitIdentifier_stmt(YangParser.Identifier_stmtContext ctx) {
-        moduleBuilder.exitNode();
-        exitLog("unknown-node", removeNodeFromPath());
-    }
-
-    @Override public void enterUnknown_statement(YangParser.Unknown_statementContext ctx) {
-        handleUnknownNode(ctx.getStart().getLine(), ctx);
-    }
-
-    @Override public void exitUnknown_statement(YangParser.Unknown_statementContext ctx) {
+    public void exitIdentifier_stmt(final YangParser.Identifier_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("unknown-node", removeNodeFromPath());
+        exitLog("unknown-node", stack.removeNodeFromPath());
     }
 
-    @Override public void enterUnknown_statement2(YangParser.Unknown_statement2Context ctx) {
+    @Override public void enterUnknown_statement(final YangParser.Unknown_statementContext ctx) {
         handleUnknownNode(ctx.getStart().getLine(), ctx);
     }
 
-    @Override public void exitUnknown_statement2(YangParser.Unknown_statement2Context ctx) {
+    @Override public void exitUnknown_statement(final YangParser.Unknown_statementContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("unknown-node", removeNodeFromPath());
-    }
-
-    @Override public void enterUnknown_statement3(YangParser.Unknown_statement3Context ctx) {
-        handleUnknownNode(ctx.getStart().getLine(), ctx);
-    }
-
-    @Override public void exitUnknown_statement3(YangParser.Unknown_statement3Context ctx) {
-        moduleBuilder.exitNode();
-        exitLog("unknown-node", removeNodeFromPath());
+        exitLog("unknown-node", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterRpc_stmt(YangParser.Rpc_stmtContext ctx) {
+    public void enterRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String rpcName = stringFromNode(ctx);
         enterLog("rpc", rpcName, line);
 
-        QName rpcQName = new QName(namespace, revision, yangModelPrefix, rpcName);
-        addNodeToPath(rpcQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName rpcQName = QName.create(moduleQName, rpcName);
+        SchemaPath path = stack.addNodeToPath(rpcQName);
 
         RpcDefinitionBuilder rpcBuilder = moduleBuilder.addRpc(line, rpcQName, path);
         moduleBuilder.enterNode(rpcBuilder);
@@ -881,20 +957,19 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitRpc_stmt(YangParser.Rpc_stmtContext ctx) {
+    public void exitRpc_stmt(final YangParser.Rpc_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("rpc", removeNodeFromPath());
+        exitLog("rpc", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterInput_stmt(YangParser.Input_stmtContext ctx) {
+    public void enterInput_stmt(final YangParser.Input_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String input = "input";
         enterLog(input, input, line);
 
-        QName rpcQName = new QName(namespace, revision, yangModelPrefix, input);
-        addNodeToPath(rpcQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName rpcQName = QName.create(moduleQName, input);
+        SchemaPath path = stack.addNodeToPath(rpcQName);
 
         ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcInput(line, rpcQName, path);
         moduleBuilder.enterNode(builder);
@@ -905,20 +980,19 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitInput_stmt(YangParser.Input_stmtContext ctx) {
+    public void exitInput_stmt(final YangParser.Input_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("input", removeNodeFromPath());
+        exitLog("input", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterOutput_stmt(YangParser.Output_stmtContext ctx) {
+    public void enterOutput_stmt(final YangParser.Output_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String output = "output";
         enterLog(output, output, line);
 
-        QName rpcQName = new QName(namespace, revision, yangModelPrefix, output);
-        addNodeToPath(rpcQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName rpcQName = QName.create(moduleQName, output);
+        SchemaPath path = stack.addNodeToPath(rpcQName);
 
         ContainerSchemaNodeBuilder builder = moduleBuilder.addRpcOutput(path, rpcQName, line);
         moduleBuilder.enterNode(builder);
@@ -929,20 +1003,19 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitOutput_stmt(YangParser.Output_stmtContext ctx) {
+    public void exitOutput_stmt(final YangParser.Output_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("output", removeNodeFromPath());
+        exitLog("output", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterFeature_stmt(YangParser.Feature_stmtContext ctx) {
+    public void enterFeature_stmt(final YangParser.Feature_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String featureName = stringFromNode(ctx);
         enterLog("feature", featureName, line);
 
-        QName featureQName = new QName(namespace, revision, yangModelPrefix, featureName);
-        addNodeToPath(featureQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        QName featureQName = QName.create(moduleQName, featureName);
+        SchemaPath path = stack.addNodeToPath(featureQName);
 
         FeatureBuilder featureBuilder = moduleBuilder.addFeature(line, featureQName, path);
         moduleBuilder.enterNode(featureBuilder);
@@ -951,19 +1024,25 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitFeature_stmt(YangParser.Feature_stmtContext ctx) {
+    public void exitFeature_stmt(final YangParser.Feature_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("feature", removeNodeFromPath());
+        exitLog("feature", stack.removeNodeFromPath());
     }
 
     @Override
-    public void enterDeviation_stmt(YangParser.Deviation_stmtContext ctx) {
+    public void enterDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
-        final String targetPath = stringFromNode(ctx);
-        enterLog("deviation", targetPath, line);
+        final String targetPathStr = stringFromNode(ctx);
+        if (!targetPathStr.startsWith("/")) {
+            throw new YangParseException(moduleName, line,
+                    "Deviation argument string must be an absolute schema node identifier.");
+        }
+        enterLog("deviation", targetPathStr, line);
 
         String reference = null;
         String deviate = null;
+
+        SchemaPath targetPath = parseXPathString(targetPathStr, line);
         DeviationBuilder builder = moduleBuilder.addDeviation(line, targetPath);
         moduleBuilder.enterNode(builder);
 
@@ -985,26 +1064,40 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
         builder.setDeviate(deviate);
     }
 
+    public SchemaPath parseXPathString(final String xpathString, final int line) {
+        final boolean absolute = !xpathString.isEmpty() && xpathString.charAt(0) == '/';
+
+        final List<QName> path = new ArrayList<>();
+        for (String pathElement : SLASH_SPLITTER.split(xpathString)) {
+            final Iterator<String> it = COLON_SPLITTER.split(pathElement).iterator();
+            final String s = it.next();
+            if (it.hasNext()) {
+                path.add(parseQName(pathElement, line));
+            } else {
+                path.add(QName.create(moduleQName, s));
+            }
+        }
+        return SchemaPath.create(path, absolute);
+    }
+
     @Override
-    public void exitDeviation_stmt(YangParser.Deviation_stmtContext ctx) {
+    public void exitDeviation_stmt(final YangParser.Deviation_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("deviation", "");
+        exitLog("deviation");
     }
 
     @Override
-    public void enterIdentity_stmt(YangParser.Identity_stmtContext ctx) {
+    public void enterIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
         final int line = ctx.getStart().getLine();
         final String identityName = stringFromNode(ctx);
         enterLog("identity", identityName, line);
 
-        final QName identityQName = new QName(namespace, revision, yangModelPrefix, identityName);
-        addNodeToPath(identityQName);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
+        final QName identityQName = QName.create(moduleQName, identityName);
+        SchemaPath path = stack.addNodeToPath(identityQName);
 
         IdentitySchemaNodeBuilder builder = moduleBuilder.addIdentity(identityQName, line, path);
         moduleBuilder.enterNode(builder);
 
-
         parseSchemaNodeArgs(ctx, builder);
 
         for (int i = 0; i < ctx.getChildCount(); i++) {
@@ -1017,68 +1110,65 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
     }
 
     @Override
-    public void exitIdentity_stmt(YangParser.Identity_stmtContext ctx) {
+    public void exitIdentity_stmt(final YangParser.Identity_stmtContext ctx) {
         moduleBuilder.exitNode();
-        exitLog("identity", removeNodeFromPath());
+        exitLog("identity", stack.removeNodeFromPath());
     }
 
     public ModuleBuilder getModuleBuilder() {
         return moduleBuilder;
     }
 
-    private void enterLog(String p1, String p2, int line) {
-        LOGGER.trace("entering {} {} ({})", p1, p2, line);
+    private static void enterLog(final String p1, final String p2, final int line) {
+        LOG.trace("entering {} {} ({})", p1, p2, line);
     }
 
-    private void exitLog(String p1, String p2) {
-        LOGGER.trace("exiting {} {}", p1, p2);
+    private static void exitLog(final String p1) {
+        LOG.trace("exiting {}", p1);
     }
 
-    private void exitLog(String p1, QName p2) {
-        LOGGER.trace("exiting {} {}", p1, p2.getLocalName());
+    private static void exitLog(final String p1, final QName p2) {
+        LOG.trace("exiting {} {}", p1, p2.getLocalName());
     }
 
-    private void setLog(String p1, String p2) {
-        LOGGER.trace("setting {} {}", p1, p2);
+    private static void setLog(final String p1, final String p2) {
+        LOG.trace("setting {} {}", p1, p2);
     }
 
     private void handleUnknownNode(final int line, final ParseTree ctx) {
         final String nodeParameter = stringFromNode(ctx);
         enterLog("unknown-node", nodeParameter, line);
 
-        QName nodeType;
         final String nodeTypeStr = ctx.getChild(0).getText();
-        final String[] splittedElement = nodeTypeStr.split(":");
-        if (splittedElement.length == 1) {
-            nodeType = new QName(namespace, revision, yangModelPrefix, splittedElement[0]);
-        } else {
-            nodeType = new QName(namespace, revision, splittedElement[0], splittedElement[1]);
-        }
+        QName nodeType = parseQName(nodeTypeStr, line);
 
         QName qname = null;
         try {
-            if (!Strings.isNullOrEmpty(nodeParameter)) {
-                String[] splittedName = nodeParameter.split(":");
-                if (splittedName.length == 2) {
-                    qname = new QName(null, null, splittedName[0], splittedName[1]);
+            //FIXME: rewrite whole method to handle unknown nodes properly.
+            // This should be bugfix for bug https://bugs.opendaylight.org/show_bug.cgi?id=1539
+            // After this fix bug https://bugs.opendaylight.org/show_bug.cgi?id=1538 MUST be fixed since
+            // they are dependent!!!
+            if (Strings.isNullOrEmpty(nodeParameter)) {
+                qname = nodeType;
+            } else {
+                final Iterable<String> splittedName = COLON_SPLITTER.split(nodeParameter);
+                final Iterator<String> it = splittedName.iterator();
+                if (Iterables.size(splittedName) == 2) {
+                    qname = parseQName(nodeParameter, line);
                 } else {
-                    qname = new QName(namespace, revision, yangModelPrefix, splittedName[0]);
+                    qname = QName.create(moduleQName, it.next());
                 }
-            } else {
-                qname = nodeType;
             }
-        } catch (IllegalArgumentException e) {
+        } catch (IllegalArgumentException | YangParseException ex) {
             qname = nodeType;
-
         }
-        addNodeToPath(qname);
-        SchemaPath path = createActualSchemaPath(actualPath.peek());
 
-        UnknownSchemaNodeBuilder builder = moduleBuilder.addUnknownSchemaNode(line, qname, path);
+        SchemaPath path = stack.addNodeToPath(qname);
+
+        UnknownSchemaNodeBuilderImpl builder = moduleBuilder.addUnknownSchemaNode(line, qname, path);
         builder.setNodeType(nodeType);
         builder.setNodeParameter(nodeParameter);
 
-
         parseSchemaNodeArgs(ctx, builder);
         moduleBuilder.enterNode(builder);
     }