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.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;
24 * Adapter allowing managed, datastore-constrained transactions to be used with methods expecting
25 * generic {@link DataBroker} transactions.
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.
31 * @deprecated This is only intended for temporary use during complex migrations to managed transactions.
34 public final class TransactionAdapter {
35 private TransactionAdapter() {
39 * Adapts the given datastore-constrained read-write transaction to a generic read-write transaction.
41 * @param datastoreTx The transaction to adapt.
42 * @return The adapted transaction.
43 * @throws NullPointerException if the provided transaction is {@code null}.
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);
51 throw new IllegalArgumentException("Unsupported TypedWriteTransaction implementation "
52 + datastoreTx.getClass());
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.
59 * @param datastoreTx The transaction to adapt.
60 * @return The adapted transaction.
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);
67 throw new IllegalArgumentException("Unsupported TypedWriteTransaction implementation "
68 + datastoreTx.getClass());
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;
76 WriteTransactionAdapter(final LogicalDatastoreType datastoreType, final D delegate) {
77 this.datastoreType = datastoreType;
78 this.delegate = delegate;
82 public <T extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
85 delegate.put(path, data);
89 public <T extends DataObject> void mergeParentStructurePut(final LogicalDatastoreType store,
90 final InstanceIdentifier<T> path, final T data) {
92 delegate.mergeParentStructurePut(path, data);
96 public <T extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
99 delegate.merge(path, data);
103 public <T extends DataObject> void mergeParentStructureMerge(final LogicalDatastoreType store,
104 final InstanceIdentifier<T> path, final T data) {
106 delegate.mergeParentStructureMerge(path, data);
110 public boolean cancel() {
111 throw new UnsupportedOperationException("Managed transactions must not be cancelled");
115 public void delete(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
117 delegate.delete(path);
121 public FluentFuture<? extends CommitInfo> commit() {
122 throw new UnsupportedOperationException("Managed transactions must not be committed");
126 public Object getIdentifier() {
127 return delegate.getIdentifier();
131 public FluentFuture<?> completionFuture() {
132 return delegate.completionFuture();
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);
160 public FluentFuture<Boolean> exists(final LogicalDatastoreType store,final InstanceIdentifier<?> path) {
162 return delegate().exists(path);