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