BUG-509: Move DataTree concepts into separate package
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / 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;
9
10 import org.opendaylight.controller.md.sal.dom.store.impl.tree.ModificationApplyOperation;
11 import org.opendaylight.controller.md.sal.dom.store.impl.tree.data.NodeModification;
12 import org.opendaylight.controller.md.sal.dom.store.impl.tree.data.StoreMetadataNode;
13 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15
16 import com.google.common.base.Optional;
17 import com.google.common.primitives.UnsignedLong;
18
19 public class OperationWithModification {
20
21     private final NodeModification modification;
22
23     private final ModificationApplyOperation applyOperation;
24
25     private OperationWithModification(final ModificationApplyOperation op, final NodeModification mod) {
26         this.modification = mod;
27         this.applyOperation = op;
28     }
29
30     public OperationWithModification write(final NormalizedNode<?, ?> value) {
31         modification.write(value);
32         applyOperation.verifyStructure(modification);
33         return this;
34     }
35
36     public OperationWithModification delete() {
37         modification.delete();
38         return this;
39     }
40
41     public NodeModification getModification() {
42         return modification;
43     }
44
45     public ModificationApplyOperation getApplyOperation() {
46         return applyOperation;
47     }
48
49     public Optional<StoreMetadataNode> apply(final Optional<StoreMetadataNode> data, final UnsignedLong subtreeVersion) {
50         return applyOperation.apply(modification, data, subtreeVersion);
51     }
52
53     public static OperationWithModification from(final ModificationApplyOperation operation,
54             final NodeModification modification) {
55         return new OperationWithModification(operation, modification);
56
57     }
58
59     public void merge(final NormalizedNode<?, ?> data) {
60         modification.merge(data);
61         applyOperation.verifyStructure(modification);
62
63     }
64
65     public OperationWithModification forChild(final PathArgument childId) {
66         NodeModification childMod = modification.modifyChild(childId);
67         Optional<ModificationApplyOperation> childOp = applyOperation.getChild(childId);
68         return from(childOp.get(),childMod);
69     }
70 }