Remove use of deprecated Guava methods
[ovsdb.git] / utils / mdsal-utils / src / main / java / org / opendaylight / ovsdb / utils / mdsal / utils / ControllerMdsalUtilsAsync.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.utils.mdsal.utils;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
21 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 @Deprecated
28 public class ControllerMdsalUtilsAsync {
29
30     private static final Logger LOG = LoggerFactory.getLogger(ControllerMdsalUtilsAsync.class);
31     private final DataBroker databroker;
32
33     /**
34      * Class constructor setting the data broker.
35      *
36      * @param dataBroker the {@link org.opendaylight.controller.md.sal.binding.api.DataBroker}
37      */
38     public ControllerMdsalUtilsAsync(final DataBroker dataBroker) {
39         this.databroker = dataBroker;
40     }
41
42     /**
43      * Executes delete as a non blocking transaction and returns the future.
44      *
45      * @param store
46      *            {@link LogicalDatastoreType} which should be modified
47      * @param path
48      *            {@link InstanceIdentifier} to read from
49      * @return The {@link CheckedFuture} object to which you can assign a
50      *         callback
51      */
52     public <D extends DataObject> CheckedFuture<Void, TransactionCommitFailedException> delete(
53                                     final LogicalDatastoreType store,
54                                     final InstanceIdentifier<D> path)  {
55         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
56         transaction.delete(store, path);
57         return transaction.submit();
58     }
59
60     /**
61      * Executes delete as a non blocking transaction and assign a default callback.
62      *
63      * @param store
64      *            {@link LogicalDatastoreType} which should be modified
65      * @param path
66      *            {@link InstanceIdentifier} to read from
67      * @param operationDesc
68      *            A brief description of the operation to perform
69      */
70     public <D extends DataObject> void delete(
71                                     final LogicalDatastoreType store,
72                                     final InstanceIdentifier<D> path,
73                                     final String operationDesc)  {
74         assignDefaultCallback(delete(store, path), operationDesc);
75     }
76
77     /**
78      * Executes put as non blocking transaction and return the future.
79      *
80      * @param logicalDatastoreType
81      *            {@link LogicalDatastoreType} which should be modified
82      * @param path
83      *            {@link InstanceIdentifier} for path to read
84      * @param <D>
85      *            The data object type
86      * @return The {@link CheckedFuture} object to which you can assign a
87      *         callback
88      */
89     public <D extends DataObject> CheckedFuture<Void, TransactionCommitFailedException> put(
90                                         final LogicalDatastoreType logicalDatastoreType,
91                                         final InstanceIdentifier<D> path,
92                                         final D data)  {
93         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
94         transaction.put(logicalDatastoreType, path, data, true);
95         return transaction.submit();
96     }
97
98     /**
99      * Executes put as non blocking transaction and assign default callback.
100      *
101      * @param logicalDatastoreType
102      *            {@link LogicalDatastoreType} which should be modified
103      * @param path
104      *            {@link InstanceIdentifier} for path to read
105      * @param <D>
106      *            The data object type
107      * @param operationDesc
108      *            A brief description of the operation to perform
109      */
110     public <D extends DataObject> void put(
111                                         final LogicalDatastoreType logicalDatastoreType,
112                                         final InstanceIdentifier<D> path,
113                                         final D data,
114                                         final String operationDesc)  {
115         assignDefaultCallback(put(logicalDatastoreType, path, data), operationDesc);
116     }
117
118     /**
119      * Executes merge as non blocking transaction and return the future.
120      *
121      * @param logicalDatastoreType
122      *            {@link LogicalDatastoreType} which should be modified
123      * @param path
124      *            {@link InstanceIdentifier} for path to read
125      * @param <D>
126      *            The data object type
127      * @param withParent
128      *            Whether or not to create missing parent.
129      * @return The {@link CheckedFuture} object to which you can assign a
130      *         callback
131      */
132     public <D extends DataObject> CheckedFuture<Void, TransactionCommitFailedException> merge(
133                                         final LogicalDatastoreType logicalDatastoreType,
134                                         final InstanceIdentifier<D> path,
135                                         final D data,
136                                         final boolean withParent)  {
137         final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
138         transaction.merge(logicalDatastoreType, path, data, withParent);
139         return transaction.submit();
140     }
141
142     /**
143      * Executes merge as non blocking transaction and assign default callback.
144      *
145      * @param logicalDatastoreType
146      *            {@link LogicalDatastoreType} which should be modified
147      * @param path
148      *            {@link InstanceIdentifier} for path to read
149      * @param <D>
150      *            The data object type
151      * @param operationDesc
152      *            A brief description of the operation to perform
153      * @param withParent
154      *            Whether or not to create missing parent.
155      */
156     public <D extends DataObject> void merge(
157                                         final LogicalDatastoreType logicalDatastoreType,
158                                         final InstanceIdentifier<D> path,
159                                         final D data,
160                                         final String operationDesc,
161                                         final boolean withParent)  {
162         assignDefaultCallback(merge(logicalDatastoreType, path, data, withParent), operationDesc);
163     }
164
165     /**
166      * Executes read as non blocking transaction and assign a default callback
167      * to close the transaction.
168      *
169      * @param store
170      *            {@link LogicalDatastoreType} to read
171      * @param path
172      *            {@link InstanceIdentifier} for path to read
173      * @param <D>
174      *            The data object type
175      * @return The {@link CheckedFuture} object to which you can assign a
176      *         callback
177      */
178     public <D extends DataObject> CheckedFuture<Optional<D>, ReadFailedException> read(
179                                         final LogicalDatastoreType store,
180                                         final InstanceIdentifier<D> path)  {
181         final ReadOnlyTransaction transaction = databroker.newReadOnlyTransaction();
182         final CheckedFuture<Optional<D>, ReadFailedException> future = transaction.read(store, path);
183         final FutureCallback<Optional<D>> closeTransactionCallback = new FutureCallback<Optional<D>>() {
184             @Override
185             public void onSuccess(final Optional<D> result) {
186                 transaction.close();
187             }
188
189             @Override
190             public void onFailure(final Throwable ex) {
191                 transaction.close();
192             }
193         };
194         Futures.addCallback(future, closeTransactionCallback, MoreExecutors.directExecutor());
195         return future;
196     }
197
198     /**
199      * Assign a default callback to a {@link CheckedFuture}. It will either log
200      * a message at DEBUG level if the transaction succeed, or will log at ERROR
201      * level and throw an {@link IllegalStateException} if the transaction
202      * failed.
203      *
204      * @param transaction
205      *            The transaction to commit.
206      * @param operationDesc
207      *            A description of the transaction to commit.
208      */
209     void assignDefaultCallback(final CheckedFuture<Void, TransactionCommitFailedException> transactionFuture,
210             final String operationDesc) {
211         Futures.addCallback(transactionFuture, new FutureCallback<Void>() {
212             @Override
213             public void onSuccess(final Void result) {
214                 LOG.debug("Transaction({}) SUCCESSFUL", operationDesc);
215             }
216
217             @Override
218             public void onFailure(final Throwable ex) {
219                 LOG.error("Transaction({}) FAILED!", operationDesc, ex);
220                 throw new IllegalStateException("  Transaction(" + operationDesc + ") not committed correctly", ex);
221             }
222         }, MoreExecutors.directExecutor());
223     }
224 }