Remove unused exceptions
[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 package org.opendaylight.controller.md.sal.binding.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Throwables;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Executor;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
20 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
21 import org.opendaylight.mdsal.binding.dom.adapter.BindingRpcFutureAware;
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>, BindingRpcFutureAware {
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     @Override
45     public ListenableFuture<RpcResult<?>> getBindingFuture() {
46         return bindingFuture;
47     }
48
49     @Override
50     public boolean cancel(final boolean mayInterruptIfRunning) {
51         return bindingFuture.cancel(mayInterruptIfRunning);
52     }
53
54     @Override
55     public void addListener(final Runnable listener, final Executor executor) {
56         bindingFuture.addListener(listener, executor);
57     }
58
59     @Override
60     public DOMRpcResult get() throws InterruptedException, ExecutionException {
61         if (result != null) {
62             return result;
63         }
64         return transformIfNecessary(bindingFuture.get());
65     }
66
67     @Override
68     public DOMRpcResult get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException,
69             TimeoutException {
70         if (result != null) {
71             return result;
72         }
73         return transformIfNecessary(bindingFuture.get(timeout, unit));
74     }
75
76     @Override
77     public DOMRpcResult checkedGet() {
78         try {
79             return get();
80         } catch (InterruptedException | ExecutionException e) {
81             // FIXME: Add exception mapping
82             throw Throwables.propagate(e);
83         }
84     }
85
86     @Override
87     public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException {
88         try {
89             return get(timeout, unit);
90         } catch (InterruptedException | ExecutionException e) {
91             // FIXME: Add exception mapping
92             throw Throwables.propagate(e);
93         }
94     }
95
96     @Override
97     public boolean isCancelled() {
98         return bindingFuture.isCancelled();
99     }
100
101     @Override
102     public boolean isDone() {
103         return bindingFuture.isDone();
104     }
105
106     private synchronized DOMRpcResult transformIfNecessary(final RpcResult<?> input) {
107         if (result == null) {
108             result = transform(input);
109         }
110         return result;
111     }
112
113     private DOMRpcResult transform(final RpcResult<?> input) {
114         if (input.isSuccessful()) {
115             final Object inputData = input.getResult();
116             if (inputData instanceof DataContainer) {
117                 return new DefaultDOMRpcResult(codec.toNormalizedNodeRpcData((DataContainer) inputData));
118             } else {
119                 return new DefaultDOMRpcResult((NormalizedNode<?, ?>) null);
120             }
121         }
122         return new DefaultDOMRpcResult(input.getErrors());
123     }
124
125 }