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