2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.api.schema.tree;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.annotations.Beta;
13 import java.util.Iterator;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Utility class holding methods useful when dealing with {@link DataTreeCandidate} instances.
25 public final class DataTreeCandidates {
26 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
28 private DataTreeCandidates() {
32 public static @NonNull DataTreeCandidate newDataTreeCandidate(final YangInstanceIdentifier rootPath,
33 final DataTreeCandidateNode rootNode) {
34 return new DefaultDataTreeCandidate(rootPath, rootNode);
37 public static @NonNull DataTreeCandidate fromNormalizedNode(final YangInstanceIdentifier rootPath,
38 final NormalizedNode<?, ?> node) {
39 return new DefaultDataTreeCandidate(rootPath, new NormalizedNodeDataTreeCandidateNode(node));
42 public static void applyToCursor(final DataTreeModificationCursor cursor, final DataTreeCandidate candidate) {
43 DataTreeCandidateNodes.applyToCursor(cursor, candidate.getRootNode());
46 public static void applyToModification(final DataTreeModification modification,
47 final DataTreeCandidate candidate) {
48 if (modification instanceof CursorAwareDataTreeModification) {
49 applyToCursorAwareModification((CursorAwareDataTreeModification) modification, candidate);
53 final DataTreeCandidateNode node = candidate.getRootNode();
54 final YangInstanceIdentifier path = candidate.getRootPath();
55 switch (node.getModificationType()) {
57 modification.delete(path);
58 LOG.debug("Modification {} deleted path {}", modification, path);
60 case SUBTREE_MODIFIED:
61 LOG.debug("Modification {} modified path {}", modification, path);
63 NodeIterator iterator = new NodeIterator(null, path, node.getChildNodes().iterator());
65 iterator = iterator.next(modification);
66 } while (iterator != null);
69 LOG.debug("Modification {} unmodified path {}", modification, path);
73 modification.write(path, node.getDataAfter().get());
74 LOG.debug("Modification {} written path {}", modification, path);
77 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
81 private static void applyToCursorAwareModification(final CursorAwareDataTreeModification modification,
82 final DataTreeCandidate candidate) {
83 final YangInstanceIdentifier candidatePath = candidate.getRootPath();
84 if (candidatePath.isEmpty()) {
85 try (DataTreeModificationCursor cursor = modification.openCursor()) {
86 DataTreeCandidateNodes.applyRootToCursor(cursor, candidate.getRootNode());
89 try (DataTreeModificationCursor cursor = modification.openCursor(candidatePath.getParent()).get()) {
90 DataTreeCandidateNodes.applyRootedNodeToCursor(cursor, candidatePath, candidate.getRootNode());
95 private static final class NodeIterator {
96 private final Iterator<DataTreeCandidateNode> iterator;
97 private final YangInstanceIdentifier path;
98 private final NodeIterator parent;
100 NodeIterator(final @Nullable NodeIterator parent, final YangInstanceIdentifier path,
101 final Iterator<DataTreeCandidateNode> iterator) {
102 this.iterator = requireNonNull(iterator);
103 this.path = requireNonNull(path);
104 this.parent = parent;
107 NodeIterator next(final DataTreeModification modification) {
108 while (iterator.hasNext()) {
109 final DataTreeCandidateNode node = iterator.next();
110 final YangInstanceIdentifier child = path.node(node.getIdentifier());
112 switch (node.getModificationType()) {
114 modification.delete(child);
115 LOG.debug("Modification {} deleted path {}", modification, child);
119 case SUBTREE_MODIFIED:
120 LOG.debug("Modification {} modified path {}", modification, child);
121 return new NodeIterator(this, child, node.getChildNodes().iterator());
123 LOG.debug("Modification {} unmodified path {}", modification, child);
127 modification.write(child, node.getDataAfter().get());
128 LOG.debug("Modification {} written path {}", modification, child);
131 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());