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