Fix eclipse/checkstyle warnings
[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
27     private DataTreeCandidates() {
28         throw new UnsupportedOperationException();
29     }
30
31     public static DataTreeCandidate newDataTreeCandidate(final YangInstanceIdentifier rootPath,
32             final DataTreeCandidateNode rootNode) {
33         return new DefaultDataTreeCandidate(rootPath, rootNode);
34     }
35
36     public static DataTreeCandidate fromNormalizedNode(final YangInstanceIdentifier rootPath,
37             final NormalizedNode<?, ?> node) {
38         return new DefaultDataTreeCandidate(rootPath, new NormalizedNodeDataTreeCandidateNode(node));
39     }
40
41     public static void applyToCursor(final DataTreeModificationCursor cursor, final DataTreeCandidate candidate) {
42         DataTreeCandidateNodes.applyToCursor(cursor, candidate.getRootNode());
43     }
44
45     public static void applyToModification(final DataTreeModification modification,
46             final DataTreeCandidate candidate) {
47         if (modification instanceof CursorAwareDataTreeModification) {
48             applyToCursorAwareModification((CursorAwareDataTreeModification) modification, candidate);
49             return;
50         }
51
52         final DataTreeCandidateNode node = candidate.getRootNode();
53         final YangInstanceIdentifier path = candidate.getRootPath();
54         switch (node.getModificationType()) {
55             case DELETE:
56                 modification.delete(path);
57                 LOG.debug("Modification {} deleted path {}", modification, path);
58                 break;
59             case SUBTREE_MODIFIED:
60                 LOG.debug("Modification {} modified path {}", modification, path);
61
62                 NodeIterator iterator = new NodeIterator(null, path, node.getChildNodes().iterator());
63                 do {
64                     iterator = iterator.next(modification);
65                 } while (iterator != null);
66                 break;
67             case UNMODIFIED:
68                 LOG.debug("Modification {} unmodified path {}", modification, path);
69                 // No-op
70                 break;
71             case WRITE:
72                 modification.write(path, node.getDataAfter().get());
73                 LOG.debug("Modification {} written path {}", modification, path);
74                 break;
75             default:
76                 throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
77         }
78     }
79
80     private static void applyToCursorAwareModification(final CursorAwareDataTreeModification modification,
81             final DataTreeCandidate candidate) {
82         final YangInstanceIdentifier candidatePath = candidate.getRootPath();
83         if (candidatePath.isEmpty()) {
84             try (DataTreeModificationCursor cursor = modification.createCursor(candidatePath)) {
85                 DataTreeCandidateNodes.applyRootToCursor(cursor, candidate.getRootNode());
86             }
87         } else {
88             try (DataTreeModificationCursor cursor = modification.createCursor(candidatePath.getParent())) {
89                 DataTreeCandidateNodes.applyRootedNodeToCursor(cursor, candidatePath, candidate.getRootNode());
90             }
91         }
92     }
93
94     private static final class NodeIterator {
95         private final Iterator<DataTreeCandidateNode> iterator;
96         private final YangInstanceIdentifier path;
97         private final NodeIterator parent;
98
99         NodeIterator(@Nullable final NodeIterator parent, @Nonnull final YangInstanceIdentifier path,
100                 @Nonnull final Iterator<DataTreeCandidateNode> iterator) {
101             this.iterator = Preconditions.checkNotNull(iterator);
102             this.path = Preconditions.checkNotNull(path);
103             this.parent = parent;
104         }
105
106         NodeIterator next(final DataTreeModification modification) {
107             while (iterator.hasNext()) {
108                 final DataTreeCandidateNode node = iterator.next();
109                 final YangInstanceIdentifier child = path.node(node.getIdentifier());
110
111                 switch (node.getModificationType()) {
112                     case DELETE:
113                         modification.delete(child);
114                         LOG.debug("Modification {} deleted path {}", modification, child);
115                         break;
116                     case APPEARED:
117                     case DISAPPEARED:
118                     case SUBTREE_MODIFIED:
119                         LOG.debug("Modification {} modified path {}", modification, child);
120                         return new NodeIterator(this, child, node.getChildNodes().iterator());
121                     case UNMODIFIED:
122                         LOG.debug("Modification {} unmodified path {}", modification, child);
123                         // No-op
124                         break;
125                     case WRITE:
126                         modification.write(child, node.getDataAfter().get());
127                         LOG.debug("Modification {} written path {}", modification, child);
128                         break;
129                     default:
130                         throw new IllegalArgumentException("Unsupported modification " + node.getModificationType());
131                 }
132             }
133
134             return parent;
135         }
136     }
137 }