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 9e1ccd897e176f97c5ace33bcb5aff214eef6ca7..2a3683d1fec7875e702a682d297b62e60eba553d 100644 (file)
@@ -63,9 +63,13 @@ public final class CopyUtils {
      * 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) {
@@ -128,7 +132,7 @@ public final 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()) {
@@ -190,7 +194,6 @@ public final 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));
         }
@@ -204,7 +207,7 @@ public final 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()) {
@@ -293,7 +296,6 @@ public final 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));
         }
@@ -307,7 +309,7 @@ public final 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()) {
@@ -332,7 +334,6 @@ public final 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));
         }
@@ -357,7 +358,7 @@ public final 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;
@@ -378,7 +379,7 @@ public final class CopyUtils {
             type = new TypeDefinitionBuilderImpl(old.getModuleName(), newParent.getLine(), newQName);
             type.setParent(newParent);
             // TODO
-            //type.setPath(newSchemaPath);
+            // type.setPath(newSchemaPath);
             type.setPath(old.getPath());
 
             if (old.getType() == null) {
@@ -430,28 +431,28 @@ public final class CopyUtils {
 
         // target child nodes
         Set<DataSchemaNodeBuilder> newTargetChildren = new HashSet<>();
-        for(DataSchemaNodeBuilder dnb : old.getTargetChildren()) {
+        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()) {
+        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()) {
+        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()) {
+        for (UnknownSchemaNodeBuilder unb : old.getTargetUnknownNodes()) {
             newTargetUnknownNodes.add(copy(unb, newParent, true));
         }
         copy.getTargetUnknownNodes().addAll(newTargetUnknownNodes);
@@ -472,7 +473,7 @@ public final 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));
         }
@@ -521,16 +522,16 @@ public final class CopyUtils {
                 newPath = Collections.singletonList(newQName);
             }
         } else if (newParent instanceof AugmentationSchemaBuilder) {
-            AugmentationSchemaBuilder augment = (AugmentationSchemaBuilder)newParent;
+            AugmentationSchemaBuilder augment = (AugmentationSchemaBuilder) newParent;
             ModuleBuilder parent = ParserUtils.getParentModule(newParent);
             if (updateQName) {
                 newQName = new QName(parent.getNamespace(), parent.getRevision(), parent.getPrefix(), old.getQName()
                         .getLocalName());
-                newPath = new ArrayList<>(augment.getTargetPath().getPath());
+                newPath = new ArrayList<>(augment.getTargetNodeSchemaPath().getPath());
                 newPath.add(newQName);
             } else {
                 newQName = old.getQName();
-                newPath = new ArrayList<>(augment.getTargetPath().getPath());
+                newPath = new ArrayList<>(augment.getTargetNodeSchemaPath().getPath());
                 newPath.add(newQName);
             }
 
@@ -567,7 +568,9 @@ public final class CopyUtils {
      * Create AnyXmlBuilder from given AnyXmlSchemaNode.
      *
      * @param anyxml
+     *            base anyxml
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -586,7 +589,9 @@ public final class CopyUtils {
      * Create GroupingBuilder from given GroupingDefinition.
      *
      * @param grouping
+     *            base grouping
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -596,7 +601,9 @@ public final 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());
@@ -611,7 +618,9 @@ public final class CopyUtils {
      * Create TypeDefinitionBuilder from given ExtendedType.
      *
      * @param typedef
+     *            base typedef
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -641,7 +650,9 @@ public final class CopyUtils {
      * Create UnknownSchemaNodeBuilder from given UnknownSchemaNode.
      *
      * @param unknownNode
+     *            base unknown node
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -668,6 +679,7 @@ public final class CopyUtils {
      * @param leaf
      *            leaf from which to create builder
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -691,7 +703,9 @@ public final class CopyUtils {
      * Create ContainerSchemaNodeBuilder from given ContainerSchemaNode.
      *
      * @param container
+     *            base container
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -705,10 +719,12 @@ public final 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;
@@ -718,7 +734,9 @@ public final class CopyUtils {
      * Create ListSchemaNodeBuilder from given ListSchemaNode.
      *
      * @param list
+     *            base list
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -731,9 +749,11 @@ public final 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;
@@ -743,7 +763,9 @@ public final class CopyUtils {
      * Create LeafListSchemaNodeBuilder from given LeafListSchemaNode.
      *
      * @param leafList
+     *            base leaf-list
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -766,7 +788,9 @@ public final class CopyUtils {
      * Create ChoiceBuilder from given ChoiceNode.
      *
      * @param choice
+     *            base choice
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line