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;
11 import com.google.common.base.Optional;
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.NormalizedNodeContainer;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.TreeNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
18 final class OperationWithModification {
20 private final ModifiedNode modification;
22 private final ModificationApplyOperation applyOperation;
24 private OperationWithModification(final ModificationApplyOperation op, final ModifiedNode mod) {
25 this.modification = mod;
26 this.applyOperation = op;
29 void write(final NormalizedNode<?, ?> value) {
30 modification.write(value);
31 applyOperation.verifyStructure(modification);
34 private void recursiveMerge(final NormalizedNode<?,?> data) {
35 if (data instanceof NormalizedNodeContainer<?,?,?>) {
36 @SuppressWarnings({ "rawtypes", "unchecked" })
37 NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> dataContainer = (NormalizedNodeContainer) data;
38 for (NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
39 PathArgument childId = child.getIdentifier();
40 forChild(childId).recursiveMerge(child);
44 modification.merge(data);
47 void merge(final NormalizedNode<?, ?> data) {
49 * A merge operation will end up overwriting parts of the tree, retaining others.
50 * We want to make sure we do not validate the complete resulting structure, but
51 * rather just what was written. In order to do that, we first pretend the data
52 * was written, run verification and then perform the merge -- with the explicit
53 * assumption that adding the newly-validated data with the previously-validated
54 * data will not result in invalid data.
56 applyOperation.verifyStructure(modification.asNewlyWritten(data));
61 modification.delete();
64 public ModifiedNode getModification() {
68 public ModificationApplyOperation getApplyOperation() {
69 return applyOperation;
72 public Optional<TreeNode> apply(final Optional<TreeNode> data, final Version version) {
73 return applyOperation.apply(modification, data, version);
76 public static OperationWithModification from(final ModificationApplyOperation operation,
77 final ModifiedNode modification) {
78 return new OperationWithModification(operation, modification);
81 private OperationWithModification forChild(final PathArgument childId) {
82 ModificationApplyOperation childOp = applyOperation.getChild(childId).get();
83 ModifiedNode childMod = modification.modifyChild(childId, childOp.getChildPolicy());
85 return from(childOp,childMod);