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

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