e4c88f22a683aa27aa7a4902fc885a1fc0cfe190
[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.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
17 import org.opendaylight.mdsal.dom.api.DOMActionException;
18 import org.opendaylight.mdsal.dom.api.DOMRpcException;
19 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
20 import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
21 import org.opendaylight.netconf.api.DocumentedException;
22 import org.opendaylight.netconf.api.NetconfDocumentedException;
23 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
24 import org.opendaylight.restconf.common.errors.RestconfError;
25 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
26 import org.opendaylight.yangtools.yang.common.RpcError;
27 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Add callback for future objects and result set to the data factory.
34  */
35 final class FutureCallbackTx {
36     private static final Logger LOG = LoggerFactory.getLogger(FutureCallbackTx.class);
37
38     private FutureCallbackTx() {
39         throw new UnsupportedOperationException("Util class");
40     }
41
42     /**
43      * Add callback to the future object.
44      *
45      * @param listenableFuture
46      *             future object
47      * @param txType
48      *             type of operation (READ, POST, PUT, DELETE)
49      * @param dataFactory
50      *             factory setting result
51      * @throws RestconfDocumentedException
52      *             if the Future throws an exception
53      */
54     // FIXME: this is a *synchronous operation* and has to die
55     static <T> void addCallback(final ListenableFuture<T> listenableFuture, final String txType,
56                                 final FutureDataFactory<? super T> dataFactory) throws RestconfDocumentedException {
57         addCallback(listenableFuture, txType, dataFactory, null, null);
58     }
59
60     /**
61      * Add callback to the future object and close transaction chain.
62      *
63      * @param listenableFuture
64      *             future object
65      * @param txType
66      *             type of operation (READ, POST, PUT, DELETE)
67      * @param dataFactory
68      *             factory setting result
69      * @param strategy Strategy for various RESTCONF operations
70      * @param path unique identifier of a particular node instance in the data tree
71      * @throws RestconfDocumentedException
72      *             if the Future throws an exception
73      */
74     // FIXME: this is a *synchronous operation* and has to die
75     static <T> void addCallback(final ListenableFuture<T> listenableFuture, final String txType,
76                                 final FutureDataFactory<? super T> dataFactory,
77                                 @Nullable final RestconfStrategy strategy,
78                                 final YangInstanceIdentifier path)
79         throws RestconfDocumentedException {
80
81         try {
82             final T result = listenableFuture.get();
83             dataFactory.setResult(result);
84             LOG.trace("Transaction({}) SUCCESSFUL", txType);
85         } catch (InterruptedException e) {
86             dataFactory.setFailureStatus();
87             LOG.warn("Transaction({}) FAILED!", txType, e);
88             throw new RestconfDocumentedException("Transaction failed", e);
89         } catch (ExecutionException e) {
90             dataFactory.setFailureStatus();
91             LOG.warn("Transaction({}) FAILED!", txType, e);
92
93             final Throwable cause = e.getCause();
94             if (cause instanceof DOMRpcException) {
95                 dataFactory.setResult((T) new DefaultDOMRpcResult(ImmutableList.of(
96                     RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", cause.getMessage()))));
97             } else if (cause instanceof DOMActionException) {
98                 dataFactory.setResult((T) new SimpleDOMActionResult(ImmutableList.of(
99                     RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", cause.getMessage()))));
100             } else if (cause instanceof TransactionCommitFailedException) {
101                 /* If device send some error message we want this message to get to client
102                    and not just to throw it away or override it with new generic message.
103                    We search for NetconfDocumentedException that was send from netconfSB
104                    and we create RestconfDocumentedException accordingly.
105                 */
106                 final List<Throwable> causalChain = Throwables.getCausalChain(cause);
107                 for (Throwable error : causalChain) {
108                     if (error instanceof DocumentedException) {
109                         final DocumentedException.ErrorTag errorTag = ((DocumentedException) error).getErrorTag();
110                         if (errorTag.equals(DocumentedException.ErrorTag.DATA_EXISTS)) {
111                             LOG.trace("Operation via Restconf was not executed because data at {} already exists",
112                                 path);
113                             throw new RestconfDocumentedException(e, new RestconfError(RestconfError.ErrorType.PROTOCOL,
114                                 RestconfError.ErrorTag.DATA_EXISTS, "Data already exists", path));
115                         } else if (errorTag.equals(DocumentedException.ErrorTag.DATA_MISSING)) {
116                             LOG.trace("Operation via Restconf was not executed because data at {} does not exist",
117                                 path);
118                             throw new RestconfDocumentedException(e, new RestconfError(RestconfError.ErrorType.PROTOCOL,
119                                 RestconfError.ErrorTag.DATA_MISSING, "Data does not exist", path));
120                         }
121                     }
122                     if (error instanceof NetconfDocumentedException) {
123                         throw new RestconfDocumentedException(error.getMessage(),
124                                 RestconfError.ErrorType.valueOfCaseInsensitive(
125                                         ((NetconfDocumentedException) error).getErrorType().getTypeValue()),
126                                 RestconfError.ErrorTag.valueOfCaseInsensitive(
127                                         ((NetconfDocumentedException) error).getErrorTag().getTagValue()), e);
128                     }
129                 }
130
131                 throw new RestconfDocumentedException("Transaction(" + txType + ") not committed correctly", e);
132             } else {
133                 throw new RestconfDocumentedException("Transaction failed", e);
134             }
135         } finally {
136             if (strategy != null) {
137                 strategy.close();
138             }
139         }
140     }
141 }