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);
27 private DataTreeCandidates() {
28 throw new UnsupportedOperationException();
31 public static DataTreeCandidate newDataTreeCandidate(final YangInstanceIdentifier rootPath,
32 final DataTreeCandidateNode rootNode) {
33 return new DefaultDataTreeCandidate(rootPath, rootNode);
36 public static DataTreeCandidate fromNormalizedNode(final YangInstanceIdentifier rootPath,
37 final NormalizedNode<?, ?> node) {
38 return new DefaultDataTreeCandidate(rootPath, new NormalizedNodeDataTreeCandidateNode(node));
41 public static void applyToCursor(final DataTreeModificationCursor cursor, final DataTreeCandidate candidate) {
42 DataTreeCandidateNodes.applyToCursor(cursor, candidate.getRootNode());
45 public static void applyToModification(final DataTreeModification modification,
46 final DataTreeCandidate candidate) {
47 if (modification instanceof CursorAwareDataTreeModification) {
48 applyToCursorAwareModification((CursorAwareDataTreeModification) modification, candidate);
52 final DataTreeCandidateNode node = candidate.getRootNode();
53 final YangInstanceIdentifier path = candidate.getRootPath();
54 switch (node.getModificationType()) {
56 modification.delete(path);
57 LOG.debug("Modification {} deleted path {}", modification, path);
59 case SUBTREE_MODIFIED:
60 LOG.debug("Modification {} modified path {}", modification, path);
62 NodeIterator iterator = new NodeIterator(null, path, node.getChildNodes().iterator());
64 iterator = iterator.next(modification);
65 } while (iterator != null);
68 LOG.debug("Modification {} unmodified path {}", modification, path);
72 modification.write(path, node.getDataAfter().get());
73 LOG.debug("Modification {} written path {}", modification, path);
76 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
80 private static void applyToCursorAwareModification(final CursorAwareDataTreeModification modification,
81 final DataTreeCandidate candidate) {
82 final YangInstanceIdentifier candidatePath = candidate.getRootPath();
83 if (candidatePath.isEmpty()) {
84 try (DataTreeModificationCursor cursor = modification.createCursor(candidatePath)) {
85 DataTreeCandidateNodes.applyRootToCursor(cursor, candidate.getRootNode());
88 try (DataTreeModificationCursor cursor = modification.createCursor(candidatePath.getParent())) {
89 DataTreeCandidateNodes.applyRootedNodeToCursor(cursor, candidatePath, candidate.getRootNode());
94 private static final class NodeIterator {
95 private final Iterator<DataTreeCandidateNode> iterator;
96 private final YangInstanceIdentifier path;
97 private final NodeIterator parent;
99 NodeIterator(@Nullable final NodeIterator parent, @Nonnull final YangInstanceIdentifier path,
100 @Nonnull final Iterator<DataTreeCandidateNode> iterator) {
101 this.iterator = Preconditions.checkNotNull(iterator);
102 this.path = Preconditions.checkNotNull(path);
103 this.parent = parent;
106 NodeIterator next(final DataTreeModification modification) {
107 while (iterator.hasNext()) {
108 final DataTreeCandidateNode node = iterator.next();
109 final YangInstanceIdentifier child = path.node(node.getIdentifier());
111 switch (node.getModificationType()) {
113 modification.delete(child);
114 LOG.debug("Modification {} deleted path {}", modification, child);
118 case SUBTREE_MODIFIED:
119 LOG.debug("Modification {} modified path {}", modification, child);
120 return new NodeIterator(this, child, node.getChildNodes().iterator());
122 LOG.debug("Modification {} unmodified path {}", modification, child);
126 modification.write(child, node.getDataAfter().get());
127 LOG.debug("Modification {} written path {}", modification, child);
130 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());