Improved sorting of augmentations before code generation.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / CopyUtils.java
index 4b5d8dc5662cd15dedf5ee043eaa31ea5b586bef..2b3e29bb7faf95abe89cf257a24835f4da7267f6 100644 (file)
@@ -10,24 +10,9 @@ package org.opendaylight.yangtools.yang.parser.util;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
-import java.util.Set;
-
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
-import org.opendaylight.yangtools.yang.model.api.ConstraintDefinition;
-import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
-import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.MustDefinition;
-import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
 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.util.ExtendedType;
 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.DataSchemaNodeBuilder;
@@ -52,18 +37,25 @@ import org.opendaylight.yangtools.yang.parser.builder.impl.UnionTypeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.impl.UsesNodeBuilderImpl;
 
-public class CopyUtils {
+public final class CopyUtils {
+
+    private CopyUtils() {
+    }
 
     /**
      * Create copy of DataSchemaNodeBuilder with new parent. If updateQName is
      * true, qname of node will be corrected based on new parent.
      *
      * @param old
+     *            builder to copy
      * @param newParent
+     *            new parent
      * @param updateQName
-     * @return
+     *            flag to indicate if qname should be updated based on new
+     *            parent location
+     * @return copy of given builder
      */
-    public static DataSchemaNodeBuilder copy(DataSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
+    public static DataSchemaNodeBuilder copy(final DataSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
         if (old instanceof AnyXmlBuilder) {
             return copy((AnyXmlBuilder) old, newParent, updateQName);
         } else if (old instanceof ChoiceBuilder) {
@@ -84,7 +76,7 @@ public class CopyUtils {
         }
     }
 
-    private static AnyXmlBuilder copy(AnyXmlBuilder old, Builder newParent, boolean updateQName) {
+    private static AnyXmlBuilder copy(final AnyXmlBuilder old, final Builder newParent, final boolean updateQName) {
         DataBean data = getdata(old, newParent, updateQName);
         QName newQName = data.qname;
         SchemaPath newSchemaPath = data.schemaPath;
@@ -92,29 +84,27 @@ public class CopyUtils {
         AnyXmlBuilder copy = new AnyXmlBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
         copyConstraints(copy.getConstraints(), old.getConstraints());
         copy.setParent(newParent);
-        copy.setPath(newSchemaPath);
         copy.setDescription(old.getDescription());
         copy.setReference(old.getReference());
         copy.setStatus(old.getStatus());
         copy.setAugmenting(old.isAugmenting());
         copy.setAddedByUses(old.isAddedByUses());
         copy.setConfiguration(old.isConfiguration());
-        for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
+        for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
         }
 
         return copy;
     }
 
-    private static ChoiceBuilder copy(ChoiceBuilder old, Builder newParent, boolean updateQName) {
+    private static ChoiceBuilder copy(final ChoiceBuilder old, final Builder newParent, final boolean updateQName) {
         DataBean data = getdata(old, newParent, updateQName);
         QName newQName = data.qname;
         SchemaPath newSchemaPath = data.schemaPath;
 
-        ChoiceBuilder copy = new ChoiceBuilder(newParent.getModuleName(), newParent.getLine(), newQName);
+        ChoiceBuilder copy = new ChoiceBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
         copyConstraints(copy.getConstraints(), old.getConstraints());
         copy.setParent(newParent);
-        copy.setPath(newSchemaPath);
         copy.setDescription(old.getDescription());
         copy.setReference(old.getReference());
         copy.setStatus(old.getStatus());
@@ -124,30 +114,28 @@ public class CopyUtils {
         for (ChoiceCaseBuilder childNode : old.getCases()) {
             copy.addCase(copy(childNode, copy, updateQName));
         }
-        for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
+        for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
             copy.addAugmentation(copyAugment(augment, copy));
         }
-        for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
+        for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
         }
 
         return copy;
     }
 
-    private static ChoiceCaseBuilder copy(ChoiceCaseBuilder old, Builder newParent, boolean updateQName) {
+    private static ChoiceCaseBuilder copy(final ChoiceCaseBuilder old, final Builder newParent, final boolean updateQName) {
         DataBean data = getdata(old, newParent, updateQName);
         QName newQName = data.qname;
         SchemaPath newSchemaPath = data.schemaPath;
 
-        ChoiceCaseBuilder copy = new ChoiceCaseBuilder(newParent.getModuleName(), newParent.getLine(), newQName);
+        ChoiceCaseBuilder copy = new ChoiceCaseBuilder(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
         copyConstraints(copy.getConstraints(), old.getConstraints());
         copy.setParent(newParent);
-        copy.setPath(newSchemaPath);
         copy.setDescription(old.getDescription());
         copy.setReference(old.getReference());
         copy.setStatus(old.getStatus());
         copy.setAugmenting(old.isAugmenting());
-        copy.getChildNodes().addAll(old.getChildNodes());
         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
             copy.addChildNode(copy(childNode, copy, updateQName));
         }
@@ -158,27 +146,26 @@ public class CopyUtils {
         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
             copy.addTypedef(copy(tdb, copy, updateQName));
         }
-        for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
+        for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
             copy.addUsesNode(copyUses(oldUses, copy));
         }
-        for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
+        for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
         }
 
         return copy;
     }
 
-    private static ContainerSchemaNodeBuilder copy(ContainerSchemaNodeBuilder old, Builder newParent,
-            boolean updateQName) {
+    private static ContainerSchemaNodeBuilder copy(final ContainerSchemaNodeBuilder old, final Builder newParent,
+            final boolean updateQName) {
         DataBean data = getdata(old, newParent, updateQName);
         QName newQName = data.qname;
         SchemaPath newSchemaPath = data.schemaPath;
 
-        ContainerSchemaNodeBuilder copy = new ContainerSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
-                newQName, newSchemaPath);
+        ContainerSchemaNodeBuilder copy = new ContainerSchemaNodeBuilder(newParent.getModuleName(),
+                newParent.getLine(), newQName, newSchemaPath);
         copyConstraints(copy.getConstraints(), old.getConstraints());
         copy.setParent(newParent);
-        copy.setPath(newSchemaPath);
         copy.setDescription(old.getDescription());
         copy.setReference(old.getReference());
         copy.setStatus(old.getStatus());
@@ -186,7 +173,6 @@ public class CopyUtils {
         copy.setAugmenting(old.isAugmenting());
         copy.setAddedByUses(old.isAddedByUses());
         copy.setConfiguration(old.isConfiguration());
-        copy.setChildNodes(old.getChildNodes());
         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
             copy.addChildNode(copy(childNode, copy, updateQName));
         }
@@ -197,43 +183,42 @@ public class CopyUtils {
         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
             copy.addTypedef(copy(tdb, copy, updateQName));
         }
-        for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
+        for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
             copy.addUsesNode(copyUses(oldUses, copy));
         }
-        for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
+        for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
             copy.addAugmentation(copyAugment(augment, copy));
         }
-        for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
+        for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
         }
 
         return copy;
     }
 
-    private static LeafSchemaNodeBuilder copy(LeafSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
+    private static LeafSchemaNodeBuilder copy(final LeafSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
         DataBean data = getdata(old, newParent, updateQName);
         QName newQName = data.qname;
         SchemaPath newSchemaPath = data.schemaPath;
 
-        LeafSchemaNodeBuilder copy = new LeafSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(), newQName,
-                newSchemaPath);
+        LeafSchemaNodeBuilder copy = new LeafSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
+                newQName, newSchemaPath);
         copyConstraints(copy.getConstraints(), old.getConstraints());
         copy.setParent(newParent);
-        copy.setPath(newSchemaPath);
         copy.setDescription(old.getDescription());
         copy.setReference(old.getReference());
         copy.setStatus(old.getStatus());
         copy.setAugmenting(old.isAugmenting());
         copy.setAddedByUses(old.isAddedByUses());
         copy.setConfiguration(old.isConfiguration());
-        for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
+        for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
         }
 
         if (old.getType() == null) {
             copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
         } else {
-            copy.setType(ParserUtils.createCorrectTypeDefinition(copy.getPath(), old.getType()));
+            copy.setType(old.getType());
         }
 
         copy.setDefaultStr(old.getDefaultStr());
@@ -242,7 +227,7 @@ public class CopyUtils {
         return copy;
     }
 
-    public static LeafListSchemaNodeBuilder copy(LeafListSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
+    public static LeafListSchemaNodeBuilder copy(final LeafListSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
         DataBean data = getdata(old, newParent, updateQName);
         QName newQName = data.qname;
         SchemaPath newSchemaPath = data.schemaPath;
@@ -251,21 +236,20 @@ public class CopyUtils {
                 newQName, newSchemaPath);
         copyConstraints(copy.getConstraints(), old.getConstraints());
         copy.setParent(newParent);
-        copy.setPath(newSchemaPath);
         copy.setDescription(old.getDescription());
         copy.setReference(old.getReference());
         copy.setStatus(old.getStatus());
         copy.setAugmenting(old.isAugmenting());
         copy.setAddedByUses(old.isAddedByUses());
         copy.setConfiguration(old.isConfiguration());
-        for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
+        for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
         }
 
         if (old.getType() == null) {
             copy.setTypedef(copy(old.getTypedef(), copy, updateQName));
         } else {
-            copy.setType(ParserUtils.createCorrectTypeDefinition(copy.getPath(), old.getType()));
+            copy.setType(old.getType());
         }
 
         copy.setUserOrdered(old.isUserOrdered());
@@ -273,23 +257,21 @@ public class CopyUtils {
         return copy;
     }
 
-    private static ListSchemaNodeBuilder copy(ListSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
+    private static ListSchemaNodeBuilder copy(final ListSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
         DataBean data = getdata(old, newParent, updateQName);
         QName newQName = data.qname;
         SchemaPath newSchemaPath = data.schemaPath;
 
-        ListSchemaNodeBuilder copy = new ListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(), newQName,
-                newSchemaPath);
+        ListSchemaNodeBuilder copy = new ListSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
+                newQName, newSchemaPath);
         copyConstraints(copy.getConstraints(), old.getConstraints());
         copy.setParent(newParent);
-        copy.setPath(newSchemaPath);
         copy.setDescription(old.getDescription());
         copy.setReference(old.getReference());
         copy.setStatus(old.getStatus());
         copy.setAugmenting(old.isAugmenting());
         copy.setAddedByUses(old.isAddedByUses());
         copy.setConfiguration(old.isConfiguration());
-        copy.setChildNodes(old.getChildNodes());
         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
             copy.addChildNode(copy(childNode, copy, updateQName));
         }
@@ -300,68 +282,33 @@ public class CopyUtils {
         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
             copy.addTypedef(copy(tdb, copy, updateQName));
         }
-        for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
+        for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
             copy.addUsesNode(copyUses(oldUses, copy));
         }
-        for (AugmentationSchemaBuilder augment : old.getAugmentations()) {
+        for (AugmentationSchemaBuilder augment : old.getAugmentationBuilders()) {
             copy.addAugmentation(copyAugment(augment, copy));
         }
-        for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
+        for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
         }
 
         copy.setUserOrdered(old.isUserOrdered());
-        copy.setKeyDefinition(old.getKeyDefinition());
-
-        return copy;
-    }
-
-    public static GroupingBuilder copy(GroupingBuilder old, Builder newParent, boolean updateQName) {
-        DataBean data = getdata(old, newParent, updateQName);
-        QName newQName = data.qname;
-        SchemaPath newSchemaPath = data.schemaPath;
-
-        GroupingBuilderImpl copy = new GroupingBuilderImpl(newParent.getModuleName(), newParent.getLine(), newQName);
-        copy.setParent(newParent);
-        copy.setPath(newSchemaPath);
-        copy.setDescription(old.getDescription());
-        copy.setReference(old.getReference());
-        copy.setStatus(old.getStatus());
-        copy.setAddedByUses(old.isAddedByUses());
-        copy.setChildNodes(old.getChildNodes());
-        for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
-            copy.addChildNode(copy(childNode, copy, updateQName));
-        }
-        copy.getGroupings().addAll(old.getGroupings());
-        for (GroupingBuilder grouping : old.getGroupingBuilders()) {
-            copy.addGrouping(copy(grouping, copy, updateQName));
-        }
-        for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
-            copy.addTypedef(copy(tdb, copy, updateQName));
-        }
-        for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
-            copy.addUsesNode(copyUses(oldUses, copy));
-        }
-        for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
-            copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
-        }
+        copy.setKeys(old.getKeys());
 
         return copy;
     }
 
-    public static GroupingBuilder copyGroupingWithoutDeep(GroupingBuilder old, Builder newParent, boolean updateQName) {
+    public static GroupingBuilder copy(final GroupingBuilder old, final Builder newParent, final boolean updateQName) {
         DataBean data = getdata(old, newParent, updateQName);
         QName newQName = data.qname;
         SchemaPath newSchemaPath = data.schemaPath;
 
-        GroupingBuilderImpl copy = new GroupingBuilderImpl(newParent.getModuleName(), newParent.getLine(), newQName);
+        GroupingBuilderImpl copy = new GroupingBuilderImpl(newParent.getModuleName(), newParent.getLine(), newQName, newSchemaPath);
         copy.setParent(newParent);
-        copy.setPath(newSchemaPath);
         copy.setDescription(old.getDescription());
         copy.setReference(old.getReference());
         copy.setStatus(old.getStatus());
         copy.setAddedByUses(old.isAddedByUses());
-        copy.setChildNodes(old.getChildNodes());
         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
             copy.addChildNode(copy(childNode, copy, updateQName));
         }
@@ -372,51 +319,47 @@ public class CopyUtils {
         for (TypeDefinitionBuilder tdb : old.getTypeDefinitionBuilders()) {
             copy.addTypedef(copy(tdb, copy, updateQName));
         }
-        for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
+        for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
             copy.addUsesNode(copyUses(oldUses, copy));
-
         }
-        for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
+        for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
         }
 
         return copy;
     }
 
-    public static TypeDefinitionBuilder copy(TypeDefinitionBuilder old, Builder newParent, boolean updateQName) {
+    public static TypeDefinitionBuilder copy(final TypeDefinitionBuilder old, final Builder newParent, final boolean updateQName) {
         DataBean data = getdata(old, newParent, updateQName);
         QName newQName = data.qname;
         SchemaPath newSchemaPath = data.schemaPath;
-        TypeDefinitionBuilder type = null;
+        TypeDefinitionBuilder type;
 
         if (old instanceof UnionTypeBuilder) {
-            UnionTypeBuilder oldUnion = (UnionTypeBuilder)old;
+            UnionTypeBuilder oldUnion = (UnionTypeBuilder) old;
             type = new UnionTypeBuilder(newParent.getModuleName(), newParent.getLine());
             type.setParent(newParent);
-            type.setPath(newSchemaPath);
-            for(TypeDefinition<?> td : oldUnion.getTypes()) {
-                type.setType(ParserUtils.createCorrectTypeDefinition(type.getPath(), td));
+            for (TypeDefinition<?> td : oldUnion.getTypes()) {
+                type.setType(td);
             }
-            for(TypeDefinitionBuilder tdb : oldUnion.getTypedefs()) {
+            for (TypeDefinitionBuilder tdb : oldUnion.getTypedefs()) {
                 type.setTypedef(copy(tdb, type, updateQName));
             }
         } else if (old instanceof IdentityrefTypeBuilder) {
             type = new IdentityrefTypeBuilder(newParent.getModuleName(), newParent.getLine(),
                     ((IdentityrefTypeBuilder) old).getBaseString(), newSchemaPath);
             type.setParent(newParent);
-            type.setPath(newSchemaPath);
         } else {
-            type = new TypeDefinitionBuilderImpl(old.getModuleName(), newParent.getLine(), newQName);
+            type = new TypeDefinitionBuilderImpl(old.getModuleName(), newParent.getLine(), newQName, old.getPath());
             type.setParent(newParent);
-            type.setPath(newSchemaPath);
 
             if (old.getType() == null) {
                 type.setTypedef(copy(old.getTypedef(), type, updateQName));
             } else {
-                type.setType(ParserUtils.createCorrectTypeDefinition(type.getPath(), old.getType()));
+                type.setType(old.getType());
             }
 
-            for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
+            for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
                 type.addUnknownNodeBuilder((copy(un, type, updateQName)));
             }
 
@@ -435,7 +378,7 @@ public class CopyUtils {
         return type;
     }
 
-    private static ConstraintsBuilder copyConstraints(ConstraintsBuilder newConstraints, ConstraintsBuilder old) {
+    private static ConstraintsBuilder copyConstraints(final ConstraintsBuilder newConstraints, final ConstraintsBuilder old) {
         newConstraints.getMustDefinitions().addAll(old.getMustDefinitions());
         newConstraints.addWhenCondition(old.getWhenCondition());
         newConstraints.setMandatory(old.isMandatory());
@@ -444,76 +387,68 @@ public class CopyUtils {
         return newConstraints;
     }
 
-    static UsesNodeBuilder copyUses(UsesNodeBuilder old, Builder newParent) {
+    private static UsesNodeBuilder copyUses(final UsesNodeBuilder old, final Builder newParent) {
         UsesNodeBuilder copy = new UsesNodeBuilderImpl(newParent.getModuleName(), newParent.getLine(),
-                old.getGroupingName());
+                old.getGroupingPathAsString());
         copy.setParent(newParent);
-        copy.setGroupingPath(old.getGroupingPath());
-        // TODO grouping vs grouping path?
+        copy.setGroupingDefinition(old.getGroupingDefinition());
         copy.setGrouping(old.getGroupingBuilder());
-        copy.setAugmenting(old.isAugmenting());
         copy.setAddedByUses(old.isAddedByUses());
         copy.getAugmentations().addAll(old.getAugmentations());
         copy.getRefineNodes().addAll(old.getRefineNodes());
         copy.getRefines().addAll(old.getRefines());
-
-        copy.setTargetChildren(old.getTargetChildren());
-        copy.setTargetTypedefs(old.getTargetTypedefs());
-        copy.setTargetGroupings(old.getTargetGroupings());
-        copy.setTargetUnknownNodes(old.getTargetUnknownNodes());
-
-        // add new uses to collection of uses in module
-        ModuleBuilder module = ParserUtils.getParentModule(newParent);
-        module.addUsesNode(copy);
-
+        copy.setAugmenting(old.isAugmenting());
         return copy;
     }
 
-    private static AugmentationSchemaBuilder copyAugment(AugmentationSchemaBuilder old, Builder newParent) {
+    private static AugmentationSchemaBuilder copyAugment(final AugmentationSchemaBuilder old, final Builder newParent) {
         AugmentationSchemaBuilderImpl copy = new AugmentationSchemaBuilderImpl(newParent.getModuleName(),
-                newParent.getLine(), old.getTargetPathAsString());
+                newParent.getLine(), old.getTargetPathAsString(), old.getOrder());
         copy.setParent(newParent);
-
+        copy.setCopyOf(old);
         copy.setDescription(old.getDescription());
         copy.setReference(old.getReference());
         copy.setStatus(old.getStatus());
         copy.addWhenCondition(old.getWhenCondition());
-        copy.setChildNodes(old.getChildNodes());
+        copy.setTargetNodeSchemaPath(old.getTargetNodeSchemaPath());
         for (DataSchemaNodeBuilder childNode : old.getChildNodeBuilders()) {
             copy.addChildNode(copy(childNode, copy, false));
         }
-        for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
+        for (UsesNodeBuilder oldUses : old.getUsesNodeBuilders()) {
             copy.addUsesNode(copyUses(oldUses, copy));
         }
-        for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
+        for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
             copy.addUnknownNodeBuilder((copy(un, copy, false)));
         }
 
         return copy;
     }
 
-    static UnknownSchemaNodeBuilder copy(UnknownSchemaNodeBuilder old, Builder newParent, boolean updateQName) {
+    public static UnknownSchemaNodeBuilder copy(final UnknownSchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
         DataBean data = getdata(old, newParent, updateQName);
         QName newQName = data.qname;
         SchemaPath newSchemaPath = data.schemaPath;
 
         UnknownSchemaNodeBuilder c = new UnknownSchemaNodeBuilder(newParent.getModuleName(), newParent.getLine(),
-                newQName);
+                newQName, newSchemaPath);
 
+        c.setNodeType(old.getNodeType());
+        c.setNodeParameter(old.getNodeParameter());
         c.setParent(newParent);
-        c.setPath(newSchemaPath);
         c.setDescription(old.getDescription());
         c.setReference(old.getReference());
         c.setStatus(old.getStatus());
         c.setAddedByUses(old.isAddedByUses());
-        for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
+        for (UnknownSchemaNodeBuilder un : old.getUnknownNodes()) {
             c.addUnknownNodeBuilder((copy(un, c, updateQName)));
         }
+        c.setExtensionBuilder(old.getExtensionBuilder());
+        c.setExtensionDefinition(old.getExtensionDefinition());
 
         return c;
     }
 
-    private static DataBean getdata(SchemaNodeBuilder old, Builder newParent, boolean updateQName) {
+    private static DataBean getdata(final SchemaNodeBuilder old, final Builder newParent, final boolean updateQName) {
         List<QName> newPath = null;
         QName newQName = null;
         if (newParent instanceof ModuleBuilder) {
@@ -527,14 +462,17 @@ public class CopyUtils {
                 newPath = Collections.singletonList(newQName);
             }
         } else if (newParent instanceof AugmentationSchemaBuilder) {
+            AugmentationSchemaBuilder augment = (AugmentationSchemaBuilder) newParent;
             ModuleBuilder parent = ParserUtils.getParentModule(newParent);
             if (updateQName) {
                 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
                         .getLocalName());
-                newPath = Collections.singletonList(newQName);
+                newPath = new ArrayList<>(augment.getTargetNodeSchemaPath().getPath());
+                newPath.add(newQName);
             } else {
                 newQName = old.getQName();
-                newPath = Collections.singletonList(newQName);
+                newPath = new ArrayList<>(augment.getTargetNodeSchemaPath().getPath());
+                newPath.add(newQName);
             }
 
         } else if (newParent instanceof SchemaNodeBuilder) {
@@ -546,293 +484,24 @@ public class CopyUtils {
                 newPath = new ArrayList<>(parent.getPath().getPath());
                 newPath.add(newQName);
             } else {
-                if(old == null) {
-                    System.out.println();
-                }
                 newQName = old.getQName();
                 newPath = new ArrayList<>(parent.getPath().getPath());
                 newPath.add(newQName);
             }
         }
 
-        SchemaPath newSchemaPath = new SchemaPath(newPath, true);
+        SchemaPath newSchemaPath = SchemaPath.create(newPath, true);
         return new DataBean(newQName, newSchemaPath);
     }
 
-    private static class DataBean {
-        private QName qname;
-        private SchemaPath schemaPath;
+    private static final class DataBean {
+        private final QName qname;
+        private final SchemaPath schemaPath;
 
-        private DataBean(QName qname, SchemaPath schemaPath) {
+        private DataBean(final QName qname, final SchemaPath schemaPath) {
             this.qname = qname;
             this.schemaPath = schemaPath;
         }
     }
 
-
-    /**
-     * Create AnyXmlBuilder from given AnyXmlSchemaNode.
-     *
-     * @param anyxml
-     * @param qname
-     * @param moduleName
-     *            current module name
-     * @param line
-     *            current line in module
-     * @return anyxml builder based on given anyxml node
-     */
-    public static AnyXmlBuilder createAnyXml(AnyXmlSchemaNode anyxml, QName qname, String moduleName, int line) {
-        final AnyXmlBuilder builder = new AnyXmlBuilder(moduleName, line, qname, anyxml.getPath());
-        convertDataSchemaNode(anyxml, builder);
-        builder.setConfiguration(anyxml.isConfiguration());
-        builder.setUnknownNodes(anyxml.getUnknownSchemaNodes());
-        return builder;
-    }
-
-    /**
-     * Create GroupingBuilder from given GroupingDefinition.
-     *
-     * @param grouping
-     * @param qname
-     * @param moduleName
-     *            current module name
-     * @param line
-     *            current line in module
-     * @return grouping builder based on given grouping node
-     */
-    public static GroupingBuilder createGrouping(GroupingDefinition grouping, QName qname, String moduleName, int line) {
-        final GroupingBuilderImpl builder = new GroupingBuilderImpl(moduleName, line, qname);
-        builder.setPath(grouping.getPath());
-        builder.setChildNodes(grouping.getChildNodes());
-        builder.setGroupings(grouping.getGroupings());
-        builder.setTypedefs(grouping.getTypeDefinitions());
-        builder.setUsesnodes(grouping.getUses());
-        builder.setUnknownNodes(grouping.getUnknownSchemaNodes());
-        builder.setDescription(grouping.getDescription());
-        builder.setReference(grouping.getReference());
-        builder.setStatus(grouping.getStatus());
-        return builder;
-    }
-
-    /**
-     * Create TypeDefinitionBuilder from given ExtendedType.
-     *
-     * @param typedef
-     * @param qname
-     * @param moduleName
-     *            current module name
-     * @param line
-     *            current line in module
-     * @return typedef builder based on given typedef node
-     */
-    public static TypeDefinitionBuilder createTypedef(ExtendedType typedef, QName qname, String moduleName, int line) {
-        final TypeDefinitionBuilderImpl builder = new TypeDefinitionBuilderImpl(moduleName, line, qname);
-        builder.setPath(typedef.getPath());
-        builder.setDefaultValue(typedef.getDefaultValue());
-        builder.setUnits(typedef.getUnits());
-        builder.setDescription(typedef.getDescription());
-        builder.setReference(typedef.getReference());
-        builder.setStatus(typedef.getStatus());
-        builder.setRanges(typedef.getRanges());
-        builder.setLengths(typedef.getLengths());
-        builder.setPatterns(typedef.getPatterns());
-        builder.setFractionDigits(typedef.getFractionDigits());
-        final TypeDefinition<?> type = typedef.getBaseType();
-        builder.setType(type);
-        builder.setUnits(typedef.getUnits());
-        builder.setUnknownNodes(typedef.getUnknownSchemaNodes());
-        return builder;
-    }
-
-    /**
-     * Create UnknownSchemaNodeBuilder from given UnknownSchemaNode.
-     *
-     * @param unknownNode
-     * @param qname
-     * @param moduleName
-     *            current module name
-     * @param line
-     *            current line in module
-     * @return unknown node builder based on given unknown node
-     */
-    public static UnknownSchemaNodeBuilder createUnknownSchemaNode(UnknownSchemaNode unknownNode, QName qname,
-            String moduleName, int line) {
-        final UnknownSchemaNodeBuilder builder = new UnknownSchemaNodeBuilder(moduleName, line, qname);
-        builder.setPath(unknownNode.getPath());
-        builder.setUnknownNodes(unknownNode.getUnknownSchemaNodes());
-        builder.setDescription(unknownNode.getDescription());
-        builder.setReference(unknownNode.getReference());
-        builder.setStatus(unknownNode.getStatus());
-        builder.setAddedByUses(unknownNode.isAddedByUses());
-        builder.setNodeType(unknownNode.getNodeType());
-        builder.setNodeParameter(unknownNode.getNodeParameter());
-        return builder;
-    }
-
-
-    /**
-     * Create LeafSchemaNodeBuilder from given LeafSchemaNode.
-     *
-     * @param leaf
-     *            leaf from which to create builder
-     * @param qname
-     * @param moduleName
-     *            current module name
-     * @param line
-     *            line in module
-     * @return leaf builder based on given leaf node
-     */
-    public static LeafSchemaNodeBuilder createLeafBuilder(LeafSchemaNode leaf, QName qname, String moduleName, int line) {
-        final LeafSchemaNodeBuilder builder = new LeafSchemaNodeBuilder(moduleName, line, qname, leaf.getPath());
-        convertDataSchemaNode(leaf, builder);
-        builder.setConfiguration(leaf.isConfiguration());
-        final TypeDefinition<?> type = leaf.getType();
-        builder.setType(type);
-        builder.setPath(leaf.getPath());
-        builder.setUnknownNodes(leaf.getUnknownSchemaNodes());
-        builder.setDefaultStr(leaf.getDefault());
-        builder.setUnits(leaf.getUnits());
-        return builder;
-    }
-
-    /**
-     * Create ContainerSchemaNodeBuilder from given ContainerSchemaNode.
-     *
-     * @param container
-     * @param qname
-     * @param moduleName
-     *            current module name
-     * @param line
-     *            current line in module
-     * @return container builder based on given container node
-     */
-    public static ContainerSchemaNodeBuilder createContainer(ContainerSchemaNode container, QName qname,
-            String moduleName, int line) {
-        final ContainerSchemaNodeBuilder builder = new ContainerSchemaNodeBuilder(moduleName, line, qname,
-                container.getPath());
-        convertDataSchemaNode(container, builder);
-        builder.setConfiguration(container.isConfiguration());
-        builder.setUnknownNodes(container.getUnknownSchemaNodes());
-        builder.setChildNodes(container.getChildNodes());
-        builder.setGroupings(container.getGroupings());
-        builder.setTypedefs(container.getTypeDefinitions());
-        builder.setAugmentations(container.getAvailableAugmentations());
-        builder.setUsesnodes(container.getUses());
-        builder.setPresence(container.isPresenceContainer());
-        return builder;
-    }
-
-    /**
-     * Create ListSchemaNodeBuilder from given ListSchemaNode.
-     *
-     * @param list
-     * @param qname
-     * @param moduleName
-     *            current module name
-     * @param line
-     *            current line in module
-     * @return list builder based on given list node
-     */
-    public static ListSchemaNodeBuilder createList(ListSchemaNode list, QName qname, String moduleName, int line) {
-        ListSchemaNodeBuilder builder = new ListSchemaNodeBuilder(moduleName, line, qname, list.getPath());
-        convertDataSchemaNode(list, builder);
-        builder.setConfiguration(list.isConfiguration());
-        builder.setUnknownNodes(list.getUnknownSchemaNodes());
-        builder.setTypedefs(list.getTypeDefinitions());
-        builder.setChildNodes(list.getChildNodes());
-        builder.setGroupings(list.getGroupings());
-        builder.setAugmentations(list.getAvailableAugmentations());
-        builder.setUsesnodes(list.getUses());
-        builder.setUserOrdered(builder.isUserOrdered());
-        return builder;
-    }
-
-    /**
-     * Create LeafListSchemaNodeBuilder from given LeafListSchemaNode.
-     *
-     * @param leafList
-     * @param qname
-     * @param moduleName
-     *            current module name
-     * @param line
-     *            current line in module
-     * @return leaf-list builder based on given leaf-list node
-     */
-    public static LeafListSchemaNodeBuilder createLeafList(LeafListSchemaNode leafList, QName qname, String moduleName,
-            int line) {
-        final LeafListSchemaNodeBuilder builder = new LeafListSchemaNodeBuilder(moduleName, line, qname,
-                leafList.getPath());
-        convertDataSchemaNode(leafList, builder);
-        builder.setConfiguration(leafList.isConfiguration());
-        builder.setType(leafList.getType());
-        builder.setUnknownNodes(leafList.getUnknownSchemaNodes());
-        builder.setUserOrdered(leafList.isUserOrdered());
-        return builder;
-    }
-
-    /**
-     * Create ChoiceBuilder from given ChoiceNode.
-     *
-     * @param choice
-     * @param qname
-     * @param moduleName
-     *            current module name
-     * @param line
-     *            current line in module
-     * @return choice builder based on given choice node
-     */
-    public static ChoiceBuilder createChoice(ChoiceNode choice, QName qname, String moduleName, int line) {
-        final ChoiceBuilder builder = new ChoiceBuilder(moduleName, line, qname);
-        convertDataSchemaNode(choice, builder);
-        builder.setConfiguration(choice.isConfiguration());
-        builder.setCases(choice.getCases());
-        builder.setUnknownNodes(choice.getUnknownSchemaNodes());
-        builder.setDefaultCase(choice.getDefaultCase());
-        return builder;
-    }
-
-
-    /**
-     * Set DataSchemaNode arguments to builder object
-     *
-     * @param node
-     *            node from which arguments should be read
-     * @param builder
-     *            builder to which arguments should be set
-     */
-    private static void convertDataSchemaNode(DataSchemaNode node, DataSchemaNodeBuilder builder) {
-        builder.setPath(node.getPath());
-        builder.setDescription(node.getDescription());
-        builder.setReference(node.getReference());
-        builder.setStatus(node.getStatus());
-        builder.setAugmenting(node.isAugmenting());
-        copyConstraintsFromDefinition(node.getConstraints(), builder.getConstraints());
-    }
-
-    /**
-     * Copy constraints from constraints definition to constraints builder.
-     *
-     * @param nodeConstraints
-     *            definition from which constraints will be copied
-     * @param constraints
-     *            builder to which constraints will be added
-     */
-    private static void copyConstraintsFromDefinition(final ConstraintDefinition nodeConstraints,
-            final ConstraintsBuilder constraints) {
-        final RevisionAwareXPath when = nodeConstraints.getWhenCondition();
-        final Set<MustDefinition> must = nodeConstraints.getMustConstraints();
-
-        if (when != null) {
-            constraints.addWhenCondition(when.toString());
-        }
-        if (must != null) {
-            for (MustDefinition md : must) {
-                constraints.addMustDefinition(md);
-            }
-        }
-        constraints.setMandatory(nodeConstraints.isMandatory());
-        constraints.setMinElements(nodeConstraints.getMinElements());
-        constraints.setMaxElements(nodeConstraints.getMaxElements());
-    }
-
 }