2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
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;
33 * Add callback for future objects and result set to the data factory.
35 final class FutureCallbackTx {
36 private static final Logger LOG = LoggerFactory.getLogger(FutureCallbackTx.class);
38 private FutureCallbackTx() {
39 throw new UnsupportedOperationException("Util class");
43 * Add callback to the future object.
45 * @param listenableFuture
48 * type of operation (READ, POST, PUT, DELETE)
50 * factory setting result
51 * @throws RestconfDocumentedException
52 * if the Future throws an exception
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);
61 * Add callback to the future object and close transaction chain.
63 * @param listenableFuture
66 * type of operation (READ, POST, PUT, DELETE)
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
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) throws RestconfDocumentedException {
80 final T result = listenableFuture.get();
81 dataFactory.setResult(result);
82 LOG.trace("Transaction({}) SUCCESSFUL", txType);
83 } catch (InterruptedException e) {
84 dataFactory.setFailureStatus();
85 LOG.warn("Transaction({}) FAILED!", txType, e);
86 throw new RestconfDocumentedException("Transaction failed", e);
87 } catch (ExecutionException e) {
88 dataFactory.setFailureStatus();
89 LOG.warn("Transaction({}) FAILED!", txType, e);
91 final Throwable cause = e.getCause();
92 if (cause instanceof DOMRpcException) {
93 dataFactory.setResult((T) new DefaultDOMRpcResult(ImmutableList.of(
94 RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", cause.getMessage()))));
95 } else if (cause instanceof DOMActionException) {
96 dataFactory.setResult((T) new SimpleDOMActionResult(ImmutableList.of(
97 RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", cause.getMessage()))));
98 } else if (cause instanceof TransactionCommitFailedException) {
99 /* If device send some error message we want this message to get to client
100 and not just to throw it away or override it with new generic message.
101 We search for NetconfDocumentedException that was send from netconfSB
102 and we create RestconfDocumentedException accordingly.
104 final List<Throwable> causalChain = Throwables.getCausalChain(cause);
105 for (Throwable error : causalChain) {
106 if (error instanceof DocumentedException) {
107 final DocumentedException.ErrorTag errorTag = ((DocumentedException) error).getErrorTag();
108 if (errorTag.equals(DocumentedException.ErrorTag.DATA_EXISTS)) {
109 LOG.trace("Operation via Restconf was not executed because data at {} already exists",
111 throw new RestconfDocumentedException(e, new RestconfError(RestconfError.ErrorType.PROTOCOL,
112 RestconfError.ErrorTag.DATA_EXISTS, "Data already exists", path));
113 } else if (errorTag.equals(DocumentedException.ErrorTag.DATA_MISSING)) {
114 LOG.trace("Operation via Restconf was not executed because data at {} does not exist",
116 throw new RestconfDocumentedException(e, new RestconfError(RestconfError.ErrorType.PROTOCOL,
117 RestconfError.ErrorTag.DATA_MISSING, "Data does not exist", path));
120 if (error instanceof NetconfDocumentedException) {
121 throw new RestconfDocumentedException(error.getMessage(),
122 RestconfError.ErrorType.valueOfCaseInsensitive(
123 ((NetconfDocumentedException) error).getErrorType().elementBody()),
124 RestconfError.ErrorTag.valueOfCaseInsensitive(
125 ((NetconfDocumentedException) error).getErrorTag().getTagValue()), e);
129 throw new RestconfDocumentedException("Transaction(" + txType + ") not committed correctly", e);
131 throw new RestconfDocumentedException("Transaction failed", e);