faed458a601c6d0aaca70f111e4cdeaaad5bca89
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / InMemoryDataTreeModificationCursor.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.tree.impl;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12
13 import java.util.ArrayDeque;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Deque;
17 import java.util.Iterator;
18 import java.util.Optional;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModificationCursor;
23
24 final class InMemoryDataTreeModificationCursor extends AbstractCursor<InMemoryDataTreeModification>
25         implements DataTreeModificationCursor {
26     private final Deque<OperationWithModification> stack = new ArrayDeque<>();
27
28     InMemoryDataTreeModificationCursor(final InMemoryDataTreeModification parent, final YangInstanceIdentifier rootPath,
29             final OperationWithModification rootOp) {
30         super(parent, rootPath);
31         stack.push(rootOp);
32     }
33
34     private OperationWithModification resolveChildModification(final PathArgument child) {
35         getParent().upgradeIfPossible();
36
37         final OperationWithModification op = stack.peek();
38         final ModificationApplyOperation operation = op.getApplyOperation().childByArg(child);
39         if (operation != null) {
40             final ModifiedNode modification = op.getModification().modifyChild(child, operation,
41                 getParent().getVersion());
42
43             return OperationWithModification.from(operation, modification);
44         }
45
46         // Node not found, construct its path
47         final Collection<PathArgument> path = new ArrayList<>();
48         path.addAll(getRootPath().getPathArguments());
49
50         final Iterator<OperationWithModification> it = stack.descendingIterator();
51         // Skip the first entry, as it's already accounted for in rootPath
52         it.next();
53
54         while (it.hasNext()) {
55             path.add(it.next().getModification().getIdentifier());
56         }
57
58         throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.", path));
59     }
60
61     @Override
62     public void enter(final PathArgument child) {
63         stack.push(resolveChildModification(child));
64     }
65
66     @Override
67     @SuppressWarnings("checkstyle:illegalCatch")
68     public void enter(final Iterable<PathArgument> path) {
69         int depth = 0;
70         for (PathArgument child : path) {
71             try {
72                 stack.push(resolveChildModification(child));
73             } catch (Exception e) {
74                 // Undo what we have done
75                 for (int i = 0; i < depth; ++i) {
76                     stack.pop();
77                 }
78                 throw new IllegalArgumentException(e);
79             }
80             depth++;
81         }
82     }
83
84     @Override
85     public void exit(final int depth) {
86         checkArgument(depth >= 0);
87         checkState(depth < stack.size());
88
89         for (int i = 0; i < depth; i++) {
90             stack.pop();
91         }
92     }
93
94     @Override
95     public Optional<NormalizedNode> readNode(final PathArgument child) {
96         return stack.peek().read(child, getParent().getVersion());
97     }
98
99     @Override
100     public void delete(final PathArgument child) {
101         ensureNotClosed();
102         resolveChildModification(child).delete();
103     }
104
105     @Override
106     public void merge(final PathArgument child, final NormalizedNode data) {
107         ensureNotClosed();
108         InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
109         resolveChildModification(child).merge(data, getParent().getVersion());
110     }
111
112     @Override
113     public void write(final PathArgument child, final NormalizedNode data) {
114         ensureNotClosed();
115         InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
116         resolveChildModification(child).write(data);
117     }
118 }