14956f1e0925da128d43033fe759cec35a45ab17
[genius.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / genius / infra / 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.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;
19
20 /**
21  * Adapter allowing managed, datastore-constrained transactions to be used with methods expecting
22  * generic {@link org.opendaylight.mdsal.binding.api.DataBroker} transactions.
23  *
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.
27  */
28 @Deprecated
29 public final class TransactionAdapter {
30     private TransactionAdapter() {
31
32     }
33
34     /**
35      * Adapts the given datastore-constrained read-write transaction to a generic read-write transaction.
36      *
37      * @param datastoreTx The transaction to adapt.
38      * @return The adapted transaction.
39      */
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);
47         }
48         throw new IllegalArgumentException(
49                 "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
50     }
51
52     /**
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.
55      *
56      * @param datastoreTx The transaction to adapt.
57      * @return The adapted transaction.
58      */
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);
65         }
66         throw new IllegalArgumentException(
67                 "Unsupported TypedWriteTransaction implementation " + datastoreTx.getClass());
68     }
69
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;
75
76         private WriteTransactionAdapter(LogicalDatastoreType datastoreType,
77                                         TypedWriteTransaction<D> delegate) {
78             this.datastoreType = datastoreType;
79             this.delegate = delegate;
80         }
81
82         @Override
83         public <T extends DataObject> void put(LogicalDatastoreType store, InstanceIdentifier<T> path,
84                                                T data) {
85             checkStore(store);
86             delegate.put(path, data);
87         }
88
89         @Override
90         public <T extends DataObject> void put(LogicalDatastoreType store, InstanceIdentifier<T> path, T data,
91                                                boolean createMissingParents) {
92             checkStore(store);
93             delegate.put(path, data, createMissingParents);
94         }
95
96         @Override
97         public <T extends DataObject> void mergeParentStructurePut(@NonNull LogicalDatastoreType store,
98                                                                    @NonNull InstanceIdentifier<T> path,
99                                                                    @NonNull T data) {
100             /////
101         }
102
103         @Override
104         public <T extends DataObject> void merge(LogicalDatastoreType store, InstanceIdentifier<T> path,
105                                                  T data) {
106             checkStore(store);
107             delegate.merge(path, data);
108         }
109
110         @Override
111         public <T extends DataObject> void merge(LogicalDatastoreType store, InstanceIdentifier<T> path,
112                                                  T data,
113                                                  boolean createMissingParents) {
114             checkStore(store);
115             delegate.merge(path, data, createMissingParents);
116         }
117
118         @Override
119         public <T extends DataObject> void mergeParentStructureMerge(@NonNull LogicalDatastoreType store,
120                                                                      @NonNull InstanceIdentifier<T> path,
121                                                                      @NonNull T data) {
122             //////
123         }
124
125         @Override
126         public boolean cancel() {
127             throw new UnsupportedOperationException("Managed transactions mustn't be cancelled");
128         }
129
130         @Override
131         public void delete(LogicalDatastoreType store, InstanceIdentifier<?> path) {
132             checkStore(store);
133             delegate.delete(path);
134         }
135
136         @Override
137         public @NonNull FluentFuture<? extends CommitInfo> commit() {
138             throw new UnsupportedOperationException("Managed transactions mustn't be committed");
139         }
140
141         void checkStore(LogicalDatastoreType store) {
142             Preconditions.checkArgument(datastoreType.equals(store), "Invalid "
143                             + "datastore %s  used instead of %s", store, datastoreType);
144         }
145
146         @Override
147         public Object getIdentifier() {
148             return delegate.getIdentifier();
149         }
150     }
151
152     private static final class ReadWriteTransactionAdapter<D extends Datastore> extends
153             WriteTransactionAdapter<D>
154             implements ReadWriteTransaction {
155         private final TypedReadWriteTransaction<D> delegate;
156
157         private ReadWriteTransactionAdapter(LogicalDatastoreType datastoreType,
158                                             TypedReadWriteTransaction<D> delegate) {
159             super(datastoreType, delegate);
160             this.delegate = delegate;
161         }
162
163         @Override
164         public @NonNull <T extends DataObject> FluentFuture<Optional<T>> read(@NonNull LogicalDatastoreType
165                                                                                                store,@NonNull
166                 InstanceIdentifier<T> path) {
167             checkStore(store);
168             return FluentFuture.from(delegate.read(path));
169         }
170
171         @Override
172         public FluentFuture<Boolean> exists(LogicalDatastoreType store, InstanceIdentifier<?> path) {
173             checkStore(store);
174             return FluentFuture.from(delegate.exists(path));
175         }
176     }
177 }