Add application of RootedDataTreeCandidates
[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 com.google.common.base.Preconditions;
12 import java.util.Iterator;
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Utility class holding methods useful when dealing with {@link DataTreeCandidate} instances.
22  */
23 @Beta
24 public final class DataTreeCandidates {
25     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
26     private DataTreeCandidates() {
27         throw new UnsupportedOperationException();
28     }
29
30     public static DataTreeCandidate newDataTreeCandidate(final YangInstanceIdentifier rootPath, final DataTreeCandidateNode rootNode) {
31         return new DefaultDataTreeCandidate(rootPath, rootNode);
32     }
33
34     public static DataTreeCandidate fromNormalizedNode(final YangInstanceIdentifier rootPath, final NormalizedNode<?, ?> node) {
35         return new DefaultDataTreeCandidate(rootPath, new NormalizedNodeDataTreeCandidateNode(node));
36     }
37
38     public static void applyToCursor(final DataTreeModificationCursor cursor, final DataTreeCandidate candidate) {
39         DataTreeCandidateNodes.applyToCursor(cursor, candidate.getRootNode());
40     }
41
42     public static void applyToModification(final DataTreeModification modification, final DataTreeCandidate candidate) {
43         if (modification instanceof CursorAwareDataTreeModification) {
44             applyToCursorAwareModification((CursorAwareDataTreeModification) modification, candidate);
45             return;
46         }
47
48         final DataTreeCandidateNode node = candidate.getRootNode();
49         final YangInstanceIdentifier path = candidate.getRootPath();
50         switch (node.getModificationType()) {
51         case DELETE:
52             modification.delete(path);
53             LOG.debug("Modification {} deleted path {}", modification, path);
54             break;
55         case SUBTREE_MODIFIED:
56             LOG.debug("Modification {} modified path {}", modification, path);
57
58             NodeIterator iterator = new NodeIterator(null, path, node.getChildNodes().iterator());
59             do {
60                 iterator = iterator.next(modification);
61             } while (iterator != null);
62             break;
63         case UNMODIFIED:
64             LOG.debug("Modification {} unmodified path {}", modification, path);
65             // No-op
66             break;
67         case WRITE:
68             modification.write(path, node.getDataAfter().get());
69             LOG.debug("Modification {} written path {}", modification, path);
70             break;
71         default:
72             throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
73         }
74     }
75
76     private static void applyToCursorAwareModification(final CursorAwareDataTreeModification modification,
77             final DataTreeCandidate candidate) {
78         final YangInstanceIdentifier candidatePath = candidate.getRootPath();
79         if (candidatePath.isEmpty()) {
80             try (DataTreeModificationCursor cursor = modification.createCursor(candidatePath)) {
81                 DataTreeCandidateNodes.applyRootToCursor(cursor, candidate.getRootNode());
82             }
83         } else {
84             try (DataTreeModificationCursor cursor = modification.createCursor(candidatePath.getParent())) {
85                 DataTreeCandidateNodes.applyRootedNodeToCursor(cursor, candidatePath, candidate.getRootNode());
86             }
87         }
88     }
89
90     private static final class NodeIterator {
91         private final Iterator<DataTreeCandidateNode> iterator;
92         private final YangInstanceIdentifier path;
93         private final NodeIterator parent;
94
95         public NodeIterator(@Nullable final NodeIterator parent, @Nonnull final YangInstanceIdentifier path,
96                 @Nonnull final Iterator<DataTreeCandidateNode> iterator) {
97             this.iterator = Preconditions.checkNotNull(iterator);
98             this.path = Preconditions.checkNotNull(path);
99             this.parent = parent;
100         }
101
102         NodeIterator next(final DataTreeModification modification) {
103             while (iterator.hasNext()) {
104                 final DataTreeCandidateNode node = iterator.next();
105                 final YangInstanceIdentifier child = path.node(node.getIdentifier());
106
107                 switch (node.getModificationType()) {
108                 case DELETE:
109                     modification.delete(child);
110                     LOG.debug("Modification {} deleted path {}", modification, child);
111                     break;
112                 case APPEARED:
113                 case DISAPPEARED:
114                 case SUBTREE_MODIFIED:
115                     LOG.debug("Modification {} modified path {}", modification, child);
116                     return new NodeIterator(this, child, node.getChildNodes().iterator());
117                 case UNMODIFIED:
118                     LOG.debug("Modification {} unmodified path {}", modification, child);
119                     // No-op
120                     break;
121                 case WRITE:
122                     modification.write(child, node.getDataAfter().get());
123                     LOG.debug("Modification {} written path {}", modification, child);
124                     break;
125                 default:
126                     throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
127                 }
128             }
129
130             return parent;
131         }
132     }
133 }