Deprecate all MD-SAL APIs
[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.ListenableFuture;
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 @Deprecated
29 public class LegacyDOMRpcResultFutureAdapter extends AbstractDOMRpcResultFutureAdapter<DOMRpcResult,
30         org.opendaylight.mdsal.dom.api.DOMRpcResult, ListenableFuture<org.opendaylight.mdsal.dom.api.DOMRpcResult>,
31         DOMRpcException> implements CheckedFuture<DOMRpcResult, DOMRpcException> {
32
33     private static final ExceptionMapper<DOMRpcException> LEGACY_DOM_RPC_EX_MAPPER =
34             new ExceptionMapper<DOMRpcException>("rpc", DOMRpcException.class) {
35         @Override
36         protected DOMRpcException newWithCause(String message, Throwable cause) {
37             return cause instanceof DOMRpcException ? (DOMRpcException)cause
38                 : cause instanceof org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException
39                     ? new DOMRpcImplementationNotAvailableException(cause.getMessage(), cause.getCause())
40                         : new DefaultDOMRpcException("RPC failed", cause);
41         }
42     };
43
44     public LegacyDOMRpcResultFutureAdapter(ListenableFuture<org.opendaylight.mdsal.dom.api.DOMRpcResult> delegate) {
45         super(delegate, LEGACY_DOM_RPC_EX_MAPPER);
46     }
47
48     @Override
49     @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
50     public DOMRpcResult checkedGet() throws DOMRpcException {
51         try {
52             return get();
53         } catch (InterruptedException | ExecutionException e) {
54             throw LEGACY_DOM_RPC_EX_MAPPER.apply(e);
55         }
56     }
57
58     @Override
59     @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
60     public DOMRpcResult checkedGet(final long timeout, final TimeUnit unit) throws TimeoutException, DOMRpcException {
61         try {
62             return get(timeout, unit);
63         } catch (InterruptedException | ExecutionException e) {
64             throw LEGACY_DOM_RPC_EX_MAPPER.apply(e);
65         }
66     }
67
68     @Override
69     protected DOMRpcResult transform(org.opendaylight.mdsal.dom.api.DOMRpcResult fromResult) {
70         return new DefaultDOMRpcResult(fromResult.getResult(), fromResult.getErrors());
71     }
72 }