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
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.api.schema.tree;
9
10 import com.google.common.annotations.Beta;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 /**
17  * Utility class holding methods useful when dealing with {@link DataTreeCandidate} instances.
18  */
19 @Beta
20 public final class DataTreeCandidates {
21     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
22     private DataTreeCandidates() {
23         throw new UnsupportedOperationException();
24     }
25
26     public static DataTreeCandidate newDataTreeCandidate(final YangInstanceIdentifier rootPath, final DataTreeCandidateNode rootNode) {
27         return new DefaultDataTreeCandidate(rootPath, rootNode);
28     }
29
30     public static DataTreeCandidate fromNormalizedNode(final YangInstanceIdentifier rootPath, final NormalizedNode<?, ?> node) {
31         return new DefaultDataTreeCandidate(rootPath, new NormalizedNodeDataTreeCandidateNode(node));
32     }
33
34     public static void applyToModification(final DataTreeModification modification, final DataTreeCandidate candidate) {
35         if (modification instanceof CursorAwareDataTreeModification) {
36             try (DataTreeModificationCursor cursor = ((CursorAwareDataTreeModification) modification).createCursor(candidate.getRootPath())) {
37                 applyNode(cursor, candidate.getRootNode());
38             }
39         } else {
40             applyNode(modification, candidate.getRootPath(), candidate.getRootNode());
41         }
42     }
43
44     private static void applyNode(final DataTreeModification modification, final YangInstanceIdentifier path, final DataTreeCandidateNode node) {
45         switch (node.getModificationType()) {
46         case DELETE:
47             modification.delete(path);
48             LOG.debug("Modification {} deleted path {}", modification, path);
49             break;
50         case SUBTREE_MODIFIED:
51             LOG.debug("Modification {} modified path {}", modification, path);
52             for (DataTreeCandidateNode child : node.getChildNodes()) {
53                 applyNode(modification, path.node(child.getIdentifier()), child);
54             }
55             break;
56         case UNMODIFIED:
57             LOG.debug("Modification {} unmodified path {}", modification, path);
58             // No-op
59             break;
60         case WRITE:
61             modification.write(path, node.getDataAfter().get());
62             LOG.debug("Modification {} written path {}", modification, path);
63             break;
64         default:
65             throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
66         }
67     }
68
69     private static void applyNode(final DataTreeModificationCursor cursor, final DataTreeCandidateNode node) {
70         switch (node.getModificationType()) {
71         case DELETE:
72             cursor.delete(node.getIdentifier());
73             break;
74         case SUBTREE_MODIFIED:
75             cursor.enter(node.getIdentifier());
76             for (DataTreeCandidateNode child : node.getChildNodes()) {
77                 applyNode(cursor, child);
78             }
79             cursor.exit();
80             break;
81         case UNMODIFIED:
82             // No-op
83             break;
84         case WRITE:
85             cursor.write(node.getIdentifier(), node.getDataAfter().get());
86             break;
87         default:
88             throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
89         }
90     }
91 }