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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
22 final class InMemoryDataTreeModificationCursor extends AbstractCursor<InMemoryDataTreeModification> implements DataTreeModificationCursor {
23 private final Deque<OperationWithModification> stack = new ArrayDeque<>();
25 InMemoryDataTreeModificationCursor(final InMemoryDataTreeModification parent, final YangInstanceIdentifier rootPath, final OperationWithModification rootOp) {
26 super(parent, rootPath);
30 private OperationWithModification resolveChildModification(final PathArgument child) {
31 getParent().upgradeIfPossible();
33 final OperationWithModification op = stack.peek();
34 final Optional<ModificationApplyOperation> potential = op.getApplyOperation().getChild(child);
35 if (potential.isPresent()) {
36 final ModificationApplyOperation operation = potential.get();
37 final ModifiedNode modification = op.getModification().modifyChild(child, operation,
38 getParent().getVersion());
40 return OperationWithModification.from(operation, modification);
43 // Node not found, construct its path
44 final Collection<PathArgument> path = new ArrayList<>();
45 path.addAll(getRootPath().getPathArguments());
47 final Iterator<OperationWithModification> it = stack.descendingIterator();
48 // Skip the first entry, as it's already accounted for in rootPath
51 while (it.hasNext()) {
52 path.add(it.next().getModification().getIdentifier());
55 throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.", path));
59 public void enter(final PathArgument child) {
60 stack.push(resolveChildModification(child));
64 public void enter(final Iterable<PathArgument> path) {
66 for (PathArgument child : path) {
68 stack.push(resolveChildModification(child));
69 } catch (Exception e) {
70 // Undo what we have done
71 for (int i = 0; i < depth; ++i) {
74 throw new IllegalArgumentException(e);
81 public void exit(final int depth) {
82 Preconditions.checkArgument(depth >= 0);
83 Preconditions.checkState(depth < stack.size());
85 for (int i = 0; i < depth; i++) {
91 public Optional<NormalizedNode<?, ?>> readNode(final PathArgument child) {
92 return stack.peek().read(child, getParent().getVersion());
96 public void delete(final PathArgument child) {
98 resolveChildModification(child).delete();
102 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
104 InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
105 resolveChildModification(child).merge(data, getParent().getVersion());
109 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
111 InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
112 resolveChildModification(child).write(data);