Merge "Bug:1238 - Revert changes to SshClientAdapter."
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / tree / data / 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.controller.md.sal.dom.store.impl.tree.data;
9
10 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.TreeNode;
11 import org.opendaylight.controller.md.sal.dom.store.impl.tree.spi.Version;
12 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
13 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
14
15 import com.google.common.base.Optional;
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         ModifiedNode childMod = modification.modifyChild(childId);
65         Optional<ModificationApplyOperation> childOp = applyOperation.getChild(childId);
66         return from(childOp.get(),childMod);
67     }
68 }