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 private static class WriteTransactionAdapter<S extends Datastore, D extends TypedWriteTransaction<S>>
77 extends ForwardingObject implements WriteTransaction {
78 private final LogicalDatastoreType datastoreType;
79 private final D delegate;
81 WriteTransactionAdapter(final LogicalDatastoreType datastoreType, final D delegate) {
82 this.datastoreType = datastoreType;
83 this.delegate = delegate;
87 public <T extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
90 delegate.put(path, data);
94 public <T extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
95 final T data, final boolean createMissingParents) {
97 delegate.put(path, data, createMissingParents);
101 public <T extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
104 delegate.merge(path, data);
108 public <T extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
109 final T data, final boolean createMissingParents) {
111 delegate.merge(path, data, createMissingParents);
115 public boolean cancel() {
116 throw new UnsupportedOperationException("Managed transactions must not be cancelled");
120 public void delete(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
122 delegate.delete(path);
126 public @NonNull FluentFuture<? extends CommitInfo> commit() {
127 throw new UnsupportedOperationException("Managed transactions must not be committed");
131 public Object getIdentifier() {
132 return delegate.getIdentifier();
136 protected D delegate() {
140 void checkStore(final LogicalDatastoreType store) {
141 checkArgument(datastoreType.equals(store), "Invalid datastore %s used instead of %s", store, datastoreType);
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);
153 public <T extends DataObject> FluentFuture<Optional<T>> read(final LogicalDatastoreType store,
154 final InstanceIdentifier<T> path) {
156 return delegate().read(path);