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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
19 * Utility class holding methods useful when dealing with {@link DataTreeCandidate} instances.
22 public final class DataTreeCandidates {
23 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
24 private DataTreeCandidates() {
25 throw new UnsupportedOperationException();
28 public static DataTreeCandidate newDataTreeCandidate(final YangInstanceIdentifier rootPath, final DataTreeCandidateNode rootNode) {
29 return new DefaultDataTreeCandidate(rootPath, rootNode);
32 public static DataTreeCandidate fromNormalizedNode(final YangInstanceIdentifier rootPath, final NormalizedNode<?, ?> node) {
33 return new DefaultDataTreeCandidate(rootPath, new NormalizedNodeDataTreeCandidateNode(node));
36 public static void applyToCursor(final DataTreeModificationCursor cursor, final DataTreeCandidate candidate) {
37 DataTreeCandidateNodes.applyToCursor(cursor, candidate.getRootNode());
40 public static void applyToModification(final DataTreeModification modification, final DataTreeCandidate candidate) {
41 if (modification instanceof CursorAwareDataTreeModification) {
42 try (DataTreeModificationCursor cursor = ((CursorAwareDataTreeModification) modification).createCursor(candidate.getRootPath())) {
43 applyToCursor(cursor, 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 final class NodeIterator {
77 private final Iterator<DataTreeCandidateNode> iterator;
78 private final YangInstanceIdentifier path;
79 private final NodeIterator parent;
81 public NodeIterator(final NodeIterator parent, final YangInstanceIdentifier path, final Iterator<DataTreeCandidateNode> iterator) {
82 this.iterator = Preconditions.checkNotNull(iterator);
83 this.parent = Preconditions.checkNotNull(parent);
84 this.path = Preconditions.checkNotNull(path);
87 NodeIterator next(final DataTreeModification modification) {
88 while (iterator.hasNext()) {
89 final DataTreeCandidateNode node = iterator.next();
90 final YangInstanceIdentifier child = path.node(node.getIdentifier());
92 switch (node.getModificationType()) {
94 modification.delete(child);
95 LOG.debug("Modification {} deleted path {}", modification, child);
97 case SUBTREE_MODIFIED:
98 LOG.debug("Modification {} modified path {}", modification, child);
99 return new NodeIterator(this, child, node.getChildNodes().iterator());
101 LOG.debug("Modification {} unmodified path {}", modification, child);
105 modification.write(child, node.getDataAfter().get());
106 LOG.debug("Modification {} written path {}", modification, child);
109 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());