BUG-2882: introduce cursor-based modification API
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / DataTreeCandidates.java
index 441f92776373383156f78479ff9838a91e578cc1..99256397b0e9197f9cb156060c8c03fb7b76278f 100644 (file)
@@ -10,12 +10,15 @@ package org.opendaylight.yangtools.yang.data.api.schema.tree;
 import com.google.common.annotations.Beta;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Utility class holding methods useful when dealing with {@link DataTreeCandidate} instances.
  */
 @Beta
 public final class DataTreeCandidates {
+    private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
     private DataTreeCandidates() {
         throw new UnsupportedOperationException();
     }
@@ -29,24 +32,57 @@ public final class DataTreeCandidates {
     }
 
     public static void applyToModification(final DataTreeModification modification, final DataTreeCandidate candidate) {
-        applyNode(modification, candidate.getRootPath(), candidate.getRootNode());
+        if (modification instanceof CursorAwareDataTreeModification) {
+            try (DataTreeModificationCursor cursor = ((CursorAwareDataTreeModification) modification).createCursor(candidate.getRootPath())) {
+                applyNode(cursor, candidate.getRootNode());
+            }
+        } else {
+            applyNode(modification, candidate.getRootPath(), candidate.getRootNode());
+        }
     }
 
     private static void applyNode(final DataTreeModification modification, final YangInstanceIdentifier path, final DataTreeCandidateNode node) {
         switch (node.getModificationType()) {
         case DELETE:
             modification.delete(path);
+            LOG.debug("Modification {} deleted path {}", modification, path);
             break;
         case SUBTREE_MODIFIED:
+            LOG.debug("Modification {} modified path {}", modification, path);
             for (DataTreeCandidateNode child : node.getChildNodes()) {
                 applyNode(modification, path.node(child.getIdentifier()), child);
             }
             break;
         case UNMODIFIED:
+            LOG.debug("Modification {} unmodified path {}", modification, path);
             // No-op
             break;
         case WRITE:
             modification.write(path, node.getDataAfter().get());
+            LOG.debug("Modification {} written path {}", modification, path);
+            break;
+        default:
+            throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
+        }
+    }
+
+    private static void applyNode(final DataTreeModificationCursor cursor, final DataTreeCandidateNode node) {
+        switch (node.getModificationType()) {
+        case DELETE:
+            cursor.delete(node.getIdentifier());
+            break;
+        case SUBTREE_MODIFIED:
+            cursor.enter(node.getIdentifier());
+            for (DataTreeCandidateNode child : node.getChildNodes()) {
+                applyNode(cursor, child);
+            }
+            cursor.exit();
+            break;
+        case UNMODIFIED:
+            // No-op
+            break;
+        case WRITE:
+            cursor.write(node.getIdentifier(), node.getDataAfter().get());
             break;
         default:
             throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());