BUG-509: reorganize data tree abstractions.
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / store / impl / MutableDataTree.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 static com.google.common.base.Preconditions.checkState;
11
12 import java.util.Map.Entry;
13 import java.util.concurrent.atomic.AtomicBoolean;
14
15 import org.opendaylight.controller.md.sal.dom.store.impl.tree.NodeModification;
16 import org.opendaylight.controller.md.sal.dom.store.impl.tree.StoreMetadataNode;
17 import org.opendaylight.controller.md.sal.dom.store.impl.tree.TreeNodeUtils;
18 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
22 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.base.Optional;
27 import com.google.common.base.Preconditions;
28
29 /*
30  * FIXME: the thread safety of concurrent write/delete/read/seal operations
31  *        needs to be evaluated.
32  */
33 class MutableDataTree {
34     private static final Logger LOG = LoggerFactory.getLogger(MutableDataTree.class);
35     private final AtomicBoolean sealed = new AtomicBoolean();
36     private final ModificationApplyOperation strategyTree;
37     private final NodeModification rootModification;
38     private final DataTree.Snapshot snapshot;
39
40     private MutableDataTree(final DataTree.Snapshot snapshot, final ModificationApplyOperation strategyTree) {
41         this.snapshot = Preconditions.checkNotNull(snapshot);
42         this.strategyTree = Preconditions.checkNotNull(strategyTree);
43         this.rootModification = NodeModification.createUnmodified(snapshot.getRootNode());
44     }
45
46     public void write(final InstanceIdentifier path, final NormalizedNode<?, ?> value) {
47         checkSealed();
48         resolveModificationFor(path).write(value);
49     }
50
51     public void merge(final InstanceIdentifier path, final NormalizedNode<?, ?> data) {
52         checkSealed();
53         mergeImpl(resolveModificationFor(path),data);
54     }
55
56     private void mergeImpl(final OperationWithModification op,final NormalizedNode<?,?> data) {
57
58         if(data instanceof NormalizedNodeContainer<?,?,?>) {
59             @SuppressWarnings({ "rawtypes", "unchecked" })
60             NormalizedNodeContainer<?,?,NormalizedNode<PathArgument, ?>> dataContainer = (NormalizedNodeContainer) data;
61             for(NormalizedNode<PathArgument, ?> child : dataContainer.getValue()) {
62                 PathArgument childId = child.getIdentifier();
63                 mergeImpl(op.forChild(childId), child);
64             }
65         }
66         op.merge(data);
67     }
68
69     public void delete(final InstanceIdentifier path) {
70         checkSealed();
71         resolveModificationFor(path).delete();
72     }
73
74     public Optional<NormalizedNode<?, ?>> read(final InstanceIdentifier path) {
75         Entry<InstanceIdentifier, NodeModification> modification = TreeNodeUtils.findClosestsOrFirstMatch(rootModification, path, NodeModification.IS_TERMINAL_PREDICATE);
76
77         Optional<StoreMetadataNode> result = resolveSnapshot(modification);
78         if (result.isPresent()) {
79             NormalizedNode<?, ?> data = result.get().getData();
80             return NormalizedNodeUtils.findNode(modification.getKey(), data, path);
81         }
82         return Optional.absent();
83     }
84
85     private Optional<StoreMetadataNode> resolveSnapshot(
86             final Entry<InstanceIdentifier, NodeModification> keyModification) {
87         InstanceIdentifier path = keyModification.getKey();
88         NodeModification modification = keyModification.getValue();
89         return resolveSnapshot(path, modification);
90     }
91
92     private Optional<StoreMetadataNode> resolveSnapshot(final InstanceIdentifier path,
93             final NodeModification modification) {
94         try {
95             Optional<Optional<StoreMetadataNode>> potentialSnapshot = modification.getSnapshotCache();
96             if(potentialSnapshot.isPresent()) {
97                 return potentialSnapshot.get();
98             }
99             return resolveModificationStrategy(path).apply(modification, modification.getOriginal(),
100                     StoreUtils.increase(snapshot.getRootNode().getSubtreeVersion()));
101         } catch (Exception e) {
102             LOG.error("Could not create snapshot for {}:{}", path,modification,e);
103             throw e;
104         }
105     }
106
107     private ModificationApplyOperation resolveModificationStrategy(final InstanceIdentifier path) {
108         LOG.trace("Resolving modification apply strategy for {}", path);
109         return TreeNodeUtils.findNodeChecked(strategyTree, path);
110     }
111
112     private OperationWithModification resolveModificationFor(final InstanceIdentifier path) {
113         NodeModification modification = rootModification;
114         // We ensure strategy is present.
115         ModificationApplyOperation operation = resolveModificationStrategy(path);
116         for (PathArgument pathArg : path.getPath()) {
117             modification = modification.modifyChild(pathArg);
118         }
119         return OperationWithModification.from(operation, modification);
120     }
121
122     public static MutableDataTree from(final DataTree.Snapshot snapshot, final ModificationApplyOperation resolver) {
123         return new MutableDataTree(snapshot, resolver);
124     }
125
126     public void seal() {
127         final boolean success = sealed.compareAndSet(false, true);
128         Preconditions.checkState(success, "Attempted to seal an already-sealed Data Tree.");
129         rootModification.seal();
130     }
131
132     private void checkSealed() {
133         checkState(!sealed.get(), "Data Tree is sealed. No further modifications allowed.");
134     }
135
136     protected NodeModification getRootModification() {
137         return rootModification;
138     }
139
140     @Override
141     public String toString() {
142         return "MutableDataTree [modification=" + rootModification + "]";
143     }
144 }