Added getParent() method to DataSchemaNode and DataNodeContainer. Fixed Bugs.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / CopyUtils.java
index 4b5d8dc5662cd15dedf5ee043eaa31ea5b586bef..2a3683d1fec7875e702a682d297b62e60eba553d 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.parser.util;
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
@@ -52,16 +53,23 @@ 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) {
         if (old instanceof AnyXmlBuilder) {
@@ -124,7 +132,7 @@ 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()) {
@@ -174,8 +182,8 @@ public class CopyUtils {
         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);
@@ -186,7 +194,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));
         }
@@ -200,7 +207,7 @@ public class CopyUtils {
         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
             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()) {
@@ -215,8 +222,8 @@ public class CopyUtils {
         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);
@@ -233,7 +240,7 @@ public class CopyUtils {
         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());
@@ -265,7 +272,7 @@ public class CopyUtils {
         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());
@@ -278,8 +285,8 @@ public class CopyUtils {
         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);
@@ -289,7 +296,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));
         }
@@ -303,7 +309,7 @@ public class CopyUtils {
         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
             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()) {
@@ -328,40 +334,6 @@ public class CopyUtils {
         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)));
-        }
-
-        return copy;
-    }
-
-    public static GroupingBuilder copyGroupingWithoutDeep(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));
         }
@@ -374,7 +346,6 @@ public class CopyUtils {
         }
         for (UsesNodeBuilder oldUses : old.getUsesNodes()) {
             copy.addUsesNode(copyUses(oldUses, copy));
-
         }
         for (UnknownSchemaNodeBuilder un : old.getUnknownNodeBuilders()) {
             copy.addUnknownNodeBuilder((copy(un, copy, updateQName)));
@@ -387,17 +358,16 @@ public class CopyUtils {
         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) {
@@ -408,12 +378,14 @@ public class CopyUtils {
         } else {
             type = new TypeDefinitionBuilderImpl(old.getModuleName(), newParent.getLine(), newQName);
             type.setParent(newParent);
-            type.setPath(newSchemaPath);
+            // TODO
+            // type.setPath(newSchemaPath);
+            type.setPath(old.getPath());
 
             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()) {
@@ -446,25 +418,48 @@ public class CopyUtils {
 
     static UsesNodeBuilder copyUses(UsesNodeBuilder old, Builder newParent) {
         UsesNodeBuilder copy = new UsesNodeBuilderImpl(newParent.getModuleName(), newParent.getLine(),
-                old.getGroupingName());
+                old.getGroupingPathAsString(), true);
         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.setAugmenting(old.isAugmenting());
+        copy.setParentAugment(old.getParentAugment());
 
-        copy.setTargetChildren(old.getTargetChildren());
-        copy.setTargetTypedefs(old.getTargetTypedefs());
-        copy.setTargetGroupings(old.getTargetGroupings());
-        copy.setTargetUnknownNodes(old.getTargetUnknownNodes());
+        // target child nodes
+        Set<DataSchemaNodeBuilder> newTargetChildren = new HashSet<>();
+        for (DataSchemaNodeBuilder dnb : old.getTargetChildren()) {
+            newTargetChildren.add(copy(dnb, newParent, true));
+        }
+        copy.getTargetChildren().addAll(newTargetChildren);
+
+        // target typedefs
+        Set<TypeDefinitionBuilder> newTargetTypedefs = new HashSet<>();
+        for (TypeDefinitionBuilder tdb : old.getTargetTypedefs()) {
+            newTargetTypedefs.add(copy(tdb, newParent, true));
+        }
+        copy.getTargetTypedefs().addAll(newTargetTypedefs);
+
+        // target groupings
+        Set<GroupingBuilder> newTargetGroupings = new HashSet<>();
+        for (GroupingBuilder gb : old.getTargetGroupings()) {
+            newTargetGroupings.add(copy(gb, newParent, true));
+        }
+        copy.getTargetGroupings().addAll(newTargetGroupings);
+
+        // target unknown nodes
+        Set<UnknownSchemaNodeBuilder> newTargetUnknownNodes = new HashSet<>();
+        for (UnknownSchemaNodeBuilder unb : old.getTargetUnknownNodes()) {
+            newTargetUnknownNodes.add(copy(unb, newParent, true));
+        }
+        copy.getTargetUnknownNodes().addAll(newTargetUnknownNodes);
 
         // add new uses to collection of uses in module
         ModuleBuilder module = ParserUtils.getParentModule(newParent);
-        module.addUsesNode(copy);
+        module.getAllUsesNodes().add(copy);
 
         return copy;
     }
@@ -478,7 +473,7 @@ public class CopyUtils {
         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));
         }
@@ -527,14 +522,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,9 +544,6 @@ 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);
@@ -559,7 +554,7 @@ public class CopyUtils {
         return new DataBean(newQName, newSchemaPath);
     }
 
-    private static class DataBean {
+    private static final class DataBean {
         private QName qname;
         private SchemaPath schemaPath;
 
@@ -569,12 +564,13 @@ public class CopyUtils {
         }
     }
 
-
     /**
      * Create AnyXmlBuilder from given AnyXmlSchemaNode.
      *
      * @param anyxml
+     *            base anyxml
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -593,7 +589,9 @@ public class CopyUtils {
      * Create GroupingBuilder from given GroupingDefinition.
      *
      * @param grouping
+     *            base grouping
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -603,7 +601,9 @@ public class CopyUtils {
     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());
+        for (DataSchemaNode child : grouping.getChildNodes()) {
+            builder.addChildNode(child);
+        }
         builder.setGroupings(grouping.getGroupings());
         builder.setTypedefs(grouping.getTypeDefinitions());
         builder.setUsesnodes(grouping.getUses());
@@ -618,7 +618,9 @@ public class CopyUtils {
      * Create TypeDefinitionBuilder from given ExtendedType.
      *
      * @param typedef
+     *            base typedef
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -633,9 +635,9 @@ public class CopyUtils {
         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.setRanges(typedef.getRangeConstraints());
+        builder.setLengths(typedef.getLengthConstraints());
+        builder.setPatterns(typedef.getPatternConstraints());
         builder.setFractionDigits(typedef.getFractionDigits());
         final TypeDefinition<?> type = typedef.getBaseType();
         builder.setType(type);
@@ -648,7 +650,9 @@ public class CopyUtils {
      * Create UnknownSchemaNodeBuilder from given UnknownSchemaNode.
      *
      * @param unknownNode
+     *            base unknown node
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -669,13 +673,13 @@ public class CopyUtils {
         return builder;
     }
 
-
     /**
      * Create LeafSchemaNodeBuilder from given LeafSchemaNode.
      *
      * @param leaf
      *            leaf from which to create builder
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -699,7 +703,9 @@ public class CopyUtils {
      * Create ContainerSchemaNodeBuilder from given ContainerSchemaNode.
      *
      * @param container
+     *            base container
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -713,10 +719,12 @@ public class CopyUtils {
         convertDataSchemaNode(container, builder);
         builder.setConfiguration(container.isConfiguration());
         builder.setUnknownNodes(container.getUnknownSchemaNodes());
-        builder.setChildNodes(container.getChildNodes());
+        for (DataSchemaNode child : container.getChildNodes()) {
+            builder.addChildNode(child);
+        }
         builder.setGroupings(container.getGroupings());
         builder.setTypedefs(container.getTypeDefinitions());
-        builder.setAugmentations(container.getAvailableAugmentations());
+        builder.getAugmentations().addAll(container.getAvailableAugmentations());
         builder.setUsesnodes(container.getUses());
         builder.setPresence(container.isPresenceContainer());
         return builder;
@@ -726,7 +734,9 @@ public class CopyUtils {
      * Create ListSchemaNodeBuilder from given ListSchemaNode.
      *
      * @param list
+     *            base list
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -739,9 +749,11 @@ public class CopyUtils {
         builder.setConfiguration(list.isConfiguration());
         builder.setUnknownNodes(list.getUnknownSchemaNodes());
         builder.setTypedefs(list.getTypeDefinitions());
-        builder.setChildNodes(list.getChildNodes());
+        for (DataSchemaNode child : list.getChildNodes()) {
+            builder.addChildNode(child);
+        }
         builder.setGroupings(list.getGroupings());
-        builder.setAugmentations(list.getAvailableAugmentations());
+        builder.getAugmentations().addAll(list.getAvailableAugmentations());
         builder.setUsesnodes(list.getUses());
         builder.setUserOrdered(builder.isUserOrdered());
         return builder;
@@ -751,7 +763,9 @@ public class CopyUtils {
      * Create LeafListSchemaNodeBuilder from given LeafListSchemaNode.
      *
      * @param leafList
+     *            base leaf-list
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -774,7 +788,9 @@ public class CopyUtils {
      * Create ChoiceBuilder from given ChoiceNode.
      *
      * @param choice
+     *            base choice
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -791,7 +807,6 @@ public class CopyUtils {
         return builder;
     }
 
-
     /**
      * Set DataSchemaNode arguments to builder object
      *