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