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
8 package org.opendaylight.ovsdb.utils.mdsal.utils;
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;
26 public class ControllerMdsalUtilsAsync {
28 private static final Logger LOG = LoggerFactory.getLogger(ControllerMdsalUtilsAsync.class);
29 private final DataBroker databroker;
32 * Class constructor setting the data broker.
34 * @param dataBroker the {@link DataBroker}
36 public ControllerMdsalUtilsAsync(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 @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();
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 @NonNull CommitInfo> put(
88 final LogicalDatastoreType logicalDatastoreType,
89 final InstanceIdentifier<D> path,
91 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
92 transaction.mergeParentStructurePut(logicalDatastoreType, path, data);
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 // 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,
135 final boolean withParent) {
136 final WriteTransaction transaction = databroker.newWriteOnlyTransaction();
138 transaction.mergeParentStructureMerge(logicalDatastoreType, path, data);
140 transaction.merge(logicalDatastoreType, path, data);
142 return transaction.commit();
146 * Executes merge as non blocking transaction and assign default callback.
148 * @param logicalDatastoreType
149 * {@link LogicalDatastoreType} which should be modified
151 * {@link InstanceIdentifier} for path to read
153 * The data object type
154 * @param operationDesc
155 * A brief description of the operation to perform
157 * Whether or not to create missing parent.
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,
164 final String operationDesc,
165 final boolean withParent) {
166 assignDefaultCallback(merge(logicalDatastoreType, path, data, withParent), operationDesc);
170 * Executes read as non blocking transaction and assign a default callback
171 * to close the transaction.
174 * {@link LogicalDatastoreType} to read
176 * {@link InstanceIdentifier} for path to read
178 * The data object type
179 * @return The {@link FluentFuture} object to which you can assign a
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>>() {
189 public void onSuccess(final Optional<D> result) {
194 public void onFailure(final Throwable ex) {
198 future.addCallback(closeTransactionCallback, MoreExecutors.directExecutor());
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
209 * The transaction to commit.
210 * @param operationDesc
211 * A description of the transaction to commit.
213 void assignDefaultCallback(final FluentFuture<? extends @NonNull CommitInfo> transactionFuture,
214 final String operationDesc) {
215 transactionFuture.addCallback(new FutureCallback<CommitInfo>() {
217 public void onSuccess(final CommitInfo result) {
218 LOG.debug("Transaction({}) SUCCESSFUL", operationDesc);
222 public void onFailure(final Throwable ex) {
223 LOG.error("Transaction({}) FAILED!", operationDesc, ex);
224 throw new IllegalStateException(" Transaction(" + operationDesc + ") not committed correctly", ex);
226 }, MoreExecutors.directExecutor());