Merge "Fix sonar issues in AnyXmlEffectiveStatementImpl"
[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 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;
17
18 /**
19  * Utility class holding methods useful when dealing with {@link DataTreeCandidate} instances.
20  */
21 @Beta
22 public final class DataTreeCandidates {
23     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidates.class);
24     private DataTreeCandidates() {
25         throw new UnsupportedOperationException();
26     }
27
28     public static DataTreeCandidate newDataTreeCandidate(final YangInstanceIdentifier rootPath, final DataTreeCandidateNode rootNode) {
29         return new DefaultDataTreeCandidate(rootPath, rootNode);
30     }
31
32     public static DataTreeCandidate fromNormalizedNode(final YangInstanceIdentifier rootPath, final NormalizedNode<?, ?> node) {
33         return new DefaultDataTreeCandidate(rootPath, new NormalizedNodeDataTreeCandidateNode(node));
34     }
35
36     public static void applyToCursor(final DataTreeModificationCursor cursor, final DataTreeCandidate candidate) {
37         DataTreeCandidateNodes.applyToCursor(cursor, candidate.getRootNode());
38     }
39
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);
44             }
45             return;
46         }
47
48         final DataTreeCandidateNode node = candidate.getRootNode();
49         final YangInstanceIdentifier path = candidate.getRootPath();
50         switch (node.getModificationType()) {
51         case DELETE:
52             modification.delete(path);
53             LOG.debug("Modification {} deleted path {}", modification, path);
54             break;
55         case SUBTREE_MODIFIED:
56             LOG.debug("Modification {} modified path {}", modification, path);
57
58             NodeIterator iterator = new NodeIterator(null, path, node.getChildNodes().iterator());
59             do {
60                 iterator = iterator.next(modification);
61             } while (iterator != null);
62             break;
63         case UNMODIFIED:
64             LOG.debug("Modification {} unmodified path {}", modification, path);
65             // No-op
66             break;
67         case WRITE:
68             modification.write(path, node.getDataAfter().get());
69             LOG.debug("Modification {} written path {}", modification, path);
70             break;
71         default:
72             throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
73         }
74     }
75
76     private static final class NodeIterator {
77         private final Iterator<DataTreeCandidateNode> iterator;
78         private final YangInstanceIdentifier path;
79         private final NodeIterator parent;
80
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);
85         }
86
87         NodeIterator next(final DataTreeModification modification) {
88             while (iterator.hasNext()) {
89                 final DataTreeCandidateNode node = iterator.next();
90                 final YangInstanceIdentifier child = path.node(node.getIdentifier());
91
92                 switch (node.getModificationType()) {
93                 case DELETE:
94                     modification.delete(child);
95                     LOG.debug("Modification {} deleted path {}", modification, child);
96                     break;
97                 case SUBTREE_MODIFIED:
98                     LOG.debug("Modification {} modified path {}", modification, child);
99                     return new NodeIterator(this, child, node.getChildNodes().iterator());
100                 case UNMODIFIED:
101                     LOG.debug("Modification {} unmodified path {}", modification, child);
102                     // No-op
103                     break;
104                 case WRITE:
105                     modification.write(child, node.getDataAfter().get());
106                     LOG.debug("Modification {} written path {}", modification, child);
107                     break;
108                 default:
109                     throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
110                 }
111             }
112
113             return parent;
114         }
115     }
116 }