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 applyToCursorAwareModification((CursorAwareDataTreeModification) modification, candidate);
48 final DataTreeCandidateNode node = candidate.getRootNode();
49 final YangInstanceIdentifier path = candidate.getRootPath();
50 switch (node.getModificationType()) {
52 modification.delete(path);
53 LOG.debug("Modification {} deleted path {}", modification, path);
55 case SUBTREE_MODIFIED:
56 LOG.debug("Modification {} modified path {}", modification, path);
58 NodeIterator iterator = new NodeIterator(null, path, node.getChildNodes().iterator());
60 iterator = iterator.next(modification);
61 } while (iterator != null);
64 LOG.debug("Modification {} unmodified path {}", modification, path);
68 modification.write(path, node.getDataAfter().get());
69 LOG.debug("Modification {} written path {}", modification, path);
72 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
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 =
81 ((CursorAwareDataTreeModification) modification).createCursor(candidatePath)) {
82 DataTreeCandidateNodes.applyRootToCursor(cursor, candidate.getRootNode());
85 try (DataTreeModificationCursor cursor =
86 ((CursorAwareDataTreeModification) modification).createCursor(candidatePath.getParent())) {
87 DataTreeCandidateNodes.applyToCursor(cursor, candidate.getRootNode());
92 private static final class NodeIterator {
93 private final Iterator<DataTreeCandidateNode> iterator;
94 private final YangInstanceIdentifier path;
95 private final NodeIterator parent;
97 public NodeIterator(@Nullable final NodeIterator parent, @Nonnull final YangInstanceIdentifier path,
98 @Nonnull final Iterator<DataTreeCandidateNode> iterator) {
99 this.iterator = Preconditions.checkNotNull(iterator);
100 this.path = Preconditions.checkNotNull(path);
101 this.parent = parent;
104 NodeIterator next(final DataTreeModification modification) {
105 while (iterator.hasNext()) {
106 final DataTreeCandidateNode node = iterator.next();
107 final YangInstanceIdentifier child = path.node(node.getIdentifier());
109 switch (node.getModificationType()) {
111 modification.delete(child);
112 LOG.debug("Modification {} deleted path {}", modification, child);
114 case SUBTREE_MODIFIED:
115 LOG.debug("Modification {} modified path {}", modification, child);
116 return new NodeIterator(this, child, node.getChildNodes().iterator());
118 LOG.debug("Modification {} unmodified path {}", modification, child);
122 modification.write(child, node.getDataAfter().get());
123 LOG.debug("Modification {} written path {}", modification, child);
126 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());