2 * Copyright (c) 2014 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.Function;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
19 final class OperationWithModification {
20 private static final Function<TreeNode, NormalizedNode<?, ?>> READ_DATA = new Function<TreeNode, NormalizedNode<?, ?>>() {
22 public NormalizedNode<?, ?> apply(final TreeNode input) {
23 return input.getData();
27 private final ModifiedNode modification;
28 private final ModificationApplyOperation applyOperation;
30 private OperationWithModification(final ModificationApplyOperation op, final ModifiedNode mod) {
31 this.modification = Preconditions.checkNotNull(mod);
32 this.applyOperation = Preconditions.checkNotNull(op);
35 void write(final NormalizedNode<?, ?> value) {
36 modification.write(value);
38 * Fast validation of structure, full validation on written data will be run during seal.
40 applyOperation.verifyStructure(value, false);
43 private void recursiveMerge(final NormalizedNode<?,?> data, final Version version) {
44 if (data instanceof NormalizedNodeContainer) {
45 @SuppressWarnings({ "rawtypes", "unchecked" })
46 final NormalizedNodeContainer<?,?, NormalizedNode<PathArgument, ?>> dataContainer =
47 (NormalizedNodeContainer) data;
50 * If there was write before on this node and it is of NormalizedNodeContainer type merge would overwrite
51 * our changes. So we create write modifications from data children to retain children created by previous
52 * write operation. These writes will then be pushed down in the tree while there are merge modifications
55 if (modification.getOperation() == LogicalOperation.WRITE) {
56 @SuppressWarnings({ "rawtypes", "unchecked" })
57 final NormalizedNodeContainer<?,?, NormalizedNode<PathArgument, ?>> oldDataContainer =
58 (NormalizedNodeContainer) modification.getWrittenValue();
59 for (final NormalizedNode<PathArgument, ?> c : oldDataContainer.getValue()) {
60 final PathArgument childId = c.getIdentifier();
62 // Acquire the child operation type if available, fall back to NONE
63 final Optional<ModifiedNode> maybeChild = modification.getChild(childId);
64 if (maybeChild.isPresent()) {
65 final ModifiedNode child = maybeChild.get();
66 final LogicalOperation op = child.getOperation();
67 if (op == LogicalOperation.TOUCH || op == LogicalOperation.NONE) {
71 // Not present, issue a write
72 forChild(childId, version).write(c);
76 for (final NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
77 final PathArgument childId = child.getIdentifier();
78 forChild(childId, version).recursiveMerge(child, version);
82 modification.merge(data);
85 void merge(final NormalizedNode<?, ?> data, final Version version) {
87 * A merge operation will end up overwriting parts of the tree, retaining others. We want to
88 * make sure we do not validate the complete resulting structure, but rather just what was
89 * written. In order to do that, we first pretend the data was written, run verification and
90 * then perform the merge -- with the explicit assumption that adding the newly-validated
91 * data with the previously-validated data will not result in invalid data.
93 * FIXME: Should be this moved to recursive merge and run for each node?
95 applyOperation.verifyStructure(data, false);
96 recursiveMerge(data, version);
100 modification.delete();
104 * Read a particular child. If the child has been modified and does not have a stable
105 * view, one will we instantiated with specified version.
111 Optional<NormalizedNode<?, ?>> read(final PathArgument child, final Version version) {
112 final Optional<ModifiedNode> maybeChild = modification.getChild(child);
113 if (maybeChild.isPresent()) {
114 final ModifiedNode childNode = maybeChild.get();
116 Optional<TreeNode> snapshot = childNode.getSnapshot();
117 if (snapshot == null) {
118 // Snapshot is not present, force instantiation
119 snapshot = applyOperation.getChild(child).get().apply(childNode, childNode.getOriginal(), version);
122 return snapshot.transform(READ_DATA);
125 Optional<TreeNode> snapshot = modification.getSnapshot();
126 if (snapshot == null) {
127 snapshot = apply(modification.getOriginal(), version);
130 if (snapshot.isPresent()) {
131 return snapshot.get().getChild(child).transform(READ_DATA);
134 return Optional.absent();
137 public ModifiedNode getModification() {
141 public ModificationApplyOperation getApplyOperation() {
142 return applyOperation;
145 public Optional<TreeNode> apply(final Optional<TreeNode> data, final Version version) {
146 return applyOperation.apply(modification, data, version);
149 public static OperationWithModification from(final ModificationApplyOperation operation,
150 final ModifiedNode modification) {
151 return new OperationWithModification(operation, modification);
154 private OperationWithModification forChild(final PathArgument childId, final Version version) {
155 final Optional<ModificationApplyOperation> maybeChildOp = applyOperation.getChild(childId);
156 Preconditions.checkArgument(maybeChildOp.isPresent(),
157 "Attempted to apply operation to non-existent child %s", childId);
159 final ModificationApplyOperation childOp = maybeChildOp.get();
160 final ModifiedNode childMod = modification.modifyChild(childId, childOp.getChildPolicy(), version);
162 return from(childOp, childMod);