Deprecate old MD-SAL APIs for removal
[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 static java.util.Objects.requireNonNull;
11
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.adapter.BindingRpcFutureAware;
23 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
24 import org.opendaylight.yangtools.yang.binding.DataContainer;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27
28 @Deprecated(forRemoval = true)
29 final class LazyDOMRpcResultFuture implements CheckedFuture<DOMRpcResult, DOMRpcException>, BindingRpcFutureAware {
30
31     private final ListenableFuture<RpcResult<?>> bindingFuture;
32     private final BindingNormalizedNodeSerializer codec;
33     private volatile DOMRpcResult result;
34
35     private LazyDOMRpcResultFuture(final ListenableFuture<RpcResult<?>> delegate,
36             final BindingNormalizedNodeSerializer codec) {
37         this.bindingFuture = requireNonNull(delegate, "delegate");
38         this.codec = requireNonNull(codec, "codec");
39     }
40
41     static CheckedFuture<DOMRpcResult, DOMRpcException> create(final BindingNormalizedNodeSerializer codec,
42             final ListenableFuture<RpcResult<?>> bindingResult) {
43         return new LazyDOMRpcResultFuture(bindingResult, codec);
44     }
45
46     @Override
47     public ListenableFuture<RpcResult<?>> getBindingFuture() {
48         return bindingFuture;
49     }
50
51     @Override
52     public boolean cancel(final boolean mayInterruptIfRunning) {
53         return bindingFuture.cancel(mayInterruptIfRunning);
54     }
55
56     @Override
57     public void addListener(final Runnable listener, final Executor executor) {
58         bindingFuture.addListener(listener, executor);
59     }
60
61     @Override
62     public DOMRpcResult get() throws InterruptedException, ExecutionException {
63         if (result != null) {
64             return result;
65         }
66         return transformIfNecessary(bindingFuture.get());
67     }
68
69     @Override
70     public DOMRpcResult get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException,
71             TimeoutException {
72         if (result != null) {
73             return result;
74         }
75         return transformIfNecessary(bindingFuture.get(timeout, unit));
76     }
77
78     @Override
79     public DOMRpcResult checkedGet() {
80         try {
81             return get();
82         } catch (InterruptedException | ExecutionException e) {
83             // FIXME: Add exception mapping
84             throw Throwables.propagate(e);
85         }
86     }
87
88     @Override
89     public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException {
90         try {
91             return get(timeout, unit);
92         } catch (InterruptedException | ExecutionException e) {
93             // FIXME: Add exception mapping
94             throw Throwables.propagate(e);
95         }
96     }
97
98     @Override
99     public boolean isCancelled() {
100         return bindingFuture.isCancelled();
101     }
102
103     @Override
104     public boolean isDone() {
105         return bindingFuture.isDone();
106     }
107
108     private synchronized DOMRpcResult transformIfNecessary(final RpcResult<?> input) {
109         if (result == null) {
110             result = transform(input);
111         }
112         return result;
113     }
114
115     private DOMRpcResult transform(final RpcResult<?> input) {
116         if (input.isSuccessful()) {
117             final Object inputData = input.getResult();
118             if (inputData instanceof DataContainer) {
119                 return new DefaultDOMRpcResult(codec.toNormalizedNodeRpcData((DataContainer) inputData));
120             } else {
121                 return new DefaultDOMRpcResult((NormalizedNode<?, ?>) null);
122             }
123         }
124         return new DefaultDOMRpcResult(input.getErrors());
125     }
126
127 }