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

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