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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayDeque;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Deque;
16 import java.util.Iterator;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
23 final class InMemoryDataTreeModificationCursor extends AbstractCursor<InMemoryDataTreeModification> implements DataTreeModificationCursor {
24 private final Deque<OperationWithModification> stack = new ArrayDeque<>();
26 InMemoryDataTreeModificationCursor(final InMemoryDataTreeModification parent, final YangInstanceIdentifier rootPath, final OperationWithModification rootOp) {
27 super(parent, rootPath);
31 private OperationWithModification resolveChildModification(final PathArgument child) {
32 getParent().upgradeIfPossible();
34 final OperationWithModification op = stack.peek();
35 final Optional<ModificationApplyOperation> potential = op.getApplyOperation().getChild(child);
36 if (potential.isPresent()) {
37 final ModificationApplyOperation operation = potential.get();
38 final ModifiedNode modification = op.getModification().modifyChild(child, operation,
39 getParent().getVersion());
41 return OperationWithModification.from(operation, modification);
44 // Node not found, construct its path
45 final Collection<PathArgument> path = new ArrayList<>();
46 path.addAll(getRootPath().getPathArguments());
48 final Iterator<OperationWithModification> it = stack.descendingIterator();
49 // Skip the first entry, as it's already accounted for in rootPath
52 while (it.hasNext()) {
53 path.add(it.next().getModification().getIdentifier());
56 throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.", path));
60 public void enter(@Nonnull final PathArgument child) {
61 stack.push(resolveChildModification(child));
65 public void enter(@Nonnull final Iterable<PathArgument> path) {
67 for (PathArgument child : path) {
69 stack.push(resolveChildModification(child));
70 } catch (Exception e) {
71 // Undo what we have done
72 for (int i = 0; i < depth; ++i) {
75 throw new IllegalArgumentException(e);
82 public void exit(final int depth) {
83 Preconditions.checkArgument(depth >= 0);
84 Preconditions.checkState(depth < stack.size());
86 for (int i = 0; i < depth; i++) {
92 public Optional<NormalizedNode<?, ?>> readNode(@Nonnull final PathArgument child) {
93 return stack.peek().read(child, getParent().getVersion());
97 public void delete(final PathArgument child) {
99 resolveChildModification(child).delete();
103 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
105 InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
106 resolveChildModification(child).merge(data, getParent().getVersion());
110 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
112 InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
113 resolveChildModification(child).write(data);