BUG-4295: fix merge callsite
[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,
38                 getParent().getVersion());
39
40             return OperationWithModification.from(operation, modification);
41         }
42
43         // Node not found, construct its path
44         final Collection<PathArgument> path = new ArrayList<>();
45         path.addAll(getRootPath().getPathArguments());
46
47         final Iterator<OperationWithModification> it = stack.descendingIterator();
48         // Skip the first entry, as it's already accounted for in rootPath
49         it.next();
50
51         while (it.hasNext()) {
52             path.add(it.next().getModification().getIdentifier());
53         }
54
55         throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.", path));
56     }
57
58     @Override
59     public void enter(final PathArgument child) {
60         stack.push(resolveChildModification(child));
61     }
62
63     @Override
64     public void enter(final Iterable<PathArgument> path) {
65         int depth = 0;
66         for (PathArgument child : path) {
67             try {
68                 stack.push(resolveChildModification(child));
69             } catch (Exception e) {
70                 // Undo what we have done
71                 for (int i = 0; i < depth; ++i) {
72                     stack.pop();
73                 }
74                 throw new IllegalArgumentException(e);
75             }
76             depth++;
77         }
78     }
79
80     @Override
81     public void exit(final int depth) {
82         Preconditions.checkArgument(depth >= 0);
83         Preconditions.checkState(depth < stack.size());
84
85         for (int i = 0; i < depth; i++) {
86             stack.pop();
87         }
88     }
89
90     @Override
91     public Optional<NormalizedNode<?, ?>> readNode(final PathArgument child) {
92         return stack.peek().read(child, getParent().getVersion());
93     }
94
95     @Override
96     public void delete(final PathArgument child) {
97         ensureNotClosed();
98         resolveChildModification(child).delete();
99     }
100
101     @Override
102     public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
103         ensureNotClosed();
104         InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
105         resolveChildModification(child).merge(data, getParent().getVersion());
106     }
107
108     @Override
109     public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
110         ensureNotClosed();
111         InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
112         resolveChildModification(child).write(data);
113     }
114 }