Fix warnings in TransactionAdapter
[mdsal.git] / binding / mdsal-binding-util / src / main / java / org / opendaylight / mdsal / binding / util / TransactionAdapter.java
1 /*
2  * Copyright © 2018 Red Hat, Inc. and others.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.util;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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;
23
24 /**
25  * Adapter allowing managed, datastore-constrained transactions to be used with methods expecting
26  * generic {@link DataBroker} transactions.
27  *
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.
31  *
32  * @deprecated This is only intended for temporary use during complex migrations to managed transactions.
33  */
34 @Deprecated
35 public final class TransactionAdapter {
36     private TransactionAdapter() {
37     }
38
39     /**
40      * Adapts the given datastore-constrained read-write transaction to a generic read-write transaction.
41      *
42      * @param datastoreTx The transaction to adapt.
43      * @return The adapted transaction.
44      * @throws NullPointerException if the provided transaction is {@code null}.
45      */
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);
53         }
54         throw new IllegalArgumentException(
55                 "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
56     }
57
58     /**
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.
61      *
62      * @param datastoreTx The transaction to adapt.
63      * @return The adapted transaction.
64      */
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);
71         }
72         throw new IllegalArgumentException(
73                 "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
74     }
75
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;
80
81         WriteTransactionAdapter(final LogicalDatastoreType datastoreType, final D delegate) {
82             this.datastoreType = datastoreType;
83             this.delegate = delegate;
84         }
85
86         @Override
87         public <T extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
88                 final T data) {
89             checkStore(store);
90             delegate.put(path, data);
91         }
92
93         @Override
94         public <T extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
95                 final T data, final boolean createMissingParents) {
96             checkStore(store);
97             delegate.put(path, data, createMissingParents);
98         }
99
100         @Override
101         public <T extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
102                 final T data) {
103             checkStore(store);
104             delegate.merge(path, data);
105         }
106
107         @Override
108         public <T extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
109                 final T data, final boolean createMissingParents) {
110             checkStore(store);
111             delegate.merge(path, data, createMissingParents);
112         }
113
114         @Override
115         public boolean cancel() {
116             throw new UnsupportedOperationException("Managed transactions must not be cancelled");
117         }
118
119         @Override
120         public void delete(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
121             checkStore(store);
122             delegate.delete(path);
123         }
124
125         @Override
126         public @NonNull FluentFuture<? extends CommitInfo> commit() {
127             throw new UnsupportedOperationException("Managed transactions must not be committed");
128         }
129
130         @Override
131         public Object getIdentifier() {
132             return delegate.getIdentifier();
133         }
134
135         @Override
136         protected D delegate() {
137             return delegate;
138         }
139
140         void checkStore(final LogicalDatastoreType store) {
141             checkArgument(datastoreType.equals(store), "Invalid datastore %s used instead of %s", store, datastoreType);
142         }
143     }
144
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);
150         }
151
152         @Override
153         public <T extends DataObject> FluentFuture<Optional<T>> read(final LogicalDatastoreType store,
154                 final InstanceIdentifier<T> path) {
155             checkStore(store);
156             return delegate().read(path);
157         }
158     }
159 }