import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
+import org.opendaylight.yangtools.concepts.Registration;
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;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
-import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
@ExtendWith(MockitoExtension.class)
@Mock
public SchemaSourceRegistry registry;
@Mock
- public SchemaSourceRegistration<?> registration;
+ public Registration registration;
public File storageDir;
*/
package org.opendaylight.yangtools.yang.model.repo.spi;
+import static java.util.Objects.requireNonNull;
import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
import com.google.common.annotations.Beta;
import java.util.List;
import java.util.Map;
import org.checkerframework.checker.lock.qual.GuardedBy;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
import org.opendaylight.yangtools.concepts.Registration;
import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
*/
@GuardedBy("this")
private final Map<SourceIdentifier, ListMultimap<Class<? extends SchemaSourceRepresentation>,
- AbstractSchemaSourceRegistration<?>>> sources = new HashMap<>();
+ SchemaSourceRegistration>> sources = new HashMap<>();
/*
* Schema source listeners.
@GuardedBy("this")
private final List<SchemaListenerRegistration> listeners = new ArrayList<>();
- @SuppressWarnings("unchecked")
- private static <T extends SchemaSourceRepresentation> ListenableFuture<T> fetchSource(
- final SourceIdentifier id, final Iterator<AbstractSchemaSourceRegistration<?>> it) {
- final AbstractSchemaSourceRegistration<?> reg = it.next();
+ private static <T extends SchemaSourceRepresentation> ListenableFuture<T> fetchSource(final SourceIdentifier id,
+ final Iterator<SchemaSourceRegistration> it) {
+ final var reg = it.next();
+ @SuppressWarnings("unchecked")
+ final var provider = (SchemaSourceProvider<T>) reg.provider();
- return Futures.catchingAsync(((SchemaSourceProvider<T>)reg.getProvider()).getSource(id), Throwable.class,
- input -> {
- LOG.debug("Failed to acquire source from {}", reg, input);
-
- if (it.hasNext()) {
- return fetchSource(id, it);
- }
-
- throw new MissingSchemaSourceException("All available providers exhausted", id, input);
- }, MoreExecutors.directExecutor());
+ return Futures.catchingAsync(provider.getSource(id), Throwable.class, input -> {
+ LOG.debug("Failed to acquire source from {}", reg, input);
+ if (it.hasNext()) {
+ return fetchSource(id, it);
+ }
+ throw new MissingSchemaSourceException("All available providers exhausted", id, input);
+ }, MoreExecutors.directExecutor());
}
@Override
public <T extends SchemaSourceRepresentation> ListenableFuture<T> getSchemaSource(final SourceIdentifier id,
final Class<T> representation) {
- final ArrayList<AbstractSchemaSourceRegistration<?>> sortedSchemaSourceRegistrations;
+ final ArrayList<SchemaSourceRegistration> sortedSchemaSourceRegistrations;
synchronized (this) {
final var srcs = sources.get(id);
return fetchSourceFuture;
}
- private synchronized <T extends SchemaSourceRepresentation> void addSource(final PotentialSchemaSource<T> source,
- final AbstractSchemaSourceRegistration<T> reg) {
+ private synchronized void addSource(final SchemaSourceRegistration reg) {
+ final var source = reg.getInstance();
sources.computeIfAbsent(source.getSourceIdentifier(), ignored -> ArrayListMultimap.create())
.put(source.getRepresentation(), reg);
}
}
- private synchronized <T extends SchemaSourceRepresentation> void removeSource(final PotentialSchemaSource<?> source,
- final SchemaSourceRegistration<?> reg) {
+ private synchronized void removeSource(final SchemaSourceRegistration reg) {
+ final var source = reg.getInstance();
final var m = sources.get(source.getSourceIdentifier());
if (m != null) {
m.remove(source.getRepresentation(), reg);
}
@Override
- public <T extends SchemaSourceRepresentation> SchemaSourceRegistration<T> registerSchemaSource(
+ public <T extends SchemaSourceRepresentation> Registration registerSchemaSource(
final SchemaSourceProvider<? super T> provider, final PotentialSchemaSource<T> source) {
- final var src = source.cachedReference();
- final var ret = new AbstractSchemaSourceRegistration<>(provider, src) {
- @Override
- protected void removeRegistration() {
- removeSource(src, this);
- }
- };
-
- addSource(src, ret);
+ final var ret = new SchemaSourceRegistration(source.cachedReference(), provider);
+ addSource(ret);
return ret;
}
@Override
public Registration registerSchemaSourceListener(final SchemaSourceListener listener) {
- final SchemaListenerRegistration ret = new SchemaListenerRegistration(listener);
+ final var ret = new SchemaListenerRegistration(listener);
synchronized (this) {
final var col = new ArrayList<PotentialSchemaSource<?>>();
}
private final class SchemaListenerRegistration extends AbstractObjectRegistration<SchemaSourceListener> {
- SchemaListenerRegistration(final SchemaSourceListener instance) {
- super(instance);
+ SchemaListenerRegistration(final SchemaSourceListener listener) {
+ super(listener);
}
@Override
}
}
- private static final class SchemaProviderCostComparator implements Comparator<AbstractSchemaSourceRegistration<?>>,
- Serializable {
+ private final class SchemaSourceRegistration extends AbstractObjectRegistration<PotentialSchemaSource<?>> {
+ private final @NonNull SchemaSourceProvider<?> provider;
+
+ SchemaSourceRegistration(final PotentialSchemaSource<?> source, final SchemaSourceProvider<?> provider) {
+ super(source);
+ this.provider = requireNonNull(provider);
+ }
+
+ @NonNull SchemaSourceProvider<?> provider() {
+ return provider;
+ }
+
+ @Override
+ protected void removeRegistration() {
+ removeSource(this);
+ }
+ }
+
+ private static final class SchemaProviderCostComparator
+ implements Comparator<SchemaSourceRegistration>, Serializable {
static final SchemaProviderCostComparator INSTANCE = new SchemaProviderCostComparator();
@java.io.Serial
private static final long serialVersionUID = 1L;
@Override
- public int compare(final AbstractSchemaSourceRegistration<?> o1, final AbstractSchemaSourceRegistration<?> o2) {
+ public int compare(final SchemaSourceRegistration o1, final SchemaSourceRegistration o2) {
return o1.getInstance().getCost() - o2.getInstance().getCost();
}
import static java.util.Objects.requireNonNull;
+import org.opendaylight.yangtools.concepts.Registration;
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;
/**
- * Abstract base class for cache-type SchemaSourceListeners. It needs to be
- * registered with a {@link SchemaSourceRegistry}, where it gets notifications
- * from. It performs filtering and {@link #offer(SchemaSourceRepresentation)}s
- * conforming sources to the subclass.
+ * Abstract base class for cache-type SchemaSourceListeners. It needs to be registered with a
+ * {@link SchemaSourceRegistry}, where it gets notifications from. It performs filtering and
+ * {@link #offer(SchemaSourceRepresentation)}s conforming sources to the subclass.
*
* @param <T> Cached schema source type.
*/
protected abstract void offer(T source);
/**
- * Register the presence of a cached schema source with the consumer. Subclasses
- * need to call this method once they have cached a schema source representation,
- * or when they have determined they have a schema source is available -- like
- * when a persistent cache reads its cache index.
+ * Register the presence of a cached schema source with the consumer. Subclasses need to call this method once they
+ * have cached a schema source representation, or when they have determined they have a schema source is available
+ * -- like when a persistent cache reads its cache index.
*
* @param sourceIdentifier Source identifier
- * @return schema source registration, which the subclass needs to
- * {@link SchemaSourceRegistration#close()} once it expunges the source
- * from the cache.
+ * @return schema source registration, which the subclass needs to {@link Registration#close()} once it expunges the
+ * source from the cache.
*/
- protected final SchemaSourceRegistration<T> register(final SourceIdentifier sourceIdentifier) {
- final PotentialSchemaSource<T> src = PotentialSchemaSource.create(sourceIdentifier, representation,
- cost.getValue());
- return consumer.registerSchemaSource(this, src);
+ protected final Registration register(final SourceIdentifier sourceIdentifier) {
+ return consumer.registerSchemaSource(this, PotentialSchemaSource.create(sourceIdentifier, representation,
+ cost.getValue()));
}
@Override
+++ /dev/null
-/*
- * Copyright (c) 2014 Cisco 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.yang.model.repo.spi;
-
-import static java.util.Objects.requireNonNull;
-
-import com.google.common.base.MoreObjects.ToStringHelper;
-import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
-
-public abstract class AbstractSchemaSourceRegistration<T extends SchemaSourceRepresentation>
- extends AbstractObjectRegistration<PotentialSchemaSource<T>> implements SchemaSourceRegistration<T> {
- private final SchemaSourceProvider<?> provider;
-
- protected AbstractSchemaSourceRegistration(final SchemaSourceProvider<?> provider,
- final PotentialSchemaSource<T> source) {
- super(source);
- this.provider = requireNonNull(provider);
- }
-
- public final SchemaSourceProvider<?> getProvider() {
- return provider;
- }
-
- @Override
- protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
- return super.addToStringAttributes(toStringHelper).add("provider", provider);
- }
-}
cache.put(srcId, source);
final var reg = register(srcId);
- final FinalizablePhantomReference<T> ref = new FinalizablePhantomReference<>(source, queue) {
+ final var ref = new FinalizablePhantomReference<>(source, queue) {
@Override
public void finalizeReferent() {
reg.close();
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
+import org.opendaylight.yangtools.concepts.Registration;
+
final class RefcountedRegistration {
- private final SchemaSourceRegistration<?> reg;
+ private final Registration reg;
private int refcount = 1;
- RefcountedRegistration(final SchemaSourceRegistration<?> reg) {
+ RefcountedRegistration(final Registration reg) {
this.reg = requireNonNull(reg);
}
+++ /dev/null
-/*
- * Copyright (c) 2014 Cisco 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.yang.model.repo.spi;
-
-import com.google.common.annotations.Beta;
-import org.opendaylight.yangtools.concepts.ObjectRegistration;
-import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
-
-/**
- * Registration of a schema source.
- */
-@Beta
-public interface SchemaSourceRegistration<T extends SchemaSourceRepresentation>
- extends ObjectRegistration<PotentialSchemaSource<T>> {
- @Override
- void close();
-}
* @param <T> schema source representation type
* @param provider Resolver which can potentially resolve the identifier
* @param source Schema source details
- * @return A registration handle. Invoking {@link SchemaSourceRegistration#close()} will cancel the registration.
+ * @return A registration handle. Invoking {@link Registration#close()} will cancel the registration.
*/
- <T extends SchemaSourceRepresentation> SchemaSourceRegistration<T> registerSchemaSource(
- SchemaSourceProvider<? super T> provider, PotentialSchemaSource<T> source);
+ <T extends SchemaSourceRepresentation> Registration registerSchemaSource(SchemaSourceProvider<? super T> provider,
+ PotentialSchemaSource<T> source);
/**
* Register a schema source listener. The listener will be notified as new sources and their representations become
public class SchemaSourceTransformer<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
implements SchemaSourceListener, SchemaSourceProvider<D> {
-
@FunctionalInterface
public interface Transformation<S extends SchemaSourceRepresentation, D extends SchemaSourceRepresentation>
extends AsyncFunction<S, D> {
@Override
public final void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
- for (PotentialSchemaSource<?> src : sources) {
- final Class<?> rep = src.getRepresentation();
+ for (var src : sources) {
+ final var rep = src.getRepresentation();
if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
registerSource(src);
}
@Override
public final void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
- final Class<?> rep = source.getRepresentation();
+ final var rep = source.getRepresentation();
if (srcClass.isAssignableFrom(rep) && dstClass != rep) {
unregisterSource(source);
}
}
private void registerSource(final PotentialSchemaSource<?> src) {
- RefcountedRegistration reg = availableSources.get(src);
+ final var reg = availableSources.get(src);
if (reg != null) {
reg.incRef();
return;
}
- final PotentialSchemaSource<D> newSrc = PotentialSchemaSource.create(src.getSourceIdentifier(), dstClass,
- src.getCost() + PotentialSchemaSource.Costs.COMPUTATION.getValue());
-
- final SchemaSourceRegistration<D> r = consumer.registerSchemaSource(this, newSrc);
- availableSources.put(src, new RefcountedRegistration(r));
+ final var newSrc = PotentialSchemaSource.create(src.getSourceIdentifier(), dstClass,
+ src.getCost() + PotentialSchemaSource.Costs.COMPUTATION.getValue());
+ availableSources.put(src, new RefcountedRegistration(consumer.registerSchemaSource(this, newSrc)));
}
private void unregisterSource(final PotentialSchemaSource<?> src) {
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
+import org.opendaylight.yangtools.concepts.Registration;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
@Mock
public SchemaSourceRegistry registry;
@Mock
- public SchemaSourceRegistration<?> registration;
+ public Registration registration;
@Test
void inMemorySchemaSourceCacheTest1() {
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
+import org.opendaylight.yangtools.concepts.Registration;
@ExtendWith(MockitoExtension.class)
class RefcountedRegistrationTest {
@Mock
- private SchemaSourceRegistration<?> reg;
+ private Registration reg;
@Test
void refcountDecTrue() {
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
+import org.opendaylight.yangtools.concepts.Registration;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.YangSchemaSourceRepresentation;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
@Mock
private SchemaSourceRegistry registry;
@Mock
- private SchemaSourceRegistration<?> registration;
+ private Registration registration;
@Test
void inMemorySchemaSourceCacheTest() {