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