2 * Copyright © 2018 Red Hat, Inc. and others.
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
8 package org.opendaylight.mdsal.binding.util;
10 import com.google.common.annotations.Beta;
11 import com.google.common.util.concurrent.FluentFuture;
12 import com.google.common.util.concurrent.Futures;
13 import edu.umd.cs.findbugs.annotations.CheckReturnValue;
14 import java.util.concurrent.CompletionStage;
15 import java.util.concurrent.Future;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.ReadTransaction;
18 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
19 import org.opendaylight.mdsal.binding.api.WriteTransaction;
22 * Managed transaction factories provide managed transactions, <em>i.e.</em> transactions which are automatically
23 * submitted or cancelled (write) or closed (read).
26 * This is a common interface for broker- and chain-based transaction managers, and should not be used directly.
29 public interface ManagedTransactionFactory {
31 * Invokes a function with a <b>NEW</b> {@link TypedReadTransaction}, and ensures that that transaction is closed.
32 * Thus when this method returns, that transaction is guaranteed to have been closed, and will never "leak" and
35 * <p>The function must not itself attempt to close the transaction. (It can't directly, since
36 * {@link TypedReadTransaction} doesn't expose a {@code close()} method.)
38 * <p>The provided transaction is specific to the given logical datastore type and cannot be used for any
41 * @param datastoreType the {@link Datastore} type that will be accessed
42 * @param txFunction the {@link InterruptibleCheckedFunction} that needs a new read transaction
43 * @return the result of the function.
44 * @throws E if an error occurs.
45 * @throws InterruptedException if the function is interrupted (this is passed through from the provided function).
47 <D extends Datastore, E extends Exception, R> R applyInterruptiblyWithNewReadOnlyTransactionAndClose(
48 Class<D> datastoreType, InterruptibleCheckedFunction<TypedReadTransaction<D>, R, E> txFunction)
49 throws E, InterruptedException;
52 * Invokes a function with a <b>NEW</b> {@link TypedReadTransaction}, and ensures that that transaction is closed.
53 * Thus when this method returns, that transaction is guaranteed to have been closed, and will never "leak" and
56 * <p>The function must not itself attempt to close the transaction. (It can't directly, since
57 * {@link TypedReadTransaction} doesn't expose a {@code close()} method.)
59 * <p>The provided transaction is specific to the given logical datastore type and cannot be used for any
62 * @param datastoreType the {@link Datastore} type that will be accessed
63 * @param txFunction the {@link InterruptibleCheckedFunction} that needs a new read transaction
64 * @return the result of the function.
65 * @throws E if an error occurs.
67 <D extends Datastore, E extends Exception, R> R applyWithNewReadOnlyTransactionAndClose(Class<D> datastoreType,
68 CheckedFunction<TypedReadTransaction<D>, R, E> txFunction) throws E;
71 * Invokes a function with a <b>NEW</b> {@link ReadWriteTransaction}, and then submits that transaction and
72 * returns the Future from that submission, or cancels it if an exception was thrown and returns a failed
73 * future with that exception. Thus when this method returns, that transaction is guaranteed to have
74 * been either submitted or cancelled, and will never "leak" and waste memory.
76 * <p>The function must not itself use
77 * {@link ReadWriteTransaction#cancel()}, or
78 * {@link ReadWriteTransaction#commit()} (it will throw an {@link UnsupportedOperationException}).
80 * <p>The provided transaction is specific to the given logical datastore type and cannot be used for any
83 * <p>This is an asynchronous API, like {@link DataBroker}'s own;
84 * when returning from this method, the operation of the Transaction may well still be ongoing in the background,
86 * calling code therefore <b>must</b> handle the returned future, e.g. by passing it onwards (return),
87 * or by itself adding callback listeners to it using {@link Futures}' methods, or by transforming it into a
88 * {@link CompletionStage}
89 * (but better NOT by using the blocking {@link Future#get()} on it).
91 * @param datastoreType the {@link Datastore} type that will be accessed
92 * @param txFunction the {@link InterruptibleCheckedFunction} that needs a new read-write transaction
93 * @return the {@link FluentFuture} returned by {@link ReadWriteTransaction#commit()}, or a failed future with an
94 * application specific exception (not from submit())
97 <D extends Datastore, E extends Exception, R>
98 FluentFuture<R> applyWithNewReadWriteTransactionAndSubmit(Class<D> datastoreType,
99 InterruptibleCheckedFunction<TypedReadWriteTransaction<D>, R, E> txFunction);
102 * Invokes a function with a <b>NEW</b> {@link ReadTransaction}, and ensures that that transaction is closed.
103 * Thus when this method returns, that transaction is guaranteed to have been closed, and will never "leak" and
106 * <p>The function must not itself attempt to close the transaction. (It can't directly, since
107 * {@link ReadTransaction} doesn't expose a {@code close()} method.)
109 * <p>The provided transaction is specific to the given logical datastore type and cannot be used for any
112 * @param datastoreType the {@link Datastore} type that will be accessed
113 * @param txConsumer the {@link InterruptibleCheckedFunction} that needs a new read transaction
114 * @throws E if an error occurs.
115 * @throws InterruptedException if the function is interrupted (this is passed through from the provided function).
117 <D extends Datastore, E extends Exception> void callInterruptiblyWithNewReadOnlyTransactionAndClose(
118 Class<D> datastoreType, InterruptibleCheckedConsumer<TypedReadTransaction<D>, E> txConsumer)
119 throws E, InterruptedException;
122 * Invokes a function with a <b>NEW</b> {@link ReadTransaction}, and ensures that that transaction is closed.
123 * Thus when this method returns, that transaction is guaranteed to have been closed, and will never "leak" and
126 * <p>The function must not itself attempt to close the transaction. (It can't directly, since
127 * {@link ReadTransaction} doesn't expose a {@code close()} method.)
129 * <p>The provided transaction is specific to the given logical datastore type and cannot be used for any
132 * @param datastoreType the {@link Datastore} type that will be accessed
133 * @param txConsumer the {@link InterruptibleCheckedFunction} that needs a new read transaction
134 * @throws E if an error occurs.
136 <D extends Datastore, E extends Exception> void callWithNewReadOnlyTransactionAndClose(Class<D> datastoreType,
137 CheckedConsumer<TypedReadTransaction<D>, E> txConsumer) throws E;
140 * Invokes a consumer with a <b>NEW</b> {@link ReadWriteTransaction}, and then submits that transaction and
141 * returns the Future from that submission, or cancels it if an exception was thrown and returns a failed
142 * future with that exception. Thus when this method returns, that transaction is guaranteed to have
143 * been either submitted or cancelled, and will never "leak" and waste memory.
145 * <p>The consumer should not (cannot) itself use
146 * {@link ReadWriteTransaction#cancel()}, or
147 * {@link ReadWriteTransaction#commit()} (it will throw an {@link UnsupportedOperationException}).
149 * <p>The provided transaction is specific to the given logical datastore type and cannot be used for any
152 * <p>This is an asynchronous API, like {@link DataBroker}'s own;
153 * when returning from this method, the operation of the Transaction may well still be ongoing in the background,
155 * calling code therefore <b>must</b> handle the returned future, e.g. by passing it onwards (return),
156 * or by itself adding callback listeners to it using {@link Futures}' methods, or by transforming it into a
157 * {@link CompletionStage}
158 * (but better NOT by using the blocking {@link Future#get()} on it).
160 * @param datastoreType the {@link Datastore} type that will be accessed
161 * @param txConsumer the {@link InterruptibleCheckedConsumer} that needs a new read-write transaction
162 * @return the {@link FluentFuture} returned by {@link ReadWriteTransaction#commit()}, or a failed future with an
163 * application specific exception (not from submit())
166 <D extends Datastore, E extends Exception>
167 FluentFuture<? extends Object> callWithNewReadWriteTransactionAndSubmit(Class<D> datastoreType,
168 InterruptibleCheckedConsumer<TypedReadWriteTransaction<D>, E> txConsumer);
171 * Invokes a consumer with a <b>NEW</b> {@link WriteTransaction}, and then submits that transaction and
172 * returns the Future from that submission, or cancels it if an exception was thrown and returns a failed
173 * future with that exception. Thus when this method returns, that transaction is guaranteed to have
174 * been either submitted or cancelled, and will never "leak" and waste memory.
176 * <p>The consumer should not (cannot) itself use
177 * {@link WriteTransaction#cancel()}, or
178 * {@link WriteTransaction#commit()} (it will throw an {@link UnsupportedOperationException}).
180 * <p>The provided transaction is specific to the given logical datastore type and cannot be used for any
183 * <p>This is an asynchronous API, like {@link DataBroker}'s own;
184 * when returning from this method, the operation of the Transaction may well still be ongoing in the background,
186 * calling code therefore <b>must</b> handle the returned future, e.g. by passing it onwards (return),
187 * or by itself adding callback listeners to it using {@link Futures}' methods, or by transforming it into a
188 * {@link CompletionStage}
189 * (but better NOT by using the blocking {@link Future#get()} on it).
191 * @param datastoreType the {@link Datastore} type that will be accessed
192 * @param txConsumer the {@link InterruptibleCheckedConsumer} that needs a new write only transaction
193 * @return the {@link FluentFuture} returned by {@link WriteTransaction#commit()}, or a failed future with an
194 * application specific exception (not from submit())
197 <D extends Datastore, E extends Exception>
198 FluentFuture<? extends Object> callWithNewWriteOnlyTransactionAndSubmit(Class<D> datastoreType,
199 InterruptibleCheckedConsumer<TypedWriteTransaction<D>, E> txConsumer);