BUG-1270: improve QNameModule reuse
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / ModuleBuilder.java
index e49a181717dacce38eb7fa82daff8001f50dc19c..db1babd6dc08ef8242c4aa37c81232224ebbc8e8 100644 (file)
@@ -1,33 +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.builder.impl;
 
+import com.google.common.base.Preconditions;
+import com.google.common.io.ByteSource;
+
+import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
 import java.util.Deque;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.TreeMap;
 import java.util.TreeSet;
 
+import org.apache.commons.io.IOUtils;
 import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Deviation;
 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
 import org.opendaylight.yangtools.yang.model.api.FeatureDefinition;
-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.ModuleImport;
@@ -36,20 +40,21 @@ import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.UsesNode;
-import org.opendaylight.yangtools.yang.parser.builder.api.AbstractDataNodeContainerBuilder;
+import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl;
 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.DocumentedNodeBuilder;
+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.util.Comparators;
-import org.opendaylight.yangtools.yang.parser.util.ModuleImportImpl;
-import org.opendaylight.yangtools.yang.parser.util.RefineHolder;
+import org.opendaylight.yangtools.yang.parser.builder.util.AbstractDocumentedDataNodeContainerBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.util.Comparators;
 import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 
 /**
@@ -57,38 +62,102 @@ import org.opendaylight.yangtools.yang.parser.util.YangParseException;
  * module/modules, these dependencies must be resolved before module is built,
  * otherwise result may not be valid.
  */
-public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
+public class ModuleBuilder extends AbstractDocumentedDataNodeContainerBuilder implements DocumentedNodeBuilder {
 
-    private final ModuleImpl instance;
+    private ModuleImpl instance;
     private final String name;
-    private final SchemaPath schemaPath;
-    private URI namespace;
+    private final String sourcePath;
+    private static final SchemaPath SCHEMA_PATH = SchemaPath.create(Collections.<QName> emptyList(), true);
     private String prefix;
-    private Date revision;
-
-    private final Deque<Builder> actualPath = new LinkedList<Builder>();
-    private final Set<TypeAwareBuilder> dirtyNodes = new HashSet<TypeAwareBuilder>();
-
-    private final Set<ModuleImport> imports = new HashSet<ModuleImport>();
-    private final List<AugmentationSchemaBuilder> addedAugments = new ArrayList<AugmentationSchemaBuilder>();
-    private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<AugmentationSchemaBuilder>();
-    private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<UsesNodeBuilder>();
-    private final List<UsesNodeBuilder> allUsesNodes = new ArrayList<UsesNodeBuilder>();
-    private final Set<RpcDefinitionBuilder> addedRpcs = new HashSet<RpcDefinitionBuilder>();
-    private final Set<NotificationBuilder> addedNotifications = new HashSet<NotificationBuilder>();
-    private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<IdentitySchemaNodeBuilder>();
-    private final Set<FeatureBuilder> addedFeatures = new HashSet<FeatureBuilder>();
-    private final Set<DeviationBuilder> addedDeviations = new HashSet<DeviationBuilder>();
-    private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<TypeDefinitionBuilder>();
-    private final List<ExtensionBuilder> addedExtensions = new ArrayList<ExtensionBuilder>();
-    private final List<UnknownSchemaNodeBuilder> allUnknownNodes = new ArrayList<UnknownSchemaNodeBuilder>();
-
-    public ModuleBuilder(final String name) {
+    private QNameModule qnameModule = QNameModule.create(null, null);
+
+    private final boolean submodule;
+    private String belongsTo;
+    private ModuleBuilder parent;
+
+    private final Deque<Builder> actualPath = new LinkedList<>();
+    private final Set<TypeAwareBuilder> dirtyNodes = new HashSet<>();
+
+    final Map<String, ModuleImport> imports = new HashMap<>();
+    final Map<String, ModuleBuilder> importedModules = new HashMap<>();
+
+    private final Set<AugmentationSchema> augments = new LinkedHashSet<>();
+    private final List<AugmentationSchemaBuilder> augmentBuilders = new ArrayList<>();
+    private final List<AugmentationSchemaBuilder> allAugments = new ArrayList<>();
+
+    private final List<GroupingBuilder> allGroupings = new ArrayList<>();
+
+    private final List<UsesNodeBuilder> allUsesNodes = new ArrayList<>();
+
+    private final Set<RpcDefinition> rpcs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
+    private final Set<RpcDefinitionBuilder> addedRpcs = new HashSet<>();
+
+    private final Set<NotificationDefinition> notifications = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
+    private final Set<NotificationBuilder> addedNotifications = new HashSet<>();
+
+    private final Set<IdentitySchemaNode> identities = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
+    private final Set<IdentitySchemaNodeBuilder> addedIdentities = new HashSet<>();
+
+    private final Set<FeatureDefinition> features = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
+    private final Set<FeatureBuilder> addedFeatures = new HashSet<>();
+
+    private final Set<Deviation> deviations = new HashSet<>();
+    private final Set<DeviationBuilder> deviationBuilders = new HashSet<>();
+
+    private final List<ExtensionDefinition> extensions = new ArrayList<>();
+    private final List<ExtensionBuilder> addedExtensions = new ArrayList<>();
+
+    private final List<UnknownSchemaNodeBuilder> allUnknownNodes = new ArrayList<>();
+
+    private final List<ListSchemaNodeBuilder> allLists = new ArrayList<>();
+
+    private String source;
+    private String yangVersion;
+    private String organization;
+    private String contact;
+
+    public ModuleBuilder(final String name, final String sourcePath) {
+        this(name, false, sourcePath);
+    }
+
+    public ModuleBuilder(final String name, final boolean submodule, final String sourcePath) {
         super(name, 0, null);
         this.name = name;
-        schemaPath = new SchemaPath(Collections.<QName> emptyList(), true);
-        instance = new ModuleImpl(name);
-        actualPath.push(this);
+        this.sourcePath = sourcePath;
+        this.submodule = submodule;
+        actualPath.push(this);//FIXME: this escapes constructor
+    }
+
+    public ModuleBuilder(final Module base) {
+        super(base.getName(), 0, QName.create(base.getQNameModule(), base.getPrefix(), base.getName()),
+                SCHEMA_PATH, base);
+        this.name = base.getName();
+        this.sourcePath = base.getModuleSourcePath();
+
+        submodule = false;
+        yangVersion = base.getYangVersion();
+        actualPath.push(this);//FIXME: this escapes constructor
+        prefix = base.getPrefix();
+        qnameModule = base.getQNameModule();
+
+        augments.addAll(base.getAugmentations());
+        rpcs.addAll(base.getRpcs());
+        notifications.addAll(base.getNotifications());
+
+        for (IdentitySchemaNode identityNode : base.getIdentities()) {
+            addedIdentities.add(new IdentitySchemaNodeBuilder(name, identityNode));
+        }
+
+        features.addAll(base.getFeatures());
+        deviations.addAll(base.getDeviations());
+        extensions.addAll(base.getExtensionSchemaNodes());
+        unknownNodes.addAll(base.getUnknownSchemaNodes());
+        source = base.getSource();
+    }
+
+    @Override
+    protected String getStatementName() {
+        return "module";
     }
 
     /**
@@ -96,122 +165,80 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
      */
     @Override
     public Module build() {
-        instance.setPrefix(prefix);
-        instance.setRevision(revision);
-        instance.setImports(imports);
-        instance.setNamespace(namespace);
-
-        // TYPEDEFS
-        final Set<TypeDefinition<?>> typedefs = new TreeSet<TypeDefinition<?>>(Comparators.SCHEMA_NODE_COMP);
-        for (TypeDefinitionBuilder tdb : addedTypedefs) {
-            typedefs.add(tdb.build());
+        if(instance != null) {
+            return instance;
         }
-        instance.setTypeDefinitions(typedefs);
 
-        // CHILD NODES
-        final Map<QName, DataSchemaNode> children = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
-        for (DataSchemaNodeBuilder child : addedChildNodes) {
-            children.put(child.getQName(), child.build());
-        }
-        instance.setChildNodes(children);
-
-        // GROUPINGS
-        final Set<GroupingDefinition> groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
-        for (GroupingBuilder gb : addedGroupings) {
-            groupings.add(gb.build());
-        }
-        instance.setGroupings(groupings);
-
-        // USES
-        final Set<UsesNode> usesDefinitions = new HashSet<UsesNode>();
-        for (UsesNodeBuilder unb : addedUsesNodes) {
-            usesDefinitions.add(unb.build());
-        }
-        instance.setUses(usesDefinitions);
+        buildChildren();
 
         // FEATURES
-        final Set<FeatureDefinition> features = new TreeSet<FeatureDefinition>(Comparators.SCHEMA_NODE_COMP);
         for (FeatureBuilder fb : addedFeatures) {
             features.add(fb.build());
         }
-        instance.setFeatures(features);
 
         // NOTIFICATIONS
-        final Set<NotificationDefinition> notifications = new TreeSet<NotificationDefinition>(
-                Comparators.SCHEMA_NODE_COMP);
         for (NotificationBuilder entry : addedNotifications) {
             notifications.add(entry.build());
         }
-        instance.setNotifications(notifications);
 
         // AUGMENTATIONS
-        final Set<AugmentationSchema> augmentations = new HashSet<AugmentationSchema>();
-        for (AugmentationSchemaBuilder builder : addedAugments) {
-            augmentations.add(builder.build());
+        for (AugmentationSchemaBuilder builder : augmentBuilders) {
+            augments.add(builder.build());
         }
-        instance.setAugmentations(augmentations);
 
         // RPCs
-        final Set<RpcDefinition> rpcs = new TreeSet<RpcDefinition>(Comparators.SCHEMA_NODE_COMP);
         for (RpcDefinitionBuilder rpc : addedRpcs) {
             rpcs.add(rpc.build());
         }
-        instance.setRpcs(rpcs);
 
         // DEVIATIONS
-        final Set<Deviation> deviations = new HashSet<Deviation>();
-        for (DeviationBuilder entry : addedDeviations) {
+        for (DeviationBuilder entry : deviationBuilders) {
             deviations.add(entry.build());
         }
-        instance.setDeviations(deviations);
 
         // EXTENSIONS
-        final List<ExtensionDefinition> extensions = new ArrayList<ExtensionDefinition>();
         for (ExtensionBuilder eb : addedExtensions) {
             extensions.add(eb.build());
         }
         Collections.sort(extensions, Comparators.SCHEMA_NODE_COMP);
-        instance.setExtensionSchemaNodes(extensions);
+
 
         // IDENTITIES
-        final Set<IdentitySchemaNode> identities = new TreeSet<IdentitySchemaNode>(Comparators.SCHEMA_NODE_COMP);
         for (IdentitySchemaNodeBuilder id : addedIdentities) {
             identities.add(id.build());
         }
-        instance.setIdentities(identities);
 
         // UNKNOWN NODES
-        final List<UnknownSchemaNode> unknownNodes = new ArrayList<UnknownSchemaNode>();
         for (UnknownSchemaNodeBuilder unb : addedUnknownNodes) {
             unknownNodes.add(unb.build());
         }
-        instance.setUnknownSchemaNodes(unknownNodes);
+        Collections.sort(unknownNodes, Comparators.SCHEMA_NODE_COMP);
 
+        instance = new ModuleImpl(name, sourcePath, this);
         return instance;
     }
 
-    public boolean isAllUsesDataCollected() {
-        for (UsesNodeBuilder usesNode : allUsesNodes) {
-            if (!usesNode.isDataCollected()) {
-                return false;
-            }
-        }
-        return true;
+    public String getModuleSourcePath() {
+        return sourcePath;
     }
 
     @Override
-    public void setParent(Builder parent) {
-        throw new YangParseException(name, 0, "Can not set parent to module");
+    public ModuleBuilder getParent() {
+        return parent;
+    }
+
+    public void setParent(final ModuleBuilder parent) {
+        this.parent = parent;
     }
 
     @Override
-    public SchemaPath getPath() {
-        return schemaPath;
+    public void setParent(final Builder parent) {
+        throw new YangParseException(name, 0, "Can not set parent to module");
     }
 
     @Override
-    public Set<TypeDefinitionBuilder> getTypeDefinitionBuilders() {
-        return addedTypedefs;
+    public SchemaPath getPath() {
+        return SCHEMA_PATH;
     }
 
     public void enterNode(final Builder node) {
@@ -230,38 +257,59 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         }
     }
 
-    public Builder getActualParent() {
-        if (actualPath.size() < 2) {
-            return null;
-        } else {
-            Builder builderChild = actualPath.removeFirst();
-            Builder builderParent = actualPath.peekFirst();
-            actualPath.addFirst(builderChild);
-            return builderParent;
-        }
-    }
-
     public Set<TypeAwareBuilder> getDirtyNodes() {
         return dirtyNodes;
     }
 
+    public Set<AugmentationSchema> getAugments() {
+        return augments;
+    }
+
+    public List<AugmentationSchemaBuilder> getAugmentBuilders() {
+        return augmentBuilders;
+    }
+
     public List<AugmentationSchemaBuilder> getAllAugments() {
         return allAugments;
     }
 
-    public Set<IdentitySchemaNodeBuilder> getIdentities() {
+    public Set<IdentitySchemaNode> getIdentities() {
+        return identities;
+    }
+
+    public Set<IdentitySchemaNodeBuilder> getAddedIdentities() {
         return addedIdentities;
     }
 
+    public Set<FeatureDefinition> getFeatures() {
+        return features;
+    }
+
+    public Set<FeatureBuilder> getAddedFeatures() {
+        return addedFeatures;
+    }
+
+    public List<GroupingBuilder> getAllGroupings() {
+        return allGroupings;
+    }
+
     public List<UsesNodeBuilder> getAllUsesNodes() {
         return allUsesNodes;
     }
 
-    public Set<DeviationBuilder> getDeviations() {
-        return addedDeviations;
+    public Set<Deviation> getDeviations() {
+        return deviations;
     }
 
-    public List<ExtensionBuilder> getExtensions() {
+    public Set<DeviationBuilder> getDeviationBuilders() {
+        return deviationBuilders;
+    }
+
+    public List<ExtensionDefinition> getExtensions() {
+        return extensions;
+    }
+
+    public List<ExtensionBuilder> getAddedExtensions() {
         return addedExtensions;
     }
 
@@ -269,16 +317,28 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         return allUnknownNodes;
     }
 
+    public List<ListSchemaNodeBuilder> getAllLists() {
+        return allLists;
+    }
+
     public String getName() {
         return name;
     }
 
     public URI getNamespace() {
-        return namespace;
+        return qnameModule.getNamespace();
+    }
+
+    public QNameModule getQNameModule() {
+        return qnameModule;
+    }
+
+    public void setQNameModule(final QNameModule qnameModule) {
+        this.qnameModule = Preconditions.checkNotNull(qnameModule);
     }
 
     public void setNamespace(final URI namespace) {
-        this.namespace = namespace;
+        this.qnameModule = QNameModule.create(namespace, qnameModule.getRevision());
     }
 
     public String getPrefix() {
@@ -286,7 +346,40 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
     }
 
     public Date getRevision() {
-        return revision;
+        return qnameModule.getRevision();
+    }
+
+    public ModuleImport getImport(final String prefix) {
+        return imports.get(prefix);
+    }
+
+    public Map<String, ModuleImport> getImports() {
+        return imports;
+    }
+
+    public ModuleBuilder getImportedModule(final String prefix) {
+        return importedModules.get(prefix);
+    }
+
+    public void addImportedModule(final String prefix, final ModuleBuilder module) {
+        checkPrefix(prefix);
+        importedModules.put(prefix, module);
+    }
+
+    protected String getSource() {
+        return source;
+    }
+
+    public boolean isSubmodule() {
+        return submodule;
+    }
+
+    public String getBelongsTo() {
+        return belongsTo;
+    }
+
+    public void setBelongsTo(final String belongsTo) {
+        this.belongsTo = belongsTo;
     }
 
     public void markActualNodeDirty() {
@@ -295,7 +388,7 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
     }
 
     public void setRevision(final Date revision) {
-        this.revision = revision;
+        this.qnameModule = QNameModule.create(qnameModule.getNamespace(), revision);
     }
 
     public void setPrefix(final String prefix) {
@@ -303,47 +396,54 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
     }
 
     public void setYangVersion(final String yangVersion) {
-        instance.setYangVersion(yangVersion);
-    }
-
-    public void setDescription(final String description) {
-        instance.setDescription(description);
-    }
-
-    public void setReference(final String reference) {
-        instance.setReference(reference);
+        this.yangVersion = yangVersion;
     }
 
     public void setOrganization(final String organization) {
-        instance.setOrganization(organization);
+        this.organization = organization;
     }
 
     public void setContact(final String contact) {
-        instance.setContact(contact);
+        this.contact = contact;
     }
 
-    public boolean addModuleImport(final String moduleName, final Date revision, final String prefix) {
+    public void addModuleImport(final String moduleName, final Date revision, final String prefix) {
+        checkPrefix(prefix);
+        checkNotSealed();
         final ModuleImport moduleImport = createModuleImport(moduleName, revision, prefix);
-        return imports.add(moduleImport);
+        imports.put(prefix, moduleImport);
     }
 
-    public Set<ModuleImport> getModuleImports() {
-        return imports;
+    private void checkPrefix(final String prefix) {
+        if (prefix == null || prefix.isEmpty()) {
+            throw new IllegalArgumentException("Cannot add imported module with undefined prefix");
+        }
+        if (prefix.equals(this.prefix)) {
+            throw new IllegalArgumentException("Cannot add imported module with prefix equals to module prefix");
+        }
     }
 
-    public ExtensionBuilder addExtension(final QName qname, final int line) {
+    public ExtensionBuilder addExtension(final QName qname, final int line, final SchemaPath path) {
+        checkNotSealed();
+        Builder parent = getActualNode();
+        if (!(parent.equals(this))) {
+            throw new YangParseException(name, line, "extension can be defined only in module or submodule");
+        }
+
         final String extName = qname.getLocalName();
         for (ExtensionBuilder addedExtension : addedExtensions) {
             if (addedExtension.getQName().getLocalName().equals(extName)) {
                 raiseYangParserException("extension", "node", extName, line, addedExtension.getLine());
             }
         }
-        final ExtensionBuilder builder = new ExtensionBuilder(name, line, qname);
+        final ExtensionBuilder builder = new ExtensionBuilderImpl(name, line, qname, path);
+        builder.setParent(parent);
         addedExtensions.add(builder);
         return builder;
     }
 
     public ContainerSchemaNodeBuilder addContainerNode(final int line, final QName qname, final SchemaPath schemaPath) {
+        checkNotSealed();
         final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(name, line, qname, schemaPath);
 
         Builder parent = getActualNode();
@@ -354,16 +454,19 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
     }
 
     public ListSchemaNodeBuilder addListNode(final int line, final QName qname, final SchemaPath schemaPath) {
+        checkNotSealed();
         final ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(name, line, qname, schemaPath);
 
         Builder parent = getActualNode();
         builder.setParent(parent);
         addChildToParent(parent, builder, qname.getLocalName());
+        allLists.add(builder);
 
         return builder;
     }
 
     public LeafSchemaNodeBuilder addLeafNode(final int line, final QName qname, final SchemaPath schemaPath) {
+        checkNotSealed();
         final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(name, line, qname, schemaPath);
 
         Builder parent = getActualNode();
@@ -374,6 +477,7 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
     }
 
     public LeafListSchemaNodeBuilder addLeafListNode(final int line, final QName qname, final SchemaPath schemaPath) {
+        checkNotSealed();
         final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(name, line, qname, schemaPath);
 
         Builder parent = getActualNode();
@@ -383,20 +487,21 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         return builder;
     }
 
-    public GroupingBuilder addGrouping(final int line, final QName qname) {
-        final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname);
+    public GroupingBuilder addGrouping(final int line, final QName qname, final SchemaPath path) {
+        checkNotSealed();
+        final GroupingBuilder builder = new GroupingBuilderImpl(name, line, qname, path);
 
         Builder parent = getActualNode();
         builder.setParent(parent);
 
         String groupingName = qname.getLocalName();
         if (parent.equals(this)) {
-            for (GroupingBuilder addedGrouping : addedGroupings) {
+            for (GroupingBuilder addedGrouping : getGroupingBuilders()) {
                 if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
                     raiseYangParserException("", "Grouping", groupingName, line, addedGrouping.getLine());
                 }
             }
-            addedGroupings.add(builder);
+            addGrouping(builder);
         } else {
             if (parent instanceof DataNodeContainerBuilder) {
                 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
@@ -419,21 +524,34 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
             }
         }
 
+        allGroupings.add(builder);
         return builder;
     }
 
-    public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr) {
-        final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr);
+    public AugmentationSchemaBuilder addAugment(final int line, final String augmentTargetStr, final int order) {
+        checkNotSealed();
+        final AugmentationSchemaBuilder builder = new AugmentationSchemaBuilderImpl(name, line, augmentTargetStr, order);
 
         Builder parent = getActualNode();
         builder.setParent(parent);
 
         if (parent.equals(this)) {
             // augment can be declared only under 'module' ...
-            addedAugments.add(builder);
+            if (!(augmentTargetStr.startsWith("/"))) {
+                throw new YangParseException(
+                        name,
+                        line,
+                        "If the 'augment' statement is on the top level in a module, the absolute form of a schema node identifier MUST be used.");
+            }
+            augmentBuilders.add(builder);
         } else {
             // ... or 'uses' statement
             if (parent instanceof UsesNodeBuilder) {
+                if (augmentTargetStr.startsWith("/")) {
+                    throw new YangParseException(name, line,
+                            "If 'augment' statement is a substatement to the 'uses' statement, it cannot contain absolute path ("
+                                    + augmentTargetStr + ")");
+                }
                 ((UsesNodeBuilder) parent).addAugment(builder);
             } else {
                 throw new YangParseException(name, line, "Augment can be declared only under module or uses statement.");
@@ -444,41 +562,31 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         return builder;
     }
 
-    @Override
-    public Set<UsesNodeBuilder> getUsesNodes() {
-        return addedUsesNodes;
-    }
-
-    @Override
-    public void addUsesNode(UsesNodeBuilder usesBuilder) {
-        addedUsesNodes.add(usesBuilder);
-        allUsesNodes.add(usesBuilder);
-    }
-
     public UsesNodeBuilder addUsesNode(final int line, final String groupingPathStr) {
+        checkNotSealed();
         final UsesNodeBuilder usesBuilder = new UsesNodeBuilderImpl(name, line, groupingPathStr);
 
         Builder parent = getActualNode();
         usesBuilder.setParent(parent);
 
         if (parent.equals(this)) {
-            addedUsesNodes.add(usesBuilder);
+            addUsesNode(usesBuilder);
         } else {
             if (!(parent instanceof DataNodeContainerBuilder)) {
                 throw new YangParseException(name, line, "Unresolved parent of uses '" + groupingPathStr + "'.");
             }
             ((DataNodeContainerBuilder) parent).addUsesNode(usesBuilder);
         }
-        if(parent instanceof AugmentationSchemaBuilder) {
+        if (parent instanceof AugmentationSchemaBuilder) {
             usesBuilder.setAugmenting(true);
-            usesBuilder.setParentAugment((AugmentationSchemaBuilder)parent);
         }
 
         allUsesNodes.add(usesBuilder);
         return usesBuilder;
     }
 
-    public void addRefine(final RefineHolder refine) {
+    public void addRefine(final RefineHolderImpl refine) {
+        checkNotSealed();
         final Builder parent = getActualNode();
         if (!(parent instanceof UsesNodeBuilder)) {
             throw new YangParseException(name, refine.getLine(), "refine can be defined only in uses statement");
@@ -487,36 +595,42 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         refine.setParent(parent);
     }
 
-    public RpcDefinitionBuilder addRpc(final int line, final QName qname) {
+    public RpcDefinitionBuilder addRpc(final int line, final QName qname, final SchemaPath path) {
+        checkNotSealed();
         Builder parent = getActualNode();
         if (!(parent.equals(this))) {
             throw new YangParseException(name, line, "rpc can be defined only in module or submodule");
         }
 
-        final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname);
+        final RpcDefinitionBuilder rpcBuilder = new RpcDefinitionBuilder(name, line, qname, path);
         rpcBuilder.setParent(parent);
 
         String rpcName = qname.getLocalName();
+        checkNotConflictingInDataNamespace(rpcName, line);
+        addedRpcs.add(rpcBuilder);
+        return rpcBuilder;
+    }
+
+    private void checkNotConflictingInDataNamespace(final String rpcName, final int line) {
+        for (DataSchemaNodeBuilder addedChild : getChildNodeBuilders()) {
+            if (addedChild.getQName().getLocalName().equals(rpcName)) {
+                raiseYangParserException("rpc", "node", rpcName, line, addedChild.getLine());
+            }
+        }
         for (RpcDefinitionBuilder rpc : addedRpcs) {
             if (rpc.getQName().getLocalName().equals(rpcName)) {
                 raiseYangParserException("", "rpc", rpcName, line, rpc.getLine());
             }
         }
-        for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
-            if (addedChild.getQName().getLocalName().equals(rpcName)) {
-                raiseYangParserException("rpc", "node", rpcName, line, addedChild.getLine());
-            }
-        }
         for (NotificationBuilder addedNotification : addedNotifications) {
             if (addedNotification.getQName().getLocalName().equals(rpcName)) {
                 raiseYangParserException("rpc", "notification", rpcName, line, addedNotification.getLine());
             }
         }
-        addedRpcs.add(rpcBuilder);
-        return rpcBuilder;
     }
 
     public ContainerSchemaNodeBuilder addRpcInput(final int line, final QName qname, final SchemaPath schemaPath) {
+        checkNotSealed();
         final Builder parent = getActualNode();
         if (!(parent instanceof RpcDefinitionBuilder)) {
             throw new YangParseException(name, line, "input can be defined only in rpc statement");
@@ -531,6 +645,7 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
     }
 
     public ContainerSchemaNodeBuilder addRpcOutput(final SchemaPath schemaPath, final QName qname, final int line) {
+        checkNotSealed();
         final Builder parent = getActualNode();
         if (!(parent instanceof RpcDefinitionBuilder)) {
             throw new YangParseException(name, line, "output can be defined only in rpc statement");
@@ -544,43 +659,35 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         return outputBuilder;
     }
 
-    public NotificationBuilder addNotification(final int line, final QName qname) {
+    public void addNotification(final NotificationDefinition notification) {
+        checkNotSealed();
+        notifications.add(notification);
+    }
+
+    public NotificationBuilder addNotification(final int line, final QName qname, final SchemaPath path) {
+        checkNotSealed();
         final Builder parent = getActualNode();
         if (!(parent.equals(this))) {
             throw new YangParseException(name, line, "notification can be defined only in module or submodule");
         }
 
         String notificationName = qname.getLocalName();
-        for (NotificationBuilder nb : addedNotifications) {
-            if (nb.getQName().equals(qname)) {
-                raiseYangParserException("", "notification", notificationName, line, nb.getLine());
-            }
-        }
-        for (RpcDefinitionBuilder rpc : addedRpcs) {
-            if (rpc.getQName().getLocalName().equals(notificationName)) {
-                raiseYangParserException("notification", "rpc", notificationName, line, rpc.getLine());
-            }
-        }
-        for (DataSchemaNodeBuilder addedChild : addedChildNodes) {
-            if (addedChild.getQName().getLocalName().equals(notificationName)) {
-                raiseYangParserException("notification", "node", notificationName, line, addedChild.getLine());
-            }
-        }
+        checkNotConflictingInDataNamespace(notificationName, line);
 
-        final NotificationBuilder builder = new NotificationBuilder(name, line, qname);
+        final NotificationBuilder builder = new NotificationBuilder(name, line, qname, path);
         builder.setParent(parent);
         addedNotifications.add(builder);
 
         return builder;
     }
 
-    public FeatureBuilder addFeature(final int line, final QName qname) {
+    public FeatureBuilder addFeature(final int line, final QName qname, final SchemaPath path) {
         Builder parent = getActualNode();
         if (!(parent.equals(this))) {
             throw new YangParseException(name, line, "feature can be defined only in module or submodule");
         }
 
-        final FeatureBuilder builder = new FeatureBuilder(name, line, qname);
+        final FeatureBuilder builder = new FeatureBuilder(name, line, qname, path);
         builder.setParent(parent);
 
         String featureName = qname.getLocalName();
@@ -593,8 +700,8 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         return builder;
     }
 
-    public ChoiceBuilder addChoice(final int line, final QName qname) {
-        final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname);
+    public ChoiceBuilder addChoice(final int line, final QName qname, final SchemaPath path) {
+        final ChoiceBuilder builder = new ChoiceBuilder(name, line, qname, path);
 
         Builder parent = getActualNode();
         builder.setParent(parent);
@@ -603,13 +710,13 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         return builder;
     }
 
-    public ChoiceCaseBuilder addCase(final int line, final QName qname) {
+    public ChoiceCaseBuilder addCase(final int line, final QName qname, final SchemaPath path) {
         Builder parent = getActualNode();
         if (parent == null || parent.equals(this)) {
             throw new YangParseException(name, line, "'case' parent not found");
         }
 
-        final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname);
+        final ChoiceCaseBuilder builder = new ChoiceCaseBuilder(name, line, qname, path);
         builder.setParent(parent);
 
         if (parent instanceof ChoiceBuilder) {
@@ -634,30 +741,25 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
     }
 
     @Override
-    public void addTypedef(TypeDefinitionBuilder typedefBuilder) {
+    public void addTypedef(final TypeDefinitionBuilder typedefBuilder) {
         String nodeName = typedefBuilder.getQName().getLocalName();
-        for (TypeDefinitionBuilder tdb : addedTypedefs) {
+        for (TypeDefinitionBuilder tdb : getTypeDefinitionBuilders()) {
             if (tdb.getQName().getLocalName().equals(nodeName)) {
                 raiseYangParserException("", "typedef", nodeName, typedefBuilder.getLine(), tdb.getLine());
             }
         }
-        addedTypedefs.add(typedefBuilder);
+        super.addTypedef(typedefBuilder);
     }
 
-    public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname) {
-        final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname);
+    public TypeDefinitionBuilderImpl addTypedef(final int line, final QName qname, final SchemaPath path) {
+        final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(name, line, qname, path);
 
         Builder parent = getActualNode();
         builder.setParent(parent);
 
         String typedefName = qname.getLocalName();
         if (parent.equals(this)) {
-            for (TypeDefinitionBuilder tdb : addedTypedefs) {
-                if (tdb.getQName().getLocalName().equals(typedefName)) {
-                    raiseYangParserException("", "typedef", typedefName, line, tdb.getLine());
-                }
-            }
-            addedTypedefs.add(builder);
+            addTypedef(builder);
         } else {
             if (parent instanceof DataNodeContainerBuilder) {
                 DataNodeContainerBuilder parentNode = (DataNodeContainerBuilder) parent;
@@ -692,7 +794,7 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         ((TypeAwareBuilder) parent).setType(type);
     }
 
-    public UnionTypeBuilder addUnionType(final int line, final URI namespace, final Date revision) {
+    public UnionTypeBuilder addUnionType(final int line, final QNameModule module) {
         final Builder parent = getActualNode();
         if (parent == null) {
             throw new YangParseException(name, line, "Unresolved parent of union type");
@@ -732,11 +834,11 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
 
         final DeviationBuilder builder = new DeviationBuilder(name, line, targetPath);
         builder.setParent(parent);
-        addedDeviations.add(builder);
+        deviationBuilders.add(builder);
         return builder;
     }
 
-    public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line) {
+    public IdentitySchemaNodeBuilder addIdentity(final QName qname, final int line, final SchemaPath path) {
         Builder parent = getActualNode();
         if (!(parent.equals(this))) {
             throw new YangParseException(name, line, "identity can be defined only in module or submodule");
@@ -748,7 +850,7 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
             }
         }
 
-        final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(name, line, qname);
+        final IdentitySchemaNodeBuilder builder = new IdentitySchemaNodeBuilder(name, line, qname, path);
         builder.setParent(parent);
         addedIdentities.add(builder);
         return builder;
@@ -760,9 +862,9 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         allUnknownNodes.add(builder);
     }
 
-    public UnknownSchemaNodeBuilder addUnknownSchemaNode(final int line, final QName qname) {
+    public UnknownSchemaNodeBuilderImpl addUnknownSchemaNode(final int line, final QName qname, final SchemaPath path) {
         final Builder parent = getActualNode();
-        final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(name, line, qname);
+        final UnknownSchemaNodeBuilderImpl builder = new UnknownSchemaNodeBuilderImpl(name, line, qname, path);
         builder.setParent(parent);
         allUnknownNodes.add(builder);
 
@@ -770,11 +872,11 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
             addedUnknownNodes.add(builder);
         } else {
             if (parent instanceof SchemaNodeBuilder) {
-                ((SchemaNodeBuilder) parent).addUnknownNodeBuilder(builder);
+                parent.addUnknownNodeBuilder(builder);
             } else if (parent instanceof DataNodeContainerBuilder) {
-                ((DataNodeContainerBuilder) parent).addUnknownNodeBuilder(builder);
-            } else if (parent instanceof RefineHolder) {
-                ((RefineHolder) parent).addUnknownNodeBuilder(builder);
+                parent.addUnknownNodeBuilder(builder);
+            } else if (parent instanceof RefineHolderImpl) {
+                parent.addUnknownNodeBuilder(builder);
             } else {
                 throw new YangParseException(name, line, "Unresolved parent of unknown node '" + qname.getLocalName()
                         + "'");
@@ -784,11 +886,19 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         return builder;
     }
 
-    public Set<RpcDefinitionBuilder> getRpcs() {
+    public Set<RpcDefinition> getRpcs() {
+        return rpcs;
+    }
+
+    public Set<RpcDefinitionBuilder> getAddedRpcs() {
         return addedRpcs;
     }
 
-    public Set<NotificationBuilder> getNotifications() {
+    public Set<NotificationDefinition> getNotifications() {
+        return notifications;
+    }
+
+    public Set<NotificationBuilder> getAddedNotifications() {
         return addedNotifications;
     }
 
@@ -797,345 +907,14 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         return "module " + name;
     }
 
-    private final class ModuleImpl implements Module {
-        private URI namespace;
-        private final String name;
-        private Date revision;
-        private String prefix;
-        private String yangVersion;
-        private String description;
-        private String reference;
-        private String organization;
-        private String contact;
-        private Set<ModuleImport> imports = Collections.emptySet();
-        private Set<FeatureDefinition> features = Collections.emptySet();
-        private Set<TypeDefinition<?>> typeDefinitions = Collections.emptySet();
-        private Set<NotificationDefinition> notifications = Collections.emptySet();
-        private Set<AugmentationSchema> augmentations = Collections.emptySet();
-        private Set<RpcDefinition> rpcs = Collections.emptySet();
-        private Set<Deviation> deviations = Collections.emptySet();
-        private Map<QName, DataSchemaNode> childNodes = Collections.emptyMap();
-        private Set<GroupingDefinition> groupings = Collections.emptySet();
-        private Set<UsesNode> uses = Collections.emptySet();
-        private List<ExtensionDefinition> extensionNodes = Collections.emptyList();
-        private Set<IdentitySchemaNode> identities = Collections.emptySet();
-        private List<UnknownSchemaNode> unknownNodes = Collections.emptyList();
-
-        private ModuleImpl(String name) {
-            this.name = name;
-        }
-
-        @Override
-        public URI getNamespace() {
-            return namespace;
-        }
-
-        private void setNamespace(URI namespace) {
-            this.namespace = namespace;
-        }
-
-        @Override
-        public String getName() {
-            return name;
-        }
-
-        @Override
-        public Date getRevision() {
-            return revision;
-        }
-
-        private void setRevision(Date revision) {
-            this.revision = revision;
-        }
-
-        @Override
-        public String getPrefix() {
-            return prefix;
-        }
-
-        private void setPrefix(String prefix) {
-            this.prefix = prefix;
-        }
-
-        @Override
-        public String getYangVersion() {
-            return yangVersion;
-        }
-
-        private void setYangVersion(String yangVersion) {
-            this.yangVersion = yangVersion;
-        }
-
-        @Override
-        public String getDescription() {
-            return description;
-        }
-
-        private void setDescription(String description) {
-            this.description = description;
-        }
-
-        @Override
-        public String getReference() {
-            return reference;
-        }
-
-        private void setReference(String reference) {
-            this.reference = reference;
-        }
-
-        @Override
-        public String getOrganization() {
-            return organization;
-        }
-
-        private void setOrganization(String organization) {
-            this.organization = organization;
-        }
-
-        @Override
-        public String getContact() {
-            return contact;
-        }
-
-        private void setContact(String contact) {
-            this.contact = contact;
-        }
-
-        @Override
-        public Set<ModuleImport> getImports() {
-            return imports;
-        }
-
-        private void setImports(Set<ModuleImport> imports) {
-            if (imports != null) {
-                this.imports = imports;
-            }
-        }
-
-        @Override
-        public Set<FeatureDefinition> getFeatures() {
-            return features;
-        }
-
-        private void setFeatures(Set<FeatureDefinition> features) {
-            if (features != null) {
-                this.features = features;
-            }
-        }
-
-        @Override
-        public Set<TypeDefinition<?>> getTypeDefinitions() {
-            return typeDefinitions;
-        }
-
-        private void setTypeDefinitions(Set<TypeDefinition<?>> typeDefinitions) {
-            if (typeDefinitions != null) {
-                this.typeDefinitions = typeDefinitions;
-            }
-        }
-
-        @Override
-        public Set<NotificationDefinition> getNotifications() {
-            return notifications;
-        }
-
-        private void setNotifications(Set<NotificationDefinition> notifications) {
-            if (notifications != null) {
-                this.notifications = notifications;
-            }
-        }
-
-        @Override
-        public Set<AugmentationSchema> getAugmentations() {
-            return augmentations;
-        }
-
-        private void setAugmentations(Set<AugmentationSchema> augmentations) {
-            if (augmentations != null) {
-                this.augmentations = augmentations;
-            }
-        }
-
-        @Override
-        public Set<RpcDefinition> getRpcs() {
-            return rpcs;
-        }
-
-        private void setRpcs(Set<RpcDefinition> rpcs) {
-            if (rpcs != null) {
-                this.rpcs = rpcs;
-            }
-        }
-
-        @Override
-        public Set<Deviation> getDeviations() {
-            return deviations;
-        }
-
-        private void setDeviations(Set<Deviation> deviations) {
-            if (deviations != null) {
-                this.deviations = deviations;
-            }
-        }
-
-        @Override
-        public Set<DataSchemaNode> getChildNodes() {
-            return new LinkedHashSet<DataSchemaNode>(childNodes.values());
-        }
-
-        private void setChildNodes(Map<QName, DataSchemaNode> childNodes) {
-            if (childNodes != null) {
-                this.childNodes = childNodes;
-            }
-        }
-
-        @Override
-        public Set<GroupingDefinition> getGroupings() {
-            return groupings;
-        }
-
-        private void setGroupings(Set<GroupingDefinition> groupings) {
-            if (groupings != null) {
-                this.groupings = groupings;
-            }
-        }
-
-        @Override
-        public Set<UsesNode> getUses() {
-            return uses;
-        }
-
-        private void setUses(Set<UsesNode> uses) {
-            if (uses != null) {
-                this.uses = uses;
-            }
-        }
-
-        @Override
-        public List<ExtensionDefinition> getExtensionSchemaNodes() {
-            return extensionNodes;
-        }
-
-        private void setExtensionSchemaNodes(final List<ExtensionDefinition> extensionNodes) {
-            if (extensionNodes != null) {
-                this.extensionNodes = extensionNodes;
-            }
-        }
-
-        @Override
-        public Set<IdentitySchemaNode> getIdentities() {
-            return identities;
-        }
-
-        private void setIdentities(final Set<IdentitySchemaNode> identities) {
-            if (identities != null) {
-                this.identities = identities;
-            }
-        }
-
-        @Override
-        public List<UnknownSchemaNode> getUnknownSchemaNodes() {
-            return unknownNodes;
-        }
-
-        private void setUnknownSchemaNodes(final List<UnknownSchemaNode> unknownNodes) {
-            if (unknownNodes != null) {
-                this.unknownNodes = unknownNodes;
-            }
-        }
-
-        @Override
-        public DataSchemaNode getDataChildByName(QName name) {
-            return childNodes.get(name);
-        }
-
-        @Override
-        public DataSchemaNode getDataChildByName(String name) {
-            DataSchemaNode result = null;
-            for (Map.Entry<QName, DataSchemaNode> entry : childNodes.entrySet()) {
-                if (entry.getKey().getLocalName().equals(name)) {
-                    result = entry.getValue();
-                    break;
-                }
-            }
-            return result;
-        }
-
-        @Override
-        public int hashCode() {
-            final int prime = 31;
-            int result = 1;
-            result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
-            result = prime * result + ((name == null) ? 0 : name.hashCode());
-            result = prime * result + ((revision == null) ? 0 : revision.hashCode());
-            result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
-            result = prime * result + ((yangVersion == null) ? 0 : yangVersion.hashCode());
-            return result;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            if (this == obj) {
-                return true;
-            }
-            if (obj == null) {
-                return false;
-            }
-            if (getClass() != obj.getClass()) {
-                return false;
-            }
-            ModuleImpl other = (ModuleImpl) obj;
-            if (namespace == null) {
-                if (other.namespace != null) {
-                    return false;
-                }
-            } else if (!namespace.equals(other.namespace)) {
-                return false;
-            }
-            if (name == null) {
-                if (other.name != null) {
-                    return false;
-                }
-            } else if (!name.equals(other.name)) {
-                return false;
-            }
-            if (revision == null) {
-                if (other.revision != null) {
-                    return false;
-                }
-            } else if (!revision.equals(other.revision)) {
-                return false;
-            }
-            if (prefix == null) {
-                if (other.prefix != null) {
-                    return false;
-                }
-            } else if (!prefix.equals(other.prefix)) {
-                return false;
-            }
-            if (yangVersion == null) {
-                if (other.yangVersion != null) {
-                    return false;
-                }
-            } else if (!yangVersion.equals(other.yangVersion)) {
-                return false;
-            }
-            return true;
+    public void setSource(final ByteSource byteSource) throws IOException {
+        try (InputStream stream = byteSource.openStream()) {
+            setSource(IOUtils.toString(stream));
         }
+    }
 
-        @Override
-        public String toString() {
-            StringBuilder sb = new StringBuilder(ModuleImpl.class.getSimpleName());
-            sb.append("[");
-            sb.append("name=" + name);
-            sb.append(", namespace=" + namespace);
-            sb.append(", revision=" + revision);
-            sb.append(", prefix=" + prefix);
-            sb.append(", yangVersion=" + yangVersion);
-            sb.append("]");
-            return sb.toString();
-        }
+    public void setSource(final String source) {
+        this.source = source;
     }
 
     /**
@@ -1156,6 +935,18 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         }
     }
 
+    public String getYangVersion() {
+        return yangVersion;
+    }
+
+    public String getContact() {
+        return contact;
+    }
+
+    public String getOrganization() {
+        return organization;
+    }
+
     /**
      * Adds child node <code>child</code> to the set of nodes child nodes.
      *
@@ -1177,7 +968,7 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         // notifications, and anyxmls defined within a parent node or at the
         // top level of the module or its submodules share the same
         // identifier namespace.
-        for (DataSchemaNodeBuilder childNode : addedChildNodes) {
+        for (DataSchemaNodeBuilder childNode : getChildNodeBuilders()) {
             if (childNode.getQName().getLocalName().equals(childName)) {
                 raiseYangParserException("'" + child + "'", "node", childName, lineNum, childNode.getLine());
             }
@@ -1192,7 +983,7 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
                 raiseYangParserException("'" + child + "'", "notification", childName, lineNum, notification.getLine());
             }
         }
-        addedChildNodes.add(child);
+        addChildNode(child);
     }
 
     /**
@@ -1239,8 +1030,7 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
     }
 
     private ModuleImport createModuleImport(final String moduleName, final Date revision, final String prefix) {
-        final ModuleImport moduleImport = new ModuleImportImpl(moduleName, revision, prefix);
-        return moduleImport;
+        return new ModuleImportImpl(moduleName, revision, prefix);
     }
 
     private void raiseYangParserException(final String cantAddType, final String type, final String name,
@@ -1255,7 +1045,7 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
 
         String msg = String.format("%s%s with same name '%s' already declared at line %d.", msgPrefix, type, name,
                 duplicateLine);
-        throw new YangParseException(moduleName, currentLine, msg);
+        throw new YangParseException(getModuleName(), currentLine, msg);
     }
 
     @Override
@@ -1263,15 +1053,14 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         final int prime = 31;
         int result = 1;
         result = prime * result + ((name == null) ? 0 : name.hashCode());
-        result = prime * result + ((namespace == null) ? 0 : namespace.hashCode());
-        result = prime * result + ((revision == null) ? 0 : revision.hashCode());
+        result = prime * result + qnameModule.hashCode();
         result = prime * result + ((prefix == null) ? 0 : prefix.hashCode());
 
         return result;
     }
 
     @Override
-    public boolean equals(Object obj) {
+    public boolean equals(final Object obj) {
         if (this == obj) {
             return true;
         }
@@ -1289,11 +1078,7 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         } else if (!name.equals(other.name)) {
             return false;
         }
-        if (namespace == null) {
-            if (other.namespace != null) {
-                return false;
-            }
-        } else if (!namespace.equals(other.namespace)) {
+        if (!qnameModule.equals(other.qnameModule)) {
             return false;
         }
         if (prefix == null) {
@@ -1303,14 +1088,10 @@ public class ModuleBuilder extends AbstractDataNodeContainerBuilder {
         } else if (!prefix.equals(other.prefix)) {
             return false;
         }
-        if (revision == null) {
-            if (other.revision != null) {
-                return false;
-            }
-        } else if (!revision.equals(other.revision)) {
-            return false;
-        }
         return true;
     }
 
+    public List<UnknownSchemaNode> getExtensionInstances() {
+        return unknownNodes;
+    }
 }