Revert "Update RPC invocation to take ContainerNode"
[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 package org.opendaylight.mdsal.binding.dom.adapter;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.AbstractFuture;
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.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
20 import org.opendaylight.mdsal.dom.api.DOMRpcException;
21 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
22 import org.opendaylight.mdsal.dom.api.DefaultDOMRpcException;
23 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
24 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
25 import org.opendaylight.yangtools.yang.binding.DataContainer;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28
29 final class LazyDOMRpcResultFuture extends AbstractFuture<DOMRpcResult> implements BindingRpcFutureAware {
30     private static final ExceptionMapper<DOMRpcException> DOM_RPC_EX_MAPPER = new ExceptionMapper<>("rpc",
31             DOMRpcException.class) {
32         @Override
33         protected DOMRpcException newWithCause(final String message, final Throwable cause) {
34             return cause instanceof DOMRpcException ? (DOMRpcException)cause
35                     : new DefaultDOMRpcException("RPC failed", cause);
36         }
37     };
38
39     private final ListenableFuture<RpcResult<?>> bindingFuture;
40     private final BindingNormalizedNodeSerializer codec;
41     private volatile DOMRpcResult result;
42
43     private LazyDOMRpcResultFuture(final ListenableFuture<RpcResult<?>> delegate,
44             final BindingNormalizedNodeSerializer codec) {
45         this.bindingFuture = requireNonNull(delegate, "delegate");
46         this.codec = requireNonNull(codec, "codec");
47     }
48
49     static @NonNull LazyDOMRpcResultFuture create(final BindingNormalizedNodeSerializer codec,
50             final ListenableFuture<RpcResult<?>> bindingResult) {
51         return new LazyDOMRpcResultFuture(bindingResult, codec);
52     }
53
54     @Override
55     public ListenableFuture<RpcResult<?>> getBindingFuture() {
56         return bindingFuture;
57     }
58
59     @Override
60     public boolean cancel(final boolean mayInterruptIfRunning) {
61         return bindingFuture.cancel(mayInterruptIfRunning);
62     }
63
64     @Override
65     public void addListener(final Runnable listener, final Executor executor) {
66         bindingFuture.addListener(listener, executor);
67     }
68
69     @Override
70     public DOMRpcResult get() throws InterruptedException, ExecutionException {
71         if (result != null) {
72             return result;
73         }
74
75         try {
76             return transformIfNecessary(bindingFuture.get());
77         } catch (ExecutionException e) {
78             throw new ExecutionException(e.getMessage(), DOM_RPC_EX_MAPPER.apply(e));
79         }
80     }
81
82     @Override
83     public DOMRpcResult get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException,
84             TimeoutException {
85         if (result != null) {
86             return result;
87         }
88
89         try {
90             return transformIfNecessary(bindingFuture.get(timeout, unit));
91         } catch (ExecutionException e) {
92             throw new ExecutionException(e.getMessage(), DOM_RPC_EX_MAPPER.apply(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             }
119
120             return new DefaultDOMRpcResult((NormalizedNode<?, ?>) null);
121         }
122         return new DefaultDOMRpcResult(input.getErrors());
123     }
124 }