2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.runtime.spi;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
18 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
19 import org.opendaylight.yangtools.yang.binding.contract.Naming;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
22 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
23 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
24 import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
26 final class DefaultModuleInfoSnapshot implements ModuleInfoSnapshot {
27 private final ImmutableMap<SourceIdentifier, YangModuleInfo> moduleInfos;
28 private final ImmutableMap<String, ClassLoader> classLoaders;
29 private final @NonNull EffectiveModelContext modelContext;
31 DefaultModuleInfoSnapshot(final EffectiveModelContext modelContext,
32 final Map<SourceIdentifier, YangModuleInfo> moduleInfos, final Map<String, ClassLoader> classLoaders) {
33 this.modelContext = requireNonNull(modelContext);
34 this.moduleInfos = ImmutableMap.copyOf(moduleInfos);
35 this.classLoaders = ImmutableMap.copyOf(classLoaders);
39 public EffectiveModelContext modelContext() {
44 public ListenableFuture<? extends YangTextSource> getSource(final SourceIdentifier sourceId) {
45 final var info = moduleInfos.get(sourceId);
47 ? Futures.immediateFailedFuture(new MissingSchemaSourceException(sourceId, "No source registered"))
48 : Futures.immediateFuture(new DelegatedYangTextSource(sourceId, info.getYangTextCharSource()));
52 public <T> Class<T> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
53 final var packageName = Naming.getModelRootPackageName(fullyQualifiedName);
54 final var loader = classLoaders.get(packageName);
56 throw new ClassNotFoundException("Package " + packageName + " not found");
58 @SuppressWarnings("unchecked")
59 final var loaded = (Class<T>) loader.loadClass(fullyQualifiedName);