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 java.util.ArrayList;
12 import java.util.List;
13 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
14 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
15 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
16 import org.opendaylight.yangtools.yang.common.RpcError;
17 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Add callback for future objects and result set to the data factory.
24 * @deprecated move to splitted module restconf-nb-rfc8040
27 final class FutureCallbackTx {
29 private static final Logger LOG = LoggerFactory.getLogger(FutureCallbackTx.class);
31 private FutureCallbackTx() {
32 throw new UnsupportedOperationException("Util class");
36 * Add callback to the future object.
38 * @param listenableFuture
41 * type of operation (READ, POST, PUT, DELETE)
43 * factory setting result
44 * @throws RestconfDocumentedException
45 * if the Future throws an exception
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) throws RestconfDocumentedException {
52 final T result = listenableFuture.checkedGet();
53 dataFactory.setResult(result);
54 LOG.trace("Transaction({}) SUCCESSFUL", txType);
55 } catch (final Exception e) {
56 dataFactory.setFailureStatus();
57 LOG.warn("Transaction({}) FAILED!", txType, e);
58 if (e instanceof DOMRpcException) {
59 final List<RpcError> rpcErrorList = new ArrayList<>();
61 RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", e.getMessage()));
62 dataFactory.setResult((T) new DefaultDOMRpcResult(rpcErrorList));
64 throw new RestconfDocumentedException(
65 "Transaction(" + txType + ") not committed correctly", e);