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 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());
44 return OperationWithModification.from(operation, modification);
47 // Node not found, construct its path
48 final Collection<PathArgument> path = new ArrayList<>();
49 path.addAll(getRootPath().getPathArguments());
51 final Iterator<OperationWithModification> it = stack.descendingIterator();
52 // Skip the first entry, as it's already accounted for in rootPath
55 while (it.hasNext()) {
56 path.add(it.next().getModification().getIdentifier());
59 throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.", path));
63 public void enter(final PathArgument child) {
64 stack.push(resolveChildModification(child));
68 @SuppressWarnings("checkstyle:illegalCatch")
69 public void enter(final Iterable<PathArgument> path) {
71 for (PathArgument child : path) {
73 stack.push(resolveChildModification(child));
74 } catch (Exception e) {
75 // Undo what we have done
76 for (int i = 0; i < depth; ++i) {
79 throw new IllegalArgumentException(e);
86 public void exit(final int depth) {
87 checkArgument(depth >= 0);
88 checkState(depth < stack.size());
90 for (int i = 0; i < depth; i++) {
96 public Optional<NormalizedNode<?, ?>> readNode(final PathArgument child) {
97 return stack.peek().read(child, getParent().getVersion());
101 public void delete(final PathArgument child) {
103 resolveChildModification(child).delete();
107 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
109 InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
110 resolveChildModification(child).merge(data, getParent().getVersion());
114 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
116 InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
117 resolveChildModification(child).write(data);