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.repo.api.MissingSchemaSourceException;
22 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
25 final class DefaultModuleInfoSnapshot implements ModuleInfoSnapshot {
26 private final ImmutableMap<SourceIdentifier, YangModuleInfo> moduleInfos;
27 private final ImmutableMap<String, ClassLoader> classLoaders;
28 private final @NonNull EffectiveModelContext effectiveModel;
30 DefaultModuleInfoSnapshot(final EffectiveModelContext effectiveModel,
31 final Map<SourceIdentifier, YangModuleInfo> moduleInfos, final Map<String, ClassLoader> classLoaders) {
32 this.effectiveModel = requireNonNull(effectiveModel);
33 this.moduleInfos = ImmutableMap.copyOf(moduleInfos);
34 this.classLoaders = ImmutableMap.copyOf(classLoaders);
38 public EffectiveModelContext getEffectiveModelContext() {
39 return effectiveModel;
43 public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
44 final YangModuleInfo info = moduleInfos.get(sourceIdentifier);
46 return Futures.immediateFailedFuture(
47 new MissingSchemaSourceException("No source registered", sourceIdentifier));
49 return Futures.immediateFuture(YangTextSchemaSource.delegateForCharSource(sourceIdentifier,
50 info.getYangTextCharSource()));
54 public <T> Class<T> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
55 final String packageName = Naming.getModelRootPackageName(fullyQualifiedName);
56 final ClassLoader loader = classLoaders.get(packageName);
58 throw new ClassNotFoundException("Package " + packageName + " not found");
60 @SuppressWarnings("unchecked")
61 final Class<T> loaded = (Class<T>) loader.loadClass(fullyQualifiedName);