groovy node-tree integration
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-data-impl / src / main / java / org / opendaylight / controller / yang / data / impl / NodeModificationBuilderImpl.java
old mode 100755 (executable)
new mode 100644 (file)
index cd71731..2ab2dd8
@@ -1,14 +1,16 @@
-/**\r
- * \r
+/*\r
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
+ *\r
+ * This program and the accompanying materials are made available under the\r
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
+ * and is available at http://www.eclipse.org/legal/epl-v10.html\r
  */\r
 package org.opendaylight.controller.yang.data.impl;\r
 \r
-import java.util.HashMap;\r
 import java.util.HashSet;\r
 import java.util.List;\r
 import java.util.Map;\r
 import java.util.Set;\r
-import java.util.Stack;\r
 \r
 import org.opendaylight.controller.yang.common.QName;\r
 import org.opendaylight.controller.yang.data.api.CompositeNode;\r
@@ -30,9 +32,7 @@ public class NodeModificationBuilderImpl implements NodeModificationBuilder {
     private SchemaContext context;\r
     \r
     private Set<MutableNode<?>> changeLog;\r
-    private Map<Node<?>, Node<?>> originalToMutable;\r
-\r
-    private MutableCompositeNode mutableRoot;\r
+    private LazyNodeToNodeMap originalToMutable;\r
 \r
     /**\r
      * @param originalTreeRootNode \r
@@ -40,24 +40,10 @@ public class NodeModificationBuilderImpl implements NodeModificationBuilder {
      */\r
     public NodeModificationBuilderImpl(CompositeNode originalTreeRootNode, SchemaContext context) {\r
         this.context = context;\r
-        originalToMutable = new HashMap<>();\r
-        mutableRoot = NodeFactory.copyDeepNode(originalTreeRootNode, originalToMutable);\r
+        originalToMutable = new LazyNodeToNodeMap();\r
         changeLog = new HashSet<>();\r
     }\r
 \r
-    /**\r
-     * add given node to it's parent's list of children\r
-     * @param newNode\r
-     */\r
-    private static void fixParentRelation(Node<?> newNode) {\r
-        if (newNode.getParent() != null) {\r
-            List<Node<?>> siblings = newNode.getParent().getChildren();\r
-            if (!siblings.contains(newNode)) {\r
-                siblings.add(newNode);\r
-            }\r
-        }\r
-    }\r
-\r
     /**\r
      * @param modNode\r
      * @param action \r
@@ -69,13 +55,13 @@ public class NodeModificationBuilderImpl implements NodeModificationBuilder {
 \r
     @Override\r
     public void addNode(MutableSimpleNode<?> newNode) {\r
-        fixParentRelation(newNode);\r
+        NodeUtils.fixParentRelation(newNode);\r
         addModificationToLog(newNode, ModifyAction.CREATE);\r
     }\r
     \r
     @Override\r
     public void addNode(MutableCompositeNode newNode) {\r
-        fixParentRelation(newNode);\r
+        NodeUtils.fixParentRelation(newNode);\r
         addModificationToLog(newNode, ModifyAction.CREATE);\r
     }\r
     \r
@@ -126,9 +112,8 @@ public class NodeModificationBuilderImpl implements NodeModificationBuilder {
             wanted.addAll(collectSelfAndAllParents(mutant));\r
         }\r
         \r
-        // TODO:: walk wanted and add relevant keys\r
+        // walk wanted and add relevant keys\r
         Map<String, ListSchemaNode>  mapOfLists = NodeUtils.buildMapOfListNodes(context);\r
-        Set<Node<?>> wantedKeys = new HashSet<>();\r
         for (Node<?> outlaw : wanted) {\r
             if (outlaw instanceof CompositeNode) {\r
                 String path = NodeUtils.buildPath(outlaw);\r
@@ -137,54 +122,27 @@ public class NodeModificationBuilderImpl implements NodeModificationBuilder {
                     if (listSchema.getQName().equals(outlaw.getNodeType())) {\r
                         // try to add key subnode to wanted list\r
                         List<QName> supportedKeys = listSchema.getKeyDefinition();\r
-                        for (Node<?> outlawChildren : ((CompositeNode) outlaw).getChildren()) {\r
-                            if (supportedKeys.contains(outlawChildren.getNodeType())) {\r
-                                wantedKeys.add(outlawChildren);\r
+                        CompositeNode outlawOriginal = ((MutableCompositeNode) outlaw).getOriginal();\r
+                        for (Node<?> outlawOriginalChild : outlawOriginal.getChildren()) {\r
+                            if (supportedKeys.contains(outlawOriginalChild.getNodeType())) {\r
+                                originalToMutable.getMutableEquivalent(outlawOriginalChild);\r
                             }\r
                         }\r
                     }\r
                 }\r
             }\r
         }\r
-        wanted.addAll(wantedKeys);\r
-        \r
-        // remove all unwanted nodes from tree\r
-        removeUnrelevantNodes(mutableRoot, wanted);\r
         \r
-        return mutableRoot;\r
-    }\r
-\r
-    /**\r
-     * @param mutableRoot2\r
-     * @param wanted\r
-     */\r
-    private static void removeUnrelevantNodes(MutableCompositeNode mutRoot,\r
-            Set<Node<?>> wanted) {\r
-        Stack<MutableNode<?>> jobQueue = new Stack<>();\r
-        jobQueue.push(mutRoot);\r
-        while (!jobQueue.isEmpty()) {\r
-            MutableNode<?> mutNode = jobQueue.pop();\r
-            if (!wanted.contains(mutNode)) {\r
-                if (mutNode.getParent() != null) {\r
-                    mutNode.getParent().getChildren().remove(mutNode);\r
-                }\r
-            } else {\r
-                if (mutNode instanceof MutableCompositeNode) {\r
-                    for (Node<?> mutChild : ((MutableCompositeNode) mutNode).getChildren()) {\r
-                        jobQueue.push((MutableNode<?>) mutChild);\r
-                    }\r
-                }\r
-            }\r
-        }\r
+        return originalToMutable.getMutableRoot();\r
     }\r
 \r
     /**\r
-     * @param focusedAncestor\r
+     * @param focusedDescendant\r
      * @return set of parents and focusedAncestor itself\r
      */\r
-    private static Set<Node<?>> collectSelfAndAllParents(Node<?> focusedAncestor) {\r
+    private static Set<Node<?>> collectSelfAndAllParents(Node<?> focusedDescendant) {\r
         Set<Node<?>> family = new HashSet<>();\r
-        Node<?> tmpNode = focusedAncestor;\r
+        Node<?> tmpNode = focusedDescendant;\r
         while (tmpNode != null) {\r
             family.add(tmpNode);\r
             tmpNode = tmpNode.getParent();\r
@@ -198,7 +156,7 @@ public class NodeModificationBuilderImpl implements NodeModificationBuilder {
      */\r
     @Override\r
     public Node<?> getMutableEquivalent(Node<?> originalNode) {\r
-        return originalToMutable.get(originalNode);\r
+        return originalToMutable.getMutableEquivalent(originalNode);\r
     }\r
 \r
 }\r