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 final TypedReadWriteTransactionImpl<?> txImpl = (TypedReadWriteTransactionImpl<?>) datastoreTx;
50 return new ReadWriteTransactionAdapter<>(txImpl.getDatastoreType(), txImpl);
52 throw new IllegalArgumentException("Unsupported TypedWriteTransaction implementation "
53 + datastoreTx.getClass());
57 * Adapts the given datastore-constrained write transaction to a generic write transaction. Note that this
58 * can be used to adapt a read-write transaction to a write transaction.
60 * @param datastoreTx The transaction to adapt.
61 * @return The adapted transaction.
63 public static WriteTransaction toWriteTransaction(final TypedWriteTransaction<? extends Datastore> datastoreTx) {
64 if (datastoreTx instanceof TypedWriteTransactionImpl) {
65 final TypedWriteTransactionImpl<?> txImpl = (TypedWriteTransactionImpl<?>) datastoreTx;
66 return new WriteTransactionAdapter<>(txImpl.getDatastoreType(), txImpl);
68 throw new IllegalArgumentException("Unsupported TypedWriteTransaction implementation "
69 + datastoreTx.getClass());
72 private static class WriteTransactionAdapter<S extends Datastore, D extends TypedWriteTransaction<S>>
73 extends ForwardingObject implements WriteTransaction {
74 private final LogicalDatastoreType datastoreType;
75 private final D delegate;
77 WriteTransactionAdapter(final LogicalDatastoreType datastoreType, final D delegate) {
78 this.datastoreType = datastoreType;
79 this.delegate = delegate;
83 public <T extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
86 delegate.put(path, data);
90 public <T extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
91 final T data, final boolean createMissingParents) {
93 delegate.put(path, data, createMissingParents);
97 public <T extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
100 delegate.merge(path, data);
104 public <T extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
105 final T data, final boolean createMissingParents) {
107 delegate.merge(path, data, createMissingParents);
111 public boolean cancel() {
112 throw new UnsupportedOperationException("Managed transactions must not be cancelled");
116 public void delete(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
118 delegate.delete(path);
122 public @NonNull FluentFuture<? extends CommitInfo> commit() {
123 throw new UnsupportedOperationException("Managed transactions must not be committed");
127 public Object getIdentifier() {
128 return delegate.getIdentifier();
132 protected D delegate() {
136 void checkStore(final LogicalDatastoreType store) {
137 checkArgument(datastoreType.equals(store), "Invalid datastore %s used instead of %s", store, datastoreType);
141 private static final class ReadWriteTransactionAdapter<S extends Datastore>
142 extends WriteTransactionAdapter<S, TypedReadWriteTransaction<S>> implements ReadWriteTransaction {
143 ReadWriteTransactionAdapter(final LogicalDatastoreType datastoreType,
144 final TypedReadWriteTransaction<S> delegate) {
145 super(datastoreType, delegate);
149 public <T extends DataObject> FluentFuture<Optional<T>> read(final LogicalDatastoreType store,
150 final InstanceIdentifier<T> path) {
152 return delegate().read(path);