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.
25 final class FutureCallbackTx {
27 private static final Logger LOG = LoggerFactory.getLogger(FutureCallbackTx.class);
29 private FutureCallbackTx() {
30 throw new UnsupportedOperationException("Util class");
34 * Add callback to the future object.
36 * @param listenableFuture
39 * type of operation (READ, POST, PUT, DELETE)
41 * factory setting result
42 * @throws RestconfDocumentedException
43 * if the Future throws an exception
45 @SuppressWarnings("checkstyle:IllegalCatch")
46 static <T, X extends Exception> void addCallback(final CheckedFuture<T, X> listenableFuture, final String txType,
47 final FutureDataFactory<T> dataFactory) throws RestconfDocumentedException {
50 final T result = listenableFuture.checkedGet();
51 dataFactory.setResult(result);
52 LOG.trace("Transaction({}) SUCCESSFUL", txType);
53 } catch (Exception e) {
54 dataFactory.setFailureStatus();
55 LOG.warn("Transaction({}) FAILED!", txType, e);
56 if (e instanceof DOMRpcException) {
57 final List<RpcError> rpcErrorList = new ArrayList<>();
59 RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", e.getMessage()));
60 dataFactory.setResult((T) new DefaultDOMRpcResult(rpcErrorList));
62 throw new RestconfDocumentedException(
63 "Transaction(" + txType + ") not committed correctly", e);