NETCONF-542: PUT request return 500 if operational data are used
[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.util.concurrent.CheckedFuture;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
15 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
16 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
17 import org.opendaylight.netconf.api.NetconfDocumentedException;
18 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
19 import org.opendaylight.restconf.common.errors.RestconfError;
20 import org.opendaylight.yangtools.yang.common.RpcError;
21 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Add callback for future objects and result set to the data factory.
27  *
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, X extends Exception> void addCallback(final CheckedFuture<T, X> listenableFuture, final String txType,
51             final FutureDataFactory<T> dataFactory) throws RestconfDocumentedException {
52
53         try {
54             final T result = listenableFuture.checkedGet();
55             dataFactory.setResult(result);
56             LOG.trace("Transaction({}) SUCCESSFUL", txType);
57         } catch (Exception e) {
58             dataFactory.setFailureStatus();
59             LOG.warn("Transaction({}) FAILED!", txType, e);
60             if (e instanceof DOMRpcException) {
61                 final List<RpcError> rpcErrorList = new ArrayList<>();
62                 rpcErrorList.add(
63                         RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", e.getMessage()));
64                 dataFactory.setResult((T) new DefaultDOMRpcResult(rpcErrorList));
65             } else if (e instanceof TransactionCommitFailedException) {
66                 /* If device send some error message we want this message to get to client
67                    and not just to throw it away or override it with new generic message.
68                    We search for NetconfDocumentedException that was send from netconfSB
69                    and we create RestconfDocumentedException accordingly.
70                 */
71                 final List<Throwable> causalChain = Throwables.getCausalChain(e);
72                 for (Throwable error : causalChain) {
73                     if (error instanceof NetconfDocumentedException) {
74                         throw new RestconfDocumentedException(error.getMessage(),
75                                 RestconfError.ErrorType.valueOfCaseInsensitive(
76                                         ((NetconfDocumentedException) error).getErrorType().getTypeValue()),
77                                 RestconfError.ErrorTag.valueOfCaseInsensitive(
78                                         ((NetconfDocumentedException) error).getErrorTag().getTagValue()), e);
79                     }
80                 }
81
82                 throw new RestconfDocumentedException("Transaction(" + txType + ") not committed correctly", e);
83             } else {
84                 throw new RestconfDocumentedException("Transaction failed", e);
85             }
86         }
87     }
88 }