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.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;
28 public class ControllerMdsalUtilsAsync {
30 private static final Logger LOG = LoggerFactory.getLogger(ControllerMdsalUtilsAsync.class);
31 private final DataBroker databroker;
34 * Class constructor setting the data broker.
36 * @param dataBroker the {@link org.opendaylight.controller.md.sal.binding.api.DataBroker}
38 public ControllerMdsalUtilsAsync(final DataBroker dataBroker) {
39 this.databroker = dataBroker;
43 * Executes delete as a non blocking transaction and returns the future.
46 * {@link LogicalDatastoreType} which should be modified
48 * {@link InstanceIdentifier} to read from
49 * @return The {@link CheckedFuture} object to which you can assign a
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();
61 * Executes delete as a non blocking transaction and assign a default callback.
64 * {@link LogicalDatastoreType} which should be modified
66 * {@link InstanceIdentifier} to read from
67 * @param operationDesc
68 * A brief description of the operation to perform
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);
78 * Executes put as non blocking transaction and return the future.
80 * @param logicalDatastoreType
81 * {@link LogicalDatastoreType} which should be modified
83 * {@link InstanceIdentifier} for path to read
85 * The data object type
86 * @return The {@link CheckedFuture} object to which you can assign a
89 public <D extends DataObject> CheckedFuture<Void, TransactionCommitFailedException> put(
90 final LogicalDatastoreType logicalDatastoreType,
91 final InstanceIdentifier<D> path,
93 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
94 transaction.put(logicalDatastoreType, path, data, true);
95 return transaction.submit();
99 * Executes put as non blocking transaction and assign default callback.
101 * @param logicalDatastoreType
102 * {@link LogicalDatastoreType} which should be modified
104 * {@link InstanceIdentifier} for path to read
106 * The data object type
107 * @param operationDesc
108 * A brief description of the operation to perform
110 public <D extends DataObject> void put(
111 final LogicalDatastoreType logicalDatastoreType,
112 final InstanceIdentifier<D> path,
114 final String operationDesc) {
115 assignDefaultCallback(put(logicalDatastoreType, path, data), operationDesc);
119 * Executes merge as non blocking transaction and return the future.
121 * @param logicalDatastoreType
122 * {@link LogicalDatastoreType} which should be modified
124 * {@link InstanceIdentifier} for path to read
126 * The data object type
128 * Whether or not to create missing parent.
129 * @return The {@link CheckedFuture} object to which you can assign a
132 public <D extends DataObject> CheckedFuture<Void, TransactionCommitFailedException> merge(
133 final LogicalDatastoreType logicalDatastoreType,
134 final InstanceIdentifier<D> path,
136 final boolean withParent) {
137 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
138 transaction.merge(logicalDatastoreType, path, data, withParent);
139 return transaction.submit();
143 * Executes merge as non blocking transaction and assign default callback.
145 * @param logicalDatastoreType
146 * {@link LogicalDatastoreType} which should be modified
148 * {@link InstanceIdentifier} for path to read
150 * The data object type
151 * @param operationDesc
152 * A brief description of the operation to perform
154 * Whether or not to create missing parent.
156 public <D extends DataObject> void merge(
157 final LogicalDatastoreType logicalDatastoreType,
158 final InstanceIdentifier<D> path,
160 final String operationDesc,
161 final boolean withParent) {
162 assignDefaultCallback(merge(logicalDatastoreType, path, data, withParent), operationDesc);
166 * Executes read as non blocking transaction and assign a default callback
167 * to close the transaction.
170 * {@link LogicalDatastoreType} to read
172 * {@link InstanceIdentifier} for path to read
174 * The data object type
175 * @return The {@link CheckedFuture} object to which you can assign a
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>>() {
185 public void onSuccess(final Optional<D> result) {
190 public void onFailure(final Throwable ex) {
194 Futures.addCallback(future, closeTransactionCallback, MoreExecutors.directExecutor());
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
205 * The transaction to commit.
206 * @param operationDesc
207 * A description of the transaction to commit.
209 void assignDefaultCallback(final CheckedFuture<Void, TransactionCommitFailedException> transactionFuture,
210 final String operationDesc) {
211 Futures.addCallback(transactionFuture, new FutureCallback<Void>() {
213 public void onSuccess(final Void result) {
214 LOG.debug("Transaction({}) SUCCESSFUL", operationDesc);
218 public void onFailure(final Throwable ex) {
219 LOG.error("Transaction({}) FAILED!", operationDesc, ex);
220 throw new IllegalStateException(" Transaction(" + operationDesc + ") not committed correctly", ex);
222 }, MoreExecutors.directExecutor());