Refactor DOMYangTextSourceProvider
[mdsal.git] / binding / mdsal-binding-runtime-spi / src / main / java / org / opendaylight / mdsal / binding / runtime / spi / DefaultModuleInfoSnapshot.java
index 4047969e8c22089459b0c461caeda208c1f06f00..a439c50899211fb61fa2a76231c34434784e7761 100644 (file)
@@ -10,53 +10,58 @@ 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;
-import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
+import org.opendaylight.yangtools.yang.binding.contract.Naming;
 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.model.repo.api.SourceIdentifier;
-import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
 
 final class DefaultModuleInfoSnapshot implements ModuleInfoSnapshot {
     private final ImmutableMap<SourceIdentifier, YangModuleInfo> moduleInfos;
     private final ImmutableMap<String, ClassLoader> classLoaders;
-    private final @NonNull EffectiveModelContext effectiveModel;
+    private final @NonNull EffectiveModelContext modelContext;
 
-    DefaultModuleInfoSnapshot(final EffectiveModelContext effectiveModel,
+    DefaultModuleInfoSnapshot(final EffectiveModelContext modelContext,
             final Map<SourceIdentifier, YangModuleInfo> moduleInfos, final Map<String, ClassLoader> classLoaders) {
-        this.effectiveModel = requireNonNull(effectiveModel);
+        this.modelContext = requireNonNull(modelContext);
         this.moduleInfos = ImmutableMap.copyOf(moduleInfos);
         this.classLoaders = ImmutableMap.copyOf(classLoaders);
     }
 
     @Override
-    public EffectiveModelContext getEffectiveModelContext() {
-        return effectiveModel;
+    public EffectiveModelContext modelContext() {
+        return modelContext;
     }
 
     @Override
-    public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
-        final YangModuleInfo info = moduleInfos.get(sourceIdentifier);
-        if (info == null) {
-            return Futures.immediateFailedFuture(
-                new MissingSchemaSourceException("No source registered", sourceIdentifier));
+    public YangTextSource yangTextSource(final SourceIdentifier sourceId) {
+        final var info = moduleInfos.get(sourceId);
+        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 Futures.immediateFuture(YangTextSchemaSource.delegateForByteSource(sourceIdentifier,
-                    info.getYangTextByteSource()));
+        return source;
     }
 
     @Override
-    public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
-        final String packageName = BindingReflections.getModelRootPackageName(fullyQualifiedName);
-        final ClassLoader loader = classLoaders.get(packageName);
+    public <T> Class<T> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
+        final var packageName = Naming.getModelRootPackageName(fullyQualifiedName);
+        final var loader = classLoaders.get(packageName);
         if (loader == null) {
             throw new ClassNotFoundException("Package " + packageName + " not found");
         }
-        return loader.loadClass(fullyQualifiedName);
+        @SuppressWarnings("unchecked")
+        final var loaded = (Class<T>) loader.loadClass(fullyQualifiedName);
+        return loaded;
     }
 }