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.CheckedFuture;
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.Optional;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.ReadTransaction;
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 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class MdsalUtilsAsync {
28 private static final Logger LOG = LoggerFactory.getLogger(MdsalUtilsAsync.class);
29 private final DataBroker databroker;
32 * Class constructor setting the data broker.
34 * @param dataBroker the {@link DataBroker}
36 public MdsalUtilsAsync(final DataBroker dataBroker) {
37 this.databroker = dataBroker;
41 * Executes delete as a non blocking transaction and returns the future.
44 * {@link LogicalDatastoreType} which should be modified
46 * {@link InstanceIdentifier} to read from
47 * @return The {@link FluentFuture} object to which you can assign a
50 public <D extends DataObject> FluentFuture<? extends 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();
59 * Executes delete as a non blocking transaction and assign a default callback.
62 * {@link LogicalDatastoreType} which should be modified
64 * {@link InstanceIdentifier} to read from
65 * @param operationDesc
66 * A brief description of the operation to perform
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);
76 * Executes put as non blocking transaction and return the future.
78 * @param logicalDatastoreType
79 * {@link LogicalDatastoreType} which should be modified
81 * {@link InstanceIdentifier} for path to read
83 * The data object type
84 * @return The {@link FluentFuture} object to which you can assign a
87 public <D extends DataObject> FluentFuture<? extends CommitInfo> put(
88 final LogicalDatastoreType logicalDatastoreType,
89 final InstanceIdentifier<D> path,
91 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
92 transaction.put(logicalDatastoreType, path, data, true);
93 return transaction.commit();
97 * Executes put as non blocking transaction and assign default callback.
99 * @param logicalDatastoreType
100 * {@link LogicalDatastoreType} which should be modified
102 * {@link InstanceIdentifier} for path to read
104 * The data object type
105 * @param operationDesc
106 * A brief description of the operation to perform
108 public <D extends DataObject> void put(
109 final LogicalDatastoreType logicalDatastoreType,
110 final InstanceIdentifier<D> path,
112 final String operationDesc) {
113 assignDefaultCallback(put(logicalDatastoreType, path, data), operationDesc);
117 * Executes merge as non blocking transaction and return the future.
119 * @param logicalDatastoreType
120 * {@link LogicalDatastoreType} which should be modified
122 * {@link InstanceIdentifier} for path to read
124 * The data object type
126 * Whether or not to create missing parent.
127 * @return The {@link FluentFuture} object to which you can assign a
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();
136 transaction.merge(logicalDatastoreType, path, data, withParent);
137 return transaction.commit();
141 * Executes merge as non blocking transaction and assign default callback.
143 * @param logicalDatastoreType
144 * {@link LogicalDatastoreType} which should be modified
146 * {@link InstanceIdentifier} for path to read
148 * The data object type
149 * @param operationDesc
150 * A brief description of the operation to perform
152 * Whether or not to create missing parent.
154 public <D extends DataObject> void merge(
155 final LogicalDatastoreType logicalDatastoreType,
156 final InstanceIdentifier<D> path,
158 final String operationDesc,
159 final boolean withParent) {
160 assignDefaultCallback(merge(logicalDatastoreType, path, data, withParent), operationDesc);
164 * Executes read as non blocking transaction and assign a default callback
165 * to close the transaction.
168 * {@link LogicalDatastoreType} to read
170 * {@link InstanceIdentifier} for path to read
172 * The data object type
173 * @return The {@link FluentFuture} object to which you can assign a
176 public <D extends DataObject> FluentFuture<Optional<D>> read(
177 final LogicalDatastoreType store,
178 final InstanceIdentifier<D> path) {
179 final ReadTransaction transaction = databroker.newReadOnlyTransaction();
180 final FluentFuture<Optional<D>> future = transaction.read(store, path);
181 final FutureCallback<Optional<D>> closeTransactionCallback = new FutureCallback<Optional<D>>() {
183 public void onSuccess(final Optional<D> result) {
188 public void onFailure(final Throwable ex) {
192 future.addCallback(closeTransactionCallback, MoreExecutors.directExecutor());
197 * Assign a default callback to a {@link CheckedFuture}. It will either log
198 * a message at DEBUG level if the transaction succeed, or will log at ERROR
199 * level and throw an {@link IllegalStateException} if the transaction
202 * @param transactionFuture
203 * The transaction to commit.
204 * @param operationDesc
205 * A description of the transaction to commit.
207 void assignDefaultCallback(final FluentFuture<? extends CommitInfo> transactionFuture,
208 final String operationDesc) {
209 transactionFuture.addCallback(new FutureCallback<CommitInfo>() {
211 public void onSuccess(final CommitInfo result) {
212 LOG.debug("Transaction({}) SUCCESSFUL", operationDesc);
216 public void onFailure(final Throwable ex) {
217 LOG.error("Transaction({}) FAILED!", operationDesc, ex);
218 throw new IllegalStateException(" Transaction(" + operationDesc + ") not committed correctly", ex);
220 }, MoreExecutors.directExecutor());