d0f42b7f9c0d3ae4df317cd2f18e0403a3a43231
[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(
110                             new org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier(
111                                 treeId.getDatastoreType().toMdsal(), treeId.getRootIdentifier()), delegateListener);
112
113                     return new ListenerRegistration<L>() {
114                         @Override
115                         public L getInstance() {
116                             return listener;
117                         }
118
119                         @Override
120                         public void close() {
121                             reg.close();
122                         }
123                     };
124                 }
125             });
126         }
127
128         final org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry delegateCohortRegistry =
129                 (org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry) delegateExtensions.get(
130                         org.opendaylight.mdsal.dom.api.DOMDataTreeCommitCohortRegistry.class);
131         if (delegateCohortRegistry != null) {
132             extBuilder.put(DOMDataTreeCommitCohortRegistry.class, delegateCohortRegistry::registerCommitCohort);
133         }
134
135         extensions = extBuilder.build();
136     }
137
138     @Override
139     protected org.opendaylight.mdsal.dom.api.DOMDataBroker delegate() {
140         return delegate;
141     }
142
143     @Override
144     public Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> getSupportedExtensions() {
145         return extensions;
146     }
147
148     @Override
149     public DOMDataReadOnlyTransaction newReadOnlyTransaction() {
150         return new DOMDataReadOnlyTransactionAdapter(delegate().newReadOnlyTransaction());
151     }
152
153     @Override
154     public DOMDataReadWriteTransaction newReadWriteTransaction() {
155         return new DOMDataTransactionAdapter(delegate().newReadWriteTransaction());
156     }
157
158     @Override
159     public DOMDataWriteTransaction newWriteOnlyTransaction() {
160         return new DOMDataTransactionAdapter(delegate().newWriteOnlyTransaction());
161     }
162
163     @Override
164     public DOMTransactionChain createTransactionChain(final TransactionChainListener listener) {
165         AtomicReference<DOMTransactionChain> legacyChain = new AtomicReference<>();
166         DOMTransactionChainListener delegateListener =
167                 new DOMTransactionChainListener() {
168             @Override
169             @SuppressWarnings("rawtypes")
170             public void onTransactionChainFailed(final org.opendaylight.mdsal.dom.api.DOMTransactionChain chain,
171                     final DOMDataTreeTransaction transaction, final Throwable cause) {
172                 listener.onTransactionChainFailed(legacyChain.get(),
173                     (AsyncTransaction) () -> transaction.getIdentifier(),
174                         cause instanceof Exception ? COMMIT_EX_MAPPER.apply((Exception)cause) : cause);
175             }
176
177             @Override
178             public void onTransactionChainSuccessful(final org.opendaylight.mdsal.dom.api.DOMTransactionChain chain) {
179                 listener.onTransactionChainSuccessful(legacyChain.get());
180             }
181         };
182
183         final org.opendaylight.mdsal.dom.api.DOMTransactionChain delegateChain =
184                 delegate().createTransactionChain(delegateListener);
185         legacyChain.set(new DOMTransactionChain() {
186             @Override
187             public DOMDataReadOnlyTransaction newReadOnlyTransaction() {
188                 return new DOMDataReadOnlyTransactionAdapter(wrapException(delegateChain::newReadOnlyTransaction));
189             }
190
191             @Override
192             public DOMDataReadWriteTransaction newReadWriteTransaction() {
193                 return new DOMDataTransactionAdapter(wrapException(delegateChain::newReadWriteTransaction));
194             }
195
196             @Override
197             public DOMDataWriteTransaction newWriteOnlyTransaction() {
198                 return new DOMDataTransactionAdapter(wrapException(delegateChain::newWriteOnlyTransaction));
199             }
200
201             @Override
202             public void close() {
203                 delegateChain.close();
204             }
205         });
206
207         return legacyChain.get();
208     }
209
210     static <T> T wrapException(final Supplier<T> supplier) {
211         try {
212             return supplier.get();
213         } catch (DOMTransactionChainClosedException e) {
214             throw new TransactionChainClosedException("Transaction chain already closed", e);
215         }
216     }
217
218     private static class DOMDataTransactionAdapter implements DOMDataReadWriteTransaction {
219         private final DOMDataTreeReadTransaction readDelegate;
220         private final DOMDataTreeWriteTransaction writeDelegate;
221         private final Object identifier;
222
223         DOMDataTransactionAdapter(@Nonnull final DOMDataTreeReadTransaction readDelegate) {
224             this.readDelegate = Preconditions.checkNotNull(readDelegate);
225             this.identifier = readDelegate.getIdentifier();
226             this.writeDelegate = null;
227         }
228
229         DOMDataTransactionAdapter(@Nonnull final DOMDataTreeWriteTransaction writeDelegate) {
230             this.writeDelegate = Preconditions.checkNotNull(writeDelegate);
231             this.identifier = writeDelegate.getIdentifier();
232             this.readDelegate = null;
233         }
234
235         DOMDataTransactionAdapter(@Nonnull final DOMDataTreeReadWriteTransaction rwDelegate) {
236             this.readDelegate = Preconditions.checkNotNull(rwDelegate);
237             this.writeDelegate = rwDelegate;
238             this.identifier = readDelegate.getIdentifier();
239         }
240
241         DOMDataTreeReadTransaction readDelegate() {
242             return readDelegate;
243         }
244
245         DOMDataTreeWriteTransaction writeDelegate() {
246             return writeDelegate;
247         }
248
249         @Override
250         public Object getIdentifier() {
251             return identifier;
252         }
253
254         @Override
255         public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(LogicalDatastoreType store,
256                 YangInstanceIdentifier path) {
257             return MappingCheckedFuture.create(readDelegate().read(store.toMdsal(), path).transform(
258                 Optional::fromJavaUtil, MoreExecutors.directExecutor()), ReadFailedExceptionAdapter.INSTANCE);
259         }
260
261         @Override
262         public CheckedFuture<Boolean, ReadFailedException> exists(LogicalDatastoreType store,
263                 YangInstanceIdentifier path) {
264             return MappingCheckedFuture.create(readDelegate().exists(store.toMdsal(), path),
265                     ReadFailedExceptionAdapter.INSTANCE);
266         }
267
268         @Override
269         public void delete(LogicalDatastoreType store, YangInstanceIdentifier path) {
270             writeDelegate().delete(store.toMdsal(), path);
271         }
272
273         @Override
274         public void put(LogicalDatastoreType store, YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
275             writeDelegate().put(store.toMdsal(), path, data);
276         }
277
278         @Override
279         public void merge(LogicalDatastoreType store, YangInstanceIdentifier path, 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.