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.
29 public final class TransactionAdapter {
30 private TransactionAdapter() {
35 * Adapts the given datastore-constrained read-write transaction to a generic read-write transaction.
37 * @param datastoreTx The transaction to adapt.
38 * @return The adapted transaction.
40 public static ReadWriteTransaction toReadWriteTransaction(
41 TypedReadWriteTransaction<? extends Datastore> datastoreTx) {
42 if (datastoreTx instanceof TypedReadWriteTransactionImpl) {
43 TypedReadWriteTransactionImpl nonSubmitCancelableDatastoreReadWriteTransaction =
44 (TypedReadWriteTransactionImpl) datastoreTx;
45 return new ReadWriteTransactionAdapter(nonSubmitCancelableDatastoreReadWriteTransaction
46 .datastoreType, nonSubmitCancelableDatastoreReadWriteTransaction);
48 throw new IllegalArgumentException(
49 "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
53 * Adapts the given datastore-constrained write transaction to a generic write transaction. Note that this
54 * can be used to adapt a read-write transaction to a write transaction.
56 * @param datastoreTx The transaction to adapt.
57 * @return The adapted transaction.
59 public static WriteTransaction toWriteTransaction(TypedWriteTransaction<? extends Datastore> datastoreTx) {
60 if (datastoreTx instanceof TypedWriteTransactionImpl) {
61 TypedWriteTransactionImpl nonSubmitCancelableDatastoreWriteTransaction =
62 (TypedWriteTransactionImpl) datastoreTx;
63 return new WriteTransactionAdapter(nonSubmitCancelableDatastoreWriteTransaction.datastoreType,
64 nonSubmitCancelableDatastoreWriteTransaction);
66 throw new IllegalArgumentException(
67 "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
70 // We want to subclass this class, even though it has a private constructor
71 @SuppressWarnings("FinalClass")
72 private static class WriteTransactionAdapter<D extends Datastore> implements WriteTransaction {
73 final LogicalDatastoreType datastoreType;
74 final TypedWriteTransaction<D> delegate;
76 private WriteTransactionAdapter(LogicalDatastoreType datastoreType,
77 TypedWriteTransaction<D> delegate) {
78 this.datastoreType = datastoreType;
79 this.delegate = delegate;
83 public <T extends DataObject> void put(LogicalDatastoreType store, InstanceIdentifier<T> path,
86 delegate.put(path, data);
90 public <T extends DataObject> void mergeParentStructurePut(LogicalDatastoreType store,
91 InstanceIdentifier<T> path, T data) {
93 delegate.mergeParentStructurePut(path, data);
97 public <T extends DataObject> void merge(LogicalDatastoreType store, InstanceIdentifier<T> path, T data) {
99 delegate.merge(path, data);
103 public <T extends DataObject> void mergeParentStructureMerge(LogicalDatastoreType store,
104 InstanceIdentifier<T> path, T data) {
106 delegate.mergeParentStructureMerge(path, data);
110 public boolean cancel() {
111 throw new UnsupportedOperationException("Managed transactions mustn't be cancelled");
115 public void delete(LogicalDatastoreType store, InstanceIdentifier<?> path) {
117 delegate.delete(path);
121 public @NonNull FluentFuture<? extends CommitInfo> commit() {
122 throw new UnsupportedOperationException("Managed transactions mustn't be committed");
125 void checkStore(LogicalDatastoreType store) {
126 Preconditions.checkArgument(datastoreType.equals(store), "Invalid "
127 + "datastore %s used instead of %s", store, datastoreType);
131 public Object getIdentifier() {
132 return delegate.getIdentifier();
136 private static final class ReadWriteTransactionAdapter<D extends Datastore> extends
137 WriteTransactionAdapter<D>
138 implements ReadWriteTransaction {
139 private final TypedReadWriteTransaction<D> delegate;
141 private ReadWriteTransactionAdapter(LogicalDatastoreType datastoreType,
142 TypedReadWriteTransaction<D> delegate) {
143 super(datastoreType, delegate);
144 this.delegate = delegate;
148 public @NonNull <T extends DataObject> FluentFuture<Optional<T>> read(@NonNull LogicalDatastoreType
150 InstanceIdentifier<T> path) {
152 return FluentFuture.from(delegate.read(path));
156 public FluentFuture<Boolean> exists(LogicalDatastoreType store, InstanceIdentifier<?> path) {
158 return FluentFuture.from(delegate.exists(path));