From 6e3de9c3e99fdf284e63f303d3a32bd4dfbd124e Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Mon, 27 Jun 2016 10:16:31 +0200 Subject: HONEYCOMB-100: introduce default 5s timeout in TranslateUtils.getReplyFor* calls Change-Id: Iac2bb428ea6adcc8d3da2238db1dec708df550f0 Signed-off-by: Marek Gradzki --- .../v3po/util/AbstractInterfaceTypeCustomizer.java | 4 +- .../v3po/translate/v3po/util/TranslateUtils.java | 56 +++++++++++++++------- .../translate/v3po/util/WriteTimeoutException.java | 33 +++++++++++++ 3 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/WriteTimeoutException.java (limited to 'v3po/vpp-translate-utils/src/main/java') diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java index e1a5bf2bc..fd08472d7 100644 --- a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/AbstractInterfaceTypeCustomizer.java @@ -65,14 +65,14 @@ public abstract class AbstractInterfaceTypeCustomizer */ @Override public final void writeCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final D dataAfter, - @Nonnull final WriteContext writeContext) throws WriteFailedException.CreateFailedException { + @Nonnull final WriteContext writeContext) throws WriteFailedException { checkProperInterfaceType(writeContext, id); writeInterface(id, dataAfter, writeContext); } protected abstract void writeInterface(final InstanceIdentifier id, final D dataAfter, final WriteContext writeContext) - throws WriteFailedException.CreateFailedException; + throws WriteFailedException; // Validation for update and delete is not necessary diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java index d44bf8fd5..1a6a50133 100644 --- a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/TranslateUtils.java @@ -39,30 +39,53 @@ import org.openvpp.jvpp.dto.JVppReply; public final class TranslateUtils { public static final Splitter COLON_SPLITTER = Splitter.on(':'); + public static final int DEFAULT_TIMEOUT_IN_SECONDS = 5; private TranslateUtils() { } - public static > REP getReply(Future future) throws VppBaseCallException { + public static > REP getReplyForWrite(@Nonnull Future future, + @Nonnull final InstanceIdentifier replyType) + throws VppBaseCallException, WriteTimeoutException { + return getReplyForWrite(future, replyType, DEFAULT_TIMEOUT_IN_SECONDS); + } + + public static > REP getReplyForWrite(@Nonnull Future future, + @Nonnull final InstanceIdentifier replyType, + @Nonnegative final int timeoutInSeconds) + throws VppBaseCallException, WriteTimeoutException { try { - return future.get(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new IllegalStateException("Interrupted", e); - } catch (ExecutionException e) { - // Execution exception could generally contains any exception - // when using exceptions instead of return codes just rethrow it for processing on corresponding place - if (e instanceof ExecutionException && (e.getCause() instanceof VppBaseCallException)) { - throw (VppBaseCallException) (e.getCause()); - } - throw new IllegalStateException(e); + return getReply(future, timeoutInSeconds); + } catch (TimeoutException e) { + throw new WriteTimeoutException(replyType, e); } } + public static > REP getReplyForRead(@Nonnull Future future, + @Nonnull final InstanceIdentifier replyType) + throws VppBaseCallException, ReadTimeoutException { + return getReplyForRead(future, replyType, DEFAULT_TIMEOUT_IN_SECONDS); + } + + public static > REP getReplyForRead(@Nonnull Future future, + @Nonnull final InstanceIdentifier replyType, + @Nonnegative final int timeoutInSeconds) + throws VppBaseCallException, ReadTimeoutException { + try { + return getReply(future, timeoutInSeconds); + } catch (TimeoutException e) { + throw new ReadTimeoutException(replyType, e); + } + } + + public static > REP getReply(@Nonnull Future future) + throws TimeoutException, VppBaseCallException { + return getReply(future, DEFAULT_TIMEOUT_IN_SECONDS); + } + public static > REP getReply(@Nonnull Future future, - @Nonnull final InstanceIdentifier replyType, @Nonnegative final int timeoutInSeconds) - throws ReadTimeoutException, VppBaseCallException { + throws TimeoutException, VppBaseCallException { try { checkArgument(timeoutInSeconds > 0, "Timeout cannot be < 0"); return future.get(timeoutInSeconds, TimeUnit.SECONDS); @@ -72,12 +95,10 @@ public final class TranslateUtils { } catch (ExecutionException e) { // Execution exception could generally contains any exception // when using exceptions instead of return codes just rethrow it for processing on corresponding place - if (e.getCause() instanceof VppBaseCallException) { + if (e instanceof ExecutionException && (e.getCause() instanceof VppBaseCallException)) { throw (VppBaseCallException) (e.getCause()); } throw new IllegalStateException(e); - } catch (TimeoutException e) { - throw new ReadTimeoutException(replyType, e); } } @@ -188,6 +209,7 @@ public final class TranslateUtils { /** * Reverses bytes in the byte array + * * @param bytes input array * @return reversed array */ diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/WriteTimeoutException.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/WriteTimeoutException.java new file mode 100644 index 000000000..9516459b2 --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/WriteTimeoutException.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.v3po.translate.v3po.util; + +import com.google.common.annotations.Beta; +import io.fd.honeycomb.v3po.translate.write.WriteFailedException; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * Thrown when write method invocation times out. + */ +@Beta +public class WriteTimeoutException extends WriteFailedException { + + public WriteTimeoutException(final InstanceIdentifier id, final Throwable cause) { + super(id, cause); + } + +} -- cgit 1.2.3-korg