e925612e63bc8e00c582b4e6c3e195c3660daab6
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / 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.nb.rfc8040.rests.utils;
9
10 import com.google.common.base.Throwables;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.List;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
16 import org.opendaylight.mdsal.dom.api.DOMRpcException;
17 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
18 import org.opendaylight.netconf.api.NetconfDocumentedException;
19 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
20 import org.opendaylight.restconf.common.errors.RestconfError;
21 import org.opendaylight.yangtools.yang.common.RpcError;
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Add callback for future objects and result set to the data factory.
28  */
29 final class FutureCallbackTx {
30
31     private static final Logger LOG = LoggerFactory.getLogger(FutureCallbackTx.class);
32
33     private FutureCallbackTx() {
34         throw new UnsupportedOperationException("Util class");
35     }
36
37     /**
38      * Add callback to the future object.
39      *
40      * @param listenableFuture
41      *             future object
42      * @param txType
43      *             type of operation (READ, POST, PUT, DELETE)
44      * @param dataFactory
45      *             factory setting result
46      * @throws RestconfDocumentedException
47      *             if the Future throws an exception
48      */
49     @SuppressWarnings("checkstyle:IllegalCatch")
50     static <T> void addCallback(final ListenableFuture<T> listenableFuture, final String txType,
51             final FutureDataFactory<? super T> dataFactory) throws RestconfDocumentedException {
52
53         try {
54             final T result = listenableFuture.get();
55             dataFactory.setResult(result);
56             LOG.trace("Transaction({}) SUCCESSFUL", txType);
57         } catch (InterruptedException e) {
58             dataFactory.setFailureStatus();
59             LOG.warn("Transaction({}) FAILED!", txType, e);
60             throw new RestconfDocumentedException("Transaction failed", e);
61         } catch (ExecutionException e) {
62             dataFactory.setFailureStatus();
63             LOG.warn("Transaction({}) FAILED!", txType, e);
64
65             final Throwable cause = e.getCause();
66             if (cause instanceof DOMRpcException) {
67                 dataFactory.setResult((T) new DefaultDOMRpcResult(ImmutableList.of(
68                     RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", cause.getMessage()))));
69             } else if (cause instanceof TransactionCommitFailedException) {
70                 /* If device send some error message we want this message to get to client
71                    and not just to throw it away or override it with new generic message.
72                    We search for NetconfDocumentedException that was send from netconfSB
73                    and we create RestconfDocumentedException accordingly.
74                 */
75                 final List<Throwable> causalChain = Throwables.getCausalChain(cause);
76                 for (Throwable error : causalChain) {
77                     if (error instanceof NetconfDocumentedException) {
78                         throw new RestconfDocumentedException(error.getMessage(),
79                                 RestconfError.ErrorType.valueOfCaseInsensitive(
80                                         ((NetconfDocumentedException) error).getErrorType().getTypeValue()),
81                                 RestconfError.ErrorTag.valueOfCaseInsensitive(
82                                         ((NetconfDocumentedException) error).getErrorTag().getTagValue()), e);
83                     }
84                 }
85
86                 throw new RestconfDocumentedException("Transaction(" + txType + ") not committed correctly", e);
87             } else {
88                 throw new RestconfDocumentedException("Transaction failed", e);
89             }
90         }
91     }
92 }