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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
17 * Utility class holding methods useful when dealing with {@link DataTreeCandidate} instances.
20 public final class DataTreeCandidates {
21 private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
22 private DataTreeCandidates() {
23 throw new UnsupportedOperationException();
26 public static DataTreeCandidate newDataTreeCandidate(final YangInstanceIdentifier rootPath, final DataTreeCandidateNode rootNode) {
27 return new DefaultDataTreeCandidate(rootPath, rootNode);
30 public static DataTreeCandidate fromNormalizedNode(final YangInstanceIdentifier rootPath, final NormalizedNode<?, ?> node) {
31 return new DefaultDataTreeCandidate(rootPath, new NormalizedNodeDataTreeCandidateNode(node));
34 public static void applyToModification(final DataTreeModification modification, final DataTreeCandidate candidate) {
35 if (modification instanceof CursorAwareDataTreeModification) {
36 try (DataTreeModificationCursor cursor = ((CursorAwareDataTreeModification) modification).createCursor(candidate.getRootPath())) {
37 applyNode(cursor, candidate.getRootNode());
40 applyNode(modification, candidate.getRootPath(), candidate.getRootNode());
44 private static void applyNode(final DataTreeModification modification, final YangInstanceIdentifier path, final DataTreeCandidateNode node) {
45 switch (node.getModificationType()) {
47 modification.delete(path);
48 LOG.debug("Modification {} deleted path {}", modification, path);
50 case SUBTREE_MODIFIED:
51 LOG.debug("Modification {} modified path {}", modification, path);
52 for (DataTreeCandidateNode child : node.getChildNodes()) {
53 applyNode(modification, path.node(child.getIdentifier()), child);
57 LOG.debug("Modification {} unmodified path {}", modification, path);
61 modification.write(path, node.getDataAfter().get());
62 LOG.debug("Modification {} written path {}", modification, path);
65 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
69 private static void applyNode(final DataTreeModificationCursor cursor, final DataTreeCandidateNode node) {
70 switch (node.getModificationType()) {
72 cursor.delete(node.getIdentifier());
74 case SUBTREE_MODIFIED:
75 cursor.enter(node.getIdentifier());
76 for (DataTreeCandidateNode child : node.getChildNodes()) {
77 applyNode(cursor, child);
85 cursor.write(node.getIdentifier(), node.getDataAfter().get());
88 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());