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

©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.