From: Robert Varga Date: Sat, 6 Jan 2024 03:48:25 +0000 (+0100) Subject: Refactor DOMYangTextSourceProvider X-Git-Tag: v13.0.0~47 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=mdsal.git;a=commitdiff_plain;h=8a0509e7b8909c4fa5b1b4b54b62a6474e55a8eb Refactor DOMYangTextSourceProvider This should really be a proper extension with its own API. While we are at it. mdsal-binding-runtime-api is made synchronous, as there is just no need to use futures. Change-Id: I3711662242df8d912c2536a3d73d4f86beea2c33 Signed-off-by: Robert Varga --- diff --git a/binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/CurrentAdapterSerializerTest.java b/binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/CurrentAdapterSerializerTest.java index e677a8625e..5db62c0fca 100644 --- a/binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/CurrentAdapterSerializerTest.java +++ b/binding/mdsal-binding-dom-adapter/src/test/java/org/opendaylight/mdsal/binding/dom/adapter/CurrentAdapterSerializerTest.java @@ -13,7 +13,6 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThrows; -import com.google.common.util.concurrent.ListenableFuture; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -36,6 +35,7 @@ import org.opendaylight.yangtools.yang.data.impl.schema.Builders; import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier; import org.opendaylight.yangtools.yang.model.api.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException; import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils; public class CurrentAdapterSerializerTest { @@ -126,19 +126,25 @@ public class CurrentAdapterSerializerTest { } @Override - public EffectiveModelContext modelContext() { - throw new UnsupportedOperationException(); + @SuppressWarnings("unchecked") + public Class loadClass(final String fullyQualifiedName) throws ClassNotFoundException { + return (Class) Class.forName(fullyQualifiedName); } @Override - public ListenableFuture getSource(final SourceIdentifier sourceIdentifier) { - throw new UnsupportedOperationException(); + public YangTextSource yangTextSource(final SourceIdentifier arg0) { + return null; } @Override - @SuppressWarnings("unchecked") - public Class loadClass(final String fullyQualifiedName) throws ClassNotFoundException { - return (Class) Class.forName(fullyQualifiedName); + public YangTextSource getYangTextSource(final SourceIdentifier sourceId) throws MissingSchemaSourceException { + throw new MissingSchemaSourceException(sourceId, "no sources"); } + + @Override + public EffectiveModelContext modelContext() { + throw new UnsupportedOperationException(); + } + } } diff --git a/binding/mdsal-binding-runtime-api/pom.xml b/binding/mdsal-binding-runtime-api/pom.xml index 06a9bd4854..87261eaeaa 100644 --- a/binding/mdsal-binding-runtime-api/pom.xml +++ b/binding/mdsal-binding-runtime-api/pom.xml @@ -46,7 +46,7 @@ org.opendaylight.yangtools - yang-repo-spi + yang-repo-api org.opendaylight.mdsal diff --git a/binding/mdsal-binding-runtime-api/src/main/java/module-info.java b/binding/mdsal-binding-runtime-api/src/main/java/module-info.java index e39a791783..dabf23dd67 100644 --- a/binding/mdsal-binding-runtime-api/src/main/java/module-info.java +++ b/binding/mdsal-binding-runtime-api/src/main/java/module-info.java @@ -11,8 +11,8 @@ module org.opendaylight.mdsal.binding.runtime.api { requires transitive org.opendaylight.yangtools.concepts; requires transitive org.opendaylight.yangtools.yang.common; requires transitive org.opendaylight.yangtools.yang.model.api; + requires transitive org.opendaylight.yangtools.yang.repo.api; requires transitive org.opendaylight.yangtools.yang.binding; - requires transitive org.opendaylight.yangtools.yang.repo.spi; requires transitive org.opendaylight.yangtools.rfc8040.model.api; requires transitive org.opendaylight.mdsal.binding.model.api; requires org.slf4j; diff --git a/binding/mdsal-binding-runtime-api/src/main/java/org/opendaylight/mdsal/binding/runtime/api/ModuleInfoSnapshot.java b/binding/mdsal-binding-runtime-api/src/main/java/org/opendaylight/mdsal/binding/runtime/api/ModuleInfoSnapshot.java index fa70bfb1f6..9b2b722d45 100644 --- a/binding/mdsal-binding-runtime-api/src/main/java/org/opendaylight/mdsal/binding/runtime/api/ModuleInfoSnapshot.java +++ b/binding/mdsal-binding-runtime-api/src/main/java/org/opendaylight/mdsal/binding/runtime/api/ModuleInfoSnapshot.java @@ -8,24 +8,31 @@ package org.opendaylight.mdsal.binding.runtime.api; import com.google.common.annotations.Beta; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.yangtools.concepts.Immutable; import org.opendaylight.yangtools.yang.binding.YangModuleInfo; import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; +import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier; import org.opendaylight.yangtools.yang.model.api.source.YangTextSource; -import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider; +import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException; /** * A snapshot of a set of {@link YangModuleInfo}s, assembled to form an {@link EffectiveModelContext}. */ @Beta -public interface ModuleInfoSnapshot extends Immutable, SchemaSourceProvider { +@NonNullByDefault +public interface ModuleInfoSnapshot extends Immutable { /** * The {@link EffectiveModelContext} resulting from all models exposed from constituent module infos. * * @return the resulting model context */ - @NonNull EffectiveModelContext modelContext(); + EffectiveModelContext modelContext(); + + @Nullable YangTextSource yangTextSource(SourceIdentifier sourceId); + + YangTextSource getYangTextSource(SourceIdentifier sourceId) throws MissingSchemaSourceException; Class loadClass(String fullyQualifiedName) throws ClassNotFoundException; } diff --git a/binding/mdsal-binding-runtime-spi/src/main/java/org/opendaylight/mdsal/binding/runtime/spi/DefaultModuleInfoSnapshot.java b/binding/mdsal-binding-runtime-spi/src/main/java/org/opendaylight/mdsal/binding/runtime/spi/DefaultModuleInfoSnapshot.java index 94298c1bb1..a439c50899 100644 --- a/binding/mdsal-binding-runtime-spi/src/main/java/org/opendaylight/mdsal/binding/runtime/spi/DefaultModuleInfoSnapshot.java +++ b/binding/mdsal-binding-runtime-spi/src/main/java/org/opendaylight/mdsal/binding/runtime/spi/DefaultModuleInfoSnapshot.java @@ -10,8 +10,6 @@ package org.opendaylight.mdsal.binding.runtime.spi; import static java.util.Objects.requireNonNull; import com.google.common.collect.ImmutableMap; -import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListenableFuture; import java.util.Map; import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot; @@ -41,11 +39,18 @@ final class DefaultModuleInfoSnapshot implements ModuleInfoSnapshot { } @Override - public ListenableFuture getSource(final SourceIdentifier sourceId) { + public YangTextSource yangTextSource(final SourceIdentifier sourceId) { final var info = moduleInfos.get(sourceId); - return info == null - ? Futures.immediateFailedFuture(new MissingSchemaSourceException(sourceId, "No source registered")) - : Futures.immediateFuture(new DelegatedYangTextSource(sourceId, info.getYangTextCharSource())); + return info == null ? null : new DelegatedYangTextSource(sourceId, info.getYangTextCharSource()); + } + + @Override + public YangTextSource getYangTextSource(final SourceIdentifier sourceId) throws MissingSchemaSourceException { + final var source = yangTextSource(sourceId); + if (source == null) { + throw new MissingSchemaSourceException(sourceId, "No source registered"); + } + return source; } @Override diff --git a/binding/mdsal-binding-runtime-spi/src/main/java/org/opendaylight/mdsal/binding/runtime/spi/ForwardingModuleInfoSnapshot.java b/binding/mdsal-binding-runtime-spi/src/main/java/org/opendaylight/mdsal/binding/runtime/spi/ForwardingModuleInfoSnapshot.java index 1564797e7e..705573915f 100644 --- a/binding/mdsal-binding-runtime-spi/src/main/java/org/opendaylight/mdsal/binding/runtime/spi/ForwardingModuleInfoSnapshot.java +++ b/binding/mdsal-binding-runtime-spi/src/main/java/org/opendaylight/mdsal/binding/runtime/spi/ForwardingModuleInfoSnapshot.java @@ -9,14 +9,16 @@ package org.opendaylight.mdsal.binding.runtime.spi; import com.google.common.annotations.Beta; import com.google.common.collect.ForwardingObject; -import com.google.common.util.concurrent.ListenableFuture; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot; import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier; import org.opendaylight.yangtools.yang.model.api.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException; @Beta +@NonNullByDefault public abstract class ForwardingModuleInfoSnapshot extends ForwardingObject implements ModuleInfoSnapshot { @Override protected abstract ModuleInfoSnapshot delegate(); @@ -27,12 +29,17 @@ public abstract class ForwardingModuleInfoSnapshot extends ForwardingObject impl } @Override - public @NonNull EffectiveModelContext modelContext() { + public EffectiveModelContext modelContext() { return delegate().modelContext(); } @Override - public ListenableFuture getSource(final SourceIdentifier sourceIdentifier) { - return delegate().getSource(sourceIdentifier); + public @Nullable YangTextSource yangTextSource(final SourceIdentifier sourceId) { + return delegate().yangTextSource(sourceId); + } + + @Override + public YangTextSource getYangTextSource(final SourceIdentifier sourceId) throws MissingSchemaSourceException { + return delegate().getYangTextSource(sourceId); } } diff --git a/dom/mdsal-dom-api/pom.xml b/dom/mdsal-dom-api/pom.xml index 37d322d84e..d19e559368 100644 --- a/dom/mdsal-dom-api/pom.xml +++ b/dom/mdsal-dom-api/pom.xml @@ -71,14 +71,6 @@ org.opendaylight.yangtools yang-model-api - - org.opendaylight.yangtools - yang-model-spi - - - org.opendaylight.yangtools - yang-repo-spi - org.opendaylight.yangtools diff --git a/dom/mdsal-dom-api/src/main/java/module-info.java b/dom/mdsal-dom-api/src/main/java/module-info.java index dad7ab6946..b253ca497c 100644 --- a/dom/mdsal-dom-api/src/main/java/module-info.java +++ b/dom/mdsal-dom-api/src/main/java/module-info.java @@ -14,8 +14,6 @@ module org.opendaylight.mdsal.dom.api { requires transitive org.opendaylight.yangtools.yang.data.api; requires transitive org.opendaylight.yangtools.yang.data.tree.api; requires transitive org.opendaylight.yangtools.yang.model.api; - requires transitive org.opendaylight.yangtools.yang.model.spi; - requires transitive org.opendaylight.yangtools.yang.repo.spi; requires transitive org.opendaylight.mdsal.common.api; requires transitive org.opendaylight.yangtools.concepts; requires org.opendaylight.yangtools.util; diff --git a/dom/mdsal-dom-api/src/main/java/org/opendaylight/mdsal/dom/api/DOMSchemaService.java b/dom/mdsal-dom-api/src/main/java/org/opendaylight/mdsal/dom/api/DOMSchemaService.java index 8770f96f20..42dbd9bb13 100644 --- a/dom/mdsal-dom-api/src/main/java/org/opendaylight/mdsal/dom/api/DOMSchemaService.java +++ b/dom/mdsal-dom-api/src/main/java/org/opendaylight/mdsal/dom/api/DOMSchemaService.java @@ -7,10 +7,13 @@ */ package org.opendaylight.mdsal.dom.api; +import com.google.common.util.concurrent.ListenableFuture; import java.util.function.Consumer; import org.eclipse.jdt.annotation.NonNullByDefault; import org.opendaylight.yangtools.concepts.Registration; import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; +import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier; +import org.opendaylight.yangtools.yang.model.api.source.YangTextSource; @NonNullByDefault public interface DOMSchemaService extends DOMService { @@ -36,4 +39,18 @@ public interface DOMSchemaService extends DOMService listener); + + /** + * An {@link Extension} exposing access to {@link YangTextSource}. + */ + @FunctionalInterface + interface YangTextSourceExtension extends Extension { + /** + * Return a future producing a {@link YangTextSource} containing the YANG text of specified source. + * + * @param sourceId A {@link SourceIdentifier} + * @return A future + */ + ListenableFuture getYangTexttSource(SourceIdentifier sourceId); + } } diff --git a/dom/mdsal-dom-api/src/main/java/org/opendaylight/mdsal/dom/api/DOMYangTextSourceProvider.java b/dom/mdsal-dom-api/src/main/java/org/opendaylight/mdsal/dom/api/DOMYangTextSourceProvider.java deleted file mode 100644 index c547b94f6a..0000000000 --- a/dom/mdsal-dom-api/src/main/java/org/opendaylight/mdsal/dom/api/DOMYangTextSourceProvider.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.mdsal.dom.api; - -import com.google.common.annotations.Beta; -import org.opendaylight.yangtools.yang.model.api.source.YangTextSource; -import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider; - -/** - * A {@link DOMSchemaService.Extension} exposing access to {@link YangTextSource}. Instances of this method should - * be acquired from {@link DOMSchemaService}. - */ -@Beta -public interface DOMYangTextSourceProvider - extends DOMSchemaService.Extension, SchemaSourceProvider { - -} diff --git a/dom/mdsal-dom-schema-osgi/src/main/java/org/opendaylight/mdsal/dom/schema/osgi/impl/OSGiDOMSchemaService.java b/dom/mdsal-dom-schema-osgi/src/main/java/org/opendaylight/mdsal/dom/schema/osgi/impl/OSGiDOMSchemaService.java index a1ace089a0..013fc91e6c 100644 --- a/dom/mdsal-dom-schema-osgi/src/main/java/org/opendaylight/mdsal/dom/schema/osgi/impl/OSGiDOMSchemaService.java +++ b/dom/mdsal-dom-schema-osgi/src/main/java/org/opendaylight/mdsal/dom/schema/osgi/impl/OSGiDOMSchemaService.java @@ -9,6 +9,7 @@ package org.opendaylight.mdsal.dom.schema.osgi.impl; import static java.util.Objects.requireNonNull; +import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; @@ -17,13 +18,13 @@ import java.util.function.Consumer; import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot; import org.opendaylight.mdsal.dom.api.DOMSchemaService; -import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider; import org.opendaylight.mdsal.dom.schema.osgi.OSGiModuleInfoSnapshot; import org.opendaylight.yangtools.concepts.AbstractRegistration; import org.opendaylight.yangtools.concepts.Registration; import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier; import org.opendaylight.yangtools.yang.model.api.source.YangTextSource; +import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException; import org.osgi.service.component.ComponentFactory; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; @@ -38,8 +39,8 @@ import org.slf4j.LoggerFactory; /** * OSGi Service Registry-backed implementation of {@link DOMSchemaService}. */ -@Component(service = DOMSchemaService.class, immediate = true) -public final class OSGiDOMSchemaService implements DOMSchemaService, DOMYangTextSourceProvider { +@Component(immediate = true) +public final class OSGiDOMSchemaService implements DOMSchemaService, DOMSchemaService.YangTextSourceExtension { private static final Logger LOG = LoggerFactory.getLogger(OSGiDOMSchemaService.class); private final List> listeners = new CopyOnWriteArrayList<>(); @@ -56,6 +57,11 @@ public final class OSGiDOMSchemaService implements DOMSchemaService, DOMYangText LOG.info("DOM Schema services activated"); } + @Override + public List supportedExtensions() { + return List.of(this); + } + @Deactivate void deactivate() { LOG.info("DOM Schema services deactivated"); @@ -110,8 +116,12 @@ public final class OSGiDOMSchemaService implements DOMSchemaService, DOMYangText } @Override - public ListenableFuture getSource(final SourceIdentifier sourceIdentifier) { - return currentSnapshot.get().getSource(sourceIdentifier); + public ListenableFuture getYangTexttSource(final SourceIdentifier sourceId) { + try { + return Futures.immediateFuture(currentSnapshot.get().getYangTextSource(sourceId)); + } catch (MissingSchemaSourceException e) { + return Futures.immediateFailedFuture(e); + } } @SuppressWarnings("checkstyle:illegalCatch") diff --git a/dom/mdsal-dom-spi/pom.xml b/dom/mdsal-dom-spi/pom.xml index 0f34d4f850..7868dae4ea 100644 --- a/dom/mdsal-dom-spi/pom.xml +++ b/dom/mdsal-dom-spi/pom.xml @@ -65,14 +65,6 @@ org.opendaylight.yangtools yang-model-api - - org.opendaylight.yangtools - yang-repo-api - - - org.opendaylight.yangtools - yang-repo-spi - org.opendaylight.mdsal mdsal-common-api @@ -92,6 +84,11 @@ yang-parser-api test + + org.opendaylight.yangtools + yang-repo-api + test + org.opendaylight.yangtools yang-test-util diff --git a/dom/mdsal-dom-spi/src/main/java/module-info.java b/dom/mdsal-dom-spi/src/main/java/module-info.java index 6b4bb0e8b1..048d94bce9 100644 --- a/dom/mdsal-dom-spi/src/main/java/module-info.java +++ b/dom/mdsal-dom-spi/src/main/java/module-info.java @@ -15,8 +15,6 @@ module org.opendaylight.mdsal.dom.spi { requires transitive org.opendaylight.yangtools.yang.common; requires transitive org.opendaylight.yangtools.yang.data.tree.api; requires transitive org.opendaylight.yangtools.yang.model.api; - requires transitive org.opendaylight.yangtools.yang.repo.api; - requires transitive org.opendaylight.yangtools.yang.repo.spi; requires com.google.common; requires org.opendaylight.yangtools.concepts; requires org.opendaylight.yangtools.odlext.model.api; diff --git a/dom/mdsal-dom-spi/src/main/java/org/opendaylight/mdsal/dom/spi/FixedDOMSchemaService.java b/dom/mdsal-dom-spi/src/main/java/org/opendaylight/mdsal/dom/spi/FixedDOMSchemaService.java index c2d49c4492..48cbd4b996 100644 --- a/dom/mdsal-dom-spi/src/main/java/org/opendaylight/mdsal/dom/spi/FixedDOMSchemaService.java +++ b/dom/mdsal-dom-spi/src/main/java/org/opendaylight/mdsal/dom/spi/FixedDOMSchemaService.java @@ -10,67 +10,44 @@ package org.opendaylight.mdsal.dom.spi; import static java.util.Objects.requireNonNull; import com.google.common.annotations.Beta; -import com.google.common.util.concurrent.ListenableFuture; import java.util.List; import java.util.function.Consumer; import java.util.function.Supplier; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.mdsal.dom.api.DOMSchemaService; -import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider; import org.opendaylight.yangtools.concepts.Registration; import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; -import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier; -import org.opendaylight.yangtools.yang.model.api.source.YangTextSource; -import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider; /** - * {@link DOMSchemaService} (and {@link DOMYangTextSourceProvider}) implementations backed by a - * {@code Supplier} (and {@link SchemaSourceProvider}) which are known to be fixed and never - * change schemas. + * {@link DOMSchemaService} backed by a {@code Supplier} + * (and potentially a {@link YangTextSourceExtension}) which are known to be fixed + * and never change schemas. * * @author Michael Vorburger.ch */ @Beta @NonNullByDefault -public sealed class FixedDOMSchemaService implements DOMSchemaService { - private static final class WithYangTextSources extends FixedDOMSchemaService implements DOMYangTextSourceProvider { - private final SchemaSourceProvider schemaSourceProvider; - - WithYangTextSources(final Supplier modelContextSupplier, - final SchemaSourceProvider schemaSourceProvider) { - super(modelContextSupplier); - this.schemaSourceProvider = requireNonNull(schemaSourceProvider); - } - - @Override - public List supportedExtensions() { - return List.of(this); - } - - @Override - public ListenableFuture getSource(final SourceIdentifier sourceIdentifier) { - return schemaSourceProvider.getSource(sourceIdentifier); - } +public record FixedDOMSchemaService( + Supplier modelContextSupplier, + @Nullable YangTextSourceExtension extension) implements DOMSchemaService { + public FixedDOMSchemaService { + requireNonNull(modelContextSupplier); } - private final Supplier modelContextSupplier; - - private FixedDOMSchemaService(final Supplier modelContextSupplier) { - this.modelContextSupplier = requireNonNull(modelContextSupplier); + public FixedDOMSchemaService(final Supplier modelContextSupplier) { + this(modelContextSupplier, null); } - public static DOMSchemaService of(final EffectiveModelContext effectiveModel) { - final var checked = requireNonNull(effectiveModel); - return new FixedDOMSchemaService(() -> checked); + public FixedDOMSchemaService(final EffectiveModelContext effectiveModel) { + this(() -> effectiveModel, null); + requireNonNull(effectiveModel); } - public static DOMSchemaService of(final Supplier modelContextSupplier) { - return new FixedDOMSchemaService(modelContextSupplier); - } - - public static DOMSchemaService of(final Supplier modelContextSupplier, - final SchemaSourceProvider yangTextSourceProvider) { - return new WithYangTextSources(modelContextSupplier, requireNonNull(yangTextSourceProvider)); + @Override + public List supportedExtensions() { + final var local = extension; + return local == null ? List.of() : List.of(local); } @Override