Split Restconf implementations (draft02 and RFC) - move
[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  * @deprecated move to splitted module restconf-nb-rfc8040
25  */
26 @Deprecated
27 final class FutureCallbackTx {
28
29     private static final Logger LOG = LoggerFactory.getLogger(FutureCallbackTx.class);
30
31     private FutureCallbackTx() {
32         throw new UnsupportedOperationException("Util class");
33     }
34
35     /**
36      * Add callback to the future object.
37      *
38      * @param listenableFuture
39      *             future object
40      * @param txType
41      *             type of operation (READ, POST, PUT, DELETE)
42      * @param dataFactory
43      *             factory setting result
44      * @throws RestconfDocumentedException
45      *             if the Future throws an exception
46      */
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 {
50
51         try {
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<>();
60                 rpcErrorList.add(
61                         RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", e.getMessage()));
62                 dataFactory.setResult((T) new DefaultDOMRpcResult(rpcErrorList));
63             } else {
64                 throw new RestconfDocumentedException(
65                         "Transaction(" + txType + ") not committed correctly", e);
66             }
67         }
68     }
69 }