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) {
44 if (data instanceof NormalizedNodeContainer) {
45 @SuppressWarnings({ "rawtypes", "unchecked" })
47 NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> dataContainer = (NormalizedNodeContainer) data;
50 * if there was write before on this node and it is of NormalizedNodeContainer type
51 * merge would overwrite our changes. So we create write modifications from data children to
52 * retain children created by past write operation.
53 * These writes will then be pushed down in the tree while there are merge modifications on these children
55 if (modification.getOperation() == LogicalOperation.WRITE) {
56 @SuppressWarnings({ "rawtypes", "unchecked" })
58 NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> odlDataContainer =
59 (NormalizedNodeContainer) modification.getWrittenValue();
60 for (final NormalizedNode<PathArgument, ?> child : odlDataContainer.getValue()) {
61 final PathArgument childId = child.getIdentifier();
62 forChild(childId).write(child);
65 for (final NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
66 final PathArgument childId = child.getIdentifier();
67 forChild(childId).recursiveMerge(child);
71 modification.merge(data);
74 void merge(final NormalizedNode<?, ?> data) {
76 * A merge operation will end up overwriting parts of the tree, retaining others. We want to
77 * make sure we do not validate the complete resulting structure, but rather just what was
78 * written. In order to do that, we first pretend the data was written, run verification and
79 * then perform the merge -- with the explicit assumption that adding the newly-validated
80 * data with the previously-validated data will not result in invalid data.
82 * FIXME: Should be this moved to recursive merge and run for each node?
84 applyOperation.verifyStructure(data, false);
89 modification.delete();
93 * Read a particular child. If the child has been modified and does not have a stable
94 * view, one will we instantiated with specified version.
100 Optional<NormalizedNode<?, ?>> read(final PathArgument child, final Version version) {
101 final Optional<ModifiedNode> maybeChild = modification.getChild(child);
102 if (maybeChild.isPresent()) {
103 final ModifiedNode childNode = maybeChild.get();
105 Optional<TreeNode> snapshot = childNode.getSnapshot();
106 if (snapshot == null) {
107 // Snapshot is not present, force instantiation
108 snapshot = applyOperation.getChild(child).get().apply(childNode, childNode.getOriginal(), version);
111 return snapshot.transform(READ_DATA);
114 Optional<TreeNode> snapshot = modification.getSnapshot();
115 if (snapshot == null) {
116 snapshot = apply(modification.getOriginal(), version);
119 if (snapshot.isPresent()) {
120 return snapshot.get().getChild(child).transform(READ_DATA);
123 return Optional.absent();
126 public ModifiedNode getModification() {
130 public ModificationApplyOperation getApplyOperation() {
131 return applyOperation;
134 public Optional<TreeNode> apply(final Optional<TreeNode> data, final Version version) {
135 return applyOperation.apply(modification, data, version);
138 public static OperationWithModification from(final ModificationApplyOperation operation,
139 final ModifiedNode modification) {
140 return new OperationWithModification(operation, modification);
143 private OperationWithModification forChild(final PathArgument childId) {
144 final Optional<ModificationApplyOperation> maybeChildOp = applyOperation.getChild(childId);
145 Preconditions.checkArgument(maybeChildOp.isPresent(), "Attempted to apply operation to non-existent child %s", childId);
147 final ModificationApplyOperation childOp = maybeChildOp.get();
148 final ModifiedNode childMod = modification.modifyChild(childId, childOp.getChildPolicy());
150 return from(childOp, childMod);