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 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;
24 final class InMemoryDataTreeModificationCursor extends AbstractCursor<InMemoryDataTreeModification>
25 implements DataTreeModificationCursor {
26 private final Deque<OperationWithModification> stack = new ArrayDeque<>();
28 InMemoryDataTreeModificationCursor(final InMemoryDataTreeModification parent, final YangInstanceIdentifier rootPath,
29 final OperationWithModification rootOp) {
30 super(parent, rootPath);
34 private OperationWithModification resolveChildModification(final PathArgument child) {
35 getParent().upgradeIfPossible();
37 final OperationWithModification op = stack.peek();
38 final ModificationApplyOperation operation = op.getApplyOperation().childByArg(child);
39 if (operation != null) {
40 final ModifiedNode modification = op.getModification().modifyChild(child, operation,
41 getParent().getVersion());
43 return OperationWithModification.from(operation, modification);
46 // Node not found, construct its path
47 final Collection<PathArgument> path = new ArrayList<>();
48 path.addAll(getRootPath().getPathArguments());
50 final Iterator<OperationWithModification> it = stack.descendingIterator();
51 // Skip the first entry, as it's already accounted for in rootPath
54 while (it.hasNext()) {
55 path.add(it.next().getModification().getIdentifier());
58 throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.", path));
62 public void enter(final PathArgument child) {
63 stack.push(resolveChildModification(child));
67 @SuppressWarnings("checkstyle:illegalCatch")
68 public void enter(final Iterable<PathArgument> path) {
70 for (PathArgument child : path) {
72 stack.push(resolveChildModification(child));
73 } catch (Exception e) {
74 // Undo what we have done
75 for (int i = 0; i < depth; ++i) {
78 throw new IllegalArgumentException(e);
85 public void exit(final int depth) {
86 checkArgument(depth >= 0);
87 checkState(depth < stack.size());
89 for (int i = 0; i < depth; i++) {
95 public Optional<NormalizedNode<?, ?>> readNode(final PathArgument child) {
96 return stack.peek().read(child, getParent().getVersion());
100 public void delete(final PathArgument child) {
102 resolveChildModification(child).delete();
106 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
108 InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
109 resolveChildModification(child).merge(data, getParent().getVersion());
113 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
115 InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
116 resolveChildModification(child).write(data);