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.Optional;
11 import com.google.common.base.Preconditions;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
13 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
17 final class OperationWithModification {
18 private final ModificationApplyOperation applyOperation;
19 private final ModifiedNode modification;
21 private OperationWithModification(final ModificationApplyOperation op, final ModifiedNode mod) {
22 this.applyOperation = Preconditions.checkNotNull(op);
23 this.modification = Preconditions.checkNotNull(mod);
26 void write(final NormalizedNode<?, ?> value) {
27 modification.write(value);
29 * Fast validation of structure, full validation on written data will be run during seal.
31 applyOperation.verifyStructure(value, false);
34 void merge(final NormalizedNode<?, ?> data, final Version version) {
36 * A merge operation will end up overwriting parts of the tree, retaining others. We want to
37 * make sure we do not validate the complete resulting structure, but rather just what was
38 * written. In order to do that, we first pretend the data was written, run verification and
39 * then perform the merge -- with the explicit assumption that adding the newly-validated
40 * data with the previously-validated data will not result in invalid data.
42 * We perform only quick validation here, full validation will be applied as-needed during
43 * preparation, as the merge is reconciled with current state.
45 applyOperation.verifyStructure(data, false);
46 applyOperation.mergeIntoModifiedNode(modification, data, version);
50 modification.delete();
54 * Read a particular child. If the child has been modified and does not have a stable
55 * view, one will we instantiated with specified version.
61 Optional<NormalizedNode<?, ?>> read(final PathArgument child, final Version version) {
62 final Optional<ModifiedNode> maybeChild = modification.getChild(child);
63 if (maybeChild.isPresent()) {
64 final ModifiedNode childNode = maybeChild.get();
66 Optional<TreeNode> snapshot = childNode.getSnapshot();
67 if (snapshot == null) {
68 // Snapshot is not present, force instantiation
69 snapshot = applyOperation.getChild(child).get().apply(childNode, childNode.getOriginal(), version);
72 return snapshot.transform(TreeNode::getData);
75 Optional<TreeNode> snapshot = modification.getSnapshot();
76 if (snapshot == null) {
77 snapshot = apply(modification.getOriginal(), version);
80 if (snapshot.isPresent()) {
81 return snapshot.get().getChild(child).transform(TreeNode::getData);
84 return Optional.absent();
87 public ModifiedNode getModification() {
91 public ModificationApplyOperation getApplyOperation() {
92 return applyOperation;
95 public Optional<TreeNode> apply(final Optional<TreeNode> data, final Version version) {
96 return applyOperation.apply(modification, data, version);
99 public static OperationWithModification from(final ModificationApplyOperation operation,
100 final ModifiedNode modification) {
101 return new OperationWithModification(operation, modification);