Minor code refactoring.
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / CopyUtils.java
index 4b5d8dc5662cd15dedf5ee043eaa31ea5b586bef..d9858bc3cc6e56d10d1f9a60e9ec9682247538b0 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) {
@@ -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);
@@ -215,8 +223,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 +241,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 +273,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 +286,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);
@@ -349,55 +357,20 @@ public class CopyUtils {
         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));
-        }
-        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 TypeDefinitionBuilder copy(TypeDefinitionBuilder old, Builder newParent, 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) {
@@ -408,12 +381,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 +421,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;
     }
@@ -479,6 +477,7 @@ public class CopyUtils {
         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 +526,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 +548,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 +558,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 +568,13 @@ public class CopyUtils {
         }
     }
 
-
     /**
      * Create AnyXmlBuilder from given AnyXmlSchemaNode.
-     *
+     * 
      * @param anyxml
+     *            base anyxml
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -591,9 +591,11 @@ public class CopyUtils {
 
     /**
      * Create GroupingBuilder from given GroupingDefinition.
-     *
+     * 
      * @param grouping
+     *            base grouping
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -616,9 +618,11 @@ 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 +637,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);
@@ -646,9 +650,11 @@ 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 +675,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
@@ -697,9 +703,11 @@ public class CopyUtils {
 
     /**
      * Create ContainerSchemaNodeBuilder from given ContainerSchemaNode.
-     *
+     * 
      * @param container
+     *            base container
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -724,9 +732,11 @@ public class CopyUtils {
 
     /**
      * Create ListSchemaNodeBuilder from given ListSchemaNode.
-     *
+     * 
      * @param list
+     *            base list
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -749,9 +759,11 @@ public class CopyUtils {
 
     /**
      * Create LeafListSchemaNodeBuilder from given LeafListSchemaNode.
-     *
+     * 
      * @param leafList
+     *            base leaf-list
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -772,9 +784,11 @@ public class CopyUtils {
 
     /**
      * Create ChoiceBuilder from given ChoiceNode.
-     *
+     * 
      * @param choice
+     *            base choice
      * @param qname
+     *            new qname
      * @param moduleName
      *            current module name
      * @param line
@@ -791,10 +805,9 @@ public class CopyUtils {
         return builder;
     }
 
-
     /**
      * Set DataSchemaNode arguments to builder object
-     *
+     * 
      * @param node
      *            node from which arguments should be read
      * @param builder
@@ -811,7 +824,7 @@ public class CopyUtils {
 
     /**
      * Copy constraints from constraints definition to constraints builder.
-     *
+     * 
      * @param nodeConstraints
      *            definition from which constraints will be copied
      * @param constraints