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