Update ParserListenerUtils to use SchemaPath
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / YangParserImpl.java
index 160e9c907b7a18dce8130eea0f37d306efff66ac..cea011edcbd4652a155d15b8185ad1d457e637a8 100644 (file)
@@ -1,18 +1,37 @@
 /*
- * 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.BuilderUtils.setNodeAddedByUses;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.wrapChildNode;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.wrapChildNodes;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.wrapGroupings;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.wrapTypedefs;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils.wrapUnknownNodes;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.TypeUtils.resolveType;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.TypeUtils.resolveTypeUnion;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.TypeUtils.resolveTypeUnionWithContext;
+import static org.opendaylight.yangtools.yang.parser.builder.impl.TypeUtils.resolveTypeWithContext;
+
+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 +39,470 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
 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.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 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.TypeDefinitionBuilder;
+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.impl.UnknownSchemaNodeBuilderImpl;
+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);
             }
+        }
 
-            return new LinkedHashSet<Module>(buildWithContext(modules, context).values());
+        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 = orderModules(sortedBuilders);
+        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);
+    }
+
+    @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);
         }
-        return Collections.emptySet();
     }
 
     @Override
+    public SchemaContext parseFiles(final Collection<File> yangFiles, final SchemaContext context) throws IOException,
+    YangSyntaxErrorException {
+        if (yangFiles == null) {
+            return resolveSchemaContext(Collections.<Module> 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 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();
+    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 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);
-                }
-            }
+    @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);
+        }
+    }
 
-            Map<ModuleBuilder, InputStream> builderToStreamMap = Maps.newHashMap();
-            final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuilders(
-                    Lists.newArrayList(inputStreams.keySet()), builderToStreamMap);
+    @Override
+    public SchemaContext parseSources(final Collection<ByteSource> sources, final SchemaContext context)
+            throws IOException, YangSyntaxErrorException {
+        if (sources == null) {
+            return resolveSchemaContext(Collections.<Module> emptySet());
+        }
 
-            for (InputStream is : inputStreams.keySet()) {
-                try {
-                    is.close();
-                } catch (IOException e) {
-                    LOG.debug("Failed to close stream.");
+        final Map<String, TreeMap<Date, ModuleBuilder>> modules = resolveModuleBuilders(sources, context);
+        final Set<Module> unsorted = new LinkedHashSet<>(buildWithContext(modules, context).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<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);
+        }
+        Map<File, Module> result = new LinkedHashMap<>();
+        for (Map.Entry<ByteSource, Module> entry : byteSourceToModule.entrySet()) {
+            result.put(byteSourceToFile.get(entry.getKey()), entry.getValue());
         }
-        return Collections.emptyMap();
+        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);
+        }
+        Map<InputStream, Module> result = new LinkedHashMap<>();
+        for (Map.Entry<ByteSource, Module> entry : sourceToModule.entrySet()) {
+            result.put(sourceToStream.get(entry.getKey()), entry.getValue());
         }
-        return retVal;
+        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 ModuleBuilder[] parseModuleBuilders(List<InputStream> inputStreams,
-            Map<ModuleBuilder, InputStream> streamToBuilderMap) {
+    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 = orderModules(sorted);
+        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;
+    }
+
+    /**
+     * 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.getModuleImports().addAll(submodule.getModuleImports());
+        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 Map<String, TreeMap<Date, 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 orderModules(sorted);
+    }
 
-        for (final ModuleBuilder builder : sorted) {
+    /**
+     * Order modules by name and revision.
+     *
+     * @param modules
+     *            modules to order
+     * @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 +511,105 @@ 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);
-            modules.put(builderName, builderByRevision);
+            result.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) {
+        Set<ModuleImport> imports = main.getModuleImports();
+
+        // 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.addAll(parent.getModuleImports());
+        }
+
+        for (ModuleImport mi : imports) {
+            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) {
+            trees.put(source, parseYangSource(source));
         }
         return trees;
     }
 
-    private ParseTree parseStream(final InputStream yangStream) {
-        ParseTree result = null;
+    private YangContext parseYangSource(final ByteSource source) throws IOException, YangSyntaxErrorException {
+        try (InputStream stream = source.openStream()) {
+            final ANTLRInputStream input = new ANTLRInputStream(stream);
+            final YangLexer lexer = new YangLexer(input);
+            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 +617,126 @@ 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);
-        resolveAugments(modules);
-        resolveUses(modules, false);
-        resolvedUsesPostProcessing(modules, false);
+        resolveAugmentsTargetPath(modules, null);
+        resolveUsesTargetGrouping(modules, null);
+        resolveUsesForGroupings(modules, null);
+        resolveUsesForNodes(modules, null);
+        resolveAugments(modules, null);
+        resolveIdentities(modules);
         resolveDeviations(modules);
 
         // 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;
     }
 
+    /**
+     * 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
+     * @param context
+     *            SchemaContext containing already resolved modules
+     * @return modules mapped on their builders
+     */
     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);
+        resolveAugmentsTargetPath(modules, context);
+        resolveUsesTargetGrouping(modules, context);
+        resolveUsesForGroupings(modules, context);
+        resolveUsesForNodes(modules, context);
+        resolveAugments(modules, context);
+        resolveIdentitiesWithContext(modules, context);
         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);
+                resolveDirtyNodes(modules, module);
             }
         }
     }
 
+    /**
+     * Resolve all unresolved type references.
+     *
+     * @param modules
+     *            all loaded modules
+     * @param context
+     *            SchemaContext containing already resolved modules
+     */
     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);
+                resolveDirtyNodesWithContext(modules, module, context);
             }
         }
     }
@@ -366,7 +760,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);
                 }
@@ -375,7 +775,7 @@ public final class YangParserImpl implements YangModelParser {
     }
 
     private void resolveDirtyNodesWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
-            final ModuleBuilder module, SchemaContext context) {
+            final ModuleBuilder module, final SchemaContext context) {
         final Set<TypeAwareBuilder> dirtyNodes = module.getDirtyNodes();
         if (!dirtyNodes.isEmpty()) {
             for (TypeAwareBuilder nodeToResolve : dirtyNodes) {
@@ -385,7 +785,10 @@ 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());
+                    idref.setBaseIdentity(identity);
+                    nodeToResolve.setType(idref.build());
                 } else {
                     resolveTypeWithContext(nodeToResolve, modules, module, context);
                 }
@@ -394,12 +797,16 @@ public final class YangParserImpl implements YangModelParser {
     }
 
     /**
-     * Correct augment target path.
+     * Traverse through augmentations of modules and fix their child nodes
+     * schema path.
      *
      * @param modules
      *            all loaded modules
+     * @param context
+     *            SchemaContext containing already resolved modules
      */
-    private void resolveAugmentsTargetPath(final Map<String, TreeMap<Date, ModuleBuilder>> modules) {
+    private void resolveAugmentsTargetPath(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()) {
@@ -409,73 +816,95 @@ public final class YangParserImpl implements YangModelParser {
         }
 
         for (AugmentationSchemaBuilder augment : allAugments) {
-            setCorrectAugmentTargetPath(modules, augment);
+            setCorrectAugmentTargetPath(modules, augment, context);
         }
     }
 
+    /**
+     * Find augment target and set correct schema path for all its child nodes.
+     *
+     * @param modules
+     *            all loaded modules
+     * @param augment
+     *            augment to resolve
+     * @param context
+     *            SchemaContext containing already resolved modules
+     */
     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();
+            final AugmentationSchemaBuilder augment, final SchemaContext context) {
+        ModuleBuilder module = BuilderUtils.getParentModule(augment);
+        SchemaPath oldSchemaPath = augment.getTargetPath();
         List<QName> newPath = new ArrayList<>();
-        for (QName qn : oldPath) {
-            ModuleBuilder currentModule = null;
-            String prefix = qn.getPrefix();
-            if (prefix == null || "".equals(prefix)) {
-                currentModule = module;
+
+        Builder parent = augment.getParent();
+        if (parent instanceof UsesNodeBuilder) {
+            DataNodeContainerBuilder usesParent = ((UsesNodeBuilder) parent).getParent();
+            newPath.addAll(usesParent.getPath().getPath());
+
+            final QName baseQName = usesParent.getQName();
+            final QNameModule qnm;
+            final String prefix;
+            if (baseQName == null) {
+                ModuleBuilder m = BuilderUtils.getParentModule(usesParent);
+                qnm = m.getQNameModule();
+                prefix = m.getPrefix();
             } else {
-                currentModule = ParserUtils.findDependentModuleBuilder(modules, module, prefix,
-                        augmentBuilder.getLine());
+                qnm = baseQName.getModule();
+                prefix = baseQName.getPrefix();
+            }
+
+            for (QName qn : oldSchemaPath.getPathFromRoot()) {
+                newPath.add(QName.create(qnm, prefix, qn.getLocalName()));
+            }
+        } else {
+            Iterable<QName> oldPath = oldSchemaPath.getPathFromRoot();
+            for (QName qn : oldPath) {
+                QNameModule qnm = module.getQNameModule();
+                String localPrefix = qn.getPrefix();
+                if (localPrefix != null && !localPrefix.isEmpty()) {
+                    ModuleBuilder currentModule = BuilderUtils.findModuleFromBuilders(modules, module, localPrefix,
+                            augment.getLine());
+                    if (currentModule == null) {
+                        Module m = BuilderUtils.findModuleFromContext(context, module, localPrefix, augment.getLine());
+                        if (m == null) {
+                            throw new YangParseException(module.getName(), augment.getLine(), "Module with prefix "
+                                    + localPrefix + " not found.");
+                        }
+                        qnm = m.getQNameModule();
+                    } else {
+                        qnm = currentModule.getQNameModule();
+                    }
+                }
+                newPath.add(new QName(qnm.getNamespace(), qnm.getRevision(), localPrefix, qn.getLocalName()));
             }
-            QName newQName = new QName(currentModule.getNamespace(), currentModule.getRevision(), prefix,
-                    qn.getLocalName());
-            newPath.add(newQName);
         }
-        augmentBuilder.setTargetPath(new SchemaPath(newPath, augmentBuilder.getTargetPath().isAbsolute()));
+        augment.setTargetNodeSchemaPath(SchemaPath.create(newPath, true));
+
+        for (DataSchemaNodeBuilder childNode : augment.getChildNodeBuilders()) {
+            correctPathForAugmentNodes(childNode, augment.getTargetNodeSchemaPath());
+        }
     }
 
     /**
-     * Go through all augment definitions and perform augmentation. It is
-     * expected that modules are already sorted by their dependencies.
+     * Set new schema path to node and all its child nodes based on given parent
+     * path. This method do not change the namespace.
      *
-     * @param modules
-     *            all loaded modules
+     * @param node
+     *            node which schema path should be updated
+     * @param parentPath
+     *            schema path of parent node
      */
-    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 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());
             }
         }
-
-        checkAugmentMandatoryNodes(allAugments);
-
-        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);
-
-            // 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++;
-
-            }
-
-            if (!resolved) {
-                throw new YangParseException(augment.getModuleName(), augment.getLine(),
-                        "Error in augment parsing: failed to find augment target");
+        if (node instanceof ChoiceBuilder) {
+            for (ChoiceCaseBuilder child : ((ChoiceBuilder) node).getCases()) {
+                correctPathForAugmentNodes(child, node.getPath());
             }
         }
     }
@@ -488,10 +917,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,138 +930,203 @@ 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
+     * @param context
+     *            SchemaContext containing already resolved modules
      */
-    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, final SchemaContext context) {
+        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();
-            }
-            firstNodeParent = findDependentModuleBuilder(modules, module, prefix, line);
-        } else if (augmentParent instanceof UsesNodeBuilder) {
-            firstNodeParent = augmentParent.getParent();
+        List<ModuleBuilder> sorted;
+        if (context == null) {
+            sorted = ModuleDependencySort.sort(all.toArray(new ModuleBuilder[all.size()]));
         } 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);
+            sorted = ModuleDependencySort.sortWithContext(context, all.toArray(new ModuleBuilder[all.size()]));
         }
 
-        return processAugmentation(augment, firstNodeParent, path);
+        for (ModuleBuilder mb : sorted) {
+            if (mb != null) {
+                List<AugmentationSchemaBuilder> augments = mb.getAllAugments();
+                checkAugmentMandatoryNodes(augments);
+                Collections.sort(augments, Comparators.AUGMENT_COMP);
+                for (AugmentationSchemaBuilder augment : augments) {
+                    if (!(augment.isResolved())) {
+                        boolean resolved = resolveAugment(augment, mb, modules, context);
+                        if (!resolved) {
+                            throw new YangParseException(augment.getModuleName(), augment.getLine(),
+                                    "Error in augment parsing: failed to find augment target: " + augment);
+                        }
+                    }
+                }
+            }
+        }
     }
 
     /**
-     * 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, final SchemaContext context) {
+        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 module
+     *            current module
+     * @param modules
+     *            all loaded modules
      * @param context
      *            SchemaContext containing already resolved modules
-     * @return true if target node found, false otherwise
+     * @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, final SchemaContext context) {
         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();
+
+        QName targetPath = augment.getTargetPath().getPathFromRoot().iterator().next();
+        ModuleBuilder targetModule = findTargetModule(targetPath, module, modules, context, augment.getLine());
+        if (targetModule == null) {
+            throw new YangParseException(module.getModuleName(), augment.getLine(), "Failed to resolve augment "
+                    + augment);
+        }
+
+        return processAugmentation(augment, targetModule);
+    }
+
+    /**
+     * Find module from loaded modules or from context based on given qname. If
+     * module is found in context, create wrapper over this module and add it to
+     * collection of loaded modules.
+     *
+     * @param qname
+     * @param module
+     *            current module
+     * @param modules
+     *            all loaded modules
+     * @param context
+     *            schema context
+     * @param line
+     *            current line
+     * @return
+     */
+    private ModuleBuilder findTargetModule(final QName qname, final ModuleBuilder module,
+            final Map<String, TreeMap<Date, ModuleBuilder>> modules, final SchemaContext context, final int line) {
+        ModuleBuilder targetModule;
+
+        String prefix = qname.getPrefix();
+        if (prefix == null || prefix.isEmpty()) {
+            targetModule = module;
         } 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);
+            targetModule = findModuleFromBuilders(modules, module, qname.getPrefix(), line);
         }
 
-        if (currentParent == null) {
-            return processAugmentationOnContext(augment, path, module, prefix, context);
+        if (targetModule == null && context != null) {
+            Module m = findModuleFromContext(context, module, prefix, line);
+            targetModule = new ModuleBuilder(m);
+            DataSchemaNode firstNode = m.getDataChildByName(qname.getLocalName());
+            DataSchemaNodeBuilder firstNodeWrapped = wrapChildNode(targetModule.getModuleName(), line, firstNode,
+                    targetModule.getPath(), firstNode.getQName());
+            targetModule.addChildNode(firstNodeWrapped);
+
+            TreeMap<Date, ModuleBuilder> map = new TreeMap<>();
+            map.put(targetModule.getRevision(), targetModule);
+            modules.put(targetModule.getModuleName(), map);
+        }
+
+        return targetModule;
+    }
+
+    private ModuleBuilder findTargetModule(final String prefix, final ModuleBuilder module,
+            final Map<String, TreeMap<Date, ModuleBuilder>> modules, final SchemaContext context, final int line) {
+        ModuleBuilder targetModule;
+
+        if (prefix == null || prefix.equals("")) {
+            targetModule = module;
         } else {
-            return processAugmentation(augment, currentParent, path);
+            targetModule = findModuleFromBuilders(modules, module, prefix, line);
         }
+
+        if (targetModule == null && context != null) {
+            Module m = findModuleFromContext(context, module, prefix, line);
+            if (m != null) {
+                targetModule = new ModuleBuilder(m);
+                TreeMap<Date, ModuleBuilder> map = new TreeMap<>();
+                map.put(targetModule.getRevision(), targetModule);
+                modules.put(targetModule.getModuleName(), map);
+            }
+        }
+
+        return targetModule;
     }
 
     /**
@@ -644,30 +1138,26 @@ public final class YangParserImpl implements YangModelParser {
      * @param module
      *            module being resolved
      */
-    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) {
+                    final String baseIdentityName = identity.getBaseIdentityName();
+                    final int line = identity.getLine();
+                    if (baseIdentityName != null) {
+                        IdentitySchemaNodeBuilder baseIdentity = findBaseIdentity(modules, module, baseIdentityName,
+                                line);
+                        if (baseIdentity == null) {
+                            throw new YangParseException(module.getName(), identity.getLine(),
+                                    "Failed to find base identity");
+                        } else {
+                            identity.setBaseIdentity(baseIdentity);
+                        }
                     }
                 }
+
             }
         }
     }
@@ -685,40 +1175,34 @@ public final class YangParserImpl implements YangModelParser {
      *            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());
+            final SchemaContext context) {
+        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) {
+                    final String baseIdentityName = identity.getBaseIdentityName();
+                    final int line = identity.getLine();
+                    if (baseIdentityName != null) {
+
+                        IdentitySchemaNodeBuilder result = null;
+                        if (baseIdentityName.indexOf(':') != -1) {
+                            final Iterator<String> split = COLON_SPLITTER.split(baseIdentityName).iterator();
+                            final String prefix = split.next();
+                            final String name = split.next();
+                            if (split.hasNext()) {
+                                throw new YangParseException(module.getName(), line,
+                                        "Failed to parse identityref base: " + baseIdentityName);
+                            }
 
-                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);
+                            ModuleBuilder dependentModule = findTargetModule(prefix, module, modules, context, line);
+                            if (dependentModule != null) {
+                                result = BuilderUtils.findIdentity(dependentModule.getAddedIdentities(), name);
+                            }
+                        } else {
+                            result = BuilderUtils.findIdentity(module.getAddedIdentities(), baseIdentityName);
                         }
+                        identity.setBaseIdentity(result);
                     }
                 }
             }
@@ -743,7 +1227,7 @@ 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) {
@@ -762,83 +1246,166 @@ public final class YangParserImpl implements YangModelParser {
     }
 
     /**
-     * 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.
+     * @param context
+     *            SchemaContext containing already resolved modules
      */
-    private void resolveUses(final Map<String, TreeMap<Date, ModuleBuilder>> modules, final boolean resolveWithContext) {
+    private void resolveUsesForGroupings(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+            final SchemaContext context) {
+        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, context);
             }
         }
     }
 
     /**
-     * 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.
+     * @param context
+     *            SchemaContext containing already resolved modules
      */
-    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,
+            final SchemaContext context) {
         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, context);
+                }
             }
         }
+    }
 
-        for (UsesNodeBuilder usesNode : allModulesUses) {
-            GroupingUtils.updateUsesParent(usesNode);
+    /**
+     * 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
+     * @param context
+     *            SchemaContext containing already resolved modules
+     */
+    private void resolveUses(final UsesNodeBuilder usesNode, final Map<String, TreeMap<Date, ModuleBuilder>> modules,
+            final SchemaContext context) {
+        if (!usesNode.isResolved()) {
+            DataNodeContainerBuilder parent = usesNode.getParent();
+            ModuleBuilder module = BuilderUtils.getParentModule(parent);
+            GroupingBuilder target = GroupingUtils.getTargetGroupingFromModules(usesNode, modules, module);
+            if (target == null) {
+                resolveUsesWithContext(usesNode);
+                usesNode.setResolved(true);
+                for (AugmentationSchemaBuilder augment : usesNode.getAugmentations()) {
+                    resolveUsesAugment(augment, module, modules, context);
+                }
+            } else {
+                parent.getChildNodeBuilders().addAll(target.instantiateChildNodes(parent));
+                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, context);
+                }
+            }
             GroupingUtils.performRefine(usesNode);
         }
+    }
 
-        if (!resolveWithContext) {
-            for (UsesNodeBuilder usesNode : allModulesUses) {
-                if (usesNode.isCopy()) {
-                    usesNode.getParent().getUsesNodes().remove(usesNode);
-                }
+    /**
+     * Copy target grouping child nodes to current location with new namespace.
+     *
+     * @param usesNode
+     *            uses node to resolve
+     * @param modules
+     *            all loaded modules
+     * @param context
+     *            SchemaContext containing already resolved modules
+     */
+    private void resolveUsesWithContext(final UsesNodeBuilder usesNode) {
+        final int line = usesNode.getLine();
+        DataNodeContainerBuilder parent = usesNode.getParent();
+        ModuleBuilder module = BuilderUtils.getParentModule(parent);
+        SchemaPath parentPath;
+
+        final QName parentQName;
+        if (parent instanceof AugmentationSchemaBuilder || parent instanceof ModuleBuilder) {
+            parentQName = QName.create(module.getQNameModule(), module.getPrefix(), "dummy");
+            if (parent instanceof AugmentationSchemaBuilder) {
+                parentPath = ((AugmentationSchemaBuilder) parent).getTargetNodeSchemaPath();
+            } else {
+                parentPath = parent.getPath();
             }
+        } else {
+            parentQName = parent.getQName();
+            parentPath = parent.getPath();
+        }
+
+        GroupingDefinition gd = usesNode.getGroupingDefinition();
+
+        Set<DataSchemaNodeBuilder> childNodes = wrapChildNodes(module.getModuleName(), line, gd.getChildNodes(),
+                parentPath, parentQName);
+        parent.getChildNodeBuilders().addAll(childNodes);
+        for (DataSchemaNodeBuilder childNode : childNodes) {
+            setNodeAddedByUses(childNode);
+        }
+
+        Set<TypeDefinitionBuilder> typedefs = wrapTypedefs(module.getModuleName(), line, gd, parentPath, parentQName);
+        parent.getTypeDefinitionBuilders().addAll(typedefs);
+        for (TypeDefinitionBuilder typedef : typedefs) {
+            setNodeAddedByUses(typedef);
+        }
+
+        Set<GroupingBuilder> groupings = wrapGroupings(module.getModuleName(), line, usesNode.getGroupingDefinition()
+                .getGroupings(), parentPath, parentQName);
+        parent.getGroupingBuilders().addAll(groupings);
+        for (GroupingBuilder gb : groupings) {
+            setNodeAddedByUses(gb);
+        }
+
+        List<UnknownSchemaNodeBuilderImpl> unknownNodes = wrapUnknownNodes(module.getModuleName(), line,
+                gd.getUnknownSchemaNodes(), parentPath, parentQName);
+        parent.getUnknownNodes().addAll(unknownNodes);
+        for (UnknownSchemaNodeBuilder un : unknownNodes) {
+            un.setAddedByUses(true);
         }
     }
 
+    /**
+     * Try to find extension builder 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(),
+                ModuleBuilder dependentModule = findModuleFromBuilders(modules, module, nodeType.getPrefix(),
                         usnb.getLine());
-                for (ExtensionBuilder extension : dependentModule.getExtensions()) {
+                for (ExtensionBuilder extension : dependentModule.getAddedExtensions()) {
                     if (extension.getQName().getLocalName().equals(nodeType.getLocalName())) {
                         usnb.setNodeType(extension.getQName());
                         usnb.setExtensionBuilder(extension);
@@ -852,13 +1419,25 @@ public final class YangParserImpl implements YangModelParser {
         }
     }
 
+    /**
+     * Try to find extension builder describing this unknown node and assign it
+     * to unknown node builder. If extension is not found in loaded modules, try
+     * to find it in context.
+     *
+     * @param modules
+     *            all loaded modules
+     * @param module
+     *            current module
+     * @param context
+     *            SchemaContext containing already resolved modules
+     */
     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());
+                ModuleBuilder dependentModuleBuilder = findModuleFromBuilders(modules, module, nodeType.getPrefix(),
+                        usnb.getLine());
 
                 if (dependentModuleBuilder == null) {
                     Module dependentModule = findModuleFromContext(context, module, nodeType.getPrefix(),
@@ -872,7 +1451,7 @@ public final class YangParserImpl implements YangModelParser {
                         }
                     }
                 } else {
-                    for (ExtensionBuilder extension : dependentModuleBuilder.getExtensions()) {
+                    for (ExtensionBuilder extension : dependentModuleBuilder.getAddedExtensions()) {
                         if (extension.getQName().getLocalName().equals(nodeType.getLocalName())) {
                             usnb.setExtensionBuilder(extension);
                             break;
@@ -912,17 +1491,17 @@ 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()) {
+        for (DeviationBuilder dev : module.getDeviationBuilders()) {
             int line = dev.getLine();
             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 = findModuleFromBuilders(modules, module, prefix, line);
             processDeviation(dev, dependentModuleBuilder, path, module);
         }
     }
@@ -959,28 +1538,25 @@ public final class YangParserImpl implements YangModelParser {
      */
     private void resolveDeviationWithContext(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
             final ModuleBuilder module, final SchemaContext context) {
-        for (DeviationBuilder dev : module.getDeviations()) {
+        for (DeviationBuilder dev : module.getDeviationBuilders()) {
             int line = dev.getLine();
             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();
             }
-            String name = null;
 
-            ModuleBuilder dependentModuleBuilder = findDependentModuleBuilder(modules, module, prefix, line);
+            ModuleBuilder dependentModuleBuilder = findModuleFromBuilders(modules, module, prefix, line);
             if (dependentModuleBuilder == null) {
-                Module dependentModule = findModuleFromContext(context, module, prefix, line);
-                Object currentParent = dependentModule;
+                Object currentParent = findModuleFromContext(context, module, prefix, line);
 
-                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);
-                    name = q.getLocalName();
+                    String name = q.getLocalName();
                     if (currentParent instanceof DataNodeContainer) {
                         currentParent = ((DataNodeContainer) currentParent).getDataChildByName(name);
                     }
@@ -1012,15 +1588,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);