Reduce use of deprecated methods
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / legacy / sharded / adapter / ShardedDOMDataBrokerDelegatingReadWriteTransaction.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.controller.md.sal.dom.broker.impl.legacy.sharded.adapter;
10
11 import static com.google.common.base.Preconditions.checkNotNull;
12 import static com.google.common.base.Preconditions.checkState;
13
14 import com.google.common.base.Optional;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.Lists;
17 import com.google.common.collect.Maps;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import com.google.common.util.concurrent.FluentFuture;
20 import com.google.common.util.concurrent.FutureCallback;
21 import com.google.common.util.concurrent.Futures;
22 import com.google.common.util.concurrent.ListenableFuture;
23 import com.google.common.util.concurrent.MoreExecutors;
24 import com.google.common.util.concurrent.SettableFuture;
25 import java.util.Map;
26 import java.util.Queue;
27 import javax.annotation.Nonnull;
28 import javax.annotation.concurrent.NotThreadSafe;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
31 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
32 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
33 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
34 import org.opendaylight.mdsal.common.api.CommitInfo;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
38 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
39 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
40 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
41 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43
44 /**
45  * Read/write transaction that delegates write and initial read to
46  * {@link org.opendaylight.mdsal.dom.broker.ShardedDOMWriteTransactionAdapter}
47  * and {@link org.opendaylight.mdsal.dom.broker.ShardedDOMReadTransactionAdapter}
48  * respectively. These two in turn rely on shard aware implementation of
49  * {@link org.opendaylight.mdsal.dom.api.DOMDataTreeService}.
50  *
51  * <p>
52  * Since reading data distributed on different subshards is not guaranteed to
53  * return all relevant data, best effort is to try to operate only on single
54  * subtree in conceptual data tree. We define this subtree by first write
55  * operation performed on transaction. All next read and write operations
56  * should be performed just in this initial subtree.
57  */
58 // FIXME explicitly enforce just one subtree requirement
59 @NotThreadSafe
60 class ShardedDOMDataBrokerDelegatingReadWriteTransaction implements DOMDataReadWriteTransaction {
61     private final DOMDataReadOnlyTransaction readTxDelegate;
62     private final DOMDataWriteTransaction writeTxDelegate;
63     private final Object txIdentifier;
64     private final ImmutableMap<LogicalDatastoreType, Queue<Modification>> modificationHistoryMap;
65     private final ImmutableMap<LogicalDatastoreType, DataTreeSnapshot> snapshotMap;
66     private final Map<LogicalDatastoreType, ListenableFuture<Optional<NormalizedNode<?, ?>>>> initialReadMap;
67     private YangInstanceIdentifier root = null;
68
69     ShardedDOMDataBrokerDelegatingReadWriteTransaction(final Object readWriteTxId, final SchemaContext ctx,
70                                                               final DOMDataReadOnlyTransaction readTxDelegate,
71                                                               final DOMDataWriteTransaction writeTxDelegate) {
72         this.readTxDelegate = checkNotNull(readTxDelegate);
73         this.writeTxDelegate = checkNotNull(writeTxDelegate);
74         this.txIdentifier = checkNotNull(readWriteTxId);
75         this.initialReadMap = Maps.newEnumMap(LogicalDatastoreType.class);
76
77         final InMemoryDataTreeFactory treeFactory = new InMemoryDataTreeFactory();
78         final ImmutableMap.Builder<LogicalDatastoreType, DataTreeSnapshot> snapshotMapBuilder = ImmutableMap.builder();
79         final ImmutableMap.Builder<LogicalDatastoreType, Queue<Modification>> modificationHistoryMapBuilder
80                 = ImmutableMap.builder();
81         for (final LogicalDatastoreType store : LogicalDatastoreType.values()) {
82             final DataTree tree = treeFactory.create(treeConfigForStore(store));
83             tree.setSchemaContext(ctx);
84             snapshotMapBuilder.put(store, tree.takeSnapshot());
85
86             modificationHistoryMapBuilder.put(store, Lists.newLinkedList());
87         }
88
89         modificationHistoryMap = modificationHistoryMapBuilder.build();
90         snapshotMap = snapshotMapBuilder.build();
91     }
92
93     @Override
94     public boolean cancel() {
95         readTxDelegate.close();
96         return writeTxDelegate.cancel();
97     }
98
99     @Override
100     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
101         if (root == null) {
102             initialRead(path);
103         }
104
105         modificationHistoryMap.get(store).add(new Modification(Modification.Operation.DELETE, path, null));
106         writeTxDelegate.delete(store, path);
107     }
108
109     @Override
110     public FluentFuture<? extends CommitInfo> commit() {
111         return writeTxDelegate.commit();
112     }
113
114     @Override
115     public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(final LogicalDatastoreType store,
116                                                                                    final YangInstanceIdentifier path) {
117         checkState(root != null,
118                    "A modify operation (put, merge or delete) must be performed prior to a read operation");
119         final SettableFuture<Optional<NormalizedNode<?, ?>>> readResult = SettableFuture.create();
120         final Queue<Modification> currentHistory = Lists.newLinkedList(modificationHistoryMap.get(store));
121         Futures.addCallback(initialReadMap.get(store), new FutureCallback<Optional<NormalizedNode<?, ?>>>() {
122             @Override
123             public void onSuccess(@Nonnull final Optional<NormalizedNode<?, ?>> result) {
124                 final DataTreeModification mod = snapshotMap.get(store).newModification();
125                 if (result.isPresent()) {
126                     mod.write(path, result.get());
127                 }
128                 applyModificationHistoryToSnapshot(mod, currentHistory);
129                 readResult.set(Optional.fromJavaUtil(mod.readNode(path)));
130             }
131
132             @Override
133             public void onFailure(final Throwable throwable) {
134                 readResult.setException(throwable);
135             }
136         }, MoreExecutors.directExecutor());
137
138         return Futures.makeChecked(readResult, ReadFailedException.MAPPER);
139     }
140
141     @Override
142     public CheckedFuture<Boolean, ReadFailedException> exists(final LogicalDatastoreType store,
143                                                               final YangInstanceIdentifier path) {
144         checkState(root != null,
145                    "A modify operation (put, merge or delete) must be performed prior to an exists operation");
146         return Futures.makeChecked(Futures.transform(read(store, path), Optional::isPresent,
147             MoreExecutors.directExecutor()), ReadFailedException.MAPPER);
148     }
149
150     @Override
151     public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
152                     final NormalizedNode<?, ?> data) {
153         if (root == null) {
154             initialRead(path);
155         }
156
157         modificationHistoryMap.get(store).add(new Modification(Modification.Operation.WRITE, path, data));
158         writeTxDelegate.put(store, path, data);
159     }
160
161     @Override
162     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
163                       final NormalizedNode<?, ?> data) {
164         if (root == null) {
165             initialRead(path);
166         }
167
168         modificationHistoryMap.get(store).add(new Modification(Modification.Operation.MERGE, path, data));
169         writeTxDelegate.merge(store, path, data);
170     }
171
172     @Override
173     public Object getIdentifier() {
174         return txIdentifier;
175     }
176
177     private void initialRead(final YangInstanceIdentifier path) {
178         root = path;
179
180         for (final LogicalDatastoreType store : LogicalDatastoreType.values()) {
181             initialReadMap.put(store, readTxDelegate.read(store, path));
182         }
183     }
184
185     private static DataTreeConfiguration treeConfigForStore(final LogicalDatastoreType store) {
186         return store == LogicalDatastoreType.CONFIGURATION ? DataTreeConfiguration.DEFAULT_CONFIGURATION
187                 : DataTreeConfiguration.DEFAULT_OPERATIONAL;
188     }
189
190     private static void applyModificationHistoryToSnapshot(final DataTreeModification dataTreeModification,
191                                                     final Queue<Modification> modificationHistory) {
192         while (!modificationHistory.isEmpty()) {
193             final Modification modification = modificationHistory.poll();
194             switch (modification.getOperation()) {
195                 case WRITE:
196                     dataTreeModification.write(modification.getPath(), modification.getData());
197                     break;
198                 case MERGE:
199                     dataTreeModification.merge(modification.getPath(), modification.getData());
200                     break;
201                 case DELETE:
202                     dataTreeModification.delete(modification.getPath());
203                     break;
204                 default:
205                     // NOOP
206             }
207         }
208     }
209
210     static class Modification {
211
212         enum Operation {
213             WRITE, MERGE, DELETE
214         }
215
216         private final NormalizedNode<?, ?> data;
217         private final YangInstanceIdentifier path;
218         private final Operation operation;
219
220         Modification(final Operation operation, final YangInstanceIdentifier path, final NormalizedNode<?, ?> data) {
221             this.data = data;
222             this.path = checkNotNull(path);
223             this.operation = checkNotNull(operation);
224         }
225
226         Operation getOperation() {
227             return operation;
228         }
229
230         YangInstanceIdentifier getPath() {
231             return path;
232         }
233
234         NormalizedNode<?, ?> getData() {
235             return data;
236         }
237     }
238 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.