10a85feba27c5cf1c2cb71eab64b97684bedbaa7
[controller.git] / opendaylight / md-sal / sal-dom-compat / src / main / java / org / opendaylight / controller / sal / core / compat / LegacyDOMRpcResultFutureAdapter.java
1 /*
2  * Copyright (c) 2018 Inocybe Technologies 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.controller.sal.core.compat;
9
10 import com.google.common.util.concurrent.CheckedFuture;
11 import com.google.common.util.concurrent.FluentFuture;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.TimeoutException;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
19 import org.opendaylight.controller.md.sal.dom.api.DefaultDOMRpcException;
20 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
21 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
22
23 /**
24  * Adapts a {@link org.opendaylight.mdsal.dom.api.DOMRpcResult} CheckedFuture to a {@link DOMRpcResult} CheckedFuture.
25  *
26  * @author Thomas Pantelis
27  */
28 public class LegacyDOMRpcResultFutureAdapter extends AbstractDOMRpcResultFutureAdapter<DOMRpcResult,
29         org.opendaylight.mdsal.dom.api.DOMRpcResult, FluentFuture<org.opendaylight.mdsal.dom.api.DOMRpcResult>,
30         DOMRpcException> implements CheckedFuture<DOMRpcResult, DOMRpcException> {
31
32     private static final ExceptionMapper<DOMRpcException> LEGACY_DOM_RPC_EX_MAPPER =
33             new ExceptionMapper<DOMRpcException>("rpc", DOMRpcException.class) {
34         @Override
35         protected DOMRpcException newWithCause(String message, Throwable cause) {
36             return cause instanceof DOMRpcException ? (DOMRpcException)cause
37                 : cause instanceof org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException
38                     ? new DOMRpcImplementationNotAvailableException(cause.getMessage(), cause.getCause())
39                         : new DefaultDOMRpcException("RPC failed", cause);
40         }
41     };
42
43     public LegacyDOMRpcResultFutureAdapter(FluentFuture<org.opendaylight.mdsal.dom.api.DOMRpcResult> delegate) {
44         super(delegate, LEGACY_DOM_RPC_EX_MAPPER);
45     }
46
47     @Override
48     @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
49     public DOMRpcResult checkedGet() throws DOMRpcException {
50         try {
51             return get();
52         } catch (InterruptedException | ExecutionException e) {
53             throw LEGACY_DOM_RPC_EX_MAPPER.apply(e);
54         }
55     }
56
57     @Override
58     @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
59     public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, DOMRpcException {
60         try {
61             return get(timeout, unit);
62         } catch (InterruptedException | ExecutionException e) {
63             throw LEGACY_DOM_RPC_EX_MAPPER.apply(e);
64         }
65     }
66
67     @Override
68     protected DOMRpcResult transform(org.opendaylight.mdsal.dom.api.DOMRpcResult fromResult) {
69         return new DefaultDOMRpcResult(fromResult.getResult(), fromResult.getErrors());
70     }
71 }