496d1c859b5174d80cf2e72964fb0d347661ac0d
[netconf.git] / restconf / sal-rest-connector / 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 com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.concurrent.CountDownLatch;
16 import javax.annotation.Nullable;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
18 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
19 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
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      */
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) {
50         final CountDownLatch responseWaiter = new CountDownLatch(1);
51         Futures.addCallback(listenableFuture, new FutureCallback<T>() {
52
53             @Override
54             public void onFailure(final Throwable throwable) {
55                 responseWaiter.countDown();
56                 handlingLoggerAndValues(throwable, txType, null, dataFactory);
57             }
58
59             @Override
60             public void onSuccess(final T result) {
61                 handlingLoggerAndValues(null, txType, result, dataFactory);
62                 responseWaiter.countDown();
63             }
64
65         });
66         try {
67             responseWaiter.await();
68         } catch (final Exception e) {
69             final String msg = "Problem while waiting for response";
70             LOG.warn(msg);
71             throw new RestconfDocumentedException(msg, e);
72         }
73     }
74
75     /**
76      * Handling logger and result of callback - on success or on failure.
77      * <ul>
78      * <li>on success - set result to the factory
79      * <li>on failure - throw exception
80      * </ul>
81      *
82      * @param throwable
83      *             exception - if callback is onFailure
84      * @param txType
85      *             type of operation (READ, POST, PUT, DELETE)
86      * @param result
87      *             result of future - if callback is on Success
88      * @param dataFactory
89      *             setter for result - in callback is onSuccess
90      */
91     protected static <T> void handlingLoggerAndValues(@Nullable final Throwable throwable, final String txType,
92             final T result, final FutureDataFactory<T> dataFactory) {
93         if (throwable != null) {
94             dataFactory.setFailureStatus();
95             LOG.warn("Transaction({}) FAILED!", txType, throwable);
96             if (throwable instanceof DOMRpcException) {
97                 final List<RpcError> rpcErrorList = new ArrayList<>();
98                 rpcErrorList.add(
99                         RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", throwable.getMessage()));
100                 dataFactory.setResult((T) new DefaultDOMRpcResult(rpcErrorList));
101             } else {
102                 throw new RestconfDocumentedException(
103                         "Transaction(" + txType + ") not committed correctly", throwable);
104             }
105         } else {
106             LOG.trace("Transaction({}) SUCCESSFUL!", txType);
107             dataFactory.setResult(result);
108         }
109     }
110 }