Enable checkstyle in mdsal-binding-util
[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     // We want to subclass this class, even though it has a private constructor
77     @SuppressWarnings("FinalClass")
78     private static class WriteTransactionAdapter<D extends Datastore, T extends TypedWriteTransaction<D>>
79             extends ForwardingObject implements WriteTransaction {
80         private final LogicalDatastoreType datastoreType;
81         private final T delegate;
82
83         private WriteTransactionAdapter(final LogicalDatastoreType datastoreType, final T delegate) {
84             this.datastoreType = datastoreType;
85             this.delegate = delegate;
86         }
87
88         @Override
89         public <T extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
90                 final T data) {
91             checkStore(store);
92             delegate.put(path, data);
93         }
94
95         @Override
96         public <T extends DataObject> void put(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
97                 final T data, final boolean createMissingParents) {
98             checkStore(store);
99             delegate.put(path, data, createMissingParents);
100         }
101
102         @Override
103         public <T extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
104                 final T data) {
105             checkStore(store);
106             delegate.merge(path, data);
107         }
108
109         @Override
110         public <T extends DataObject> void merge(final LogicalDatastoreType store, final InstanceIdentifier<T> path,
111                 final T data, final boolean createMissingParents) {
112             checkStore(store);
113             delegate.merge(path, data, createMissingParents);
114         }
115
116         @Override
117         public boolean cancel() {
118             throw new UnsupportedOperationException("Managed transactions must not be cancelled");
119         }
120
121         @Override
122         public void delete(final LogicalDatastoreType store, final InstanceIdentifier<?> path) {
123             checkStore(store);
124             delegate.delete(path);
125         }
126
127         @Override
128         public @NonNull FluentFuture<? extends CommitInfo> commit() {
129             throw new UnsupportedOperationException("Managed transactions must not be committed");
130         }
131
132         void checkStore(final LogicalDatastoreType store) {
133             checkArgument(datastoreType.equals(store), "Invalid datastore %s used instead of %s", store, datastoreType);
134         }
135
136         @Override
137         public Object getIdentifier() {
138             return delegate.getIdentifier();
139         }
140
141         @Override
142         protected T delegate() {
143             return delegate;
144         }
145     }
146
147     private static final class ReadWriteTransactionAdapter<D extends Datastore>
148             extends WriteTransactionAdapter<D, TypedReadWriteTransaction<D>> implements ReadWriteTransaction {
149         private ReadWriteTransactionAdapter(final LogicalDatastoreType datastoreType,
150                 final TypedReadWriteTransaction<D> delegate) {
151             super(datastoreType, delegate);
152         }
153
154         @Override
155         public <T extends DataObject> FluentFuture<Optional<T>> read(final LogicalDatastoreType store,
156                 final InstanceIdentifier<T> path) {
157             checkStore(store);
158             return delegate().read(path);
159         }
160     }
161 }