a4dbe6fa23d420012a563578d66953801abf06df
[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.concurrent.CountDownLatch;
14 import javax.annotation.Nullable;
15 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * Add callback for future objects and result set to the data factory.
21  *
22  */
23 final class FutureCallbackTx {
24
25     private final static Logger LOG = LoggerFactory.getLogger(FutureCallbackTx.class);
26
27     private FutureCallbackTx() {
28         throw new UnsupportedOperationException("Util class");
29     }
30
31     /**
32      * Add callback to the future object
33      *
34      * @param listenableFuture
35      *            - future object
36      * @param txType
37      *            - type of operation (READ, POST, PUT, DELETE)
38      * @param dataFactory
39      *            - factory setting result
40      */
41     static <T, X extends Exception> void addCallback(final CheckedFuture<T, X> listenableFuture, final String txType,
42             final FutureDataFactory<T> dataFactory) {
43         final CountDownLatch responseWaiter = new CountDownLatch(1);
44         Futures.addCallback(listenableFuture, new FutureCallback<T>() {
45
46             @Override
47             public void onFailure(final Throwable t) {
48                 responseWaiter.countDown();
49                 handlingLoggerAndValues(t, txType, null, null);
50             }
51
52             @Override
53             public void onSuccess(final T result) {
54                 handlingLoggerAndValues(null, txType, result, dataFactory);
55                 responseWaiter.countDown();
56             }
57
58         });
59         try {
60             responseWaiter.await();
61         } catch (final Exception e) {
62             final String msg = "Problem while waiting for response";
63             LOG.warn(msg);
64             throw new RestconfDocumentedException(msg, e);
65         }
66     }
67
68     /**
69      * Handling logger and result of callback - on success or on failure
70      * <ul>
71      * <li>on success - set result to the factory
72      * <li>on failure - throw exception
73      * </ul>
74      *
75      * @param t
76      *            - exception - if callback is onFailure
77      * @param txType
78      *            - type of operation (READ, POST, PUT, DELETE)
79      * @param result
80      *            - result of future - if callback is on Success
81      * @param dataFactory
82      *            - setter for result - in callback is onSuccess
83      */
84     protected static <T> void handlingLoggerAndValues(@Nullable final Throwable t, final String txType,
85             final T result, final FutureDataFactory<T> dataFactory) {
86         if (t != null) {
87             LOG.warn("Transaction({}) FAILED!", txType, t);
88             throw new RestconfDocumentedException("  Transaction(" + txType + ") not committed correctly", t);
89         } else {
90             LOG.trace("Transaction({}) SUCCESSFUL!", txType);
91             dataFactory.setResult(result);
92         }
93     }
94 }