+++ /dev/null
-/*
- * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.yangtools.util.concurrent;
-
-import static java.util.Objects.requireNonNull;
-
-import com.google.common.util.concurrent.AbstractCheckedFuture;
-import com.google.common.util.concurrent.ListenableFuture;
-import java.util.concurrent.CancellationException;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.TimeoutException;
-import java.util.function.Function;
-import javax.annotation.Nonnull;
-
-/**
- * An implementation of CheckedFuture that provides similar behavior for the <code>get</code> methods
- * that the <code>checkedGet</code> methods provide.
- *
- * <p>For {@link CancellationException} and {@link InterruptedException}, the specified exception mapper
- * is invoked to translate them to the checked exception type.
- *
- * <p>For {@link ExecutionException}, the mapper is invoked to translate the cause to the checked exception
- * and a new ExecutionException is thrown with the translated cause.
- *
- * @author Thomas Pantelis
- *
- * @param <V> The result type returned by this Future's get method
- * @param <X> The checked exception type
- */
-public final class MappingCheckedFuture<V, X extends Exception> extends AbstractCheckedFuture<V, X> {
-
- private final Function<Exception, X> mapper;
-
- private MappingCheckedFuture(final ListenableFuture<V> delegate, final Function<Exception, X> mapper) {
- super(delegate);
- this.mapper = requireNonNull(mapper);
- }
-
- /**
- * Creates a new <code>MappingCheckedFuture</code> that wraps the given {@link ListenableFuture}
- * delegate.
- *
- * @param delegate the {@link ListenableFuture} to wrap
- * @param mapper the mapping {@link Function} used to translate exceptions from the delegate
- * @return a new <code>MappingCheckedFuture</code>
- */
- public static <V, X extends Exception> MappingCheckedFuture<V, X> create(
- final ListenableFuture<V> delegate, final Function<Exception, X> mapper) {
- return new MappingCheckedFuture<>(delegate, mapper);
- }
-
- @Override
- @SuppressWarnings("checkstyle:parameterName")
- protected X mapException(@Nonnull final Exception e) {
- return mapper.apply(e);
- }
-
- private ExecutionException wrapInExecutionException(final String message, final Exception ex) {
- return new ExecutionException(message, mapException(ex));
- }
-
- @Override
- public V get() throws InterruptedException, ExecutionException {
- try {
- return super.get();
- } catch (final InterruptedException e) {
- Thread.currentThread().interrupt();
- throw wrapInExecutionException("Operation was interrupted", e);
- } catch (final CancellationException e) {
- throw wrapInExecutionException("Operation was cancelled", e);
- } catch (final ExecutionException e) {
- throw wrapInExecutionException(e.getMessage(), e);
- }
- }
-
- @Override
- public V get(final long timeout, @Nonnull final TimeUnit unit)
- throws InterruptedException, ExecutionException, TimeoutException {
- try {
- return super.get(timeout, unit);
- } catch (final InterruptedException e) {
- Thread.currentThread().interrupt();
- throw wrapInExecutionException("Operation was interrupted", e);
- } catch (final CancellationException e) {
- throw wrapInExecutionException("Operation was cancelled", e);
- } catch (final ExecutionException e) {
- throw wrapInExecutionException(e.getMessage(), e);
- }
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.yangtools.util.concurrent;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
-
-import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.SettableFuture;
-import java.util.concurrent.CancellationException;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.TimeUnit;
-import java.util.concurrent.atomic.AtomicReference;
-import org.junit.Test;
-
-/**
- * Unit tests for MappingCheckedFuture.
- *
- * @author Thomas Pantelis
- */
-public class MappingCheckedFutureTest {
-
- interface FutureInvoker {
- void invokeGet(CheckedFuture<?,?> future) throws Exception;
-
- Throwable extractWrappedTestEx(Exception from);
- }
-
- static class TestException extends Exception {
- private static final long serialVersionUID = 1L;
-
- TestException(final String message, final Throwable cause) {
- super(message, cause);
- }
- }
-
- static final ExceptionMapper<TestException> MAPPER = new ExceptionMapper<TestException>(
- "Test", TestException.class) {
-
- @Override
- protected TestException newWithCause(final String message, final Throwable cause) {
- return new TestException(message, cause);
- }
- };
-
- static final FutureInvoker GET = new FutureInvoker() {
- @Override
- public void invokeGet(final CheckedFuture<?, ?> future) throws Exception {
- future.get();
- }
-
- @Override
- public Throwable extractWrappedTestEx(final Exception from) {
- if (from instanceof ExecutionException) {
- return from.getCause();
- }
-
- return from;
- }
- };
-
- static final FutureInvoker TIMED_GET = new FutureInvoker() {
- @Override
- public void invokeGet(final CheckedFuture<?, ?> future) throws Exception {
- future.get(1, TimeUnit.HOURS);
- }
-
- @Override
- public Throwable extractWrappedTestEx(final Exception from) {
- if (from instanceof ExecutionException) {
- return from.getCause();
- }
-
- return from;
- }
- };
-
- static final FutureInvoker CHECKED_GET = new FutureInvoker() {
- @Override
- public void invokeGet(final CheckedFuture<?,?> future) throws Exception {
- future.checkedGet();
- }
-
- @Override
- public Throwable extractWrappedTestEx(final Exception from) {
- return from;
- }
- };
-
- static final FutureInvoker TIMED_CHECKED_GET = new FutureInvoker() {
- @Override
- public void invokeGet(final CheckedFuture<?,?> future) throws Exception {
- future.checkedGet(50, TimeUnit.MILLISECONDS);
- }
-
- @Override
- public Throwable extractWrappedTestEx(final Exception from) {
- return from;
- }
- };
-
- @Test
- public void testGet() throws Exception {
- SettableFuture<String> delegate = SettableFuture.create();
- MappingCheckedFuture<String,TestException> future = MappingCheckedFuture.create(delegate, MAPPER);
- delegate.set("test");
- assertEquals("get", "test", future.get());
- }
-
- @Test
- public void testGetWithExceptions() throws Exception {
- testExecutionException(GET, new RuntimeException());
- testExecutionException(GET, new TestException("mock", null));
- testCancellationException(GET);
- testInterruptedException(GET);
- }
-
- @Test
- public void testTimedGet() throws Exception {
- SettableFuture<String> delegate = SettableFuture.create();
- MappingCheckedFuture<String,TestException> future = MappingCheckedFuture.create(delegate, MAPPER);
- delegate.set("test");
- assertEquals("get", "test", future.get(50, TimeUnit.MILLISECONDS));
- }
-
- @Test
- public void testTimedGetWithExceptions() throws Exception {
- testExecutionException(TIMED_GET, new RuntimeException());
- testCancellationException(TIMED_GET);
- testInterruptedException(TIMED_GET);
- }
-
- @Test
- public void testCheckedGetWithExceptions() throws Exception {
- testExecutionException(CHECKED_GET, new RuntimeException());
- testCancellationException(CHECKED_GET);
- testInterruptedException(CHECKED_GET);
- }
-
- @Test
- public void testTimedCheckedWithExceptions() throws Exception {
- testExecutionException(TIMED_CHECKED_GET, new RuntimeException());
- testCancellationException(TIMED_CHECKED_GET);
- testInterruptedException(TIMED_CHECKED_GET);
- }
-
- @SuppressWarnings("checkstyle:illegalCatch")
- private static void testExecutionException(final FutureInvoker invoker, final Throwable cause) {
- SettableFuture<String> delegate = SettableFuture.create();
- MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
-
- delegate.setException(cause);
-
- try {
- invoker.invokeGet(mappingFuture);
- fail("Expected exception thrown");
- } catch (Exception e) {
- Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
- assertNotNull("Expected returned exception is null", expectedTestEx);
- assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
-
- if (cause instanceof TestException) {
- assertNull("Expected null cause", expectedTestEx.getCause());
- } else {
- assertSame("TestException cause", cause, expectedTestEx.getCause());
- }
- }
- }
-
- @SuppressWarnings("checkstyle:illegalCatch")
- private static void testCancellationException(final FutureInvoker invoker) {
- SettableFuture<String> delegate = SettableFuture.create();
- MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
-
- mappingFuture.cancel(false);
-
- try {
- invoker.invokeGet(mappingFuture);
- fail("Expected exception thrown");
- } catch (Exception e) {
- Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
- assertNotNull("Expected returned exception is null", expectedTestEx);
- assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
- assertEquals("TestException cause type", CancellationException.class, expectedTestEx.getCause().getClass());
- }
- }
-
- @SuppressWarnings("checkstyle:illegalCatch")
- private static void testInterruptedException(final FutureInvoker invoker) throws Exception {
- SettableFuture<String> delegate = SettableFuture.create();
- final MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
-
- final AtomicReference<AssertionError> assertError = new AtomicReference<>();
- final CountDownLatch doneLatch = new CountDownLatch(1);
- Thread thread = new Thread() {
- @Override
- public void run() {
- try {
- doInvoke();
- } catch (AssertionError e) {
- assertError.set(e);
- } finally {
- doneLatch.countDown();
- }
- }
-
- void doInvoke() {
- try {
- invoker.invokeGet(mappingFuture);
- fail("Expected exception thrown");
- } catch (Exception e) {
- Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
- assertNotNull("Expected returned exception is null", expectedTestEx);
- assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
- assertEquals("TestException cause type", InterruptedException.class,
- expectedTestEx.getCause().getClass());
- }
- }
- };
- thread.start();
-
- thread.interrupt();
- assertTrue("get call completed", doneLatch.await(5, TimeUnit.SECONDS));
-
- if (assertError.get() != null) {
- throw assertError.get();
- }
- }
-}
package org.opendaylight.yangtools.yang.data.api.schema.xpath;
import com.google.common.annotations.Beta;
-import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.ListenableFuture;
import java.util.Optional;
import java.util.concurrent.Future;
import javax.annotation.Nonnull;
* @throws NullPointerException if any of the arguments are null
* @throws IllegalArgumentException if the path does not match the path at which this expression was compiled
*/
- CheckedFuture<Optional<? extends XPathResult<?>>, XPathExpressionException> evaluateLazily(
- @Nonnull XPathDocument document, @Nonnull YangInstanceIdentifier path);
+ ListenableFuture<Optional<? extends XPathResult<?>>> evaluateLazily(@Nonnull XPathDocument document,
+ @Nonnull YangInstanceIdentifier path);
}
package org.opendaylight.yangtools.yang.model.repo.api;
import com.google.common.annotations.Beta;
-import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.ListenableFuture;
import java.util.Collection;
import java.util.Set;
import javax.annotation.Nonnull;
* with an explanation why the creation of the schema context
* failed.
*/
- default CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(
+ default ListenableFuture<SchemaContext> createSchemaContext(
@Nonnull final Collection<SourceIdentifier> requiredSources) {
return createSchemaContext(requiredSources, StatementParserMode.DEFAULT_MODE);
}
* with an explanation why the creation of the schema context
* failed.
*/
- default CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(
+ default ListenableFuture<SchemaContext> createSchemaContext(
final Collection<SourceIdentifier> requiredSources, final StatementParserMode statementParserMode) {
return createSchemaContext(requiredSources, statementParserMode, null);
}
* with an explanation why the creation of the schema context
* failed.
*/
- default CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(
+ default ListenableFuture<SchemaContext> createSchemaContext(
@Nonnull final Collection<SourceIdentifier> requiredSources, final Set<QName> supportedFeatures) {
return createSchemaContext(requiredSources, StatementParserMode.DEFAULT_MODE, supportedFeatures);
}
* with an explanation why the creation of the schema context
* failed.
*/
- CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(
- Collection<SourceIdentifier> requiredSources, StatementParserMode statementParserMode,
- Set<QName> supportedFeatures);
+ ListenableFuture<SchemaContext> createSchemaContext(Collection<SourceIdentifier> requiredSources,
+ StatementParserMode statementParserMode, Set<QName> supportedFeatures);
}
package org.opendaylight.yangtools.yang.model.repo.api;
import com.google.common.annotations.Beta;
-import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.ListenableFuture;
import javax.annotation.Nonnull;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
*/
SchemaContextFactory createSchemaContextFactory(@Nonnull SchemaSourceFilter filter);
- <T extends SchemaSourceRepresentation> CheckedFuture<T, SchemaSourceException> getSchemaSource(
- @Nonnull SourceIdentifier id, @Nonnull Class<T> represetation);
+ <T extends SchemaSourceRepresentation> ListenableFuture<T> getSchemaSource(@Nonnull SourceIdentifier id,
+ @Nonnull Class<T> represetation);
}
package org.opendaylight.yangtools.yang.model.repo.spi;
import com.google.common.annotations.Beta;
-import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.ListenableFuture;
import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
* this different invocation of this method may produce different results.
*
* @param sourceIdentifier source identifier
- * @return source representation if supplied YANG module is available
- *
+ * @return future source representation, if supplied YANG module is available
*/
- CheckedFuture<? extends T, SchemaSourceException> getSource(SourceIdentifier sourceIdentifier);
+ ListenableFuture<? extends T> getSource(SourceIdentifier sourceIdentifier);
}
}
private void testSetOfModules(final Collection<SourceIdentifier> source) throws Exception {
- final SchemaContext schemaContext = schemaContextFactory.createSchemaContext(source).checkedGet();
+ final SchemaContext schemaContext = schemaContextFactory.createSchemaContext(source).get();
final File outDir = new File("target/collection");
outDir.mkdirs();
for (final Module module : schemaContext.getModules()) {
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
-import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.GuardedBy;
-import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
-import org.opendaylight.yangtools.util.concurrent.ReflectiveExceptionMapper;
import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
@Beta
public abstract class AbstractSchemaRepository implements SchemaRepository, SchemaSourceRegistry {
private static final Logger LOG = LoggerFactory.getLogger(AbstractSchemaRepository.class);
- private static final ExceptionMapper<SchemaSourceException> FETCH_MAPPER = ReflectiveExceptionMapper.create(
- "Schema source fetch", SchemaSourceException.class);
/*
* Source identifier -> representation -> provider map. We usually are looking for
}
@Override
- public <T extends SchemaSourceRepresentation> CheckedFuture<T, SchemaSourceException> getSchemaSource(
+ public <T extends SchemaSourceRepresentation> ListenableFuture<T> getSchemaSource(
@Nonnull final SourceIdentifier id, @Nonnull final Class<T> representation) {
final ArrayList<AbstractSchemaSourceRegistration<?>> sortedSchemaSourceRegistrations;
final ListMultimap<Class<? extends SchemaSourceRepresentation>, AbstractSchemaSourceRegistration<?>> srcs =
sources.get(id);
if (srcs == null) {
- return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException(
- "No providers registered for source" + id, id));
+ return Futures.immediateFailedFuture(new MissingSchemaSourceException(
+ "No providers registered for source" + id, id));
}
sortedSchemaSourceRegistrations = Lists.newArrayList(srcs.get(representation));
final Iterator<AbstractSchemaSourceRegistration<?>> regs = sortedSchemaSourceRegistrations.iterator();
if (!regs.hasNext()) {
- return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException(
+ return Futures.immediateFailedFuture(new MissingSchemaSourceException(
"No providers for source " + id + " representation " + representation + " available", id));
}
}
}, MoreExecutors.directExecutor());
- return Futures.makeChecked(fetchSourceFuture, FETCH_MAPPER);
+ return fetchSourceFuture;
}
private synchronized <T extends SchemaSourceRepresentation> void addSource(final PotentialSchemaSource<T> source,
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
-import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
}
@Override
- public synchronized CheckedFuture<? extends T, SchemaSourceException> getSource(
+ public synchronized ListenableFuture<? extends T> getSource(
final SourceIdentifier sourceIdentifier) {
final File file = sourceIdToFile(sourceIdentifier, storageDirectory);
if (file.exists() && file.canRead()) {
LOG.trace("Source {} found in cache as {}", sourceIdentifier, file);
final SchemaSourceRepresentation restored = STORAGE_ADAPTERS.get(representation).restore(sourceIdentifier,
file);
- return Futures.immediateCheckedFuture(representation.cast(restored));
+ return Futures.immediateFuture(representation.cast(restored));
}
LOG.debug("Source {} not found in cache as {}", sourceIdentifier, file);
- return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found",
- sourceIdentifier));
+ return Futures.immediateFailedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
}
@Override
import com.google.common.base.FinalizableReferenceQueue;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
-import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
}
@Override
- public CheckedFuture<? extends T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
+ public ListenableFuture<? extends T> getSource(final SourceIdentifier sourceIdentifier) {
final T present = cache.getIfPresent(sourceIdentifier);
if (present != null) {
- return Futures.immediateCheckedFuture(present);
+ return Futures.immediateFuture(present);
}
- return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException("Source not found",
- sourceIdentifier));
+ return Futures.immediateFailedFuture(new MissingSchemaSourceException("Source not found", sourceIdentifier));
}
@Override
import static java.util.Objects.requireNonNull;
import com.google.common.util.concurrent.AsyncFunction;
-import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;
-import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
-import org.opendaylight.yangtools.util.concurrent.ReflectiveExceptionMapper;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
public class SchemaSourceTransformer<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
implements SchemaSourceListener, SchemaSourceProvider<D> {
- private static final ExceptionMapper<SchemaSourceException> MAPPER = ReflectiveExceptionMapper.create(
- "Source transformation", SchemaSourceException.class);
@FunctionalInterface
public interface Transformation<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
extends AsyncFunction<S, D> {
@Override
- CheckedFuture<D, SchemaSourceException> apply(@Nonnull S input) throws Exception;
+ ListenableFuture<D> apply(@Nonnull S input) throws Exception;
}
private final Map<PotentialSchemaSource<?>, RefcountedRegistration> sources = new HashMap<>();
}
@Override
- public CheckedFuture<D, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
- final CheckedFuture<S, SchemaSourceException> f = provider.getSchemaSource(sourceIdentifier, srcClass);
- return Futures.makeChecked(Futures.transformAsync(f, function, MoreExecutors.directExecutor()), MAPPER);
+ public ListenableFuture<D> getSource(final SourceIdentifier sourceIdentifier) {
+ return Futures.transformAsync(provider.getSchemaSource(sourceIdentifier, srcClass), function,
+ MoreExecutors.directExecutor());
}
@Override
import com.google.common.base.MoreObjects;
import com.google.common.collect.Collections2;
import com.google.common.io.Files;
-import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.ListenableFuture;
import java.io.ByteArrayInputStream;
import java.io.File;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
final YangTextSchemaSource source = new TestingYangSource("test", "2013-12-12", content);
cache.offer(source);
final SourceIdentifier sourceIdentifier = RevisionSourceIdentifier.create("test", "2013-12-12");
- final CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException> checked = cache
- .getSource(sourceIdentifier);
+ final ListenableFuture<? extends YangTextSchemaSource> checked = cache.getSource(sourceIdentifier);
Assert.assertNotNull(checked);
- final YangTextSchemaSource checkedGet = checked.checkedGet();
+ final YangTextSchemaSource checkedGet = checked.get();
Assert.assertEquals(sourceIdentifier, checkedGet.getIdentifier());
Assert.assertTrue(checked.isDone());
}
package org.opendaylight.yangtools.yang.model.repo.util;
import com.google.common.util.concurrent.AsyncFunction;
-import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.ListenableFuture;
import java.util.Arrays;
import java.util.concurrent.Future;
import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
}
@Override
- public CheckedFuture<? extends YangSchemaSourceRepresentation, SchemaSourceException> getSource(
+ public ListenableFuture<? extends YangSchemaSourceRepresentation> getSource(
final SourceIdentifier sourceIdentifier) {
- return Mockito.mock(CheckedFuture.class);
+ return Mockito.mock(ListenableFuture.class);
}
}
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.AsyncFunction;
-import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
}
@Override
- public CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(
- final Collection<SourceIdentifier> requiredSources, final StatementParserMode statementParserMode,
- final Set<QName> supportedFeatures) {
+ public ListenableFuture<SchemaContext> createSchemaContext(final Collection<SourceIdentifier> requiredSources,
+ final StatementParserMode statementParserMode, final Set<QName> supportedFeatures) {
return createSchemaContext(requiredSources,
statementParserMode == StatementParserMode.SEMVER_MODE ? this.semVerCache : this.cache,
new AssembleSources(Optional.ofNullable(supportedFeatures), statementParserMode));
}
- private CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(
- final Collection<SourceIdentifier> requiredSources,
+ private ListenableFuture<SchemaContext> createSchemaContext(final Collection<SourceIdentifier> requiredSources,
final Cache<Collection<SourceIdentifier>, SchemaContext> cache,
final AsyncFunction<List<ASTSchemaSource>, SchemaContext> assembleSources) {
// Make sources unique
final SchemaContext existing = cache.getIfPresent(uniqueSourceIdentifiers);
if (existing != null) {
LOG.debug("Returning cached context {}", existing);
- return Futures.immediateCheckedFuture(existing);
+ return Futures.immediateFuture(existing);
}
// Request all sources be loaded
}
}, MoreExecutors.directExecutor());
- return Futures.makeChecked(cf, MAPPER);
+ return cf;
}
private ListenableFuture<ASTSchemaSource> requestSource(final SourceIdentifier identifier) {
}
final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(
- statementParserMode, supportedFeatures);
+ statementParserMode);
+ if (supportedFeatures.isPresent()) {
+ reactor.setSupportedFeatures(supportedFeatures.get());
+ }
for (final Entry<SourceIdentifier, ASTSchemaSource> e : srcs.entrySet()) {
final ASTSchemaSource ast = e.getValue();
throw new SchemaResolutionException("Failed to resolve required models", ex.getSourceIdentifier(), ex);
}
- return Futures.immediateCheckedFuture(schemaContext);
+ return Futures.immediateFuture(schemaContext);
}
}
}
import com.google.common.annotations.Beta;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Preconditions;
+import com.google.common.base.Verify;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
-import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedDeque;
+import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nonnull;
} while (ver != version);
while (true) {
- final CheckedFuture<SchemaContext, SchemaResolutionException> f = factory.createSchemaContext(sources,
- statementParserMode);
+ final ListenableFuture<SchemaContext> f = factory.createSchemaContext(sources, statementParserMode);
try {
- sc = Optional.of(f.checkedGet());
+ sc = Optional.of(f.get());
break;
- } catch (SchemaResolutionException e) {
+ } catch (InterruptedException e) {
+ throw new RuntimeException("Interrupted while assembling schema context", e);
+ } catch (ExecutionException e) {
LOG.info("Failed to fully assemble schema context for {}", sources, e);
- sources = e.getResolvedSources();
+ final Throwable cause = e.getCause();
+ Verify.verify(cause instanceof SchemaResolutionException);
+ sources = ((SchemaResolutionException) cause).getResolvedSources();
}
}
}
@Override
- public synchronized CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(
+ public synchronized ListenableFuture<YangTextSchemaSource> getSource(
final SourceIdentifier sourceIdentifier) {
final Collection<YangTextSchemaSource> ret = texts.get(sourceIdentifier);
LOG.debug("Lookup {} result {}", sourceIdentifier, ret);
if (ret.isEmpty()) {
- return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException(
+ return Futures.immediateFailedFuture(new MissingSchemaSourceException(
"URL for " + sourceIdentifier + " not registered", sourceIdentifier));
}
- return Futures.immediateCheckedFuture(ret.iterator().next());
+ return Futures.immediateFuture(ret.iterator().next());
}
/**
@Beta
public SchemaContext trySchemaContext(final StatementParserMode statementParserMode)
throws SchemaResolutionException {
- final SchemaContextFactory factory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
- return factory.createSchemaContext(ImmutableSet.copyOf(requiredSources), statementParserMode).checkedGet();
+ final ListenableFuture<SchemaContext> future = repository
+ .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT)
+ .createSchemaContext(ImmutableSet.copyOf(requiredSources), statementParserMode);
+
+ try {
+ return future.get();
+ } catch (InterruptedException e) {
+ throw new RuntimeException("Interrupted while waiting for SchemaContext assembly", e);
+ } catch (ExecutionException e) {
+ final Throwable cause = e.getCause();
+ if (cause instanceof SchemaResolutionException) {
+ throw (SchemaResolutionException) cause;
+ }
+
+ throw new SchemaResolutionException("Failed to assemble SchemaContext", e);
+ }
}
@Override
public final class YinTextToDomTransformer extends SchemaSourceTransformer<YinTextSchemaSource, YinDomSchemaSource> {
private YinTextToDomTransformer(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
super(provider, YinTextSchemaSource.class, consumer, YinDomSchemaSource.class,
- input -> Futures.immediateCheckedFuture(transformSource(input)));
+ input -> Futures.immediateFuture(transformSource(input)));
}
public static YinTextToDomTransformer create(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
package org.opendaylight.yangtools.yang.parser.util;
import com.google.common.annotations.Beta;
-import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
import java.io.IOException;
import java.util.Optional;
-import javax.annotation.Nonnull;
import org.antlr.v4.runtime.ParserRuleContext;
import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
*/
@Beta
public final class TextToASTTransformer extends SchemaSourceTransformer<YangTextSchemaSource, ASTSchemaSource> {
-
- /**
- * A text-to-AST Transformation.
- *
- * @deprecated Use {@link TextToASTTransformer#transformText(YangTextSchemaSource)} instead.
- */
- @Deprecated
- public static final class TextToASTTransformation implements Transformation<YangTextSchemaSource, ASTSchemaSource> {
- @Override
- public CheckedFuture<ASTSchemaSource, SchemaSourceException> apply(@Nonnull final YangTextSchemaSource input)
- throws IOException, YangSyntaxErrorException {
- final YangStatementStreamSource src = YangStatementStreamSource.create(input);
-
- final ParserRuleContext ctx = src.getYangAST();
- LOG.debug("Model {} parsed successfully", input);
-
- //:TODO missing validation (YangModelBasicValidationListener should be re-implemented to new parser)
-
- final Optional<String> opt = input.getSymbolicName();
- final ASTSchemaSource result = opt.isPresent()
- ? ASTSchemaSource.create(opt.get(), input.getIdentifier(), ctx)
- : ASTSchemaSource.create(input.getIdentifier(), ctx);
-
- return Futures.immediateCheckedFuture(result);
- }
- }
-
- /**
- * Singleton instance of {@link TextToASTTransformation}.
- *
- * @deprecated Use {@link TextToASTTransformer#transformText(YangTextSchemaSource)} instead.
- */
- @Deprecated
- public static final TextToASTTransformation TRANSFORMATION = new TextToASTTransformation();
-
private static final Logger LOG = LoggerFactory.getLogger(TextToASTTransformer.class);
private TextToASTTransformer(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
- super(provider, YangTextSchemaSource.class, consumer, ASTSchemaSource.class, TRANSFORMATION);
+ super(provider, YangTextSchemaSource.class, consumer, ASTSchemaSource.class,
+ input -> Futures.immediateFuture(transformText(input)));
}
public static TextToASTTransformer create(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
public static ASTSchemaSource transformText(final YangTextSchemaSource text) throws SchemaSourceException,
IOException, YangSyntaxErrorException {
- return TRANSFORMATION.apply(text).checkedGet();
+ final YangStatementStreamSource src = YangStatementStreamSource.create(text);
+ final ParserRuleContext ctx = src.getYangAST();
+ LOG.debug("Model {} parsed successfully", text);
+
+ // TODO: missing validation (YangModelBasicValidationListener should be re-implemented to new parser)
+
+ final Optional<String> opt = text.getSymbolicName();
+ final ASTSchemaSource result = opt.isPresent() ? ASTSchemaSource.create(opt.get(), text.getIdentifier(), ctx)
+ : ASTSchemaSource.create(text.getIdentifier(), ctx);
+
+ return result;
}
}
import static org.junit.Assert.fail;
import com.google.common.collect.ImmutableList;
-import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.ListenableFuture;
+import java.util.concurrent.ExecutionException;
import org.junit.Test;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
setAndRegister(sharedSchemaRepository, bar1);
setAndRegister(sharedSchemaRepository, bar2);
- final SchemaContextFactory fact = sharedSchemaRepository
- .createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
-
- final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContextFuture = fact
- .createSchemaContext(ImmutableList.of(foo.getId(), bar1.getId(), bar2.getId()));
+ final SchemaContextFactory fact = sharedSchemaRepository.createSchemaContextFactory(
+ SchemaSourceFilter.ALWAYS_ACCEPT);
+ final ListenableFuture<SchemaContext> schemaContextFuture = fact.createSchemaContext(
+ ImmutableList.of(foo.getId(), bar1.getId(), bar2.getId()));
assertTrue(schemaContextFuture.isDone());
try {
- schemaContextFuture.checkedGet();
+ schemaContextFuture.get();
fail("Test should fail due to invalid imports of yang source.");
- } catch (final SchemaResolutionException e) {
- assertTrue(e.getCause().getMessage().startsWith("Module:bar imported twice with different revisions"));
+ } catch (final ExecutionException e) {
+ assertTrue(e.getCause().getMessage().startsWith(
+ "Module:bar imported twice with different revisions"));
}
}
package org.opendaylight.yangtools.yang.parser.repo;
-import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
}
@Override
- public CheckedFuture<T, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
- return Futures.makeChecked(future, input -> new SchemaSourceException("Failed", input));
+ public ListenableFuture<T> getSource(final SourceIdentifier sourceIdentifier) {
+ return future;
}
public T getSchemaSourceRepresentation() {
final TextToASTTransformer transformer = TextToASTTransformer.create(repository, repository);
repository.registerSchemaSourceListener(transformer);
- repository.registerSchemaSource(sourceIdentifier -> Futures.immediateCheckedFuture(source1),
+ repository.registerSchemaSource(sourceIdentifier -> Futures.immediateFuture(source1),
PotentialSchemaSource.create(s1, YangTextSchemaSource.class, 1));
- repository.registerSchemaSource(sourceIdentifier -> Futures.immediateCheckedFuture(source2),
+ repository.registerSchemaSource(sourceIdentifier -> Futures.immediateFuture(source2),
PotentialSchemaSource.create(s2, YangTextSchemaSource.class, 1));
}
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
-import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
final Throwable ex = new IllegalStateException("failed schema");
remoteInetTypesYang.setException(ex);
- final CheckedFuture<SchemaContext, SchemaResolutionException> schemaContextFuture =
- fact.createSchemaContext(ImmutableList.of(remoteInetTypesYang.getId()));
+ final ListenableFuture<SchemaContext> schemaContextFuture = fact.createSchemaContext(
+ ImmutableList.of(remoteInetTypesYang.getId()));
try {
- schemaContextFuture.checkedGet();
- } catch (final SchemaResolutionException e) {
+ schemaContextFuture.get();
+ } catch (final ExecutionException e) {
assertNotNull(e.getCause());
assertNotNull(e.getCause().getCause());
assertSame(ex, e.getCause().getCause());
final SourceIdentifier runningId = RevisionSourceIdentifier.create("running", Optional.of("2012-12-12"));
- sharedSchemaRepository.registerSchemaSource(sourceIdentifier -> Futures.immediateCheckedFuture(
+ sharedSchemaRepository.registerSchemaSource(sourceIdentifier -> Futures.immediateFuture(
new YangTextSchemaSource(runningId) {
@Override
protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.ListenableFuture;
import java.io.IOException;
import java.net.URL;
import org.junit.Test;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
-import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
assertEquals(bazModuleId, baz.get().getIdentifier());
final SourceIdentifier foobarModuleId = RevisionSourceIdentifier.create("foobar", "2016-09-26");
- final CheckedFuture<YangTextSchemaSource, SchemaSourceException> foobar =
- yangTextSchemaContextResolver.getSource(foobarModuleId);
+ final ListenableFuture<YangTextSchemaSource> foobar = yangTextSchemaContextResolver.getSource(foobarModuleId);
assertTrue(foobar.isDone());
try {
- foobar.checkedGet();
+ foobar.get();
fail("A MissingSchemaSourceException should have been thrown.");
- } catch (MissingSchemaSourceException ex) {
- assertEquals("URL for RevisionSourceIdentifier [name=foobar@2016-09-26] not registered", ex.getMessage());
+ } catch (ExecutionException e) {
+ assertEquals("URL for RevisionSourceIdentifier [name=foobar@2016-09-26] not registered",
+ e.getCause().getMessage());
}
Optional<SchemaContext> schemaContextOptional = yangTextSchemaContextResolver.getSchemaContext();