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 04e3b204bcbc12f7ae3ab795bb558810e066559b..99256397b0e9197f9cb156060c8c03fb7b76278f 100644 (file)
@@ -32,7 +32,13 @@ 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) {
@@ -59,4 +65,27 @@ public final class DataTreeCandidates {
             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());
+        }
+    }
 }