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.getChildPolicy());
39 return OperationWithModification.from(operation, modification);
42 // Node not found, construct its path
43 final Collection<PathArgument> path = new ArrayList<>();
44 path.addAll(getRootPath().getPathArguments());
46 final Iterator<OperationWithModification> it = stack.descendingIterator();
47 // Skip the first entry, as it's already accounted for in rootPath
50 while (it.hasNext()) {
51 path.add(it.next().getModification().getIdentifier());
54 throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.", path));
58 public void enter(final PathArgument child) {
59 stack.push(resolveChildModification(child));
63 public void enter(final Iterable<PathArgument> path) {
65 for (PathArgument child : path) {
67 stack.push(resolveChildModification(child));
68 } catch (Exception e) {
69 // Undo what we have done
70 for (int i = 0; i < depth; ++i) {
73 throw new IllegalArgumentException(e);
80 public void exit(final int depth) {
81 Preconditions.checkArgument(depth >= 0);
82 Preconditions.checkState(depth < stack.size());
84 for (int i = 0; i < depth; i++) {
90 public Optional<NormalizedNode<?, ?>> readNode(final PathArgument child) {
91 return stack.peek().read(child, getParent().getVersion());
95 public void delete(final PathArgument child) {
97 resolveChildModification(child).delete();
101 public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
103 InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
104 resolveChildModification(child).merge(data);
108 public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
110 InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
111 resolveChildModification(child).write(data);