Merge "Bug 2363, Bug 2205. Beta version of LeafRefContext tree computation"
[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 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 /**
17  * Utility class holding methods useful when dealing with {@link DataTreeCandidate} instances.
18  */
19 @Beta
20 public final class DataTreeCandidates {
21     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
22     private DataTreeCandidates() {
23         throw new UnsupportedOperationException();
24     }
25
26     public static DataTreeCandidate newDataTreeCandidate(final YangInstanceIdentifier rootPath, final DataTreeCandidateNode rootNode) {
27         return new DefaultDataTreeCandidate(rootPath, rootNode);
28     }
29
30     public static DataTreeCandidate fromNormalizedNode(final YangInstanceIdentifier rootPath, final NormalizedNode<?, ?> node) {
31         return new DefaultDataTreeCandidate(rootPath, new NormalizedNodeDataTreeCandidateNode(node));
32     }
33
34     public static void applyToModification(final DataTreeModification modification, final DataTreeCandidate candidate) {
35         applyNode(modification, candidate.getRootPath(), candidate.getRootNode());
36     }
37
38     private static void applyNode(final DataTreeModification modification, final YangInstanceIdentifier path, final DataTreeCandidateNode node) {
39         switch (node.getModificationType()) {
40         case DELETE:
41             modification.delete(path);
42             LOG.debug("Modification {} deleted path {}", modification, path);
43             break;
44         case SUBTREE_MODIFIED:
45             LOG.debug("Modification {} modified path {}", modification, path);
46             for (DataTreeCandidateNode child : node.getChildNodes()) {
47                 applyNode(modification, path.node(child.getIdentifier()), child);
48             }
49             break;
50         case UNMODIFIED:
51             LOG.debug("Modification {} unmodified path {}", modification, path);
52             // No-op
53             break;
54         case WRITE:
55             modification.write(path, node.getDataAfter().get());
56             LOG.debug("Modification {} written path {}", modification, path);
57             break;
58         default:
59             throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
60         }
61     }
62 }