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

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