2 * Copyright (c) 2016 Cisco Systems, Inc. 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.restconf.restful.utils;
10 import com.google.common.util.concurrent.CheckedFuture;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.concurrent.CountDownLatch;
16 import javax.annotation.Nullable;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
18 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
19 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
20 import org.opendaylight.yangtools.yang.common.RpcError;
21 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * Add callback for future objects and result set to the data factory.
29 final class FutureCallbackTx {
31 private static final Logger LOG = LoggerFactory.getLogger(FutureCallbackTx.class);
33 private FutureCallbackTx() {
34 throw new UnsupportedOperationException("Util class");
38 * Add callback to the future object.
40 * @param listenableFuture
43 * type of operation (READ, POST, PUT, DELETE)
45 * factory setting result
47 @SuppressWarnings("checkstyle:IllegalCatch")
48 static <T, X extends Exception> void addCallback(final CheckedFuture<T, X> listenableFuture, final String txType,
49 final FutureDataFactory<T> dataFactory) {
50 final CountDownLatch responseWaiter = new CountDownLatch(1);
51 Futures.addCallback(listenableFuture, new FutureCallback<T>() {
54 public void onFailure(final Throwable throwable) {
55 responseWaiter.countDown();
56 handlingLoggerAndValues(throwable, txType, null, dataFactory);
60 public void onSuccess(final T result) {
61 handlingLoggerAndValues(null, txType, result, dataFactory);
62 responseWaiter.countDown();
67 responseWaiter.await();
68 } catch (final Exception e) {
69 final String msg = "Problem while waiting for response";
71 throw new RestconfDocumentedException(msg, e);
76 * Handling logger and result of callback - on success or on failure.
78 * <li>on success - set result to the factory
79 * <li>on failure - throw exception
83 * exception - if callback is onFailure
85 * type of operation (READ, POST, PUT, DELETE)
87 * result of future - if callback is on Success
89 * setter for result - in callback is onSuccess
91 protected static <T> void handlingLoggerAndValues(@Nullable final Throwable throwable, final String txType,
92 final T result, final FutureDataFactory<T> dataFactory) {
93 if (throwable != null) {
94 dataFactory.setFailureStatus();
95 LOG.warn("Transaction({}) FAILED!", txType, throwable);
96 if (throwable instanceof DOMRpcException) {
97 final List<RpcError> rpcErrorList = new ArrayList<>();
99 RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", throwable.getMessage()));
100 dataFactory.setResult((T) new DefaultDOMRpcResult(rpcErrorList));
102 throw new RestconfDocumentedException(
103 "Transaction(" + txType + ") not committed correctly", throwable);
106 LOG.trace("Transaction({}) SUCCESSFUL!", txType);
107 dataFactory.setResult(result);