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