2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.mdsal.binding.dom.adapter;
11 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
13 import org.opendaylight.mdsal.dom.api.DOMRpcException;
14 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
15 import com.google.common.base.Preconditions;
16 import com.google.common.base.Throwables;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Executor;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.TimeoutException;
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;
28 final class LazyDOMRpcResultFuture implements CheckedFuture<DOMRpcResult, DOMRpcException> {
30 private final ListenableFuture<RpcResult<?>> bindingFuture;
31 private final BindingNormalizedNodeCodecRegistry codec;
32 private volatile DOMRpcResult result;
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");
40 static CheckedFuture<DOMRpcResult, DOMRpcException> create(final BindingNormalizedNodeCodecRegistry codec,
41 final ListenableFuture<RpcResult<?>> bindingResult) {
42 return new LazyDOMRpcResultFuture(bindingResult, codec);
45 ListenableFuture<RpcResult<?>> getBindingFuture() {
50 public boolean cancel(final boolean mayInterruptIfRunning) {
51 return bindingFuture.cancel(mayInterruptIfRunning);
55 public void addListener(final Runnable listener, final Executor executor) {
56 bindingFuture.addListener(listener, executor);
60 public DOMRpcResult get() throws InterruptedException, ExecutionException {
64 return transformIfNecessary(bindingFuture.get());
68 public DOMRpcResult get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException,
73 return transformIfNecessary(bindingFuture.get(timeout, unit));
77 public DOMRpcResult checkedGet() throws DOMRpcException {
80 } catch (InterruptedException | ExecutionException e) {
81 // FIXME: Add exception mapping
82 throw Throwables.propagate(e);
87 public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, DOMRpcException {
89 return get(timeout, unit);
90 } catch (InterruptedException | ExecutionException e) {
91 // FIXME: Add exception mapping
92 throw Throwables.propagate(e);
97 public boolean isCancelled() {
98 return bindingFuture.isCancelled();
102 public boolean isDone() {
103 return bindingFuture.isDone();
106 private synchronized DOMRpcResult transformIfNecessary(final RpcResult<?> input) {
107 if (result == null) {
108 result = transform(input);
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));
119 return new DefaultDOMRpcResult((NormalizedNode<?, ?>) null);
122 return new DefaultDOMRpcResult(input.getErrors());