MRI version bump for Aluminium
[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
29 public final class TransactionAdapter {
30     private TransactionAdapter() {
31
32     }
33
34     /**
35      * Adapts the given datastore-constrained read-write transaction to a generic read-write transaction.
36      *
37      * @param datastoreTx The transaction to adapt.
38      * @return The adapted transaction.
39      */
40     public static ReadWriteTransaction toReadWriteTransaction(
41             TypedReadWriteTransaction<? extends Datastore> datastoreTx) {
42         if (datastoreTx instanceof TypedReadWriteTransactionImpl) {
43             TypedReadWriteTransactionImpl nonSubmitCancelableDatastoreReadWriteTransaction =
44                     (TypedReadWriteTransactionImpl) datastoreTx;
45             return new ReadWriteTransactionAdapter(nonSubmitCancelableDatastoreReadWriteTransaction
46                     .datastoreType, nonSubmitCancelableDatastoreReadWriteTransaction);
47         }
48         throw new IllegalArgumentException(
49                 "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
50     }
51
52     /**
53      * Adapts the given datastore-constrained write transaction to a generic write transaction. Note that this
54      * can be used to adapt a read-write transaction to a write transaction.
55      *
56      * @param datastoreTx The transaction to adapt.
57      * @return The adapted transaction.
58      */
59     public static WriteTransaction toWriteTransaction(TypedWriteTransaction<? extends Datastore> datastoreTx) {
60         if (datastoreTx instanceof TypedWriteTransactionImpl) {
61             TypedWriteTransactionImpl nonSubmitCancelableDatastoreWriteTransaction =
62                     (TypedWriteTransactionImpl) datastoreTx;
63             return new WriteTransactionAdapter(nonSubmitCancelableDatastoreWriteTransaction.datastoreType,
64                     nonSubmitCancelableDatastoreWriteTransaction);
65         }
66         throw new IllegalArgumentException(
67                 "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
68     }
69
70     // We want to subclass this class, even though it has a private constructor
71     @SuppressWarnings("FinalClass")
72     private static class WriteTransactionAdapter<D extends Datastore> implements WriteTransaction {
73         final LogicalDatastoreType datastoreType;
74         final TypedWriteTransaction<D> delegate;
75
76         private WriteTransactionAdapter(LogicalDatastoreType datastoreType,
77                                         TypedWriteTransaction<D> delegate) {
78             this.datastoreType = datastoreType;
79             this.delegate = delegate;
80         }
81
82         @Override
83         public <T extends DataObject> void put(LogicalDatastoreType store, InstanceIdentifier<T> path,
84                                                T data) {
85             checkStore(store);
86             delegate.put(path, data);
87         }
88
89         @Override
90         public <T extends DataObject> void mergeParentStructurePut(LogicalDatastoreType store,
91                                                                    InstanceIdentifier<T> path, T data) {
92             checkStore(store);
93             delegate.mergeParentStructurePut(path, data);
94         }
95
96         @Override
97         public <T extends DataObject> void merge(LogicalDatastoreType store, InstanceIdentifier<T> path, T data) {
98             checkStore(store);
99             delegate.merge(path, data);
100         }
101
102         @Override
103         public <T extends DataObject> void mergeParentStructureMerge(LogicalDatastoreType store,
104                                                                      InstanceIdentifier<T> path, T data) {
105             checkStore(store);
106             delegate.mergeParentStructureMerge(path, data);
107         }
108
109         @Override
110         public boolean cancel() {
111             throw new UnsupportedOperationException("Managed transactions mustn't be cancelled");
112         }
113
114         @Override
115         public void delete(LogicalDatastoreType store, InstanceIdentifier<?> path) {
116             checkStore(store);
117             delegate.delete(path);
118         }
119
120         @Override
121         public @NonNull FluentFuture<? extends CommitInfo> commit() {
122             throw new UnsupportedOperationException("Managed transactions mustn't be committed");
123         }
124
125         void checkStore(LogicalDatastoreType store) {
126             Preconditions.checkArgument(datastoreType.equals(store), "Invalid "
127                             + "datastore %s  used instead of %s", store, datastoreType);
128         }
129
130         @Override
131         public Object getIdentifier() {
132             return delegate.getIdentifier();
133         }
134     }
135
136     private static final class ReadWriteTransactionAdapter<D extends Datastore> extends
137             WriteTransactionAdapter<D>
138             implements ReadWriteTransaction {
139         private final TypedReadWriteTransaction<D> delegate;
140
141         private ReadWriteTransactionAdapter(LogicalDatastoreType datastoreType,
142                                             TypedReadWriteTransaction<D> delegate) {
143             super(datastoreType, delegate);
144             this.delegate = delegate;
145         }
146
147         @Override
148         public @NonNull <T extends DataObject> FluentFuture<Optional<T>> read(@NonNull LogicalDatastoreType
149                                                                                                store,@NonNull
150                 InstanceIdentifier<T> path) {
151             checkStore(store);
152             return FluentFuture.from(delegate.read(path));
153         }
154
155         @Override
156         public FluentFuture<Boolean> exists(LogicalDatastoreType store, InstanceIdentifier<?> path) {
157             checkStore(store);
158             return FluentFuture.from(delegate.exists(path));
159         }
160     }
161 }