Exclude test utilities from sonar coverage statistics
[honeycomb.git] / vpp-common / vpp-translate-utils / src / main / java / io / fd / honeycomb / translate / v3po / util / JvppReplyConsumer.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.translate.v3po.util;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.Future;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.TimeoutException;
25 import javax.annotation.Nonnegative;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.openvpp.jvpp.VppBaseCallException;
29 import org.openvpp.jvpp.dto.JVppReply;
30
31 /**
32  * Trait providing logic for consuming reply's to jvpp api calls
33  */
34 public interface JvppReplyConsumer {
35
36     int DEFAULT_TIMEOUT_IN_SECONDS = 5;
37
38     default <REP extends JVppReply<?>> REP getReplyForWrite(@Nonnull Future<REP> future,
39                                                             @Nonnull final InstanceIdentifier<?> replyType)
40             throws VppBaseCallException, WriteTimeoutException {
41         return getReplyForWrite(future, replyType, DEFAULT_TIMEOUT_IN_SECONDS);
42     }
43
44     default <REP extends JVppReply<?>> REP getReplyForWrite(@Nonnull Future<REP> future,
45                                                             @Nonnull final InstanceIdentifier<?> replyType,
46                                                             @Nonnegative final int timeoutInSeconds)
47             throws VppBaseCallException, WriteTimeoutException {
48         try {
49             return getReply(future, timeoutInSeconds);
50         } catch (TimeoutException e) {
51             throw new WriteTimeoutException(replyType, e);
52         }
53     }
54
55     default <REP extends JVppReply<?>> REP getReplyForRead(@Nonnull Future<REP> future,
56                                                            @Nonnull final InstanceIdentifier<?> replyType)
57             throws VppBaseCallException, ReadTimeoutException {
58         return getReplyForRead(future, replyType, DEFAULT_TIMEOUT_IN_SECONDS);
59     }
60
61     default <REP extends JVppReply<?>> REP getReplyForRead(@Nonnull Future<REP> future,
62                                                            @Nonnull final InstanceIdentifier<?> replyType,
63                                                            @Nonnegative final int timeoutInSeconds)
64             throws VppBaseCallException, ReadTimeoutException {
65         try {
66             return getReply(future, timeoutInSeconds);
67         } catch (TimeoutException e) {
68             throw new ReadTimeoutException(replyType, e);
69         }
70     }
71
72     default <REP extends JVppReply<?>> REP getReply(@Nonnull Future<REP> future)
73             throws TimeoutException, VppBaseCallException {
74         return getReply(future, DEFAULT_TIMEOUT_IN_SECONDS);
75     }
76
77     default <REP extends JVppReply<?>> REP getReply(@Nonnull Future<REP> future,
78                                                     @Nonnegative final int timeoutInSeconds)
79             throws TimeoutException, VppBaseCallException {
80         try {
81             checkArgument(timeoutInSeconds > 0, "Timeout cannot be < 0");
82             return future.get(timeoutInSeconds, TimeUnit.SECONDS);
83         } catch (InterruptedException e) {
84             Thread.currentThread().interrupt();
85             throw new IllegalStateException("Interrupted", e);
86         } catch (ExecutionException e) {
87             // Execution exception could generally contains any exception
88             // when using exceptions instead of return codes just rethrow it for processing on corresponding place
89             if (e instanceof ExecutionException && (e.getCause() instanceof VppBaseCallException)) {
90                 throw (VppBaseCallException) (e.getCause());
91             }
92             throw new IllegalStateException(e);
93         }
94     }
95 }