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 static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.collect.ForwardingObject;
13 import com.google.common.util.concurrent.FluentFuture;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
18 import org.opendaylight.mdsal.binding.api.WriteTransaction;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 * Adapter allowing managed, datastore-constrained transactions to be used with methods expecting
26 * generic {@link DataBroker} transactions.
28 * <p>The adapted transactions maintain the following constraints: they cannot be cancelled or
29 * submitted (only the transaction manager can do this), and they cannot access a logical datastore
30 * other than the one they were created for.
32 * @deprecated This is only intended for temporary use during complex migrations to managed transactions.
35 public final class TransactionAdapter {
36 private TransactionAdapter() {
40 * Adapts the given datastore-constrained read-write transaction to a generic read-write transaction.
42 * @param datastoreTx The transaction to adapt.
43 * @return The adapted transaction.
44 * @throws NullPointerException if the provided transaction is {@code null}.
46 public static ReadWriteTransaction toReadWriteTransaction(
47 final TypedReadWriteTransaction<? extends Datastore> datastoreTx) {
48 if (datastoreTx instanceof TypedReadWriteTransactionImpl) {
49 TypedReadWriteTransactionImpl nonSubmitCancelableDatastoreReadWriteTransaction =
50 (TypedReadWriteTransactionImpl) datastoreTx;
51 return new ReadWriteTransactionAdapter(nonSubmitCancelableDatastoreReadWriteTransaction.datastoreType,
52 nonSubmitCancelableDatastoreReadWriteTransaction);
54 throw new IllegalArgumentException(
55 "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
59 * Adapts the given datastore-constrained write transaction to a generic write transaction. Note that this
60 * can be used to adapt a read-write transaction to a write transaction.
62 * @param datastoreTx The transaction to adapt.
63 * @return The adapted transaction.
65 public static WriteTransaction toWriteTransaction(final TypedWriteTransaction<? extends Datastore> datastoreTx) {
66 if (datastoreTx instanceof TypedWriteTransactionImpl) {
67 TypedWriteTransactionImpl nonSubmitCancelableDatastoreWriteTransaction =
68 (TypedWriteTransactionImpl) datastoreTx;
69 return new WriteTransactionAdapter(nonSubmitCancelableDatastoreWriteTransaction.datastoreType,
70 nonSubmitCancelableDatastoreWriteTransaction);
72 throw new IllegalArgumentException(
73 "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
76 // We want to subclass this class, even though it has a private constructor
77 @SuppressWarnings("FinalClass")
78 private static class WriteTransactionAdapter<D extends Datastore, T extends TypedWriteTransaction<D>>
79 extends ForwardingObject implements WriteTransaction {
80 private final LogicalDatastoreType datastoreType;
81 private final T delegate;
83 private WriteTransactionAdapter(final LogicalDatastoreType datastoreType, final T delegate) {
84 this.datastoreType = datastoreType;
85 this.delegate = delegate;
89 public <T extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
92 delegate.put(path, data);
96 public <T extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
97 final T data, final boolean createMissingParents) {
99 delegate.put(path, data, createMissingParents);
103 public <T extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
106 delegate.merge(path, data);
110 public <T extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
111 final T data, final boolean createMissingParents) {
113 delegate.merge(path, data, createMissingParents);
117 public boolean cancel() {
118 throw new UnsupportedOperationException("Managed transactions must not be cancelled");
122 public void delete(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
124 delegate.delete(path);
128 public @NonNull FluentFuture<? extends CommitInfo> commit() {
129 throw new UnsupportedOperationException("Managed transactions must not be committed");
132 void checkStore(final LogicalDatastoreType store) {
133 checkArgument(datastoreType.equals(store), "Invalid datastore %s used instead of %s", store, datastoreType);
137 public Object getIdentifier() {
138 return delegate.getIdentifier();
142 protected T delegate() {
147 private static final class ReadWriteTransactionAdapter<D extends Datastore>
148 extends WriteTransactionAdapter<D, TypedReadWriteTransaction<D>> implements ReadWriteTransaction {
149 private ReadWriteTransactionAdapter(final LogicalDatastoreType datastoreType,
150 final TypedReadWriteTransaction<D> delegate) {
151 super(datastoreType, delegate);
155 public <T extends DataObject> FluentFuture<Optional<T>> read(final LogicalDatastoreType store,
156 final InstanceIdentifier<T> path) {
158 return delegate().read(path);