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