BUG-3018: introduce DataTreeCandidateNodes
[yangtools.git] / yang / yang-data-api / src / main / java / org / opendaylight / yangtools / yang / data / api / schema / tree / DataTreeCandidates.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.api.schema.tree;
9
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
14 /**
15  * Utility class holding methods useful when dealing with {@link DataTreeCandidate} instances.
16  */
17 @Beta
18 public final class DataTreeCandidates {
19     private DataTreeCandidates() {
20         throw new UnsupportedOperationException();
21     }
22
23     public static DataTreeCandidate newDataTreeCandidate(final YangInstanceIdentifier rootPath, final DataTreeCandidateNode rootNode) {
24         return new DefaultDataTreeCandidate(rootPath, rootNode);
25     }
26
27     public static DataTreeCandidate fromNormalizedNode(final YangInstanceIdentifier rootPath, final NormalizedNode<?, ?> node) {
28         return new DefaultDataTreeCandidate(rootPath, new NormalizedNodeDataTreeCandidateNode(node));
29     }
30
31     public static void applyToModification(final DataTreeModification modification, final DataTreeCandidate candidate) {
32         applyNode(modification, candidate.getRootPath(), candidate.getRootNode());
33     }
34
35     private static void applyNode(final DataTreeModification modification, final YangInstanceIdentifier path, final DataTreeCandidateNode node) {
36         switch (node.getModificationType()) {
37         case DELETE:
38             modification.delete(path);
39             break;
40         case SUBTREE_MODIFIED:
41             for (DataTreeCandidateNode child : node.getChildNodes()) {
42                 applyNode(modification, path.node(child.getIdentifier()), child);
43             }
44             break;
45         case UNMODIFIED:
46             // No-op
47             break;
48         case WRITE:
49             modification.write(path, node.getDataAfter().get());
50             break;
51         default:
52             throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
53         }
54     }
55 }