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 final static 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 static <T, X extends Exception> void addCallback(final CheckedFuture<T, X> listenableFuture, final String txType,
48 final FutureDataFactory<T> dataFactory) {
49 final CountDownLatch responseWaiter = new CountDownLatch(1);
50 Futures.addCallback(listenableFuture, new FutureCallback<T>() {
53 public void onFailure(final Throwable t) {
54 responseWaiter.countDown();
55 handlingLoggerAndValues(t, txType, null, dataFactory);
59 public void onSuccess(final T result) {
60 handlingLoggerAndValues(null, txType, result, dataFactory);
61 responseWaiter.countDown();
66 responseWaiter.await();
67 } catch (final Exception e) {
68 final String msg = "Problem while waiting for response";
70 throw new RestconfDocumentedException(msg, e);
75 * Handling logger and result of callback - on success or on failure
77 * <li>on success - set result to the factory
78 * <li>on failure - throw exception
82 * - exception - if callback is onFailure
84 * - type of operation (READ, POST, PUT, DELETE)
86 * - result of future - if callback is on Success
88 * - setter for result - in callback is onSuccess
90 protected static <T> void handlingLoggerAndValues(@Nullable final Throwable t, final String txType,
91 final T result, final FutureDataFactory<T> dataFactory) {
93 LOG.warn("Transaction({}) FAILED!", txType, t);
94 if (t instanceof DOMRpcException) {
95 final List<RpcError> rpcErrorList = new ArrayList<>();
96 rpcErrorList.add(RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", t.getMessage()));
97 dataFactory.setResult((T) new DefaultDOMRpcResult(rpcErrorList));
99 throw new RestconfDocumentedException(" Transaction(" + txType + ") not committed correctly", t);
102 LOG.trace("Transaction({}) SUCCESSFUL!", txType);
103 dataFactory.setResult(result);