Simplify ListenableFuture translation
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / compat / LegacyDOMStoreAdapter.java
1 /*
2  * Copyright (c) 2016 Brocade Communications Systems, Inc. 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.cluster.datastore.compat;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ForwardingObject;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.cluster.datastore.DistributedDataStoreInterface;
18 import org.opendaylight.controller.md.sal.common.api.MappingCheckedFuture;
19 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
20 import org.opendaylight.controller.sal.core.compat.ReadFailedExceptionAdapter;
21 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
22 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
24 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
25 import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29
30 /**
31  * Adapter between the legacy controller API-based DOMStore and the mdsal API-based DOMStore.
32  *
33  * @author Thomas Pantelis
34  */
35 public class LegacyDOMStoreAdapter extends ForwardingObject implements DOMStore, AutoCloseable {
36     private final DistributedDataStoreInterface delegate;
37
38     public LegacyDOMStoreAdapter(DistributedDataStoreInterface delegate) {
39         this.delegate = delegate;
40     }
41
42     @Override
43     public DOMStoreReadTransaction newReadOnlyTransaction() {
44         return new DOMStoreTransactionAdapter(delegate().newReadOnlyTransaction());
45     }
46
47     @Override
48     public DOMStoreWriteTransaction newWriteOnlyTransaction() {
49         return new DOMStoreTransactionAdapter(delegate().newWriteOnlyTransaction());
50     }
51
52     @Override
53     public DOMStoreReadWriteTransaction newReadWriteTransaction() {
54         return new DOMStoreTransactionAdapter(delegate().newReadWriteTransaction());
55     }
56
57     @Override
58     public DOMStoreTransactionChain createTransactionChain() {
59         final org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain delegateChain =
60                 delegate().createTransactionChain();
61         return new DOMStoreTransactionChain() {
62             @Override
63             public DOMStoreReadTransaction newReadOnlyTransaction() {
64                 return new DOMStoreTransactionAdapter(delegateChain.newReadOnlyTransaction());
65             }
66
67             @Override
68             public DOMStoreWriteTransaction newWriteOnlyTransaction() {
69                 return new DOMStoreTransactionAdapter(delegateChain.newWriteOnlyTransaction());
70             }
71
72             @Override
73             public DOMStoreReadWriteTransaction newReadWriteTransaction() {
74                 return new DOMStoreTransactionAdapter(delegateChain.newReadWriteTransaction());
75             }
76
77             @Override
78             public void close() {
79                 delegateChain.close();
80             }
81         };
82     }
83
84     @Override
85     public void close() {
86     }
87
88     @Override
89     protected DistributedDataStoreInterface delegate() {
90         return delegate;
91     }
92
93     private static class DOMStoreTransactionAdapter implements DOMStoreReadWriteTransaction {
94         private final org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction readDelegate;
95         private final org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction writeDelegate;
96         private final Object identifier;
97
98         DOMStoreTransactionAdapter(@Nonnull org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction readDelegate) {
99             this.readDelegate = Preconditions.checkNotNull(readDelegate);
100             this.identifier = readDelegate.getIdentifier();
101             this.writeDelegate = null;
102         }
103
104         DOMStoreTransactionAdapter(
105                 @Nonnull org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction writeDelegate) {
106             this.writeDelegate = Preconditions.checkNotNull(writeDelegate);
107             this.identifier = writeDelegate.getIdentifier();
108             this.readDelegate = null;
109         }
110
111         DOMStoreTransactionAdapter(
112                 @Nonnull org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction rwDelegate) {
113             this.readDelegate = Preconditions.checkNotNull(rwDelegate);
114             this.writeDelegate = rwDelegate;
115             this.identifier = readDelegate.getIdentifier();
116         }
117
118         @Override
119         public Object getIdentifier() {
120             return identifier;
121         }
122
123         @Override
124         public void close() {
125             if (writeDelegate != null) {
126                 writeDelegate.close();
127             } else {
128                 readDelegate.close();
129             }
130         }
131
132         @Override
133         public void write(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
134             writeDelegate.write(path, data);
135         }
136
137         @Override
138         public void merge(YangInstanceIdentifier path, NormalizedNode<?, ?> data) {
139             writeDelegate.merge(path, data);
140         }
141
142         @Override
143         public void delete(YangInstanceIdentifier path) {
144             writeDelegate.delete(path);
145         }
146
147         @Override
148         public DOMStoreThreePhaseCommitCohort ready() {
149             final org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort cohort = writeDelegate.ready();
150             return new DOMStoreThreePhaseCommitCohort() {
151                 @Override
152                 public ListenableFuture<Boolean> canCommit() {
153                     return cohort.canCommit();
154                 }
155
156                 @Override
157                 public ListenableFuture<Void> preCommit() {
158                     return cohort.preCommit();
159                 }
160
161                 @Override
162                 public ListenableFuture<Void> commit() {
163                     return cohort.commit();
164                 }
165
166                 @Override
167                 public ListenableFuture<Void> abort() {
168                     return cohort.abort();
169                 }
170             };
171         }
172
173         @Override
174         public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(YangInstanceIdentifier path) {
175             return MappingCheckedFuture.create(readDelegate.read(path).transform(
176                 Optional::fromJavaUtil, MoreExecutors.directExecutor()), ReadFailedExceptionAdapter.INSTANCE);
177         }
178
179         @Override
180         public CheckedFuture<Boolean, ReadFailedException> exists(YangInstanceIdentifier path) {
181             return MappingCheckedFuture.create(readDelegate.exists(path), ReadFailedExceptionAdapter.INSTANCE);
182         }
183     }
184 }

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