Add MXBean to report shard registered DTCL/DCL info
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / LazyDOMRpcResultFuture.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.controller.md.sal.binding.impl;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Throwables;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Executor;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
21 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
22 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
23 import org.opendaylight.yangtools.yang.binding.DataContainer;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26
27 final class LazyDOMRpcResultFuture implements CheckedFuture<DOMRpcResult, DOMRpcException> {
28
29     private final ListenableFuture<RpcResult<?>> bindingFuture;
30     private final BindingNormalizedNodeSerializer codec;
31     private volatile DOMRpcResult result;
32
33     private LazyDOMRpcResultFuture(final ListenableFuture<RpcResult<?>> delegate,
34             final BindingNormalizedNodeSerializer codec) {
35         this.bindingFuture = Preconditions.checkNotNull(delegate, "delegate");
36         this.codec = Preconditions.checkNotNull(codec, "codec");
37     }
38
39     static CheckedFuture<DOMRpcResult, DOMRpcException> create(final BindingNormalizedNodeSerializer codec,
40             final ListenableFuture<RpcResult<?>> bindingResult) {
41         return new LazyDOMRpcResultFuture(bindingResult, codec);
42     }
43
44     ListenableFuture<RpcResult<?>> getBindingFuture() {
45         return bindingFuture;
46     }
47
48     @Override
49     public boolean cancel(final boolean mayInterruptIfRunning) {
50         return bindingFuture.cancel(mayInterruptIfRunning);
51     }
52
53     @Override
54     public void addListener(final Runnable listener, final Executor executor) {
55         bindingFuture.addListener(listener, executor);
56     }
57
58     @Override
59     public DOMRpcResult get() throws InterruptedException, ExecutionException {
60         if (result != null) {
61             return result;
62         }
63         return transformIfNecessary(bindingFuture.get());
64     }
65
66     @Override
67     public DOMRpcResult get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException,
68             TimeoutException {
69         if (result != null) {
70             return result;
71         }
72         return transformIfNecessary(bindingFuture.get(timeout, unit));
73     }
74
75     @Override
76     public DOMRpcResult checkedGet() throws DOMRpcException {
77         try {
78             return get();
79         } catch (InterruptedException | ExecutionException e) {
80             // FIXME: Add exception mapping
81             throw Throwables.propagate(e);
82         }
83     }
84
85     @Override
86     public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, DOMRpcException {
87         try {
88             return get(timeout, unit);
89         } catch (InterruptedException | ExecutionException e) {
90             // FIXME: Add exception mapping
91             throw Throwables.propagate(e);
92         }
93     }
94
95     @Override
96     public boolean isCancelled() {
97         return bindingFuture.isCancelled();
98     }
99
100     @Override
101     public boolean isDone() {
102         return bindingFuture.isDone();
103     }
104
105     private synchronized DOMRpcResult transformIfNecessary(final RpcResult<?> input) {
106         if (result == null) {
107             result = transform(input);
108         }
109         return result;
110     }
111
112     private DOMRpcResult transform(final RpcResult<?> input) {
113         if (input.isSuccessful()) {
114             final Object inputData = input.getResult();
115             if (inputData instanceof DataContainer) {
116                 return new DefaultDOMRpcResult(codec.toNormalizedNodeRpcData((DataContainer) inputData));
117             } else {
118                 return new DefaultDOMRpcResult((NormalizedNode<?, ?>) null);
119             }
120         }
121         return new DefaultDOMRpcResult(input.getErrors());
122     }
123
124 }