Unit test for RestconfInvokeOperationsUtil class
[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 final static 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     static <T, X extends Exception> void addCallback(final CheckedFuture<T, X> listenableFuture, final String txType,
48             final FutureDataFactory<T> dataFactory) {
49         final CountDownLatch responseWaiter = new CountDownLatch(1);
50         Futures.addCallback(listenableFuture, new FutureCallback<T>() {
51
52             @Override
53             public void onFailure(final Throwable t) {
54                 responseWaiter.countDown();
55                 handlingLoggerAndValues(t, txType, null, dataFactory);
56             }
57
58             @Override
59             public void onSuccess(final T result) {
60                 handlingLoggerAndValues(null, txType, result, dataFactory);
61                 responseWaiter.countDown();
62             }
63
64         });
65         try {
66             responseWaiter.await();
67         } catch (final Exception e) {
68             final String msg = "Problem while waiting for response";
69             LOG.warn(msg);
70             throw new RestconfDocumentedException(msg, e);
71         }
72     }
73
74     /**
75      * Handling logger and result of callback - on success or on failure
76      * <ul>
77      * <li>on success - set result to the factory
78      * <li>on failure - throw exception
79      * </ul>
80      *
81      * @param t
82      *            - exception - if callback is onFailure
83      * @param txType
84      *            - type of operation (READ, POST, PUT, DELETE)
85      * @param result
86      *            - result of future - if callback is on Success
87      * @param dataFactory
88      *            - setter for result - in callback is onSuccess
89      */
90     protected static <T> void handlingLoggerAndValues(@Nullable final Throwable t, final String txType,
91             final T result, final FutureDataFactory<T> dataFactory) {
92         if (t != null) {
93             LOG.warn("Transaction({}) FAILED!", txType, t);
94             if (t instanceof DOMRpcException) {
95                 final List<RpcError> rpcErrorList = new ArrayList<>();
96                 rpcErrorList.add(RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", t.getMessage()));
97                 dataFactory.setResult((T) new DefaultDOMRpcResult(rpcErrorList));
98             } else {
99                 throw new RestconfDocumentedException("  Transaction(" + txType + ") not committed correctly", t);
100             }
101         } else {
102             LOG.trace("Transaction({}) SUCCESSFUL!", txType);
103             dataFactory.setResult(result);
104         }
105     }
106 }