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;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
16 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
17 import org.opendaylight.yangtools.yang.binding.contract.Naming;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
20 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
21 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
22 import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
24 final class DefaultModuleInfoSnapshot implements ModuleInfoSnapshot {
25 private final ImmutableMap<SourceIdentifier, YangModuleInfo> moduleInfos;
26 private final ImmutableMap<String, ClassLoader> classLoaders;
27 private final @NonNull EffectiveModelContext modelContext;
29 DefaultModuleInfoSnapshot(final EffectiveModelContext modelContext,
30 final Map<SourceIdentifier, YangModuleInfo> moduleInfos, final Map<String, ClassLoader> classLoaders) {
31 this.modelContext = requireNonNull(modelContext);
32 this.moduleInfos = ImmutableMap.copyOf(moduleInfos);
33 this.classLoaders = ImmutableMap.copyOf(classLoaders);
37 public EffectiveModelContext modelContext() {
42 public YangTextSource yangTextSource(final SourceIdentifier sourceId) {
43 final var info = moduleInfos.get(sourceId);
44 return info == null ? null : new DelegatedYangTextSource(sourceId, info.getYangTextCharSource());
48 public YangTextSource getYangTextSource(final SourceIdentifier sourceId) throws MissingSchemaSourceException {
49 final var source = yangTextSource(sourceId);
51 throw new MissingSchemaSourceException(sourceId, "No source registered");
57 public <T> Class<T> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
58 final var packageName = Naming.getModelRootPackageName(fullyQualifiedName);
59 final var loader = classLoaders.get(packageName);
61 throw new ClassNotFoundException("Package " + packageName + " not found");
63 @SuppressWarnings("unchecked")
64 final var loaded = (Class<T>) loader.loadClass(fullyQualifiedName);