Merge "BUG-576: fixed invalid key used to search map of modules."
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / OperationWithModification.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10
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.tree.spi.TreeNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
16
17 final class OperationWithModification {
18
19     private final ModifiedNode modification;
20
21     private final ModificationApplyOperation applyOperation;
22
23     private OperationWithModification(final ModificationApplyOperation op, final ModifiedNode mod) {
24         this.modification = mod;
25         this.applyOperation = op;
26     }
27
28     public OperationWithModification write(final NormalizedNode<?, ?> value) {
29         modification.write(value);
30         applyOperation.verifyStructure(modification);
31         return this;
32     }
33
34     public OperationWithModification delete() {
35         modification.delete();
36         return this;
37     }
38
39     public ModifiedNode getModification() {
40         return modification;
41     }
42
43     public ModificationApplyOperation getApplyOperation() {
44         return applyOperation;
45     }
46
47     public Optional<TreeNode> apply(final Optional<TreeNode> data, final Version version) {
48         return applyOperation.apply(modification, data, version);
49     }
50
51     public static OperationWithModification from(final ModificationApplyOperation operation,
52             final ModifiedNode modification) {
53         return new OperationWithModification(operation, modification);
54
55     }
56
57     public void merge(final NormalizedNode<?, ?> data) {
58         modification.merge(data);
59         applyOperation.verifyStructure(modification);
60
61     }
62
63     public OperationWithModification forChild(final PathArgument childId) {
64         ModificationApplyOperation childOp = applyOperation.getChild(childId).get();
65         boolean isOrdered = true;
66         if (childOp instanceof SchemaAwareApplyOperation) {
67             isOrdered = ((SchemaAwareApplyOperation) childOp).isOrdered();
68         }
69         ModifiedNode childMod = modification.modifyChild(childId, isOrdered);
70
71         return from(childOp,childMod);
72     }
73 }