Introduced Lazy DOM Rpc Result future.
[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 terms of the Eclipse
5  * Public License v1.0 which accompanies this distribution, and is available at
6  * 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.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
22 import org.opendaylight.yangtools.yang.binding.DataContainer;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25
26 final class LazyDOMRpcResultFuture implements CheckedFuture<DOMRpcResult, DOMRpcException> {
27
28     private final ListenableFuture<RpcResult<?>> bindingFuture;
29     private final BindingNormalizedNodeCodecRegistry codec;
30     private volatile DOMRpcResult result;
31
32     private LazyDOMRpcResultFuture(final ListenableFuture<RpcResult<?>> delegate,
33             final BindingNormalizedNodeCodecRegistry codec) {
34         this.bindingFuture = Preconditions.checkNotNull(delegate, "delegate");
35         this.codec = Preconditions.checkNotNull(codec, "codec");
36     }
37
38     static CheckedFuture<DOMRpcResult, DOMRpcException> create(final BindingNormalizedNodeCodecRegistry codec,
39             final ListenableFuture<RpcResult<?>> bindingResult) {
40         return new LazyDOMRpcResultFuture(bindingResult, codec);
41     }
42
43     ListenableFuture<RpcResult<?>> getBindingFuture() {
44         return bindingFuture;
45     }
46
47     @Override
48     public boolean cancel(final boolean mayInterruptIfRunning) {
49         return bindingFuture.cancel(mayInterruptIfRunning);
50     }
51
52     @Override
53     public void addListener(final Runnable listener, final Executor executor) {
54         bindingFuture.addListener(listener, executor);
55     }
56
57     @Override
58     public DOMRpcResult get() throws InterruptedException, ExecutionException {
59         if (result != null) {
60             return result;
61         }
62         return transformIfNecessary(bindingFuture.get());
63     }
64
65     @Override
66     public DOMRpcResult get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException,
67             TimeoutException {
68         if (result != null) {
69             return result;
70         }
71         return transformIfNecessary(bindingFuture.get(timeout, unit));
72     }
73
74     @Override
75     public DOMRpcResult checkedGet() throws DOMRpcException {
76         try {
77             return get();
78         } catch (InterruptedException | ExecutionException e) {
79             // FIXME: Add exception mapping
80             throw Throwables.propagate(e);
81         }
82     }
83
84     @Override
85     public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, DOMRpcException {
86         try {
87             return get(timeout, unit);
88         } catch (InterruptedException | ExecutionException e) {
89             // FIXME: Add exception mapping
90             throw Throwables.propagate(e);
91         }
92     }
93
94     @Override
95     public boolean isCancelled() {
96         return bindingFuture.isCancelled();
97     }
98
99     @Override
100     public boolean isDone() {
101         return bindingFuture.isDone();
102     }
103
104     private synchronized DOMRpcResult transformIfNecessary(final RpcResult<?> input) {
105         if (result == null) {
106             result = transform(input);
107         }
108         return result;
109     }
110
111     private DOMRpcResult transform(final RpcResult<?> input) {
112         if (input.isSuccessful()) {
113             final Object inputData = input.getResult();
114             if (inputData instanceof DataContainer) {
115                 return new DefaultDOMRpcResult(codec.toNormalizedNodeRpcData((DataContainer) inputData));
116             } else {
117                 return new DefaultDOMRpcResult((NormalizedNode<?, ?>) null);
118             }
119         }
120         return new DefaultDOMRpcResult(input.getErrors());
121     }
122
123 }