Split Restconf implementations (draft02 and RFC) - move restconf
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / restconf / restful / utils / FutureCallbackTx.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.restful.utils;
9
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;
20
21 /**
22  * Add callback for future objects and result set to the data factory.
23  *
24  */
25 final class FutureCallbackTx {
26
27     private static final Logger LOG = LoggerFactory.getLogger(FutureCallbackTx.class);
28
29     private FutureCallbackTx() {
30         throw new UnsupportedOperationException("Util class");
31     }
32
33     /**
34      * Add callback to the future object.
35      *
36      * @param listenableFuture
37      *             future object
38      * @param txType
39      *             type of operation (READ, POST, PUT, DELETE)
40      * @param dataFactory
41      *             factory setting result
42      * @throws RestconfDocumentedException
43      *             if the Future throws an exception
44      */
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 {
48
49         try {
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<>();
58                 rpcErrorList.add(
59                         RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", e.getMessage()));
60                 dataFactory.setResult((T) new DefaultDOMRpcResult(rpcErrorList));
61             } else {
62                 throw new RestconfDocumentedException(
63                         "Transaction(" + txType + ") not committed correctly", e);
64             }
65         }
66     }
67 }