Simplify ListenableFuture translation
[controller.git] / opendaylight / md-sal / sal-dom-compat / src / main / java / org / opendaylight / controller / sal / core / compat / LegacyDOMDataBrokerAdapter.java
1 /*
2  * Copyright (c) 2017 Inocybe Technologies 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.sal.core.compat;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ClassToInstanceMap;
13 import com.google.common.collect.ForwardingObject;
14 import com.google.common.collect.ImmutableClassToInstanceMap;
15 import com.google.common.collect.ImmutableClassToInstanceMap.Builder;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.FluentFuture;
18 import com.google.common.util.concurrent.FutureCallback;
19 import com.google.common.util.concurrent.MoreExecutors;
20 import com.google.common.util.concurrent.SettableFuture;
21 import java.util.Map;
22 import java.util.concurrent.atomic.AtomicReference;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.controller.md.sal.common.api.MappingCheckedFuture;
25 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.DataStoreUnavailableException;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
29 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
30 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
31 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
32 import org.opendaylight.controller.md.sal.dom.api.ClusteredDOMDataTreeChangeListener;
33 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
34 import org.opendaylight.controller.md.sal.dom.api.DOMDataBrokerExtension;
35 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
36 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
37 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeListener;
38 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
39 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeCommitCohortRegistry;
40 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
41 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
42 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
43 import org.opendaylight.mdsal.common.api.CommitInfo;
44 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
45 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
46 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
47 import org.opendaylight.yangtools.concepts.ListenerRegistration;
48 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
51
52 /**
53  * Adapter between the legacy controller API-based DOMDataBroker and the mdsal API-based DOMDataBroker.
54  *
55  * @author Thomas Pantelis
56  */
57 public class LegacyDOMDataBrokerAdapter extends ForwardingObject implements DOMDataBroker {
58     private static final ExceptionMapper<TransactionCommitFailedException> COMMIT_EX_MAPPER =
59             new ExceptionMapper<TransactionCommitFailedException>("commit", TransactionCommitFailedException.class) {
60         @Override
61         protected TransactionCommitFailedException newWithCause(String message, Throwable cause) {
62             if (cause instanceof org.opendaylight.mdsal.common.api.OptimisticLockFailedException) {
63                 return new OptimisticLockFailedException(cause.getMessage(), cause.getCause());
64             } else if (cause instanceof org.opendaylight.mdsal.common.api.TransactionCommitFailedException) {
65                 Throwable rootCause = cause.getCause();
66                 if (rootCause instanceof org.opendaylight.mdsal.common.api.DataStoreUnavailableException) {
67                     rootCause = new DataStoreUnavailableException(rootCause.getMessage(), rootCause.getCause());
68                 }
69
70                 return new TransactionCommitFailedException(cause.getMessage(), rootCause);
71             }
72
73             return new TransactionCommitFailedException(message, cause);
74         }
75     };
76
77     private final org.opendaylight.mdsal.dom.api.DOMDataBroker delegate;
78     private final ClassToInstanceMap<DOMDataBrokerExtension> extensions;
79
80     public LegacyDOMDataBrokerAdapter(org.opendaylight.mdsal.dom.api.DOMDataBroker delegate) {
81         this.delegate = delegate;
82
83         ClassToInstanceMap<org.opendaylight.mdsal.dom.api.DOMDataBrokerExtension> delegateExtensions =
84                 delegate.getExtensions();
85
86         Builder<DOMDataBrokerExtension> extBuilder = ImmutableClassToInstanceMap.builder();
87         final org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService delegateTreeChangeService =
88                 (org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService) delegateExtensions.get(
89                         org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService.class);
90         if (delegateTreeChangeService != null) {
91             extBuilder.put(DOMDataTreeChangeService.class, new DOMDataTreeChangeService() {
92                 @Override
93                 public <L extends DOMDataTreeChangeListener> ListenerRegistration<L> registerDataTreeChangeListener(
94                         DOMDataTreeIdentifier treeId, final L listener) {
95                     final org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener delegateListener;
96                     if (listener instanceof ClusteredDOMDataTreeChangeListener) {
97                         delegateListener = (org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener)
98                             listener::onDataTreeChanged;
99                     } else {
100                         delegateListener = listener::onDataTreeChanged;
101                     }
102
103                     final ListenerRegistration<org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener> reg =
104                         delegateTreeChangeService.registerDataTreeChangeListener(
105                             new org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier(convert(treeId.getDatastoreType()),
106                                     treeId.getRootIdentifier()), delegateListener);
107
108                     return new ListenerRegistration<L>() {
109                         @Override
110                         public L getInstance() {
111                             return listener;
112                         }
113
114                         @Override
115                         public void close() {
116                             reg.close();
117                         }
118                     };
119                 }
120             });
121         }
122
123         final org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry delegateCohortRegistry =
124                 (org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry) delegateExtensions.get(
125                         org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry.class);
126         if (delegateCohortRegistry != null) {
127             extBuilder.put(DOMDataTreeCommitCohortRegistry.class, delegateCohortRegistry::registerCommitCohort);
128         }
129
130         extensions = extBuilder.build();
131     }
132
133     @Override
134     protected org.opendaylight.mdsal.dom.api.DOMDataBroker delegate() {
135         return delegate;
136     }
137
138     @Override
139     public Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> getSupportedExtensions() {
140         return extensions;
141     }
142
143     @Override
144     public DOMDataReadOnlyTransaction newReadOnlyTransaction() {
145         return new DOMDataReadOnlyTransactionAdapter(delegate().newReadOnlyTransaction());
146     }
147
148     @Override
149     public DOMDataReadWriteTransaction newReadWriteTransaction() {
150         return new DOMDataTransactionAdapter(delegate().newReadWriteTransaction());
151     }
152
153     @Override
154     public DOMDataWriteTransaction newWriteOnlyTransaction() {
155         return new DOMDataTransactionAdapter(delegate().newWriteOnlyTransaction());
156     }
157
158     @Override
159     public DOMTransactionChain createTransactionChain(final TransactionChainListener listener) {
160         AtomicReference<DOMTransactionChain> legacyChain = new AtomicReference<>();
161         org.opendaylight.mdsal.common.api.TransactionChainListener delegateListener =
162                 new org.opendaylight.mdsal.common.api.TransactionChainListener() {
163             @SuppressWarnings("rawtypes")
164             @Override
165             public void onTransactionChainFailed(final org.opendaylight.mdsal.common.api.TransactionChain<?, ?> chain,
166                     final org.opendaylight.mdsal.common.api.AsyncTransaction<?, ?> transaction, final Throwable cause) {
167                 listener.onTransactionChainFailed(legacyChain.get(),
168                         (AsyncTransaction) () -> transaction.getIdentifier(),
169                             cause instanceof Exception ? COMMIT_EX_MAPPER.apply((Exception)cause) : cause);
170             }
171
172             @Override
173             public void onTransactionChainSuccessful(org.opendaylight.mdsal.common.api.TransactionChain<?, ?> chain) {
174                 listener.onTransactionChainSuccessful(legacyChain.get());
175             }
176         };
177
178         final org.opendaylight.mdsal.dom.api.DOMTransactionChain delegateChain =
179                 delegate().createTransactionChain(delegateListener);
180         legacyChain.set(new DOMTransactionChain() {
181             @Override
182             public DOMDataReadOnlyTransaction newReadOnlyTransaction() {
183                 return new DOMDataReadOnlyTransactionAdapter(delegateChain.newReadOnlyTransaction());
184             }
185
186             @Override
187             public DOMDataReadWriteTransaction newReadWriteTransaction() {
188                 return new DOMDataTransactionAdapter(delegateChain.newReadWriteTransaction());
189             }
190
191             @Override
192             public DOMDataWriteTransaction newWriteOnlyTransaction() {
193                 return new DOMDataTransactionAdapter(delegateChain.newWriteOnlyTransaction());
194             }
195
196             @Override
197             public void close() {
198                 delegateChain.close();
199             }
200         });
201
202         return legacyChain.get();
203     }
204
205     private static org.opendaylight.mdsal.common.api.LogicalDatastoreType convert(LogicalDatastoreType datastoreType) {
206         return org.opendaylight.mdsal.common.api.LogicalDatastoreType.valueOf(datastoreType.name());
207     }
208
209     private static class DOMDataTransactionAdapter implements DOMDataReadWriteTransaction {
210         private final DOMDataTreeReadTransaction readDelegate;
211         private final DOMDataTreeWriteTransaction writeDelegate;
212         private final Object identifier;
213
214         DOMDataTransactionAdapter(@Nonnull DOMDataTreeReadTransaction readDelegate) {
215             this.readDelegate = Preconditions.checkNotNull(readDelegate);
216             this.identifier = readDelegate.getIdentifier();
217             this.writeDelegate = null;
218         }
219
220         DOMDataTransactionAdapter(@Nonnull DOMDataTreeWriteTransaction writeDelegate) {
221             this.writeDelegate = Preconditions.checkNotNull(writeDelegate);
222             this.identifier = writeDelegate.getIdentifier();
223             this.readDelegate = null;
224         }
225
226         DOMDataTransactionAdapter(@Nonnull DOMDataTreeReadWriteTransaction rwDelegate) {
227             this.readDelegate = Preconditions.checkNotNull(rwDelegate);
228             this.writeDelegate = rwDelegate;
229             this.identifier = readDelegate.getIdentifier();
230         }
231
232         DOMDataTreeReadTransaction readDelegate() {
233             return readDelegate;
234         }
235
236         DOMDataTreeWriteTransaction writeDelegate() {
237             return writeDelegate;
238         }
239
240         @Override
241         public Object getIdentifier() {
242             return identifier;
243         }
244
245         @Override
246         public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(LogicalDatastoreType store,
247                 YangInstanceIdentifier path) {
248             return MappingCheckedFuture.create(readDelegate().read(convert(store), path).transform(
249                 Optional::fromJavaUtil, MoreExecutors.directExecutor()), ReadFailedExceptionAdapter.INSTANCE);
250         }
251
252         @Override
253         public CheckedFuture<Boolean, ReadFailedException> exists(LogicalDatastoreType store,
254                 YangInstanceIdentifier path) {
255             return MappingCheckedFuture.create(readDelegate().exists(convert(store), path),
256                     ReadFailedExceptionAdapter.INSTANCE);
257         }
258
259         @Override
260         public void delete(LogicalDatastoreType store, YangInstanceIdentifier path) {
261             writeDelegate().delete(convert(store), path);
262         }
263
264         @Override
265         public void put(LogicalDatastoreType store, YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
266             writeDelegate().put(convert(store), path, data);
267         }
268
269         @Override
270         public void merge(LogicalDatastoreType store, YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
271             writeDelegate().merge(convert(store), path, data);
272         }
273
274         @Override
275         public boolean cancel() {
276             return writeDelegate().cancel();
277         }
278
279         @Override
280         public FluentFuture<? extends CommitInfo> commit() {
281             final SettableFuture<CommitInfo> resultFuture = SettableFuture.create();
282             writeDelegate().commit().addCallback(new FutureCallback<CommitInfo>() {
283                 @Override
284                 public void onSuccess(CommitInfo result) {
285                     resultFuture.set(result);
286                 }
287
288                 @Override
289                 public void onFailure(Throwable ex) {
290                     if (ex instanceof Exception) {
291                         resultFuture.setException(COMMIT_EX_MAPPER.apply((Exception)ex));
292                     } else {
293                         resultFuture.setException(ex);
294                     }
295                 }
296             }, MoreExecutors.directExecutor());
297
298             return resultFuture;
299         }
300     }
301
302     private static class DOMDataReadOnlyTransactionAdapter implements DOMDataReadOnlyTransaction {
303         private final DOMDataTransactionAdapter adapter;
304
305         DOMDataReadOnlyTransactionAdapter(DOMDataTreeReadTransaction delegateTx) {
306             adapter = new DOMDataTransactionAdapter(delegateTx);
307         }
308
309         @Override
310         public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(LogicalDatastoreType store,
311                 YangInstanceIdentifier path) {
312             return adapter.read(store, path);
313         }
314
315         @Override
316         public CheckedFuture<Boolean, ReadFailedException> exists(LogicalDatastoreType store,
317                 YangInstanceIdentifier path) {
318             return adapter.exists(store, path);
319         }
320
321         @Override
322         public Object getIdentifier() {
323             return adapter.getIdentifier();
324         }
325
326         @Override
327         public void close() {
328             adapter.readDelegate().close();
329         }
330     }
331 }

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