2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
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;
25 final class InMemoryDataTreeModificationCursor extends AbstractCursor<InMemoryDataTreeModification>
26 implements DataTreeModificationCursor {
27 private final Deque<OperationWithModification> stack = new ArrayDeque<>();
29 InMemoryDataTreeModificationCursor(final InMemoryDataTreeModification parent, final YangInstanceIdentifier rootPath,
30 final OperationWithModification rootOp) {
31 super(parent, rootPath);
35 private OperationWithModification resolveChildModification(final PathArgument child) {
36 getParent().upgradeIfPossible();
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());
45 return OperationWithModification.from(operation, modification);
48 // Node not found, construct its path
49 final Collection<PathArgument> path = new ArrayList<>();
50 path.addAll(getRootPath().getPathArguments());
52 final Iterator<OperationWithModification> it = stack.descendingIterator();
53 // Skip the first entry, as it's already accounted for in rootPath
56 while (it.hasNext()) {
57 path.add(it.next().getModification().getIdentifier());
60 throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.", path));
64 public void enter(@Nonnull final PathArgument child) {
65 stack.push(resolveChildModification(child));
69 @SuppressWarnings("checkstyle:illegalCatch")
70 public void enter(@Nonnull final Iterable<PathArgument> path) {
72 for (PathArgument child : path) {
74 stack.push(resolveChildModification(child));
75 } catch (Exception e) {
76 // Undo what we have done
77 for (int i = 0; i < depth; ++i) {
80 throw new IllegalArgumentException(e);
87 public void exit(final int depth) {
88 checkArgument(depth >= 0);
89 checkState(depth < stack.size());
91 for (int i = 0; i < depth; i++) {
97 public Optional<NormalizedNode<?, ?>> readNode(@Nonnull final PathArgument child) {
98 return stack.peek().read(child, getParent().getVersion());
102 public void delete(final PathArgument child) {
104 resolveChildModification(child).delete();
108 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
110 InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
111 resolveChildModification(child).merge(data, getParent().getVersion());
115 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
117 InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
118 resolveChildModification(child).write(data);