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.controller.md.sal.binding.impl;
11 import com.google.common.base.Preconditions;
12 import com.google.common.base.Throwables;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Executor;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
21 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
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;
27 final class LazyDOMRpcResultFuture implements CheckedFuture<DOMRpcResult, DOMRpcException> {
29 private final ListenableFuture<RpcResult<?>> bindingFuture;
30 private final BindingNormalizedNodeSerializer codec;
31 private volatile DOMRpcResult result;
33 private LazyDOMRpcResultFuture(final ListenableFuture<RpcResult<?>> delegate,
34 final BindingNormalizedNodeSerializer codec) {
35 this.bindingFuture = Preconditions.checkNotNull(delegate, "delegate");
36 this.codec = Preconditions.checkNotNull(codec, "codec");
39 static CheckedFuture<DOMRpcResult, DOMRpcException> create(final BindingNormalizedNodeSerializer codec,
40 final ListenableFuture<RpcResult<?>> bindingResult) {
41 return new LazyDOMRpcResultFuture(bindingResult, codec);
44 ListenableFuture<RpcResult<?>> getBindingFuture() {
49 public boolean cancel(final boolean mayInterruptIfRunning) {
50 return bindingFuture.cancel(mayInterruptIfRunning);
54 public void addListener(final Runnable listener, final Executor executor) {
55 bindingFuture.addListener(listener, executor);
59 public DOMRpcResult get() throws InterruptedException, ExecutionException {
63 return transformIfNecessary(bindingFuture.get());
67 public DOMRpcResult get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException,
72 return transformIfNecessary(bindingFuture.get(timeout, unit));
76 public DOMRpcResult checkedGet() throws DOMRpcException {
79 } catch (InterruptedException | ExecutionException e) {
80 // FIXME: Add exception mapping
81 throw Throwables.propagate(e);
86 public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, DOMRpcException {
88 return get(timeout, unit);
89 } catch (InterruptedException | ExecutionException e) {
90 // FIXME: Add exception mapping
91 throw Throwables.propagate(e);
96 public boolean isCancelled() {
97 return bindingFuture.isCancelled();
101 public boolean isDone() {
102 return bindingFuture.isDone();
105 private synchronized DOMRpcResult transformIfNecessary(final RpcResult<?> input) {
106 if (result == null) {
107 result = transform(input);
112 private DOMRpcResult transform(final RpcResult<?> input) {
113 if (input.isSuccessful()) {
114 final Object inputData = input.getResult();
115 if (inputData instanceof DataContainer) {
116 return new DefaultDOMRpcResult(codec.toNormalizedNodeRpcData((DataContainer) inputData));
118 return new DefaultDOMRpcResult((NormalizedNode<?, ?>) null);
121 return new DefaultDOMRpcResult(input.getErrors());