Renamed controller.md.sal.dom.api to mdsal.dom.api
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / 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.mdsal.binding.dom.adapter;
10
11 import org.opendaylight.mdsal.dom.api.DOMRpcException;
12 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
13
14 import com.google.common.base.Preconditions;
15 import com.google.common.base.Throwables;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.Executor;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
23 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
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 final class LazyDOMRpcResultFuture implements CheckedFuture<DOMRpcResult, DOMRpcException> {
29
30     private final ListenableFuture<RpcResult<?>> bindingFuture;
31     private final BindingNormalizedNodeCodecRegistry codec;
32     private volatile DOMRpcResult result;
33
34     private LazyDOMRpcResultFuture(final ListenableFuture<RpcResult<?>> delegate,
35             final BindingNormalizedNodeCodecRegistry codec) {
36         this.bindingFuture = Preconditions.checkNotNull(delegate, "delegate");
37         this.codec = Preconditions.checkNotNull(codec, "codec");
38     }
39
40     static CheckedFuture<DOMRpcResult, DOMRpcException> create(final BindingNormalizedNodeCodecRegistry codec,
41             final ListenableFuture<RpcResult<?>> bindingResult) {
42         return new LazyDOMRpcResultFuture(bindingResult, codec);
43     }
44
45     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() throws DOMRpcException {
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, DOMRpcException {
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 }