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 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;
21 * Utility class holding methods useful when dealing with {@link DataTreeCandidate} instances.
24 public final class DataTreeCandidates {
25 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
26 private DataTreeCandidates() {
27 throw new UnsupportedOperationException();
30 public static DataTreeCandidate newDataTreeCandidate(final YangInstanceIdentifier rootPath, final DataTreeCandidateNode rootNode) {
31 return new DefaultDataTreeCandidate(rootPath, rootNode);
34 public static DataTreeCandidate fromNormalizedNode(final YangInstanceIdentifier rootPath, final NormalizedNode<?, ?> node) {
35 return new DefaultDataTreeCandidate(rootPath, new NormalizedNodeDataTreeCandidateNode(node));
38 public static void applyToCursor(final DataTreeModificationCursor cursor, final DataTreeCandidate candidate) {
39 DataTreeCandidateNodes.applyToCursor(cursor, candidate.getRootNode());
42 public static void applyToModification(final DataTreeModification modification, final DataTreeCandidate candidate) {
43 if (modification instanceof CursorAwareDataTreeModification) {
44 try (DataTreeModificationCursor cursor = ((CursorAwareDataTreeModification) modification).createCursor(candidate.getRootPath())) {
45 applyToCursor(cursor, candidate);
50 final DataTreeCandidateNode node = candidate.getRootNode();
51 final YangInstanceIdentifier path = candidate.getRootPath();
52 switch (node.getModificationType()) {
54 modification.delete(path);
55 LOG.debug("Modification {} deleted path {}", modification, path);
57 case SUBTREE_MODIFIED:
58 LOG.debug("Modification {} modified path {}", modification, path);
60 NodeIterator iterator = new NodeIterator(null, path, node.getChildNodes().iterator());
62 iterator = iterator.next(modification);
63 } while (iterator != null);
66 LOG.debug("Modification {} unmodified path {}", modification, path);
70 modification.write(path, node.getDataAfter().get());
71 LOG.debug("Modification {} written path {}", modification, path);
74 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
78 private static final class NodeIterator {
79 private final Iterator<DataTreeCandidateNode> iterator;
80 private final YangInstanceIdentifier path;
81 private final NodeIterator parent;
83 public NodeIterator(@Nullable final NodeIterator parent, @Nonnull final YangInstanceIdentifier path,
84 @Nonnull final Iterator<DataTreeCandidateNode> iterator) {
85 this.iterator = Preconditions.checkNotNull(iterator);
86 this.path = Preconditions.checkNotNull(path);
90 NodeIterator next(final DataTreeModification modification) {
91 while (iterator.hasNext()) {
92 final DataTreeCandidateNode node = iterator.next();
93 final YangInstanceIdentifier child = path.node(node.getIdentifier());
95 switch (node.getModificationType()) {
97 modification.delete(child);
98 LOG.debug("Modification {} deleted path {}", modification, child);
100 case SUBTREE_MODIFIED:
101 LOG.debug("Modification {} modified path {}", modification, child);
102 return new NodeIterator(this, child, node.getChildNodes().iterator());
104 LOG.debug("Modification {} unmodified path {}", modification, child);
108 modification.write(child, node.getDataAfter().get());
109 LOG.debug("Modification {} written path {}", modification, child);
112 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());