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

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