From 9c6ff9ef2270954a89b49f013d2072d3f26c0f1f Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 8 Dec 2023 14:34:15 +0100 Subject: [PATCH] Remove SchemaSourceRegistration Previous patch has touched yang.model.repo.spi. Since we are in the area, also remove SchemaSourceRegistration to keep the API consistent. JIRA: YANGTOOLS-1551 Change-Id: Iba1ea385c8f72433410c283a523207d2d3d77dfb Signed-off-by: Robert Varga --- .../fs/FilesystemSchemaSourceCacheTest.java | 4 +- .../repo/spi/AbstractSchemaRepository.java | 83 +++++++++++-------- .../repo/spi/AbstractSchemaSourceCache.java | 27 +++--- .../spi/AbstractSchemaSourceRegistration.java | 34 -------- .../repo/spi/GuavaSchemaSourceCache.java | 2 +- .../repo/spi/RefcountedRegistration.java | 6 +- .../repo/spi/SchemaSourceRegistration.java | 22 ----- .../model/repo/spi/SchemaSourceRegistry.java | 6 +- .../repo/spi/SchemaSourceTransformer.java | 17 ++-- .../repo/spi/GuavaSchemaSourceCacheTest.java | 3 +- .../repo/spi/RefcountedRegistrationTest.java | 3 +- .../repo/spi/SoftSchemaSourceCacheTest.java | 3 +- 12 files changed, 82 insertions(+), 128 deletions(-) delete mode 100644 yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaSourceRegistration.java delete mode 100644 yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistration.java diff --git a/yang/yang-repo-fs/src/test/java/org/opendaylight/yangtools/yang/model/repo/fs/FilesystemSchemaSourceCacheTest.java b/yang/yang-repo-fs/src/test/java/org/opendaylight/yangtools/yang/model/repo/fs/FilesystemSchemaSourceCacheTest.java index 08bbc03d34..e6dd9515da 100644 --- a/yang/yang-repo-fs/src/test/java/org/opendaylight/yangtools/yang/model/repo/fs/FilesystemSchemaSourceCacheTest.java +++ b/yang/yang-repo-fs/src/test/java/org/opendaylight/yangtools/yang/model/repo/fs/FilesystemSchemaSourceCacheTest.java @@ -41,11 +41,11 @@ import org.mockito.Mock; 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) @@ -54,7 +54,7 @@ public class FilesystemSchemaSourceCacheTest { @Mock public SchemaSourceRegistry registry; @Mock - public SchemaSourceRegistration registration; + public Registration registration; public File storageDir; diff --git a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaRepository.java b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaRepository.java index 7b59636210..9d95628b50 100644 --- a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaRepository.java +++ b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaRepository.java @@ -7,6 +7,7 @@ */ 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; @@ -25,6 +26,7 @@ import java.util.Iterator; 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; @@ -48,7 +50,7 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche */ @GuardedBy("this") private final Map, - AbstractSchemaSourceRegistration>> sources = new HashMap<>(); + SchemaSourceRegistration>> sources = new HashMap<>(); /* * Schema source listeners. @@ -56,27 +58,25 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche @GuardedBy("this") private final List listeners = new ArrayList<>(); - @SuppressWarnings("unchecked") - private static ListenableFuture fetchSource( - final SourceIdentifier id, final Iterator> it) { - final AbstractSchemaSourceRegistration reg = it.next(); + private static ListenableFuture fetchSource(final SourceIdentifier id, + final Iterator it) { + final var reg = it.next(); + @SuppressWarnings("unchecked") + final var provider = (SchemaSourceProvider) reg.provider(); - return Futures.catchingAsync(((SchemaSourceProvider)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 ListenableFuture getSchemaSource(final SourceIdentifier id, final Class representation) { - final ArrayList> sortedSchemaSourceRegistrations; + final ArrayList sortedSchemaSourceRegistrations; synchronized (this) { final var srcs = sources.get(id); @@ -117,8 +117,8 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche return fetchSourceFuture; } - private synchronized void addSource(final PotentialSchemaSource source, - final AbstractSchemaSourceRegistration reg) { + private synchronized void addSource(final SchemaSourceRegistration reg) { + final var source = reg.getInstance(); sources.computeIfAbsent(source.getSourceIdentifier(), ignored -> ArrayListMultimap.create()) .put(source.getRepresentation(), reg); @@ -128,8 +128,8 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche } } - private synchronized 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); @@ -145,23 +145,16 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche } @Override - public SchemaSourceRegistration registerSchemaSource( + public Registration registerSchemaSource( final SchemaSourceProvider provider, final PotentialSchemaSource 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>(); @@ -180,8 +173,8 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche } private final class SchemaListenerRegistration extends AbstractObjectRegistration { - SchemaListenerRegistration(final SchemaSourceListener instance) { - super(instance); + SchemaListenerRegistration(final SchemaSourceListener listener) { + super(listener); } @Override @@ -190,15 +183,33 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche } } - private static final class SchemaProviderCostComparator implements Comparator>, - Serializable { + private final class SchemaSourceRegistration extends AbstractObjectRegistration> { + 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, 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(); } diff --git a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaSourceCache.java b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaSourceCache.java index 1390f8b05f..0d6e1776b6 100644 --- a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaSourceCache.java +++ b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaSourceCache.java @@ -9,15 +9,15 @@ package org.opendaylight.yangtools.yang.model.repo.spi; 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 Cached schema source type. */ @@ -44,20 +44,17 @@ public abstract class AbstractSchemaSourceCache register(final SourceIdentifier sourceIdentifier) { - final PotentialSchemaSource 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 diff --git a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaSourceRegistration.java b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaSourceRegistration.java deleted file mode 100644 index 865abf941c..0000000000 --- a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/AbstractSchemaSourceRegistration.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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 - extends AbstractObjectRegistration> implements SchemaSourceRegistration { - private final SchemaSourceProvider provider; - - protected AbstractSchemaSourceRegistration(final SchemaSourceProvider provider, - final PotentialSchemaSource 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); - } -} diff --git a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/GuavaSchemaSourceCache.java b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/GuavaSchemaSourceCache.java index 4720e37519..65f038e090 100644 --- a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/GuavaSchemaSourceCache.java +++ b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/GuavaSchemaSourceCache.java @@ -83,7 +83,7 @@ public final class GuavaSchemaSourceCache cache.put(srcId, source); final var reg = register(srcId); - final FinalizablePhantomReference ref = new FinalizablePhantomReference<>(source, queue) { + final var ref = new FinalizablePhantomReference<>(source, queue) { @Override public void finalizeReferent() { reg.close(); diff --git a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/RefcountedRegistration.java b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/RefcountedRegistration.java index b409df0376..45c541c06c 100644 --- a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/RefcountedRegistration.java +++ b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/RefcountedRegistration.java @@ -10,11 +10,13 @@ package org.opendaylight.yangtools.yang.model.repo.spi; 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); } diff --git a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistration.java b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistration.java deleted file mode 100644 index 781bfd56bd..0000000000 --- a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistration.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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 - extends ObjectRegistration> { - @Override - void close(); -} diff --git a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistry.java b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistry.java index f7cb93c5f2..246353351b 100644 --- a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistry.java +++ b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceRegistry.java @@ -26,10 +26,10 @@ public interface SchemaSourceRegistry { * @param 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. */ - SchemaSourceRegistration registerSchemaSource( - SchemaSourceProvider provider, PotentialSchemaSource source); + Registration registerSchemaSource(SchemaSourceProvider provider, + PotentialSchemaSource source); /** * Register a schema source listener. The listener will be notified as new sources and their representations become diff --git a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformer.java b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformer.java index 30764f5741..21872ae530 100644 --- a/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformer.java +++ b/yang/yang-repo-spi/src/main/java/org/opendaylight/yangtools/yang/model/repo/spi/SchemaSourceTransformer.java @@ -21,7 +21,6 @@ import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; public class SchemaSourceTransformer implements SchemaSourceListener, SchemaSourceProvider { - @FunctionalInterface public interface Transformation extends AsyncFunction { @@ -58,8 +57,8 @@ public class SchemaSourceTransformer> 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); } @@ -68,24 +67,22 @@ public class SchemaSourceTransformer 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 newSrc = PotentialSchemaSource.create(src.getSourceIdentifier(), dstClass, - src.getCost() + PotentialSchemaSource.Costs.COMPUTATION.getValue()); - - final SchemaSourceRegistration 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) { diff --git a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/GuavaSchemaSourceCacheTest.java b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/GuavaSchemaSourceCacheTest.java index 0b5f885bba..9e84d86e1c 100644 --- a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/GuavaSchemaSourceCacheTest.java +++ b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/GuavaSchemaSourceCacheTest.java @@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test; 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; @@ -37,7 +38,7 @@ class GuavaSchemaSourceCacheTest { @Mock public SchemaSourceRegistry registry; @Mock - public SchemaSourceRegistration registration; + public Registration registration; @Test void inMemorySchemaSourceCacheTest1() { diff --git a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/RefcountedRegistrationTest.java b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/RefcountedRegistrationTest.java index a3124d2f55..17c0c26023 100644 --- a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/RefcountedRegistrationTest.java +++ b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/RefcountedRegistrationTest.java @@ -15,11 +15,12 @@ import org.junit.jupiter.api.Test; 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() { diff --git a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/SoftSchemaSourceCacheTest.java b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/SoftSchemaSourceCacheTest.java index 2dae5fb1a7..cf9c3ad208 100644 --- a/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/SoftSchemaSourceCacheTest.java +++ b/yang/yang-repo-spi/src/test/java/org/opendaylight/yangtools/yang/model/repo/spi/SoftSchemaSourceCacheTest.java @@ -22,6 +22,7 @@ import org.junit.jupiter.api.Test; 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; @@ -33,7 +34,7 @@ class SoftSchemaSourceCacheTest { @Mock private SchemaSourceRegistry registry; @Mock - private SchemaSourceRegistration registration; + private Registration registration; @Test void inMemorySchemaSourceCacheTest() { -- 2.36.6