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.genius.infra;
9 import com.google.common.base.Preconditions;
10 import com.google.common.util.concurrent.FluentFuture;
11 import java.util.Optional;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
14 import org.opendaylight.mdsal.binding.api.WriteTransaction;
15 import org.opendaylight.mdsal.common.api.CommitInfo;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.yangtools.yang.binding.DataObject;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 * Adapter allowing managed, datastore-constrained transactions to be used with methods expecting
22 * generic {@link org.opendaylight.mdsal.binding.api.DataBroker} transactions.
24 * <p>The adapted transactions maintain the following constraints: they cannot be cancelled or
25 * submitted (only the transaction manager can do this), and they cannot access a logical datastore
26 * other than the one they were created for.
28 * @deprecated Use {@link org.opendaylight.mdsal.binding.util.TransactionAdapter} instead.
30 @Deprecated(forRemoval = true)
31 public final class TransactionAdapter {
32 private TransactionAdapter() {
37 * Adapts the given datastore-constrained read-write transaction to a generic read-write transaction.
39 * @param datastoreTx The transaction to adapt.
40 * @return The adapted transaction.
42 public static ReadWriteTransaction toReadWriteTransaction(
43 TypedReadWriteTransaction<? extends Datastore> datastoreTx) {
44 if (datastoreTx instanceof TypedReadWriteTransactionImpl) {
45 TypedReadWriteTransactionImpl nonSubmitCancelableDatastoreReadWriteTransaction =
46 (TypedReadWriteTransactionImpl) datastoreTx;
47 return new ReadWriteTransactionAdapter(nonSubmitCancelableDatastoreReadWriteTransaction
48 .datastoreType, nonSubmitCancelableDatastoreReadWriteTransaction);
50 throw new IllegalArgumentException(
51 "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
55 * Adapts the given datastore-constrained write transaction to a generic write transaction. Note that this
56 * can be used to adapt a read-write transaction to a write transaction.
58 * @param datastoreTx The transaction to adapt.
59 * @return The adapted transaction.
61 public static WriteTransaction toWriteTransaction(TypedWriteTransaction<? extends Datastore> datastoreTx) {
62 if (datastoreTx instanceof TypedWriteTransactionImpl) {
63 TypedWriteTransactionImpl nonSubmitCancelableDatastoreWriteTransaction =
64 (TypedWriteTransactionImpl) datastoreTx;
65 return new WriteTransactionAdapter(nonSubmitCancelableDatastoreWriteTransaction.datastoreType,
66 nonSubmitCancelableDatastoreWriteTransaction);
68 throw new IllegalArgumentException(
69 "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
72 // We want to subclass this class, even though it has a private constructor
73 @SuppressWarnings("FinalClass")
74 private static class WriteTransactionAdapter<D extends Datastore> implements WriteTransaction {
75 final LogicalDatastoreType datastoreType;
76 final TypedWriteTransaction<D> delegate;
78 private WriteTransactionAdapter(LogicalDatastoreType datastoreType,
79 TypedWriteTransaction<D> delegate) {
80 this.datastoreType = datastoreType;
81 this.delegate = delegate;
85 public <T extends DataObject> void put(LogicalDatastoreType store, InstanceIdentifier<T> path,
88 delegate.put(path, data);
92 public <T extends DataObject> void mergeParentStructurePut(LogicalDatastoreType store,
93 InstanceIdentifier<T> path, T data) {
95 delegate.mergeParentStructurePut(path, data);
99 public <T extends DataObject> void merge(LogicalDatastoreType store, InstanceIdentifier<T> path, T data) {
101 delegate.merge(path, data);
105 public <T extends DataObject> void mergeParentStructureMerge(LogicalDatastoreType store,
106 InstanceIdentifier<T> path, T data) {
108 delegate.mergeParentStructureMerge(path, data);
112 public boolean cancel() {
113 throw new UnsupportedOperationException("Managed transactions mustn't be cancelled");
117 public void delete(LogicalDatastoreType store, InstanceIdentifier<?> path) {
119 delegate.delete(path);
123 public @NonNull FluentFuture<? extends CommitInfo> commit() {
124 throw new UnsupportedOperationException("Managed transactions mustn't be committed");
127 void checkStore(LogicalDatastoreType store) {
128 Preconditions.checkArgument(datastoreType.equals(store), "Invalid "
129 + "datastore %s used instead of %s", store, datastoreType);
133 public Object getIdentifier() {
134 return delegate.getIdentifier();
138 private static final class ReadWriteTransactionAdapter<D extends Datastore> extends
139 WriteTransactionAdapter<D>
140 implements ReadWriteTransaction {
141 private final TypedReadWriteTransaction<D> delegate;
143 private ReadWriteTransactionAdapter(LogicalDatastoreType datastoreType,
144 TypedReadWriteTransaction<D> delegate) {
145 super(datastoreType, delegate);
146 this.delegate = delegate;
150 public @NonNull <T extends DataObject> FluentFuture<Optional<T>> read(@NonNull LogicalDatastoreType
152 InstanceIdentifier<T> path) {
154 return FluentFuture.from(delegate.read(path));
158 public FluentFuture<Boolean> exists(LogicalDatastoreType store, InstanceIdentifier<?> path) {
160 return FluentFuture.from(delegate.exists(path));