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