Merge "BUG-997: Introduce ASTSchemaSource"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / YangParserImpl.java
index 160e9c907b7a18dce8130eea0f37d306efff66ac..bf163b9ec7a56569bdf0e1e0c68ae5971b7e45a4 100644 (file)
@@ -1,18 +1,28 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
- *
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.yangtools.yang.parser.impl;
 
-import static org.opendaylight.yangtools.yang.parser.util.ParserUtils.*;
-import static org.opendaylight.yangtools.yang.parser.util.TypeUtils.*;
-
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.fillAugmentTarget;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.findBaseIdentity;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.findModuleFromBuilders;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.findModuleFromContext;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.findSchemaNode;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.findSchemaNodeInModule;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.processAugmentation;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.TypeUtils.resolveType;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.TypeUtils.resolveTypeUnion;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
+import com.google.common.collect.HashBiMap;
+import com.google.common.io.ByteSource;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -20,218 +30,490 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Set;
 import java.util.TreeMap;
-
+import javax.annotation.concurrent.Immutable;
 import org.antlr.v4.runtime.ANTLRInputStream;
 import org.antlr.v4.runtime.CommonTokenStream;
 import org.antlr.v4.runtime.tree.ParseTree;
 import org.antlr.v4.runtime.tree.ParseTreeWalker;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangLexer;
 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser;
+import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.YangContext;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
-import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
-import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
+import org.opendaylight.yangtools.yang.model.api.ModuleImport;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
-import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
+import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
+import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationTargetBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
+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.SchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.UnknownSchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.UsesNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils;
+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.DeviationBuilder;
-import org.opendaylight.yangtools.yang.parser.builder.impl.ExtensionBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.impl.GroupingUtils;
 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentitySchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.IdentityrefTypeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleImpl;
 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.GroupingUtils;
+import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
-import org.opendaylight.yangtools.yang.parser.util.ParserUtils;
+import org.opendaylight.yangtools.yang.parser.util.NamedByteArrayInputStream;
+import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
+import org.opendaylight.yangtools.yang.parser.util.NamedInputStream;
 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
-import org.opendaylight.yangtools.yang.validator.YangModelBasicValidator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
-
-public final class YangParserImpl implements YangModelParser {
+@Immutable
+public final class YangParserImpl implements YangContextParser {
     private static final Logger LOG = LoggerFactory.getLogger(YangParserImpl.class);
-
     private static final String FAIL_DEVIATION_TARGET = "Failed to find deviation target.";
+    private static final Splitter COLON_SPLITTER = Splitter.on(':');
+    private static final YangParserImpl INSTANCE = new YangParserImpl();
+
+    public static YangParserImpl getInstance() {
+        return INSTANCE;
+    }
 
     @Override
-    public Set<Module> parseYangModels(final List<File> yangFiles) {
-        return Sets.newLinkedHashSet(parseYangModelsMapped(yangFiles).values());
+    @Deprecated
+    public Set<Module> parseYangModels(final File yangFile, final File directory) {
+        try {
+            return parseFile(yangFile, directory).getModules();
+        } catch (IOException | YangSyntaxErrorException e) {
+            throw new YangParseException("Failed to parse yang data", e);
+        }
     }
 
     @Override
-    public Set<Module> parseYangModels(final List<File> yangFiles, final SchemaContext context) {
-        if (yangFiles != null) {
-            final Map<InputStream, File> inputStreams = Maps.newHashMap();
-
-            for (final File yangFile : yangFiles) {
-                try {
-                    inputStreams.put(new FileInputStream(yangFile), yangFile);
-                } catch (FileNotFoundException e) {
-                    LOG.warn("Exception while reading yang file: " + yangFile.getName(), e);
-                }
-            }
+    public SchemaContext parseFile(final File yangFile, final File directory) throws IOException,
+    YangSyntaxErrorException {
+        Preconditions.checkState(yangFile.exists(), yangFile + " does not exists");
+        Preconditions.checkState(directory.exists(), directory + " does not exists");
+        Preconditions.checkState(directory.isDirectory(), directory + " is not a directory");
 
-            Map<ModuleBuilder, InputStream> builderToStreamMap = Maps.newHashMap();
+        final String yangFileName = yangFile.getName();
+        final String[] fileList = checkNotNull(directory.list(), directory + " not found or is not a directory");
 
-            final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuilders(
-                    Lists.newArrayList(inputStreams.keySet()), builderToStreamMap);
+        Map<ByteSource, File> sourceToFile = new LinkedHashMap<>();
+        ByteSource mainFileSource = BuilderUtils.fileToByteSource(yangFile);
+        sourceToFile.put(mainFileSource, yangFile);
 
-            for (InputStream is : inputStreams.keySet()) {
-                try {
-                    is.close();
-                } catch (IOException e) {
-                    LOG.debug("Failed to close stream.");
-                }
+        for (String fileName : fileList) {
+            if (fileName.equals(yangFileName)) {
+                continue;
+            }
+            File dependency = new File(directory, fileName);
+            if (dependency.isFile()) {
+                sourceToFile.put(BuilderUtils.fileToByteSource(dependency), dependency);
             }
+        }
+
+        Map<ByteSource, ModuleBuilder> sourceToBuilder = parseSourcesToBuilders(sourceToFile.keySet());
+        ModuleBuilder main = sourceToBuilder.get(mainFileSource);
+
+        List<ModuleBuilder> moduleBuilders = new ArrayList<>();
+        moduleBuilders.add(main);
+        filterImports(main, new ArrayList<>(sourceToBuilder.values()), moduleBuilders);
+        Collection<ModuleBuilder> resolved = resolveSubmodules(moduleBuilders);
+
+        // module builders sorted by dependencies
+        List<ModuleBuilder> sortedBuilders = ModuleDependencySort.sort(resolved);
+        LinkedHashMap<String, TreeMap<Date, ModuleBuilder>> modules = resolveModulesWithImports(sortedBuilders, null);
+        Collection<Module> unsorted = build(modules).values();
+        Set<Module> result = new LinkedHashSet<>(
+                ModuleDependencySort.sort(unsorted.toArray(new Module[unsorted.size()])));
+        return resolveSchemaContext(result);
+    }
+
+    @Override
+    @Deprecated
+    public Set<Module> parseYangModels(final List<File> yangFiles) {
+        return parseFiles(yangFiles).getModules();
+    }
+
+    @Override
+    public SchemaContext parseFiles(final Collection<File> yangFiles) {
+        Collection<Module> unsorted = parseYangModelsMapped(yangFiles).values();
+        Set<Module> sorted = new LinkedHashSet<>(
+                ModuleDependencySort.sort(unsorted.toArray(new Module[unsorted.size()])));
+        return resolveSchemaContext(sorted);
+    }
 
-            return new LinkedHashSet<Module>(buildWithContext(modules, context).values());
+    @Override
+    @Deprecated
+    public Set<Module> parseYangModels(final List<File> yangFiles, final SchemaContext context) {
+        try {
+            return parseFiles(yangFiles, context).getModules();
+        } catch (IOException | YangSyntaxErrorException e) {
+            throw new YangParseException("Failed to parse yang data", e);
+        }
+    }
+
+    @Override
+    public SchemaContext parseFiles(final Collection<File> yangFiles, final SchemaContext context) throws IOException,
+    YangSyntaxErrorException {
+        if (yangFiles == null) {
+            return resolveSchemaContext(Collections.<Module> emptySet());
         }
-        return Collections.emptySet();
+
+        Collection<ByteSource> sources = BuilderUtils.filesToByteSources(yangFiles);
+        return parseSources(sources, context);
     }
 
     @Override
+    @Deprecated
     public Set<Module> parseYangModelsFromStreams(final List<InputStream> yangModelStreams) {
-        return Sets.newHashSet(parseYangModelsFromStreamsMapped(yangModelStreams).values());
+        try {
+            Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(yangModelStreams);
+            return parseSources(sources).getModules();
+        } catch (IOException | YangSyntaxErrorException e) {
+            throw new YangParseException("Failed to parse yang data", e);
+        }
+    }
+
+    @Override
+    public SchemaContext parseSources(final Collection<ByteSource> sources) throws IOException,
+    YangSyntaxErrorException {
+        Collection<Module> unsorted = parseYangModelSources(sources).values();
+        Set<Module> sorted = new LinkedHashSet<>(
+                ModuleDependencySort.sort(unsorted.toArray(new Module[unsorted.size()])));
+        return resolveSchemaContext(sorted);
     }
 
     @Override
-    public Set<Module> parseYangModelsFromStreams(final List<InputStream> yangModelStreams, SchemaContext context) {
-        if (yangModelStreams != null) {
-            Map<ModuleBuilder, InputStream> builderToStreamMap = Maps.newHashMap();
-            final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuildersWithContext(
-                    yangModelStreams, builderToStreamMap, context);
-            return new LinkedHashSet<Module>(buildWithContext(modules, context).values());
-        }
-        return Collections.emptySet();
+    @Deprecated
+    public Set<Module> parseYangModelsFromStreams(final List<InputStream> yangModelStreams, final SchemaContext context) {
+        try {
+            Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(yangModelStreams);
+            return parseSources(sources, context).getModules();
+        } catch (IOException | YangSyntaxErrorException e) {
+            throw new YangParseException("Failed to parse yang data", e);
+        }
     }
 
     @Override
-    public Map<File, Module> parseYangModelsMapped(List<File> yangFiles) {
-        if (yangFiles != null) {
-            final Map<InputStream, File> inputStreams = Maps.newHashMap();
-
-            for (final File yangFile : yangFiles) {
-                try {
-                    inputStreams.put(new FileInputStream(yangFile), yangFile);
-                } catch (FileNotFoundException e) {
-                    LOG.warn("Exception while reading yang file: " + yangFile.getName(), e);
+    public SchemaContext parseSources(final Collection<ByteSource> sources, final SchemaContext context)
+            throws IOException, YangSyntaxErrorException {
+        if (sources == null) {
+            return resolveSchemaContext(Collections.<Module> emptySet());
+        }
+
+        final List<ModuleBuilder> sorted = resolveModuleBuilders(sources, context);
+        final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModulesWithImports(sorted, context);
+
+        final Set<Module> unsorted = new LinkedHashSet<>(build(modules).values());
+        if (context != null) {
+            for (Module m : context.getModules()) {
+                if (!unsorted.contains(m)) {
+                    unsorted.add(m);
                 }
             }
+        }
+        Set<Module> result = new LinkedHashSet<>(
+                ModuleDependencySort.sort(unsorted.toArray(new Module[unsorted.size()])));
+        return resolveSchemaContext(result);
+    }
 
-            Map<ModuleBuilder, InputStream> builderToStreamMap = Maps.newHashMap();
-            final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuilders(
-                    Lists.newArrayList(inputStreams.keySet()), builderToStreamMap);
-
-            for (InputStream is : inputStreams.keySet()) {
-                try {
-                    is.close();
-                } catch (IOException e) {
-                    LOG.debug("Failed to close stream.");
+    private LinkedHashMap<String, TreeMap<Date, ModuleBuilder>> resolveModulesWithImports(final List<ModuleBuilder> sorted,
+            final SchemaContext context) {
+        final LinkedHashMap<String, TreeMap<Date, ModuleBuilder>> modules = orderModules(sorted);
+        for (ModuleBuilder module : sorted) {
+            if (module != null) {
+                for (ModuleImport imp : module.getImports().values()) {
+                    String prefix = imp.getPrefix();
+                    ModuleBuilder targetModule = findModuleFromBuilders(modules, module, prefix, 0);
+                    if (targetModule == null) {
+                        Module result = findModuleFromContext(context, module, prefix, 0);
+                        targetModule = new ModuleBuilder(result);
+                        TreeMap<Date, ModuleBuilder> map = modules.get(prefix);
+                        if (map == null) {
+                            map = new TreeMap<>();
+                            map.put(targetModule.getRevision(), targetModule);
+                            modules.put(targetModule.getName(), map);
+                        } else {
+                            map.put(targetModule.getRevision(), targetModule);
+                        }
+                    }
+                    module.addImportedModule(prefix, targetModule);
                 }
             }
+        }
+        return modules;
+    }
 
-            Map<File, Module> retVal = Maps.newLinkedHashMap();
-            Map<ModuleBuilder, Module> builderToModuleMap = build(modules);
+    @Override
+    public Map<File, Module> parseYangModelsMapped(final Collection<File> yangFiles) {
+        if (yangFiles == null || yangFiles.isEmpty()) {
+            return Collections.emptyMap();
+        }
 
-            for (Entry<ModuleBuilder, Module> builderToModule : builderToModuleMap.entrySet()) {
-                retVal.put(inputStreams.get(builderToStreamMap.get(builderToModule.getKey())),
-                        builderToModule.getValue());
-            }
+        Map<ByteSource, File> byteSourceToFile = new HashMap<>();
+        for (final File file : yangFiles) {
+            ByteSource source = new ByteSource() {
+                @Override
+                public InputStream openStream() throws IOException {
+                    return new NamedFileInputStream(file, file.getPath());
+                }
+            };
+            byteSourceToFile.put(source, file);
+        }
 
-            return retVal;
+        Map<ByteSource, Module> byteSourceToModule;
+        try {
+            byteSourceToModule = parseYangModelSources(byteSourceToFile.keySet());
+        } catch (IOException | YangSyntaxErrorException e) {
+            throw new YangParseException("Failed to parse yang data", e);
         }
-        return Collections.emptyMap();
+        Map<File, Module> result = new LinkedHashMap<>();
+        for (Map.Entry<ByteSource, Module> entry : byteSourceToModule.entrySet()) {
+            result.put(byteSourceToFile.get(entry.getKey()), entry.getValue());
+        }
+        return result;
     }
 
     @Override
-    public Map<InputStream, Module> parseYangModelsFromStreamsMapped(final List<InputStream> yangModelStreams) {
-        Map<ModuleBuilder, InputStream> builderToStreamMap = Maps.newHashMap();
+    public Map<InputStream, Module> parseYangModelsFromStreamsMapped(final Collection<InputStream> yangModelStreams) {
+        if (yangModelStreams == null || yangModelStreams.isEmpty()) {
+            return Collections.emptyMap();
+        }
 
-        final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuilders(yangModelStreams,
-                builderToStreamMap);
-        Map<InputStream, Module> retVal = Maps.newLinkedHashMap();
-        Map<ModuleBuilder, Module> builderToModuleMap = build(modules);
+        Map<ByteSource, InputStream> sourceToStream = new HashMap<>();
+        for (final InputStream stream : yangModelStreams) {
+            ByteSource source = new ByteSource() {
+                @Override
+                public InputStream openStream() throws IOException {
+                    return NamedByteArrayInputStream.create(stream);
+                }
+            };
+            sourceToStream.put(source, stream);
+        }
 
-        for (Entry<ModuleBuilder, Module> builderToModule : builderToModuleMap.entrySet()) {
-            retVal.put(builderToStreamMap.get(builderToModule.getKey()), builderToModule.getValue());
+        Map<ByteSource, Module> sourceToModule;
+        try {
+            sourceToModule = parseYangModelSources(sourceToStream.keySet());
+        } catch (IOException | YangSyntaxErrorException e) {
+            throw new YangParseException("Failed to parse yang data", e);
         }
-        return retVal;
+        Map<InputStream, Module> result = new LinkedHashMap<>();
+        for (Map.Entry<ByteSource, Module> entry : sourceToModule.entrySet()) {
+            result.put(sourceToStream.get(entry.getKey()), entry.getValue());
+        }
+        return result;
     }
 
     @Override
     public SchemaContext resolveSchemaContext(final Set<Module> modules) {
-        return new SchemaContextImpl(modules);
+        // after merging parse method with this one, add support for getting
+        // submodule sources.
+        Map<ModuleIdentifier, String> identifiersToSources = new HashMap<>();
+        for (Module module : modules) {
+            ModuleImpl moduleImpl = (ModuleImpl) module;
+            identifiersToSources.put(module, moduleImpl.getSource());
+        }
+        return new SchemaContextImpl(modules, identifiersToSources);
+    }
+
+    private Map<ByteSource, Module> parseYangModelSources(final Collection<ByteSource> sources) throws IOException,
+    YangSyntaxErrorException {
+        if (sources == null || sources.isEmpty()) {
+            return Collections.emptyMap();
+        }
+
+        Map<ByteSource, ModuleBuilder> sourceToBuilder = resolveSources(sources);
+        // sort and check for duplicates
+        List<ModuleBuilder> sorted = ModuleDependencySort.sort(sourceToBuilder.values());
+        Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModulesWithImports(sorted, null);
+        Map<ModuleBuilder, Module> builderToModule = build(modules);
+        Map<ModuleBuilder, ByteSource> builderToSource = HashBiMap.create(sourceToBuilder).inverse();
+        sorted = ModuleDependencySort.sort(builderToModule.keySet());
+
+        Map<ByteSource, Module> result = new LinkedHashMap<>();
+        for (ModuleBuilder moduleBuilder : sorted) {
+            Module value = checkNotNull(builderToModule.get(moduleBuilder), "Cannot get module for %s", moduleBuilder);
+            result.put(builderToSource.get(moduleBuilder), value);
+        }
+
+        return result;
     }
 
-    private ModuleBuilder[] parseModuleBuilders(List<InputStream> inputStreams,
-            Map<ModuleBuilder, InputStream> streamToBuilderMap) {
+    /**
+     * Parse streams and resolve submodules.
+     *
+     * @param streams
+     *            collection of streams to parse
+     * @return map, where key is source stream and value is module builder
+     *         parsed from stream
+     * @throws YangSyntaxErrorException
+     */
+    // TODO: remove ByteSource result after removing YangModelParser
+    private Map<ByteSource, ModuleBuilder> resolveSources(final Collection<ByteSource> streams) throws IOException,
+    YangSyntaxErrorException {
+        Map<ByteSource, ModuleBuilder> builders = parseSourcesToBuilders(streams);
+        return resolveSubmodules(builders);
+    }
 
+    private Map<ByteSource, ModuleBuilder> parseSourcesToBuilders(final Collection<ByteSource> sources)
+            throws IOException, YangSyntaxErrorException {
         final ParseTreeWalker walker = new ParseTreeWalker();
-        final List<ParseTree> trees = parseStreams(inputStreams);
-        final ModuleBuilder[] builders = new ModuleBuilder[trees.size()];
+        final Map<ByteSource, ParseTree> sourceToTree = parseYangSources(sources);
+        final Map<ByteSource, ModuleBuilder> sourceToBuilder = new LinkedHashMap<>();
 
         // validate yang
-        new YangModelBasicValidator(walker).validate(trees);
-
-        YangParserListenerImpl yangModelParser = null;
-        for (int i = 0; i < trees.size(); i++) {
-            yangModelParser = new YangParserListenerImpl();
-            walker.walk(yangModelParser, trees.get(i));
+        new YangModelBasicValidator(walker).validate(sourceToTree.values());
+
+        YangParserListenerImpl yangModelParser;
+        for (Map.Entry<ByteSource, ParseTree> entry : sourceToTree.entrySet()) {
+            ByteSource source = entry.getKey();
+            String path = null; // TODO refactor to Optional
+            // TODO refactor so that path can be retrieved without opening
+            // stream: NamedInputStream -> NamedByteSource ?
+            try (InputStream stream = source.openStream()) {
+                if (stream instanceof NamedInputStream) {
+                    path = stream.toString();
+                }
+            }
+            yangModelParser = new YangParserListenerImpl(path);
+            walker.walk(yangModelParser, entry.getValue());
             ModuleBuilder moduleBuilder = yangModelParser.getModuleBuilder();
+            moduleBuilder.setSource(source);
+            sourceToBuilder.put(source, moduleBuilder);
+        }
+        return sourceToBuilder;
+    }
+
+    private Map<ByteSource, ModuleBuilder> resolveSubmodules(final Map<ByteSource, ModuleBuilder> builders) {
+        Map<ByteSource, ModuleBuilder> modules = new HashMap<>();
+        Set<ModuleBuilder> submodules = new HashSet<>();
+        for (Map.Entry<ByteSource, ModuleBuilder> entry : builders.entrySet()) {
+            ModuleBuilder moduleBuilder = entry.getValue();
+            if (moduleBuilder.isSubmodule()) {
+                submodules.add(moduleBuilder);
+            } else {
+                modules.put(entry.getKey(), moduleBuilder);
+            }
+        }
 
-            // We expect the order of trees and streams has to be the same
-            streamToBuilderMap.put(moduleBuilder, inputStreams.get(i));
-            builders[i] = moduleBuilder;
+        Collection<ModuleBuilder> values = modules.values();
+        for (ModuleBuilder submodule : submodules) {
+            for (ModuleBuilder module : values) {
+                if (module.getName().equals(submodule.getBelongsTo())) {
+                    addSubmoduleToModule(submodule, module);
+                }
+            }
         }
-        return builders;
+        return modules;
     }
 
-    private Map<String, TreeMap<Date, ModuleBuilder>> resolveModuleBuilders(final List<InputStream> yangFileStreams,
-            Map<ModuleBuilder, InputStream> streamToBuilderMap) {
-        return resolveModuleBuildersWithContext(yangFileStreams, streamToBuilderMap, null);
+    /**
+     * Traverse collection of builders, find builders representing submodule and
+     * add this submodule to its parent module.
+     *
+     * @param builders
+     *            collection of builders containing modules and submodules
+     * @return collection of module builders
+     */
+    private Collection<ModuleBuilder> resolveSubmodules(final Collection<ModuleBuilder> builders) {
+        Collection<ModuleBuilder> modules = new HashSet<>();
+        Set<ModuleBuilder> submodules = new HashSet<>();
+        for (ModuleBuilder moduleBuilder : builders) {
+            if (moduleBuilder.isSubmodule()) {
+                submodules.add(moduleBuilder);
+            } else {
+                modules.add(moduleBuilder);
+            }
+        }
+
+        for (ModuleBuilder submodule : submodules) {
+            for (ModuleBuilder module : modules) {
+                if (module.getName().equals(submodule.getBelongsTo())) {
+                    addSubmoduleToModule(submodule, module);
+                }
+            }
+        }
+        return modules;
     }
 
-    private Map<String, TreeMap<Date, ModuleBuilder>> resolveModuleBuildersWithContext(
-            final List<InputStream> yangFileStreams, final Map<ModuleBuilder, InputStream> streamToBuilderMap,
-            final SchemaContext context) {
-        final ModuleBuilder[] builders = parseModuleBuilders(yangFileStreams, streamToBuilderMap);
+    private void addSubmoduleToModule(final ModuleBuilder submodule, final ModuleBuilder module) {
+        submodule.setParent(module);
+        module.getDirtyNodes().addAll(submodule.getDirtyNodes());
+        module.getImports().putAll(submodule.getImports());
+        module.getAugments().addAll(submodule.getAugments());
+        module.getAugmentBuilders().addAll(submodule.getAugmentBuilders());
+        module.getAllAugments().addAll(submodule.getAllAugments());
+        module.getChildNodeBuilders().addAll(submodule.getChildNodeBuilders());
+        module.getChildNodes().putAll(submodule.getChildNodes());
+        module.getGroupings().addAll(submodule.getGroupings());
+        module.getGroupingBuilders().addAll(submodule.getGroupingBuilders());
+        module.getTypeDefinitions().addAll(submodule.getTypeDefinitions());
+        module.getTypeDefinitionBuilders().addAll(submodule.getTypeDefinitionBuilders());
+        module.getUsesNodes().addAll(submodule.getUsesNodes());
+        module.getUsesNodeBuilders().addAll(submodule.getUsesNodeBuilders());
+        module.getAllGroupings().addAll(submodule.getAllGroupings());
+        module.getAllUsesNodes().addAll(submodule.getAllUsesNodes());
+        module.getRpcs().addAll(submodule.getRpcs());
+        module.getAddedRpcs().addAll(submodule.getAddedRpcs());
+        module.getNotifications().addAll(submodule.getNotifications());
+        module.getAddedNotifications().addAll(submodule.getAddedNotifications());
+        module.getIdentities().addAll(submodule.getIdentities());
+        module.getAddedIdentities().addAll(submodule.getAddedIdentities());
+        module.getFeatures().addAll(submodule.getFeatures());
+        module.getAddedFeatures().addAll(submodule.getAddedFeatures());
+        module.getDeviations().addAll(submodule.getDeviations());
+        module.getDeviationBuilders().addAll(submodule.getDeviationBuilders());
+        module.getExtensions().addAll(submodule.getExtensions());
+        module.getAddedExtensions().addAll(submodule.getAddedExtensions());
+        module.getUnknownNodes().addAll(submodule.getUnknownNodes());
+        module.getAllUnknownNodes().addAll(submodule.getAllUnknownNodes());
+    }
 
-        // LinkedHashMap must be used to preserve order
-        final LinkedHashMap<String, TreeMap<Date, ModuleBuilder>> modules = new LinkedHashMap<String, TreeMap<Date, ModuleBuilder>>();
+    private List<ModuleBuilder> resolveModuleBuilders(final Collection<ByteSource> yangFileStreams,
+            final SchemaContext context) throws IOException, YangSyntaxErrorException {
+        Map<ByteSource, ModuleBuilder> parsedBuilders = resolveSources(yangFileStreams);
+        ModuleBuilder[] builders = new ModuleBuilder[parsedBuilders.size()];
+        parsedBuilders.values().toArray(builders);
 
         // module dependency graph sorted
-        List<ModuleBuilder> sorted = null;
+        List<ModuleBuilder> sorted;
         if (context == null) {
             sorted = ModuleDependencySort.sort(builders);
         } else {
             sorted = ModuleDependencySort.sortWithContext(context, builders);
         }
+        return sorted;
+    }
 
-        for (final ModuleBuilder builder : sorted) {
+    /**
+     * Order modules by name and revision.
+     *
+     * @param modules
+     *            topologically sorted modules
+     * @return modules ordered by name and revision
+     */
+    private LinkedHashMap<String, TreeMap<Date, ModuleBuilder>> orderModules(final List<ModuleBuilder> modules) {
+        final LinkedHashMap<String, TreeMap<Date, ModuleBuilder>> result = new LinkedHashMap<>();
+        for (final ModuleBuilder builder : modules) {
             if (builder == null) {
                 continue;
             }
@@ -240,34 +522,106 @@ public final class YangParserImpl implements YangModelParser {
             if (builderRevision == null) {
                 builderRevision = new Date(0L);
             }
-            TreeMap<Date, ModuleBuilder> builderByRevision = modules.get(builderName);
+            TreeMap<Date, ModuleBuilder> builderByRevision = result.get(builderName);
             if (builderByRevision == null) {
-                builderByRevision = new TreeMap<Date, ModuleBuilder>();
+                builderByRevision = new TreeMap<>();
+                builderByRevision.put(builderRevision, builder);
+                result.put(builderName, builderByRevision);
+            } else {
+                builderByRevision.put(builderRevision, builder);
             }
-            builderByRevision.put(builderRevision, builder);
-            modules.put(builderName, builderByRevision);
         }
-        return modules;
+        return result;
     }
 
-    private List<ParseTree> parseStreams(final List<InputStream> yangStreams) {
-        final List<ParseTree> trees = new ArrayList<ParseTree>();
-        for (InputStream yangStream : yangStreams) {
-            trees.add(parseStream(yangStream));
+    /**
+     * Find {@code main} dependencies from {@code other} and add them to
+     * {@code filtered}.
+     *
+     * @param main
+     *            main yang module
+     * @param other
+     *            all loaded modules
+     * @param filtered
+     *            collection to fill up
+     */
+    private void filterImports(final ModuleBuilder main, final Collection<ModuleBuilder> other,
+            final Collection<ModuleBuilder> filtered) {
+        Map<String, ModuleImport> imports = main.getImports();
+
+        // if this is submodule, add parent to filtered and pick its imports
+        if (main.isSubmodule()) {
+            TreeMap<Date, ModuleBuilder> dependencies = new TreeMap<>();
+            for (ModuleBuilder mb : other) {
+                if (mb.getName().equals(main.getBelongsTo())) {
+                    dependencies.put(mb.getRevision(), mb);
+                }
+            }
+            ModuleBuilder parent = dependencies.get(dependencies.firstKey());
+            filtered.add(parent);
+            imports.putAll(parent.getImports());
+        }
+
+        for (ModuleImport mi : imports.values()) {
+            for (ModuleBuilder builder : other) {
+                if (mi.getModuleName().equals(builder.getModuleName())) {
+                    if (mi.getRevision() == null) {
+                        if (!filtered.contains(builder)) {
+                            filtered.add(builder);
+                            filterImports(builder, other, filtered);
+                        }
+                    } else {
+                        if (mi.getRevision().equals(builder.getRevision())) {
+                            if (!filtered.contains(builder)) {
+                                filtered.add(builder);
+                                filterImports(builder, other, filtered);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    private Map<ByteSource, ParseTree> parseYangSources(final Collection<ByteSource> sources) throws IOException, YangSyntaxErrorException {
+        final Map<ByteSource, ParseTree> trees = new HashMap<>();
+        for (ByteSource source : sources) {
+            try (InputStream stream = source.openStream()) {
+                trees.put(source, parseYangSource(stream));
+            }
         }
         return trees;
     }
 
-    private ParseTree parseStream(final InputStream yangStream) {
-        ParseTree result = null;
+    public static YangContext parseYangSource(final InputStream stream) throws IOException, YangSyntaxErrorException {
+        final YangLexer lexer = new YangLexer(new ANTLRInputStream(stream));
+        final CommonTokenStream tokens = new CommonTokenStream(lexer);
+        final YangParser parser = new YangParser(tokens);
+        parser.removeErrorListeners();
+
+        final YangErrorListener errorListener = new YangErrorListener();
+        parser.addErrorListener(errorListener);
+
+        final YangContext result = parser.yang();
+        errorListener.validate();
+
+        return result;
+    }
+
+    /**
+     * Mini parser: This parsing context does not validate full YANG module,
+     * only parses header up to the revisions and imports.
+     *
+     * @see org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo
+     */
+    public static YangContext parseStreamWithoutErrorListeners(final InputStream yangStream) {
+        YangContext result = null;
         try {
             final ANTLRInputStream input = new ANTLRInputStream(yangStream);
             final YangLexer lexer = new YangLexer(input);
             final CommonTokenStream tokens = new CommonTokenStream(lexer);
             final YangParser parser = new YangParser(tokens);
             parser.removeErrorListeners();
-            parser.addErrorListener(new YangErrorListener());
-
             result = parser.yang();
         } catch (IOException e) {
             LOG.warn("Exception while reading yang file: " + yangStream, e);
@@ -275,74 +629,61 @@ public final class YangParserImpl implements YangModelParser {
         return result;
     }
 
+    /**
+     * Creates builder-to-module map based on given modules. Method first
+     * resolve unresolved type references, instantiate groupings through uses
+     * statements and perform augmentation.
+     *
+     * Node resolving must be performed in following order:
+     * <ol>
+     * <li>
+     * unresolved type references</li>
+     * <li>
+     * uses in groupings</li>
+     * <li>
+     * uses in other nodes</li>
+     * <li>
+     * augments</li>
+     * </ol>
+     *
+     * @param modules
+     *            all loaded modules
+     * @return modules mapped on their builders
+     */
     private Map<ModuleBuilder, Module> build(final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
-        // fix unresolved nodes
-        resolveAugmentsTargetPath(modules);
-        resolveUsesTargetGrouping(modules, null);
         resolveDirtyNodes(modules);
+        resolveAugmentsTargetPath(modules);
+        resolveUsesTargetGrouping(modules);
+        resolveUsesForGroupings(modules);
+        resolveUsesForNodes(modules);
         resolveAugments(modules);
-        resolveUses(modules, false);
-        resolvedUsesPostProcessing(modules, false);
+        resolveIdentities(modules);
         resolveDeviations(modules);
 
         // build
-        final Map<ModuleBuilder, Module> result = new LinkedHashMap<ModuleBuilder, Module>();
-        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
-            final Map<Date, Module> modulesByRevision = new HashMap<Date, Module>();
-            for (Map.Entry<Date, ModuleBuilder> childEntry : entry.getValue().entrySet()) {
-                final ModuleBuilder moduleBuilder = childEntry.getValue();
-                final Module module = moduleBuilder.build();
-                modulesByRevision.put(childEntry.getKey(), module);
-                result.put(moduleBuilder, module);
-            }
-        }
-        return result;
-    }
-
-    private Map<ModuleBuilder, Module> buildWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final SchemaContext context) {
-        // fix unresolved nodes
-        // TODO
-        // fixAugmentsTargetPath(modules);
-        resolveUsesTargetGrouping(modules, context);
-        resolvedDirtyNodesWithContext(modules, context);
-        resolveAugmentsWithContext(modules, context);
-        resolveUses(modules, true);
-        resolveDeviationsWithContext(modules, context);
-
-        // build
-        final Map<ModuleBuilder, Module> result = new LinkedHashMap<ModuleBuilder, Module>();
+        final Map<ModuleBuilder, Module> result = new LinkedHashMap<>();
         for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
-            final Map<Date, Module> modulesByRevision = new HashMap<Date, Module>();
             for (Map.Entry<Date, ModuleBuilder> childEntry : entry.getValue().entrySet()) {
                 final ModuleBuilder moduleBuilder = childEntry.getValue();
                 final Module module = moduleBuilder.build();
-                modulesByRevision.put(childEntry.getKey(), module);
                 result.put(moduleBuilder, module);
             }
         }
         return result;
     }
 
+    /**
+     * Resolve all unresolved type references.
+     *
+     * @param modules
+     *            all loaded modules
+     */
     private void resolveDirtyNodes(final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
         for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
             for (Map.Entry<Date, ModuleBuilder> childEntry : entry.getValue().entrySet()) {
                 final ModuleBuilder module = childEntry.getValue();
-                resolveDirtyNodes(modules, module);
-                resolveIdentities(modules, module);
                 resolveUnknownNodes(modules, module);
-            }
-        }
-    }
-
-    private void resolvedDirtyNodesWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final SchemaContext context) {
-        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
-            for (Map.Entry<Date, ModuleBuilder> childEntry : entry.getValue().entrySet()) {
-                final ModuleBuilder module = childEntry.getValue();
-                resolveDirtyNodesWithContext(modules, module, context);
-                resolveIdentitiesWithContext(modules, module, context);
-                resolveUnknownNodesWithContext(modules, module, context);
+                resolveDirtyNodes(modules, module);
             }
         }
     }
@@ -366,7 +707,13 @@ public final class YangParserImpl implements YangModelParser {
                 } else if (nodeToResolve.getTypedef() instanceof IdentityrefTypeBuilder) {
                     // special handling for identityref types
                     IdentityrefTypeBuilder idref = (IdentityrefTypeBuilder) nodeToResolve.getTypedef();
-                    nodeToResolve.setType(new IdentityrefType(findFullQName(modules, module, idref), idref.getPath()));
+                    IdentitySchemaNodeBuilder identity = findBaseIdentity(modules, module, idref.getBaseString(),
+                            idref.getLine());
+                    if (identity == null) {
+                        throw new YangParseException(module.getName(), idref.getLine(), "Failed to find base identity");
+                    }
+                    idref.setBaseIdentity(identity);
+                    nodeToResolve.setType(idref.build());
                 } else {
                     resolveType(nodeToResolve, modules, module);
                 }
@@ -374,27 +721,9 @@ public final class YangParserImpl implements YangModelParser {
         }
     }
 
-    private void resolveDirtyNodesWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final ModuleBuilder module, SchemaContext context) {
-        final Set<TypeAwareBuilder> dirtyNodes = module.getDirtyNodes();
-        if (!dirtyNodes.isEmpty()) {
-            for (TypeAwareBuilder nodeToResolve : dirtyNodes) {
-                if (nodeToResolve instanceof UnionTypeBuilder) {
-                    // special handling for union types
-                    resolveTypeUnionWithContext((UnionTypeBuilder) nodeToResolve, modules, module, context);
-                } else if (nodeToResolve.getTypedef() instanceof IdentityrefTypeBuilder) {
-                    // special handling for identityref types
-                    IdentityrefTypeBuilder idref = (IdentityrefTypeBuilder) nodeToResolve.getTypedef();
-                    nodeToResolve.setType(new IdentityrefType(findFullQName(modules, module, idref), idref.getPath()));
-                } else {
-                    resolveTypeWithContext(nodeToResolve, modules, module, context);
-                }
-            }
-        }
-    }
-
     /**
-     * Correct augment target path.
+     * Traverse through augmentations of modules and fix their child nodes
+     * schema path.
      *
      * @param modules
      *            all loaded modules
@@ -413,69 +742,93 @@ public final class YangParserImpl implements YangModelParser {
         }
     }
 
-    private void setCorrectAugmentTargetPath(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final AugmentationSchemaBuilder augmentBuilder) {
-        ModuleBuilder module = ParserUtils.getParentModule(augmentBuilder);
-        SchemaPath oldSchemaPath = augmentBuilder.getTargetPath();
-        List<QName> oldPath = oldSchemaPath.getPath();
-        List<QName> newPath = new ArrayList<>();
-        for (QName qn : oldPath) {
-            ModuleBuilder currentModule = null;
-            String prefix = qn.getPrefix();
-            if (prefix == null || "".equals(prefix)) {
-                currentModule = module;
-            } else {
-                currentModule = ParserUtils.findDependentModuleBuilder(modules, module, prefix,
-                        augmentBuilder.getLine());
-            }
-            QName newQName = new QName(currentModule.getNamespace(), currentModule.getRevision(), prefix,
-                    qn.getLocalName());
-            newPath.add(newQName);
-        }
-        augmentBuilder.setTargetPath(new SchemaPath(newPath, augmentBuilder.getTargetPath().isAbsolute()));
-    }
-
     /**
-     * Go through all augment definitions and perform augmentation. It is
-     * expected that modules are already sorted by their dependencies.
+     * Find augment target and set correct schema path for all its child nodes.
      *
      * @param modules
      *            all loaded modules
+     * @param augment
+     *            augment to resolve
      */
-    private void resolveAugments(final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
-        // collect augments from all loaded modules
-        final List<AugmentationSchemaBuilder> allAugments = new ArrayList<>();
-        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
-            for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue().entrySet()) {
-                allAugments.addAll(inner.getValue().getAllAugments());
+    private void setCorrectAugmentTargetPath(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+            final AugmentationSchemaBuilder augment) {
+        ModuleBuilder module = BuilderUtils.getParentModule(augment);
+        final SchemaPath newSchemaPath;
+
+        Builder parent = augment.getParent();
+        if (parent instanceof UsesNodeBuilder) {
+            DataNodeContainerBuilder usesParent = ((UsesNodeBuilder) parent).getParent();
+
+            QName baseQName = usesParent.getQName();
+            final QNameModule qnm;
+            String prefix;
+            if (baseQName == null) {
+                ModuleBuilder m = BuilderUtils.getParentModule(usesParent);
+                qnm = m.getQNameModule();
+                prefix = m.getPrefix();
+            } else {
+                qnm = baseQName.getModule();
+                prefix = baseQName.getPrefix();
             }
-        }
 
-        checkAugmentMandatoryNodes(allAugments);
+            SchemaPath s = usesParent.getPath();
+            for (QName qn : augment.getTargetPath().getPathFromRoot()) {
+                s = s.createChild(QName.create(qnm, prefix, qn.getLocalName()));
+            }
 
-        for (int i = 0; i < allAugments.size(); i++) {
-            // pick one augment
-            final AugmentationSchemaBuilder augment = allAugments.get(i);
-            // create collection of others
-            List<AugmentationSchemaBuilder> others = new ArrayList<>(allAugments);
-            others.remove(augment);
+            newSchemaPath = s;
+        } else {
+            final List<QName> newPath = new ArrayList<>();
+
+            for (QName qn : augment.getTargetPath().getPathFromRoot()) {
+                QNameModule qnm = module.getQNameModule();
+                String localPrefix = qn.getPrefix();
+                if (localPrefix != null && !localPrefix.isEmpty()) {
+                    ModuleBuilder currentModule = BuilderUtils.getModuleByPrefix(module, localPrefix);
+                    if (currentModule == null) {
+                        throw new YangParseException(module.getName(), augment.getLine(), "Module with prefix "
+                                + localPrefix + " not found.");
+                    }
+                    qnm = currentModule.getQNameModule();
+                }
+                newPath.add(QName.create(qnm, localPrefix, qn.getLocalName()));
+            }
 
-            // try to resolve it
-            boolean resolved = resolveAugment(modules, augment);
-            // while not resolved
-            int j = 0;
-            while (!(resolved) && j < others.size()) {
-                // try to resolve next augment
-                resolveAugment(modules, others.get(j));
-                // then try to resolve first again
-                resolved = resolveAugment(modules, augment);
-                j++;
+            /*
+             * FIXME: this method of SchemaPath construction is highly ineffective.
+             *        It would be great if we could actually dive into the context,
+             *        find the actual target node and reuse its SchemaPath. Can we
+             *        do that?
+             */
+            newSchemaPath = SchemaPath.create(newPath, true);
+        }
+        augment.setTargetNodeSchemaPath(newSchemaPath);
 
-            }
+        for (DataSchemaNodeBuilder childNode : augment.getChildNodeBuilders()) {
+            correctPathForAugmentNodes(childNode, augment.getTargetNodeSchemaPath());
+        }
+    }
 
-            if (!resolved) {
-                throw new YangParseException(augment.getModuleName(), augment.getLine(),
-                        "Error in augment parsing: failed to find augment target");
+    /**
+     * Set new schema path to node and all its child nodes based on given parent
+     * path. This method do not change the namespace.
+     *
+     * @param node
+     *            node which schema path should be updated
+     * @param parentPath
+     *            schema path of parent node
+     */
+    private void correctPathForAugmentNodes(final DataSchemaNodeBuilder node, final SchemaPath parentPath) {
+        SchemaPath newPath = parentPath.createChild(node.getQName());
+        node.setPath(newPath);
+        if (node instanceof DataNodeContainerBuilder) {
+            for (DataSchemaNodeBuilder child : ((DataNodeContainerBuilder) node).getChildNodeBuilders()) {
+                correctPathForAugmentNodes(child, node.getPath());
+            }
+        }
+        if (node instanceof ChoiceBuilder) {
+            for (ChoiceCaseBuilder child : ((ChoiceBuilder) node).getCases()) {
+                correctPathForAugmentNodes(child, node.getPath());
             }
         }
     }
@@ -488,10 +841,10 @@ public final class YangParserImpl implements YangModelParser {
      * @param augments
      *            augments to check
      */
-    private void checkAugmentMandatoryNodes(Collection<AugmentationSchemaBuilder> augments) {
+    private void checkAugmentMandatoryNodes(final Collection<AugmentationSchemaBuilder> augments) {
         for (AugmentationSchemaBuilder augment : augments) {
-            String augmentPrefix = augment.getTargetPath().getPath().get(0).getPrefix();
-            ModuleBuilder module = ParserUtils.getParentModule(augment);
+            String augmentPrefix = augment.getTargetPath().getPathFromRoot().iterator().next().getPrefix();
+            ModuleBuilder module = BuilderUtils.getParentModule(augment);
             String modulePrefix = module.getPrefix();
 
             if (augmentPrefix == null || augmentPrefix.isEmpty() || augmentPrefix.equals(modulePrefix)) {
@@ -501,227 +854,168 @@ public final class YangParserImpl implements YangModelParser {
             for (DataSchemaNodeBuilder childNode : augment.getChildNodeBuilders()) {
                 if (childNode.getConstraints().isMandatory()) {
                     throw new YangParseException(augment.getModuleName(), augment.getLine(),
-                            "Error in augment parsing: cannot augment mandatory node");
+                            "Error in augment parsing: cannot augment mandatory node "
+                                    + childNode.getQName().getLocalName());
                 }
             }
         }
     }
 
     /**
-     * Search for augment target and perform augmentation.
+     * Go through all augment definitions and resolve them.
      *
      * @param modules
-     *            all loaded modules
-     * @param augment
-     *            augment to resolve
-     * @return true if target node found, false otherwise
+     *            all loaded modules topologically sorted (based on dependencies
+     *            between each other)
      */
-    private boolean resolveAugment(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final AugmentationSchemaBuilder augment) {
-        if (augment.isResolved()) {
-            return true;
+    private void resolveAugments(final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
+        List<ModuleBuilder> all = new ArrayList<>();
+        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
+            for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue().entrySet()) {
+                all.add(inner.getValue());
+            }
         }
 
-        int line = augment.getLine();
-        ModuleBuilder module = getParentModule(augment);
-        List<QName> path = augment.getTargetPath().getPath();
-        Builder augmentParent = augment.getParent();
-
-        Builder firstNodeParent = null;
-        if (augmentParent instanceof ModuleBuilder) {
-            // if augment is defined under module, parent of first node is
-            // target module
-            final QName firstNameInPath = path.get(0);
-            String prefix = firstNameInPath.getPrefix();
-            if (prefix == null) {
-                prefix = module.getPrefix();
+        for (ModuleBuilder mb : all) {
+            if (mb != null) {
+                List<AugmentationSchemaBuilder> augments = mb.getAllAugments();
+                checkAugmentMandatoryNodes(augments);
+                Collections.sort(augments, Comparators.AUGMENT_BUILDER_COMP);
+                for (AugmentationSchemaBuilder augment : augments) {
+                    if (!(augment.isResolved())) {
+                        boolean resolved = resolveAugment(augment, mb, modules);
+                        if (!resolved) {
+                            throw new YangParseException(augment.getModuleName(), augment.getLine(),
+                                    "Error in augment parsing: failed to find augment target: " + augment);
+                        }
+                    }
+                }
             }
-            firstNodeParent = findDependentModuleBuilder(modules, module, prefix, line);
-        } else if (augmentParent instanceof UsesNodeBuilder) {
-            firstNodeParent = augmentParent.getParent();
-        } else {
-            // augment can be defined only under module or uses
-            throw new YangParseException(augment.getModuleName(), line,
-                    "Failed to parse augment: Unresolved parent of augment: " + augmentParent);
         }
-
-        return processAugmentation(augment, firstNodeParent, path);
     }
 
     /**
-     * Go through all augment definitions and resolve them. This method works in
-     * same way as {@link #resolveAugments(Map)} except that if target node is
-     * not found in loaded modules, it search for target node in given context.
+     * Perform augmentation defined under uses statement.
      *
+     * @param augment
+     *            augment to resolve
+     * @param module
+     *            current module
      * @param modules
      *            all loaded modules
-     * @param context
-     *            SchemaContext containing already resolved modules
+     * @return true if augment process succeed
      */
-    private void resolveAugmentsWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final SchemaContext context) {
-        // collect augments from all loaded modules
-        final List<AugmentationSchemaBuilder> allAugments = new ArrayList<>();
-        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
-            for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue().entrySet()) {
-                allAugments.addAll(inner.getValue().getAllAugments());
-            }
+    private boolean resolveUsesAugment(final AugmentationSchemaBuilder augment, final ModuleBuilder module,
+            final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
+        if (augment.isResolved()) {
+            return true;
         }
 
-        for (int i = 0; i < allAugments.size(); i++) {
-            // pick augment from list
-            final AugmentationSchemaBuilder augment = allAugments.get(i);
-            // try to resolve it
-            boolean resolved = resolveAugmentWithContext(modules, augment, context);
-            // while not resolved
-            int j = i + 1;
-            while (!(resolved) && j < allAugments.size()) {
-                // try to resolve next augment
-                resolveAugmentWithContext(modules, allAugments.get(j), context);
-                // then try to resolve first again
-                resolved = resolveAugmentWithContext(modules, augment, context);
-                j++;
-            }
+        UsesNodeBuilder usesNode = (UsesNodeBuilder) augment.getParent();
+        DataNodeContainerBuilder parentNode = usesNode.getParent();
+        Optional<SchemaNodeBuilder> potentialTargetNode;
+        SchemaPath resolvedTargetPath = augment.getTargetNodeSchemaPath();
+        if (parentNode instanceof ModuleBuilder && resolvedTargetPath.isAbsolute()) {
+            // Uses is directly used in module body, we lookup
+            // We lookup in data namespace to find correct augmentation target
+            potentialTargetNode = findSchemaNodeInModule(resolvedTargetPath, (ModuleBuilder) parentNode);
+        } else {
+            // Uses is used in local context (be it data namespace or grouping
+            // namespace,
+            // since all nodes via uses are imported to localName, it is safe to
+            // to proceed only with local names.
+            //
+            // Conflicting elements in other namespaces are still not present
+            // since resolveUsesAugment occurs before augmenting from external
+            // modules.
+            potentialTargetNode = Optional.<SchemaNodeBuilder> fromNullable(findSchemaNode(augment.getTargetPath()
+                    .getPath(), (SchemaNodeBuilder) parentNode));
+        }
 
-            if (!resolved) {
-                throw new YangParseException(augment.getModuleName(), augment.getLine(),
-                        "Error in augment parsing: failed to find augment target");
+        if (potentialTargetNode.isPresent()) {
+            SchemaNodeBuilder targetNode = potentialTargetNode.get();
+            if (targetNode instanceof AugmentationTargetBuilder) {
+                fillAugmentTarget(augment, targetNode);
+                ((AugmentationTargetBuilder) targetNode).addAugmentation(augment);
+                augment.setResolved(true);
+                return true;
+            } else {
+                throw new YangParseException(module.getName(), augment.getLine(), String.format(
+                        "Failed to resolve augment in uses. Invalid augment target: %s", potentialTargetNode));
             }
+        } else {
+            throw new YangParseException(module.getName(), augment.getLine(), String.format(
+                    "Failed to resolve augment in uses. Invalid augment target path: %s", augment.getTargetPath()));
         }
+
     }
 
     /**
-     * Search for augment target and perform augmentation.
+     * Find augment target module and perform augmentation.
      *
-     * @param modules
-     *            all loaded modules
      * @param augment
      *            augment to resolve
-     * @param context
-     *            SchemaContext containing already resolved modules
-     * @return true if target node found, false otherwise
+     * @param module
+     *            current module
+     * @param modules
+     *            all loaded modules
+     * @return true if augment process succeed
      */
-    private boolean resolveAugmentWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final AugmentationSchemaBuilder augment, final SchemaContext context) {
+    private boolean resolveAugment(final AugmentationSchemaBuilder augment, final ModuleBuilder module,
+            final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
         if (augment.isResolved()) {
             return true;
         }
-        int line = augment.getLine();
-        ModuleBuilder module = getParentModule(augment);
-        List<QName> path = augment.getTargetPath().getPath();
-        final QName firstNameInPath = path.get(0);
-        String prefix = firstNameInPath.getPrefix();
-        if (prefix == null) {
-            prefix = module.getPrefix();
-        }
-        Builder augmentParent = augment.getParent();
-        Builder currentParent = null;
-
-        if (augmentParent instanceof ModuleBuilder) {
-            // if augment is defined under module, first parent is target module
-            currentParent = findDependentModuleBuilder(modules, module, prefix, line);
-        } else if (augmentParent instanceof UsesNodeBuilder) {
-            currentParent = augmentParent.getParent();
-        } else {
-            // augment can be defined only under module or uses
-            throw new YangParseException(augment.getModuleName(), augment.getLine(),
-                    "Error in augment parsing: Unresolved parent of augment: " + augmentParent);
-        }
 
-        if (currentParent == null) {
-            return processAugmentationOnContext(augment, path, module, prefix, context);
-        } else {
-            return processAugmentation(augment, currentParent, path);
+        QName targetModuleName = augment.getTargetPath().getPathFromRoot().iterator().next();
+        ModuleBuilder targetModule = BuilderUtils.getModuleByPrefix(module, targetModuleName.getPrefix());
+        if (targetModule == null) {
+            throw new YangParseException(module.getModuleName(), augment.getLine(), "Failed to resolve augment "
+                    + augment);
         }
+
+        return processAugmentation(augment, targetModule);
     }
 
     /**
      * Go through identity statements defined in current module and resolve
-     * their 'base' statement if present.
+     * their 'base' statement.
      *
      * @param modules
-     *            all modules
-     * @param module
-     *            module being resolved
+     *            all loaded modules
      */
-    private void resolveIdentities(final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module) {
-        final Set<IdentitySchemaNodeBuilder> identities = module.getIdentities();
-        for (IdentitySchemaNodeBuilder identity : identities) {
-            final String baseIdentityName = identity.getBaseIdentityName();
-            if (baseIdentityName != null) {
-                String baseIdentityPrefix = null;
-                String baseIdentityLocalName = null;
-                if (baseIdentityName.contains(":")) {
-                    final String[] splitted = baseIdentityName.split(":");
-                    baseIdentityPrefix = splitted[0];
-                    baseIdentityLocalName = splitted[1];
-                } else {
-                    baseIdentityPrefix = module.getPrefix();
-                    baseIdentityLocalName = baseIdentityName;
-                }
-                final ModuleBuilder dependentModule = findDependentModuleBuilder(modules, module, baseIdentityPrefix,
-                        identity.getLine());
-
-                final Set<IdentitySchemaNodeBuilder> dependentModuleIdentities = dependentModule.getIdentities();
-                for (IdentitySchemaNodeBuilder idBuilder : dependentModuleIdentities) {
-                    if (idBuilder.getQName().getLocalName().equals(baseIdentityLocalName)) {
-                        identity.setBaseIdentity(idBuilder);
-                    }
+    private void resolveIdentities(final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
+        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
+            for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue().entrySet()) {
+                ModuleBuilder module = inner.getValue();
+                final Set<IdentitySchemaNodeBuilder> identities = module.getAddedIdentities();
+                for (IdentitySchemaNodeBuilder identity : identities) {
+                    resolveIdentity(modules, module, identity);
                 }
             }
         }
     }
 
-    /**
-     * Go through identity statements defined in current module and resolve
-     * their 'base' statement. Method tries to find base identity in given
-     * modules. If base identity is not found, method will search it in context.
-     *
-     * @param modules
-     *            all loaded modules
-     * @param module
-     *            current module
-     * @param context
-     *            SchemaContext containing already resolved modules
-     */
-    private void resolveIdentitiesWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final ModuleBuilder module, final SchemaContext context) {
-        final Set<IdentitySchemaNodeBuilder> identities = module.getIdentities();
-        for (IdentitySchemaNodeBuilder identity : identities) {
-            final String baseIdentityName = identity.getBaseIdentityName();
-            if (baseIdentityName != null) {
-                String baseIdentityPrefix = null;
-                String baseIdentityLocalName = null;
-                if (baseIdentityName.contains(":")) {
-                    final String[] splitted = baseIdentityName.split(":");
-                    baseIdentityPrefix = splitted[0];
-                    baseIdentityLocalName = splitted[1];
-                } else {
-                    baseIdentityPrefix = module.getPrefix();
-                    baseIdentityLocalName = baseIdentityName;
-                }
-                final ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, module,
-                        baseIdentityPrefix, identity.getLine());
-
-                if (dependentModuleBuilder == null) {
-                    final Module dependentModule = findModuleFromContext(context, module, baseIdentityPrefix,
-                            identity.getLine());
-                    final Set<IdentitySchemaNode> dependentModuleIdentities = dependentModule.getIdentities();
-                    for (IdentitySchemaNode idNode : dependentModuleIdentities) {
-                        if (idNode.getQName().getLocalName().equals(baseIdentityLocalName)) {
-                            identity.setBaseIdentity(idNode);
-                        }
-                    }
-                } else {
-                    final Set<IdentitySchemaNodeBuilder> dependentModuleIdentities = dependentModuleBuilder
-                            .getIdentities();
-                    for (IdentitySchemaNodeBuilder idBuilder : dependentModuleIdentities) {
-                        if (idBuilder.getQName().getLocalName().equals(baseIdentityLocalName)) {
-                            identity.setBaseIdentity(idBuilder);
-                        }
-                    }
+    private void resolveIdentity(final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module,
+            final IdentitySchemaNodeBuilder identity) {
+        final String baseIdentityName = identity.getBaseIdentityName();
+        if (baseIdentityName != null) {
+            IdentitySchemaNodeBuilder result = null;
+            if (baseIdentityName.contains(":")) {
+                final int line = identity.getLine();
+                String[] splittedBase = baseIdentityName.split(":");
+                if (splittedBase.length > 2) {
+                    throw new YangParseException(module.getName(), line, "Failed to parse identityref base: "
+                            + baseIdentityName);
                 }
+                String prefix = splittedBase[0];
+                String name = splittedBase[1];
+                ModuleBuilder dependentModule = BuilderUtils.getModuleByPrefix(module, prefix);
+                result = BuilderUtils.findIdentity(dependentModule.getAddedIdentities(), name);
+            } else {
+                result = BuilderUtils.findIdentity(module.getAddedIdentities(), baseIdentityName);
             }
+            identity.setBaseIdentity(result);
         }
     }
 
@@ -730,12 +1024,8 @@ public final class YangParserImpl implements YangModelParser {
      *
      * @param modules
      *            all loaded modules
-     * @param context
-     *            SchemaContext containing already resolved modules or null if
-     *            context is not available
      */
-    private void resolveUsesTargetGrouping(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final SchemaContext context) {
+    private void resolveUsesTargetGrouping(final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
         final List<UsesNodeBuilder> allUses = new ArrayList<>();
         for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
             for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue().entrySet()) {
@@ -743,149 +1033,175 @@ public final class YangParserImpl implements YangModelParser {
             }
         }
         for (UsesNodeBuilder usesNode : allUses) {
-            ModuleBuilder module = ParserUtils.getParentModule(usesNode);
+            ModuleBuilder module = BuilderUtils.getParentModule(usesNode);
             final GroupingBuilder targetGroupingBuilder = GroupingUtils.getTargetGroupingFromModules(usesNode, modules,
                     module);
             if (targetGroupingBuilder == null) {
-                if (context == null) {
-                    throw new YangParseException(module.getName(), usesNode.getLine(), "Referenced grouping '"
-                            + usesNode.getGroupingPathAsString() + "' not found.");
-                } else {
-                    GroupingDefinition targetGroupingDefinition = GroupingUtils.getTargetGroupingFromContext(usesNode,
-                            module, context);
-                    usesNode.setGroupingDefinition(targetGroupingDefinition);
-                }
-            } else {
-                usesNode.setGrouping(targetGroupingBuilder);
+                throw new YangParseException(module.getName(), usesNode.getLine(), "Referenced grouping '"
+                        + usesNode.getGroupingPathAsString() + "' not found.");
             }
+            usesNode.setGrouping(targetGroupingBuilder);
         }
     }
 
     /**
-     * Copy data from uses target. Augmentations have to be resolved already.
+     * Resolve uses statements defined in groupings.
      *
      * @param modules
      *            all loaded modules
-     * @param resolveWithContext
-     *            boolean value which says whether
-     *            {@link GroupingUtils#collectUsesDataFromContext(UsesNodeBuilder)
-     *            collectUsesDataFromContext} should be used for processing of
-     *            individual uses node.
      */
-    private void resolveUses(final Map<String, TreeMap<Date, ModuleBuilder>> modules, final boolean resolveWithContext) {
+    private void resolveUsesForGroupings(final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
+        final Set<GroupingBuilder> allGroupings = new HashSet<>();
         for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
             for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue().entrySet()) {
                 ModuleBuilder module = inner.getValue();
-                List<UsesNodeBuilder> usesNodes = null;
-                boolean dataCollected = module.isAllUsesDataCollected();
-
-                while (!dataCollected) {
-                    usesNodes = new ArrayList<>(module.getAllUsesNodes());
-                    for (UsesNodeBuilder usesNode : usesNodes) {
-                        if (!usesNode.isDataCollected()) {
-                            if (resolveWithContext && usesNode.getGroupingBuilder() == null) {
-                                GroupingUtils.collectUsesDataFromContext(usesNode);
-                            } else {
-                                GroupingUtils.collectUsesData(usesNode);
-                            }
-                        }
-                    }
-                    dataCollected = module.isAllUsesDataCollected();
-                }
+                allGroupings.addAll(module.getAllGroupings());
+            }
+        }
+        final List<GroupingBuilder> sorted = GroupingSort.sort(allGroupings);
+        for (GroupingBuilder gb : sorted) {
+            List<UsesNodeBuilder> usesNodes = new ArrayList<>(GroupingSort.getAllUsesNodes(gb));
+            Collections.sort(usesNodes, new GroupingUtils.UsesComparator());
+            for (UsesNodeBuilder usesNode : usesNodes) {
+                resolveUses(usesNode, modules);
             }
         }
     }
 
     /**
-     * Update uses parent and perform refinement.
+     * Resolve uses statements.
      *
      * @param modules
      *            all loaded modules
-     * @param resolveWithContext
-     *            boolean value which says whether
-     *            {@link GroupingUtils#collectUsesDataFromContext(UsesNodeBuilder)
-     *            collectUsesDataFromContext} should be used for processing of
-     *            individual uses node.
      */
-    private void resolvedUsesPostProcessing(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final boolean resolveWithContext) {
-        // new loop is must because in collecting data process new uses could
-        // be created
-        final List<UsesNodeBuilder> allModulesUses = new ArrayList<>();
+    private void resolveUsesForNodes(final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
         for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
             for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue().entrySet()) {
-                allModulesUses.addAll(inner.getValue().getAllUsesNodes());
+                ModuleBuilder module = inner.getValue();
+                List<UsesNodeBuilder> usesNodes = module.getAllUsesNodes();
+                Collections.sort(usesNodes, new GroupingUtils.UsesComparator());
+                for (UsesNodeBuilder usesNode : usesNodes) {
+                    resolveUses(usesNode, modules);
+                }
             }
         }
+    }
+
+    /**
+     * Find target grouping and copy its child nodes to current location with
+     * new namespace.
+     *
+     * @param usesNode
+     *            uses node to resolve
+     * @param modules
+     *            all loaded modules
+     */
+    private void resolveUses(final UsesNodeBuilder usesNode, final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
+        if (!usesNode.isResolved()) {
+            DataNodeContainerBuilder parent = usesNode.getParent();
+            ModuleBuilder module = BuilderUtils.getParentModule(parent);
+            GroupingBuilder target = GroupingUtils.getTargetGroupingFromModules(usesNode, modules, module);
+
+            int index = nodeAfterUsesIndex(usesNode);
+            List<DataSchemaNodeBuilder> targetNodes = target.instantiateChildNodes(parent);
+            for (DataSchemaNodeBuilder targetNode : targetNodes) {
+                parent.addChildNode(index++, targetNode);
+            }
+            parent.getTypeDefinitionBuilders().addAll(target.instantiateTypedefs(parent));
+            parent.getGroupingBuilders().addAll(target.instantiateGroupings(parent));
+            parent.getUnknownNodes().addAll(target.instantiateUnknownNodes(parent));
+            usesNode.setResolved(true);
+            for (AugmentationSchemaBuilder augment : usesNode.getAugmentations()) {
+                resolveUsesAugment(augment, module, modules);
+            }
 
-        for (UsesNodeBuilder usesNode : allModulesUses) {
-            GroupingUtils.updateUsesParent(usesNode);
             GroupingUtils.performRefine(usesNode);
         }
+    }
+
+    private int nodeAfterUsesIndex(final UsesNodeBuilder usesNode) {
+        DataNodeContainerBuilder parent = usesNode.getParent();
+        int usesLine = usesNode.getLine();
+
+        List<DataSchemaNodeBuilder> childNodes = parent.getChildNodeBuilders();
+        if (childNodes.isEmpty()) {
+            return 0;
+        }
 
-        if (!resolveWithContext) {
-            for (UsesNodeBuilder usesNode : allModulesUses) {
-                if (usesNode.isCopy()) {
-                    usesNode.getParent().getUsesNodes().remove(usesNode);
+        DataSchemaNodeBuilder nextNodeAfterUses = null;
+        for (DataSchemaNodeBuilder childNode : childNodes) {
+            if (!(childNode.isAddedByUses()) && !(childNode.isAugmenting())) {
+                if (childNode.getLine() > usesLine) {
+                    nextNodeAfterUses = childNode;
+                    break;
                 }
             }
         }
+
+        // uses is declared after child nodes
+        if (nextNodeAfterUses == null) {
+            return childNodes.size();
+        }
+
+        return parent.getChildNodeBuilders().indexOf(nextNodeAfterUses);
     }
 
+    /**
+     * Try to find extension describing this unknown node and assign it to
+     * unknown node builder.
+     *
+     * @param modules
+     *            all loaded modules
+     * @param module
+     *            current module
+     */
     private void resolveUnknownNodes(final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module) {
         for (UnknownSchemaNodeBuilder usnb : module.getAllUnknownNodes()) {
             QName nodeType = usnb.getNodeType();
-            try {
-                ModuleBuilder dependentModule = findDependentModuleBuilder(modules, module, nodeType.getPrefix(),
-                        usnb.getLine());
-                for (ExtensionBuilder extension : dependentModule.getExtensions()) {
-                    if (extension.getQName().getLocalName().equals(nodeType.getLocalName())) {
-                        usnb.setNodeType(extension.getQName());
-                        usnb.setExtensionBuilder(extension);
-                        break;
-                    }
+            ModuleBuilder dependentModuleBuilder = BuilderUtils.getModuleByPrefix(module, nodeType.getPrefix());
+            if (dependentModuleBuilder == null) {
+                LOG.warn(
+                        "Error in module {} at line {}: Failed to resolve node {}: no such extension definition found.",
+                        module.getName(), usnb.getLine(), usnb);
+                continue;
+            }
+            ExtensionBuilder extBuilder = findExtBuilder(nodeType.getLocalName(),
+                    dependentModuleBuilder.getAddedExtensions());
+            if (extBuilder == null) {
+                ExtensionDefinition extDef = findExtDef(nodeType.getLocalName(), dependentModuleBuilder.getExtensions());
+                if (extDef == null) {
+                    LOG.warn(
+                            "Error in module {} at line {}: Failed to resolve node {}: no such extension definition found.",
+                            module.getName(), usnb.getLine(), usnb);
+                } else {
+                    usnb.setNodeType(new QName(extDef.getQName().getNamespace(), extDef.getQName().getRevision(),
+                            nodeType.getPrefix(), extDef.getQName().getLocalName()));
+                    usnb.setExtensionDefinition(extDef);
                 }
-            } catch (YangParseException e) {
-                throw new YangParseException(module.getName(), usnb.getLine(), "Failed to resolve node " + usnb
-                        + ": no such extension definition found.", e);
+            } else {
+                usnb.setNodeType(QName.create(extBuilder.getQName().getModule(),
+                        nodeType.getPrefix(), extBuilder.getQName().getLocalName()));
+                usnb.setExtensionBuilder(extBuilder);
             }
         }
     }
 
-    private void resolveUnknownNodesWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final ModuleBuilder module, final SchemaContext context) {
-        for (UnknownSchemaNodeBuilder usnb : module.getAllUnknownNodes()) {
-            QName nodeType = usnb.getNodeType();
-            try {
-                ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, module,
-                        nodeType.getPrefix(), usnb.getLine());
-
-                if (dependentModuleBuilder == null) {
-                    Module dependentModule = findModuleFromContext(context, module, nodeType.getPrefix(),
-                            usnb.getLine());
-                    for (ExtensionDefinition e : dependentModule.getExtensionSchemaNodes()) {
-                        if (e.getQName().getLocalName().equals(nodeType.getLocalName())) {
-                            usnb.setNodeType(new QName(e.getQName().getNamespace(), e.getQName().getRevision(),
-                                    nodeType.getPrefix(), e.getQName().getLocalName()));
-                            usnb.setExtensionDefinition(e);
-                            break;
-                        }
-                    }
-                } else {
-                    for (ExtensionBuilder extension : dependentModuleBuilder.getExtensions()) {
-                        if (extension.getQName().getLocalName().equals(nodeType.getLocalName())) {
-                            usnb.setExtensionBuilder(extension);
-                            break;
-                        }
-                    }
-                }
-
-            } catch (YangParseException e) {
-                throw new YangParseException(module.getName(), usnb.getLine(), "Failed to resolve node " + usnb
-                        + ": no such extension definition found.", e);
+    private ExtensionBuilder findExtBuilder(final String name, final Collection<ExtensionBuilder> extensions) {
+        for (ExtensionBuilder extension : extensions) {
+            if (extension.getQName().getLocalName().equals(name)) {
+                return extension;
             }
+        }
+        return null;
+    }
 
+    private ExtensionDefinition findExtDef(final String name, final Collection<ExtensionDefinition> extensions) {
+        for (ExtensionDefinition extension : extensions) {
+            if (extension.getQName().getLocalName().equals(name)) {
+                return extension;
+            }
         }
+        return null;
     }
 
     /**
@@ -912,93 +1228,20 @@ public final class YangParserImpl implements YangModelParser {
      *            module in which resolve deviations
      */
     private void resolveDeviation(final Map<String, TreeMap<Date, ModuleBuilder>> modules, final ModuleBuilder module) {
-        for (DeviationBuilder dev : module.getDeviations()) {
-            int line = dev.getLine();
+        for (DeviationBuilder dev : module.getDeviationBuilders()) {
             SchemaPath targetPath = dev.getTargetPath();
-            List<QName> path = targetPath.getPath();
-            QName q0 = path.get(0);
+            Iterable<QName> path = targetPath.getPathFromRoot();
+            QName q0 = path.iterator().next();
             String prefix = q0.getPrefix();
             if (prefix == null) {
                 prefix = module.getPrefix();
             }
 
-            ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, module, prefix, line);
+            ModuleBuilder dependentModuleBuilder = BuilderUtils.getModuleByPrefix(module, prefix);
             processDeviation(dev, dependentModuleBuilder, path, module);
         }
     }
 
-    /**
-     * Traverse through modules and resolve their deviation statements with
-     * given context.
-     *
-     * @param modules
-     *            all loaded modules
-     * @param context
-     *            already resolved context
-     */
-    private void resolveDeviationsWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final SchemaContext context) {
-        for (Map.Entry<String, TreeMap<Date, ModuleBuilder>> entry : modules.entrySet()) {
-            for (Map.Entry<Date, ModuleBuilder> inner : entry.getValue().entrySet()) {
-                ModuleBuilder b = inner.getValue();
-                resolveDeviationWithContext(modules, b, context);
-            }
-        }
-    }
-
-    /**
-     * Traverse through module and resolve its deviation statements with given
-     * context.
-     *
-     * @param modules
-     *            all loaded modules
-     * @param module
-     *            module in which resolve deviations
-     * @param context
-     *            already resolved context
-     */
-    private void resolveDeviationWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final ModuleBuilder module, final SchemaContext context) {
-        for (DeviationBuilder dev : module.getDeviations()) {
-            int line = dev.getLine();
-            SchemaPath targetPath = dev.getTargetPath();
-            List<QName> path = targetPath.getPath();
-            QName q0 = path.get(0);
-            String prefix = q0.getPrefix();
-            if (prefix == null) {
-                prefix = module.getPrefix();
-            }
-            String name = null;
-
-            ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, module, prefix, line);
-            if (dependentModuleBuilder == null) {
-                Module dependentModule = findModuleFromContext(context, module, prefix, line);
-                Object currentParent = dependentModule;
-
-                for (int i = 0; i < path.size(); i++) {
-                    if (currentParent == null) {
-                        throw new YangParseException(module.getName(), line, FAIL_DEVIATION_TARGET);
-                    }
-                    QName q = path.get(i);
-                    name = q.getLocalName();
-                    if (currentParent instanceof DataNodeContainer) {
-                        currentParent = ((DataNodeContainer) currentParent).getDataChildByName(name);
-                    }
-                }
-
-                if (currentParent == null) {
-                    throw new YangParseException(module.getName(), line, FAIL_DEVIATION_TARGET);
-                }
-                if (currentParent instanceof SchemaNode) {
-                    dev.setTargetPath(((SchemaNode) currentParent).getPath());
-                }
-
-            } else {
-                processDeviation(dev, dependentModuleBuilder, path, module);
-            }
-        }
-    }
-
     /**
      * Correct deviation target path in deviation builder.
      *
@@ -1012,15 +1255,14 @@ public final class YangParserImpl implements YangModelParser {
      *            current module
      */
     private void processDeviation(final DeviationBuilder dev, final ModuleBuilder dependentModuleBuilder,
-            final List<QName> path, final ModuleBuilder module) {
+            final Iterable<QName> path, final ModuleBuilder module) {
         final int line = dev.getLine();
         Builder currentParent = dependentModuleBuilder;
 
-        for (int i = 0; i < path.size(); i++) {
+        for (QName q : path) {
             if (currentParent == null) {
                 throw new YangParseException(module.getName(), line, FAIL_DEVIATION_TARGET);
             }
-            QName q = path.get(i);
             String name = q.getLocalName();
             if (currentParent instanceof DataNodeContainerBuilder) {
                 currentParent = ((DataNodeContainerBuilder) currentParent).getDataChildByName(name);