2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.util;
19 import static com.google.common.base.Preconditions.checkArgument;
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;
32 * Trait providing logic for consuming reply's to jvpp api calls
34 public interface JvppReplyConsumer {
36 int DEFAULT_TIMEOUT_IN_SECONDS = 5;
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);
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 {
49 return getReply(future, timeoutInSeconds);
50 } catch (TimeoutException e) {
51 throw new WriteTimeoutException(replyType, e);
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);
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 {
66 return getReply(future, timeoutInSeconds);
67 } catch (TimeoutException e) {
68 throw new ReadTimeoutException(replyType, e);
72 default <REP extends JVppReply<?>> REP getReply(@Nonnull Future<REP> future)
73 throws TimeoutException, VppBaseCallException {
74 return getReply(future, DEFAULT_TIMEOUT_IN_SECONDS);
77 default <REP extends JVppReply<?>> REP getReply(@Nonnull Future<REP> future,
78 @Nonnegative final int timeoutInSeconds)
79 throws TimeoutException, VppBaseCallException {
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());
92 throw new IllegalStateException(e);