2 * Copyright (c) 2016 Inocybe Technologies and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.utils.mdsal.utils;
11 import com.google.common.util.concurrent.FluentFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.Optional;
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;
25 public class MdsalUtilsAsync {
27 private static final Logger LOG = LoggerFactory.getLogger(MdsalUtilsAsync.class);
28 private final DataBroker databroker;
31 * Class constructor setting the data broker.
33 * @param dataBroker the {@link DataBroker}
35 public MdsalUtilsAsync(final DataBroker dataBroker) {
36 this.databroker = dataBroker;
40 * Executes delete as a non blocking transaction and returns the future.
43 * {@link LogicalDatastoreType} which should be modified
45 * {@link InstanceIdentifier} to read from
46 * @return The {@link FluentFuture} object to which you can assign a
49 public <D extends DataObject> FluentFuture<? extends CommitInfo> delete(
50 final LogicalDatastoreType store,
51 final InstanceIdentifier<D> path) {
52 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
53 transaction.delete(store, path);
54 return transaction.commit();
58 * Executes delete as a non blocking transaction and assign a default callback.
61 * {@link LogicalDatastoreType} which should be modified
63 * {@link InstanceIdentifier} to read from
64 * @param operationDesc
65 * A brief description of the operation to perform
67 public <D extends DataObject> void delete(
68 final LogicalDatastoreType store,
69 final InstanceIdentifier<D> path,
70 final String operationDesc) {
71 assignDefaultCallback(delete(store, path), operationDesc);
75 * Executes put as non blocking transaction and return the future.
77 * @param logicalDatastoreType
78 * {@link LogicalDatastoreType} which should be modified
80 * {@link InstanceIdentifier} for path to read
82 * The data object type
83 * @return The {@link FluentFuture} object to which you can assign a
86 public <D extends DataObject> FluentFuture<? extends CommitInfo> put(
87 final LogicalDatastoreType logicalDatastoreType,
88 final InstanceIdentifier<D> path,
90 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
91 transaction.mergeParentStructurePut(logicalDatastoreType, path, data);
92 return transaction.commit();
96 * Executes put as non blocking transaction and assign default callback.
98 * @param logicalDatastoreType
99 * {@link LogicalDatastoreType} which should be modified
101 * {@link InstanceIdentifier} for path to read
103 * The data object type
104 * @param operationDesc
105 * A brief description of the operation to perform
107 public <D extends DataObject> void put(
108 final LogicalDatastoreType logicalDatastoreType,
109 final InstanceIdentifier<D> path,
111 final String operationDesc) {
112 assignDefaultCallback(put(logicalDatastoreType, path, data), operationDesc);
116 * Executes merge as non blocking transaction and return the future.
118 * @param logicalDatastoreType
119 * {@link LogicalDatastoreType} which should be modified
121 * {@link InstanceIdentifier} for path to read
123 * The data object type
125 * Whether or not to create missing parent.
126 * @return The {@link FluentFuture} object to which you can assign a
129 // FIXME: eliminate the boolean flag here to separate out the distinct code paths
130 public <D extends DataObject> FluentFuture<? extends CommitInfo> merge(
131 final LogicalDatastoreType logicalDatastoreType,
132 final InstanceIdentifier<D> path,
134 final boolean withParent) {
135 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
137 transaction.mergeParentStructureMerge(logicalDatastoreType, path, data);
139 transaction.merge(logicalDatastoreType, path, data);
141 return transaction.commit();
145 * Executes merge as non blocking transaction and assign default callback.
147 * @param logicalDatastoreType
148 * {@link LogicalDatastoreType} which should be modified
150 * {@link InstanceIdentifier} for path to read
152 * The data object type
153 * @param operationDesc
154 * A brief description of the operation to perform
156 * Whether or not to create missing parent.
158 // FIXME: eliminate the boolean flag here to separate out the distinct code paths
159 public <D extends DataObject> void merge(
160 final LogicalDatastoreType logicalDatastoreType,
161 final InstanceIdentifier<D> path,
163 final String operationDesc,
164 final boolean withParent) {
165 assignDefaultCallback(merge(logicalDatastoreType, path, data, withParent), operationDesc);
169 * Executes read as non blocking transaction and assign a default callback
170 * to close the transaction.
173 * {@link LogicalDatastoreType} to read
175 * {@link InstanceIdentifier} for path to read
177 * The data object type
178 * @return The {@link FluentFuture} object to which you can assign a
181 public <D extends DataObject> FluentFuture<Optional<D>> read(
182 final LogicalDatastoreType store,
183 final InstanceIdentifier<D> path) {
184 final ReadTransaction transaction = databroker.newReadOnlyTransaction();
185 final FluentFuture<Optional<D>> future = transaction.read(store, path);
186 final FutureCallback<Optional<D>> closeTransactionCallback = new FutureCallback<Optional<D>>() {
188 public void onSuccess(final Optional<D> result) {
193 public void onFailure(final Throwable ex) {
197 future.addCallback(closeTransactionCallback, MoreExecutors.directExecutor());
202 * Assign a default callback to a {@link FluentFuture}. It will either log
203 * a message at DEBUG level if the transaction succeed, or will log at ERROR
204 * level and throw an {@link IllegalStateException} if the transaction
207 * @param transactionFuture
208 * The transaction to commit.
209 * @param operationDesc
210 * A description of the transaction to commit.
212 void assignDefaultCallback(final FluentFuture<? extends CommitInfo> transactionFuture,
213 final String operationDesc) {
214 transactionFuture.addCallback(new FutureCallback<CommitInfo>() {
216 public void onSuccess(final CommitInfo result) {
217 LOG.debug("Transaction({}) SUCCESSFUL", operationDesc);
221 public void onFailure(final Throwable ex) {
222 LOG.error("Transaction({}) FAILED!", operationDesc, ex);
223 throw new IllegalStateException(" Transaction(" + operationDesc + ") not committed correctly", ex);
225 }, MoreExecutors.directExecutor());