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
8 package org.opendaylight.controller.md.sal.binding.impl;
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;
28 final class LazyDOMRpcResultFuture implements CheckedFuture<DOMRpcResult, DOMRpcException>, BindingRpcFutureAware {
30 private final ListenableFuture<RpcResult<?>> bindingFuture;
31 private final BindingNormalizedNodeSerializer codec;
32 private volatile DOMRpcResult result;
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");
40 static CheckedFuture<DOMRpcResult, DOMRpcException> create(final BindingNormalizedNodeSerializer codec,
41 final ListenableFuture<RpcResult<?>> bindingResult) {
42 return new LazyDOMRpcResultFuture(bindingResult, codec);
46 public ListenableFuture<RpcResult<?>> getBindingFuture() {
51 public boolean cancel(final boolean mayInterruptIfRunning) {
52 return bindingFuture.cancel(mayInterruptIfRunning);
56 public void addListener(final Runnable listener, final Executor executor) {
57 bindingFuture.addListener(listener, executor);
61 public DOMRpcResult get() throws InterruptedException, ExecutionException {
65 return transformIfNecessary(bindingFuture.get());
69 public DOMRpcResult get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException,
74 return transformIfNecessary(bindingFuture.get(timeout, unit));
78 public DOMRpcResult checkedGet() {
81 } catch (InterruptedException | ExecutionException e) {
82 // FIXME: Add exception mapping
83 throw Throwables.propagate(e);
88 public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException {
90 return get(timeout, unit);
91 } catch (InterruptedException | ExecutionException e) {
92 // FIXME: Add exception mapping
93 throw Throwables.propagate(e);
98 public boolean isCancelled() {
99 return bindingFuture.isCancelled();
103 public boolean isDone() {
104 return bindingFuture.isDone();
107 private synchronized DOMRpcResult transformIfNecessary(final RpcResult<?> input) {
108 if (result == null) {
109 result = transform(input);
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));
120 return new DefaultDOMRpcResult((NormalizedNode<?, ?>) null);
123 return new DefaultDOMRpcResult(input.getErrors());