Deprecate ManagedNewTransactionRunner et al.
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / infra / TransactionAdapter.java
1 /*
2  * Copyright © 2018 Red Hat, Inc. and others.
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.genius.infra;
9 import com.google.common.base.Preconditions;
10 import com.google.common.util.concurrent.FluentFuture;
11 import java.util.Optional;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
14 import org.opendaylight.mdsal.binding.api.WriteTransaction;
15 import org.opendaylight.mdsal.common.api.CommitInfo;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19
20 /**
21  * Adapter allowing managed, datastore-constrained transactions to be used with methods expecting
22  * generic {@link org.opendaylight.mdsal.binding.api.DataBroker} transactions.
23  *
24  * <p>The adapted transactions maintain the following constraints: they cannot be cancelled or
25  * submitted (only the transaction manager can do this), and they cannot access a logical datastore
26  * other than the one they were created for.
27  *
28  * @deprecated Use {@link org.opendaylight.mdsal.binding.util.TransactionAdapter} instead.
29  */
30 @Deprecated(forRemoval = true)
31 public final class TransactionAdapter {
32     private TransactionAdapter() {
33
34     }
35
36     /**
37      * Adapts the given datastore-constrained read-write transaction to a generic read-write transaction.
38      *
39      * @param datastoreTx The transaction to adapt.
40      * @return The adapted transaction.
41      */
42     public static ReadWriteTransaction toReadWriteTransaction(
43             TypedReadWriteTransaction<? extends Datastore> datastoreTx) {
44         if (datastoreTx instanceof TypedReadWriteTransactionImpl) {
45             TypedReadWriteTransactionImpl nonSubmitCancelableDatastoreReadWriteTransaction =
46                     (TypedReadWriteTransactionImpl) datastoreTx;
47             return new ReadWriteTransactionAdapter(nonSubmitCancelableDatastoreReadWriteTransaction
48                     .datastoreType, nonSubmitCancelableDatastoreReadWriteTransaction);
49         }
50         throw new IllegalArgumentException(
51                 "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
52     }
53
54     /**
55      * Adapts the given datastore-constrained write transaction to a generic write transaction. Note that this
56      * can be used to adapt a read-write transaction to a write transaction.
57      *
58      * @param datastoreTx The transaction to adapt.
59      * @return The adapted transaction.
60      */
61     public static WriteTransaction toWriteTransaction(TypedWriteTransaction<? extends Datastore> datastoreTx) {
62         if (datastoreTx instanceof TypedWriteTransactionImpl) {
63             TypedWriteTransactionImpl nonSubmitCancelableDatastoreWriteTransaction =
64                     (TypedWriteTransactionImpl) datastoreTx;
65             return new WriteTransactionAdapter(nonSubmitCancelableDatastoreWriteTransaction.datastoreType,
66                     nonSubmitCancelableDatastoreWriteTransaction);
67         }
68         throw new IllegalArgumentException(
69                 "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
70     }
71
72     // We want to subclass this class, even though it has a private constructor
73     @SuppressWarnings("FinalClass")
74     private static class WriteTransactionAdapter<D extends Datastore> implements WriteTransaction {
75         final LogicalDatastoreType datastoreType;
76         final TypedWriteTransaction<D> delegate;
77
78         private WriteTransactionAdapter(LogicalDatastoreType datastoreType,
79                                         TypedWriteTransaction<D> delegate) {
80             this.datastoreType = datastoreType;
81             this.delegate = delegate;
82         }
83
84         @Override
85         public <T extends DataObject> void put(LogicalDatastoreType store, InstanceIdentifier<T> path,
86                                                T data) {
87             checkStore(store);
88             delegate.put(path, data);
89         }
90
91         @Override
92         public <T extends DataObject> void mergeParentStructurePut(LogicalDatastoreType store,
93                                                                    InstanceIdentifier<T> path, T data) {
94             checkStore(store);
95             delegate.mergeParentStructurePut(path, data);
96         }
97
98         @Override
99         public <T extends DataObject> void merge(LogicalDatastoreType store, InstanceIdentifier<T> path, T data) {
100             checkStore(store);
101             delegate.merge(path, data);
102         }
103
104         @Override
105         public <T extends DataObject> void mergeParentStructureMerge(LogicalDatastoreType store,
106                                                                      InstanceIdentifier<T> path, T data) {
107             checkStore(store);
108             delegate.mergeParentStructureMerge(path, data);
109         }
110
111         @Override
112         public boolean cancel() {
113             throw new UnsupportedOperationException("Managed transactions mustn't be cancelled");
114         }
115
116         @Override
117         public void delete(LogicalDatastoreType store, InstanceIdentifier<?> path) {
118             checkStore(store);
119             delegate.delete(path);
120         }
121
122         @Override
123         public @NonNull FluentFuture<? extends CommitInfo> commit() {
124             throw new UnsupportedOperationException("Managed transactions mustn't be committed");
125         }
126
127         void checkStore(LogicalDatastoreType store) {
128             Preconditions.checkArgument(datastoreType.equals(store), "Invalid "
129                             + "datastore %s  used instead of %s", store, datastoreType);
130         }
131
132         @Override
133         public Object getIdentifier() {
134             return delegate.getIdentifier();
135         }
136     }
137
138     private static final class ReadWriteTransactionAdapter<D extends Datastore> extends
139             WriteTransactionAdapter<D>
140             implements ReadWriteTransaction {
141         private final TypedReadWriteTransaction<D> delegate;
142
143         private ReadWriteTransactionAdapter(LogicalDatastoreType datastoreType,
144                                             TypedReadWriteTransaction<D> delegate) {
145             super(datastoreType, delegate);
146             this.delegate = delegate;
147         }
148
149         @Override
150         public @NonNull <T extends DataObject> FluentFuture<Optional<T>> read(@NonNull LogicalDatastoreType
151                                                                                                store,@NonNull
152                 InstanceIdentifier<T> path) {
153             checkStore(store);
154             return FluentFuture.from(delegate.read(path));
155         }
156
157         @Override
158         public FluentFuture<Boolean> exists(LogicalDatastoreType store, InstanceIdentifier<?> path) {
159             checkStore(store);
160             return FluentFuture.from(delegate.exists(path));
161         }
162     }
163 }